-
-
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.
ra-no-code - Introduce Resource Configuration
- Loading branch information
Showing
9 changed files
with
350 additions
and
17 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
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
45 changes: 45 additions & 0 deletions
45
packages/ra-no-code/src/ResourceConfiguration/FieldConfiguration.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,45 @@ | ||
import * as React from 'react'; | ||
import { | ||
getFieldLabelTranslationArgs, | ||
InferenceTypes, | ||
useTranslate, | ||
} from 'ra-core'; | ||
import { SelectInput, TextInput } from 'ra-ui-materialui'; | ||
import { CardContent } from '@material-ui/core'; | ||
import { textChangeRangeIsUnchanged } from 'typescript'; | ||
|
||
export const FieldConfiguration = props => { | ||
const { index, field, resource } = props; | ||
const translate = useTranslate(); | ||
const labelArgs = getFieldLabelTranslationArgs({ | ||
source: field.props.source, | ||
resource, | ||
label: field.props.label, | ||
}); | ||
|
||
return ( | ||
<CardContent> | ||
<TextInput | ||
source={`fields[${index}].props.source`} | ||
label="Source" | ||
fullWidth | ||
disabled | ||
/> | ||
<TextInput | ||
source={`fields[${index}].props.label`} | ||
label="Label" | ||
fullWidth | ||
initialValue={translate(...labelArgs)} | ||
/> | ||
<SelectInput | ||
source={`fields[${index}].type`} | ||
label="Type" | ||
fullWidth | ||
choices={InferenceTypes.map(type => ({ | ||
id: type, | ||
name: type, | ||
}))} | ||
/> | ||
</CardContent> | ||
); | ||
}; |
40 changes: 40 additions & 0 deletions
40
packages/ra-no-code/src/ResourceConfiguration/FieldConfigurationTab.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,40 @@ | ||
import * as React from 'react'; | ||
import { Tab } from '@material-ui/core'; | ||
import { makeStyles } from '@material-ui/core/styles'; | ||
import { getFieldLabelTranslationArgs, useTranslate } from 'ra-core'; | ||
|
||
export const FieldConfigurationTab = ({ field, resource, ...props }) => { | ||
const classes = useStyles(); | ||
const translate = useTranslate(); | ||
const labelArgs = getFieldLabelTranslationArgs({ | ||
source: field.props.source, | ||
resource, | ||
label: field.props.label, | ||
}); | ||
|
||
return ( | ||
<Tab | ||
{...props} | ||
key={field.props.source} | ||
label={translate(...labelArgs)} | ||
id={`nav-tab-${field.props.source}`} | ||
aria-controls={`nav-tabpanel-${field.props.source}`} | ||
classes={classes} | ||
/> | ||
); | ||
}; | ||
|
||
const useStyles = makeStyles(theme => ({ | ||
root: { | ||
paddingTop: theme.spacing(1), | ||
paddingBottom: theme.spacing(1), | ||
paddingLeft: theme.spacing(2), | ||
paddingRight: theme.spacing(2), | ||
textTransform: 'none', | ||
minHeight: 0, | ||
fontWeight: 'normal', | ||
}, | ||
selected: { | ||
fontWeight: 'bold', | ||
}, | ||
})); |
186 changes: 186 additions & 0 deletions
186
packages/ra-no-code/src/ResourceConfiguration/ResourceConfiguration.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,186 @@ | ||
import * as React from 'react'; | ||
import { useEffect, useState } from 'react'; | ||
import { | ||
Avatar, | ||
Card, | ||
CardActions, | ||
CardContent, | ||
CardHeader, | ||
Divider, | ||
IconButton, | ||
Tabs, | ||
} from '@material-ui/core'; | ||
import { makeStyles } from '@material-ui/core/styles'; | ||
import MoreVertIcon from '@material-ui/icons/MoreVert'; | ||
import { | ||
FormWithRedirect, | ||
InferredElementDescription, | ||
RecordContextProvider, | ||
SaveContextProvider, | ||
} from 'ra-core'; | ||
import { SaveButton, TextInput } from 'ra-ui-materialui'; | ||
import { ResourceConfiguration } from './ResourceConfigurationContext'; | ||
import { useResourceConfiguration } from './useResourceConfiguration'; | ||
import { FieldConfiguration } from './FieldConfiguration'; | ||
import { FieldConfigurationTab } from './FieldConfigurationTab'; | ||
|
||
export const ResourceConfigurationPage = ({ | ||
resource, | ||
}: { | ||
resource: string; | ||
}) => { | ||
const [resourceConfiguration, actions] = useResourceConfiguration(resource); | ||
const [activeField, setActiveField] = useState< | ||
InferredElementDescription | ||
>(); | ||
const classes = useStyles(); | ||
|
||
const save = (values: ResourceConfiguration) => { | ||
actions.update(values); | ||
}; | ||
const saveContext = { | ||
save, | ||
setOnFailure: () => {}, | ||
setOnSuccess: () => {}, | ||
}; | ||
|
||
const handleTabChange = (event, newValue) => { | ||
const newField = resourceConfiguration.fields.find( | ||
f => f.props.source === newValue | ||
); | ||
setActiveField(newField); | ||
}; | ||
|
||
useEffect(() => { | ||
if (resourceConfiguration && resourceConfiguration.fields) { | ||
setActiveField(resourceConfiguration.fields[0]); | ||
} | ||
}, [resourceConfiguration]); | ||
|
||
if (!resourceConfiguration || !activeField) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<RecordContextProvider value={resourceConfiguration}> | ||
<SaveContextProvider value={saveContext}> | ||
<FormWithRedirect | ||
save={save} | ||
initialValues={resourceConfiguration} | ||
render={({ handleSubmitWithRedirect }) => ( | ||
<Card> | ||
<CardHeader | ||
avatar={ | ||
<Avatar> | ||
{ | ||
// Here will go an icon selector | ||
( | ||
resourceConfiguration.label || | ||
resourceConfiguration.name | ||
).substr(0, 1) | ||
} | ||
</Avatar> | ||
} | ||
action={ | ||
// Will display a menu to delete the resource maybe ? | ||
<IconButton aria-label="settings"> | ||
<MoreVertIcon /> | ||
</IconButton> | ||
} | ||
title={`Configuration of ${ | ||
resourceConfiguration.label || | ||
resourceConfiguration.name | ||
}`} | ||
/> | ||
|
||
<CardContent> | ||
<TextInput | ||
source="label" | ||
initialValue={ | ||
resourceConfiguration.label || | ||
resourceConfiguration.name | ||
} | ||
/> | ||
</CardContent> | ||
<Divider /> | ||
<div className={classes.fields}> | ||
<Tabs | ||
orientation="vertical" | ||
value={activeField.props.source} | ||
onChange={handleTabChange} | ||
className={classes.fieldList} | ||
> | ||
{resourceConfiguration.fields.map(field => ( | ||
<FieldConfigurationTab | ||
key={`${field.props.source}_tab`} | ||
field={field} | ||
value={field.props.source} | ||
resource={resource} | ||
/> | ||
))} | ||
</Tabs> | ||
{resourceConfiguration.fields.map( | ||
(field, index) => ( | ||
<div | ||
key={`${field.props.source}_panel`} | ||
role="tabpanel" | ||
hidden={ | ||
activeField.props.source !== | ||
field.props.source | ||
} | ||
id={`nav-tabpanel-${field.props.source}`} | ||
aria-labelledby={`nav-tab-${field.props.source}`} | ||
> | ||
{activeField.props.source === | ||
field.props.source ? ( | ||
<FieldConfiguration | ||
key={field.props.source} | ||
field={field} | ||
index={index} | ||
className={ | ||
classes.fieldPanel | ||
} | ||
resource={resource} | ||
/> | ||
) : null} | ||
</div> | ||
) | ||
)} | ||
</div> | ||
<CardActions className={classes.actions}> | ||
<SaveButton | ||
handleSubmitWithRedirect={ | ||
handleSubmitWithRedirect | ||
} | ||
/> | ||
</CardActions> | ||
</Card> | ||
)} | ||
/> | ||
</SaveContextProvider> | ||
</RecordContextProvider> | ||
); | ||
}; | ||
|
||
const useStyles = makeStyles(theme => ({ | ||
fields: { | ||
display: 'flex', | ||
}, | ||
fieldList: { | ||
backgroundColor: theme.palette.background.default, | ||
}, | ||
fieldTitle: { | ||
paddingTop: theme.spacing(1), | ||
paddingBottom: theme.spacing(1), | ||
paddingLeft: theme.spacing(2), | ||
paddingRight: theme.spacing(2), | ||
textTransform: 'none', | ||
minHeight: 0, | ||
}, | ||
fieldPanel: { | ||
flexGrow: 1, | ||
}, | ||
actions: { | ||
backgroundColor: theme.palette.background.default, | ||
}, | ||
})); |
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
Oops, something went wrong.