-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6246 from marmelab/ra-no-code-relationships
ra-no-code Add support for simple references
- Loading branch information
Showing
14 changed files
with
293 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,21 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { Root } from 'ra-no-code'; | ||
import { defaultTheme } from 'react-admin'; | ||
import { | ||
unstable_createMuiStrictModeTheme, | ||
createMuiTheme, | ||
} from '@material-ui/core/styles'; | ||
|
||
// FIXME MUI bug https://github.com/mui-org/material-ui/issues/13394 | ||
const theme = | ||
process.env.NODE_ENV !== 'production' | ||
? unstable_createMuiStrictModeTheme(defaultTheme) | ||
: createMuiTheme(defaultTheme); | ||
|
||
ReactDOM.render( | ||
<React.StrictMode> | ||
<Root /> | ||
<Root theme={theme} /> | ||
</React.StrictMode>, | ||
document.getElementById('root') | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
packages/ra-no-code/src/ResourceConfiguration/ConfigurationInputsFromFieldDefinition.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import React from 'react'; | ||
import { FormDataConsumer, InferredElementDescription } from 'ra-core'; | ||
import { SelectInput } from 'ra-ui-materialui'; | ||
import get from 'lodash/get'; | ||
import { useResourcesConfiguration } from './useResourcesConfiguration'; | ||
|
||
export const ConfigurationInputsFromFieldDefinition = ({ | ||
definition, | ||
sourcePrefix, | ||
}: { | ||
definition: InferredElementDescription; | ||
sourcePrefix?: string; | ||
}) => { | ||
const [resources] = useResourcesConfiguration(); | ||
|
||
switch (definition.type) { | ||
case 'reference': | ||
return ( | ||
<> | ||
<SelectInput | ||
source={`${sourcePrefix}.props.reference`} | ||
label="Referenced resource" | ||
fullWidth | ||
choices={Object.keys(resources).map(name => ({ | ||
id: name, | ||
name: resources[name].label || resources[name].name, | ||
}))} | ||
/> | ||
<SelectInput | ||
source={`${sourcePrefix}.options.selectionType`} | ||
label="How to select the reference" | ||
fullWidth | ||
choices={ReferenceSelectionChoice} | ||
/> | ||
<FormDataConsumer> | ||
{({ formData, ...rest }) => { | ||
const resourceName = get( | ||
formData, | ||
`${sourcePrefix}.props.reference` | ||
); | ||
if (!resourceName) return null; | ||
|
||
const resource = resources[resourceName]; | ||
return ( | ||
<SelectInput | ||
source={`${sourcePrefix}.options.referenceField`} | ||
label="Displayed field" | ||
choices={resource.fields.map(field => ({ | ||
id: field.props.source, | ||
name: | ||
field.props.label || | ||
field.props.source, | ||
}))} | ||
{...rest} | ||
/> | ||
); | ||
}} | ||
</FormDataConsumer> | ||
</> | ||
); | ||
default: | ||
return null; | ||
} | ||
}; | ||
|
||
const ReferenceSelectionChoice = [ | ||
{ id: 'select', name: 'Simple list' }, | ||
{ id: 'autocomplete', name: 'Searchable list' }, | ||
{ id: 'radio', name: 'Radio buttons' }, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
packages/ra-no-code/src/builders/ReferenceInputChildFromDefinition.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import * as React from 'react'; | ||
import { | ||
AutocompleteInput, | ||
RadioButtonGroupInput, | ||
SelectInput, | ||
} from 'ra-ui-materialui'; | ||
import { ReferenceFieldConfiguration } from '../ResourceConfiguration'; | ||
|
||
export const ReferenceInputChildFromDefinition = ({ | ||
definition, | ||
...props | ||
}: ReferenceInputChildFromDefinitionProps) => { | ||
if (definition.options.selectionType === 'select') { | ||
return ( | ||
<SelectInput | ||
optionText={definition.options.referenceField} | ||
{...props} | ||
/> | ||
); | ||
} | ||
|
||
if (definition.options.selectionType === 'autocomplete') { | ||
return ( | ||
<AutocompleteInput | ||
optionText={definition.options.referenceField} | ||
{...props} | ||
/> | ||
); | ||
} | ||
|
||
if (definition.options.selectionType === 'radio') { | ||
return ( | ||
<RadioButtonGroupInput | ||
optionText={definition.options.referenceField} | ||
{...props} | ||
/> | ||
); | ||
} | ||
}; | ||
|
||
interface ReferenceInputChildFromDefinitionProps { | ||
definition: ReferenceFieldConfiguration; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.