|
| 1 | +/** |
| 2 | + * --------------------------------------------------------------------- |
| 3 | + * Copyright (c) 2020 EclipseSource Munich |
| 4 | + * Licensed under MIT |
| 5 | + * https://github.com/eclipsesource/jsonforms-editor/blob/master/LICENSE |
| 6 | + * --------------------------------------------------------------------- |
| 7 | + */ |
| 8 | + |
| 9 | +import { Grid, TextField } from '@material-ui/core'; |
| 10 | +import MenuItem from '@material-ui/core/MenuItem'; |
| 11 | +import React from 'react'; |
| 12 | + |
| 13 | +import { useDispatch, useSchema } from '../context'; |
| 14 | +import { |
| 15 | + Actions, |
| 16 | + getChildren, |
| 17 | + getScope, |
| 18 | + isArrayElement, |
| 19 | + isObjectElement, |
| 20 | + SchemaElement, |
| 21 | +} from '../model'; |
| 22 | +import { EditorControl } from '../model/uischema'; |
| 23 | + |
| 24 | +interface EditableControlProps { |
| 25 | + uischema: EditorControl; |
| 26 | +} |
| 27 | +export const EditableControl: React.FC<EditableControlProps> = ({ |
| 28 | + uischema, |
| 29 | +}) => { |
| 30 | + const dispatch = useDispatch(); |
| 31 | + const baseSchema = useSchema() as SchemaElement; |
| 32 | + const handleLabelChange = (event: React.ChangeEvent<HTMLInputElement>) => { |
| 33 | + dispatch( |
| 34 | + Actions.updateUISchemaElement(uischema.uuid, { |
| 35 | + label: event.target.value, |
| 36 | + }) |
| 37 | + ); |
| 38 | + }; |
| 39 | + const handleScopeChange = (event: React.ChangeEvent<HTMLInputElement>) => { |
| 40 | + dispatch( |
| 41 | + Actions.changeControlScope( |
| 42 | + uischema.uuid, |
| 43 | + (event.target.value as any).scope, |
| 44 | + (event.target.value as any).uuid |
| 45 | + ) |
| 46 | + ); |
| 47 | + }; |
| 48 | + const scopes = getChildren(baseSchema) |
| 49 | + .flatMap((child) => { |
| 50 | + // if the child is the only item of an array, use its children instead |
| 51 | + if ( |
| 52 | + (isObjectElement(child) && |
| 53 | + isArrayElement(child.parent) && |
| 54 | + child.parent.items === child) || |
| 55 | + isObjectElement(child) |
| 56 | + ) { |
| 57 | + return getChildren(child); |
| 58 | + } |
| 59 | + return [child]; |
| 60 | + }) |
| 61 | + .map((e) => ({ scope: `#${getScope(e)}`, uuid: e.uuid })); |
| 62 | + const scopeValues = scopes.filter((s) => s.scope.endsWith(uischema.scope)); |
| 63 | + const scopeValue = |
| 64 | + scopeValues && scopeValues.length === 1 ? scopeValues[0] : ''; |
| 65 | + return ( |
| 66 | + <Grid container direction={'column'}> |
| 67 | + <Grid item xs> |
| 68 | + <TextField |
| 69 | + id='filled-name' |
| 70 | + label='Label' |
| 71 | + value={uischema.label ?? ''} |
| 72 | + onChange={handleLabelChange} |
| 73 | + fullWidth |
| 74 | + /> |
| 75 | + </Grid> |
| 76 | + <Grid item xs> |
| 77 | + <TextField |
| 78 | + id='standard-select-currency' |
| 79 | + select |
| 80 | + label='Scope' |
| 81 | + value={scopeValue} |
| 82 | + onChange={handleScopeChange} |
| 83 | + fullWidth |
| 84 | + helperText='Please select your scope' |
| 85 | + > |
| 86 | + {scopes.map((scope) => ( |
| 87 | + <MenuItem key={scope.scope} value={scope as any}> |
| 88 | + {scope.scope} |
| 89 | + </MenuItem> |
| 90 | + ))} |
| 91 | + </TextField> |
| 92 | + </Grid> |
| 93 | + </Grid> |
| 94 | + ); |
| 95 | +}; |
0 commit comments