-
-
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
321 additions
and
18 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
27 changes: 27 additions & 0 deletions
27
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,27 @@ | ||
import * as React from 'react'; | ||
import { InferenceTypes } from 'ra-core'; | ||
import { SelectInput, TextInput } from 'ra-ui-materialui'; | ||
import { CardContent } from '@material-ui/core'; | ||
|
||
export const FieldConfiguration = props => { | ||
const { index } = props; | ||
|
||
return ( | ||
<CardContent> | ||
<TextInput | ||
source={`fields[${index}].props.label`} | ||
label="Label" | ||
fullWidth | ||
/> | ||
<SelectInput | ||
source={`fields[${index}].type`} | ||
label="Type" | ||
fullWidth | ||
choices={InferenceTypes.map(type => ({ | ||
id: type, | ||
name: type, | ||
}))} | ||
/> | ||
</CardContent> | ||
); | ||
}; |
33 changes: 33 additions & 0 deletions
33
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,33 @@ | ||
import * as React from 'react'; | ||
import { Tab } from '@material-ui/core'; | ||
import { makeStyles } from '@material-ui/core/styles'; | ||
|
||
export const FieldConfigurationTab = ({ field, ...props }) => { | ||
const classes = useStyles(); | ||
|
||
return ( | ||
<Tab | ||
{...props} | ||
key={field.props.source} | ||
label={field.props.label} | ||
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', | ||
}, | ||
})); |
177 changes: 177 additions & 0 deletions
177
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,177 @@ | ||
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 [definition, 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 = definition.fields.find( | ||
f => f.props.source === newValue | ||
); | ||
setActiveField(newField); | ||
}; | ||
|
||
useEffect(() => { | ||
if (definition && definition.fields) { | ||
setActiveField(definition.fields[0]); | ||
} | ||
}, [definition]); | ||
|
||
if (!definition || !activeField) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<RecordContextProvider value={definition}> | ||
<SaveContextProvider value={saveContext}> | ||
<FormWithRedirect | ||
save={save} | ||
initialValues={definition} | ||
render={({ handleSubmitWithRedirect }) => ( | ||
<Card> | ||
<CardHeader | ||
avatar={ | ||
<Avatar> | ||
{ | ||
// Here will go an icon selector | ||
( | ||
definition.label || | ||
definition.name | ||
).substr(0, 1) | ||
} | ||
</Avatar> | ||
} | ||
action={ | ||
// Will display a menu to delete the resource maybe ? | ||
<IconButton aria-label="settings"> | ||
<MoreVertIcon /> | ||
</IconButton> | ||
} | ||
title={`Configuration of ${ | ||
definition.label || definition.name | ||
}`} | ||
/> | ||
|
||
<CardContent> | ||
<TextInput | ||
source="label" | ||
initialValue={ | ||
definition.label || definition.name | ||
} | ||
/> | ||
</CardContent> | ||
<Divider /> | ||
<div className={classes.fields}> | ||
<Tabs | ||
orientation="vertical" | ||
value={activeField.props.source} | ||
onChange={handleTabChange} | ||
className={classes.fieldList} | ||
> | ||
{definition.fields.map(field => ( | ||
<FieldConfigurationTab | ||
key={field.props.source} | ||
field={field} | ||
value={field.props.source} | ||
/> | ||
))} | ||
</Tabs> | ||
{definition.fields.map((field, index) => ( | ||
<div | ||
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} | ||
/> | ||
) : 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.