From 60f2af79cc4dba72f93caa23e76edf5ba0f3c7e1 Mon Sep 17 00:00:00 2001 From: Eugen Neufeld Date: Mon, 22 Jun 2020 17:06:05 +0200 Subject: [PATCH] Add enum autocomplete renderers Adds the `MuiAutocomplete' component which is used by the new autocomplete enum and oneOfEnum controls when autocomplete is not turned off via the config or ui schema options. We reuse the autocomplete component of '@material-ui/lab'. The new renderers are kept separate in 'extendedMaterialRenderers' as we don't want to force this dependency to all consumers. --- package-lock.json | 12 +++ packages/examples/src/enum.ts | 80 ++++++++++++++++-- packages/material/example/index.ts | 5 +- packages/material/package.json | 4 + .../src/controls/MaterialEnumControl.tsx | 7 +- .../src/controls/MaterialOneOfEnumControl.tsx | 7 +- packages/material/src/controls/index.ts | 2 +- .../MaterialAutocompleteEnumControl.tsx | 55 +++++++++++++ .../MaterialAutocompleteOneOfEnumControl.tsx | 55 +++++++++++++ .../material/src/extended/MuiAutocomplete.tsx | 82 +++++++++++++++++++ packages/material/src/extended/index.ts | 56 +++++++++++++ packages/material/src/index.ts | 2 +- packages/material/tsconfig.json | 3 +- 13 files changed, 350 insertions(+), 20 deletions(-) create mode 100644 packages/material/src/extended/MaterialAutocompleteEnumControl.tsx create mode 100644 packages/material/src/extended/MaterialAutocompleteOneOfEnumControl.tsx create mode 100644 packages/material/src/extended/MuiAutocomplete.tsx create mode 100644 packages/material/src/extended/index.ts diff --git a/package-lock.json b/package-lock.json index 89727442c..c6feaf9c3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2779,6 +2779,18 @@ "@babel/runtime": "^7.4.4" } }, + "@material-ui/lab": { + "version": "4.0.0-alpha.56", + "resolved": "https://registry.npmjs.org/@material-ui/lab/-/lab-4.0.0-alpha.56.tgz", + "integrity": "sha512-xPlkK+z/6y/24ka4gVJgwPfoCF4RCh8dXb1BNE7MtF9bXEBLN/lBxNTK8VAa0qm3V2oinA6xtUIdcRh0aeRtVw==", + "requires": { + "@babel/runtime": "^7.4.4", + "@material-ui/utils": "^4.10.2", + "clsx": "^1.0.4", + "prop-types": "^15.7.2", + "react-is": "^16.8.0" + } + }, "@material-ui/pickers": { "version": "3.2.10", "resolved": "https://registry.npmjs.org/@material-ui/pickers/-/pickers-3.2.10.tgz", diff --git a/packages/examples/src/enum.ts b/packages/examples/src/enum.ts index 2a57dd219..b61a6ad5a 100644 --- a/packages/examples/src/enum.ts +++ b/packages/examples/src/enum.ts @@ -1,7 +1,7 @@ /* The MIT License - Copyright (c) 2017-2019 EclipseSource Munich + Copyright (c) 2017-2020 EclipseSource Munich https://github.com/eclipsesource/jsonforms Permission is hereby granted, free of charge, to any person obtaining a copy @@ -23,7 +23,6 @@ THE SOFTWARE. */ import { registerExamples } from './register'; -import { UISchemaElement } from '@jsonforms/core'; export const schema = { type: 'object', @@ -32,19 +31,90 @@ export const schema = { type: 'string', enum: ['foo', 'bar'] }, + plainEnumSet: { + type: 'string', + enum: ['foo', 'bar'] + }, oneOfEnum: { type: 'string', oneOf: [ {const: 'foo', title: 'Foo'}, {const: 'bar', title: 'Bar'} ] - } + }, + oneOfEnumSet: { + type: 'string', + oneOf: [ + {const: 'foo', title: 'Foo'}, + {const: 'bar', title: 'Bar'} + ] + } } }; -export const uischema: UISchemaElement = undefined; +export const uischema = { + type: 'VerticalLayout', + elements: [ + { + type: 'Group', + label: 'Enums', + elements: [ + { + type: 'Control', + scope: '#/properties/plainEnum' + }, + { + type: 'Control', + scope: '#/properties/plainEnumSet' + }, + { + type: 'Control', + scope: '#/properties/plainEnum', + options: { + autocomplete: false + } + }, + { + type: 'Control', + scope: '#/properties/plainEnumSet', + options: { + autocomplete: false + } + }, + ] + }, + { + type: 'Group', + label: 'One of Enums', + elements: [ + { + type: 'Control', + scope: '#/properties/oneOfEnum' + }, + { + type: 'Control', + scope: '#/properties/oneOfEnumSet' + }, + { + type: 'Control', + scope: '#/properties/oneOfEnum', + options: { + autocomplete: false + } + }, + { + type: 'Control', + scope: '#/properties/oneOfEnumSet', + options: { + autocomplete: false + } + }, + ] + } + ] +}; -export const data = {}; +export const data = {plainEnumSet: 'foo', oneOfEnumSet: 'bar'}; registerExamples([ { diff --git a/packages/material/example/index.ts b/packages/material/example/index.ts index 7a8c512b9..513984f18 100644 --- a/packages/material/example/index.ts +++ b/packages/material/example/index.ts @@ -23,6 +23,7 @@ THE SOFTWARE. */ import { renderExample } from '../../example/src/index'; -import { materialCells, materialRenderers } from '../src'; +import { materialCells } from '../src'; +import { extendedMaterialRenderers } from '../src/extended'; -renderExample(materialRenderers, materialCells); +renderExample(extendedMaterialRenderers, materialCells); diff --git a/packages/material/package.json b/packages/material/package.json index 3dd4c4129..03db3ae9b 100644 --- a/packages/material/package.json +++ b/packages/material/package.json @@ -78,11 +78,15 @@ "@material-ui/icons": "^4.5.1", "react-redux": "^7.1.3" }, + "optionalPeerDependencies": { + "@material-ui/lab": "^4.0.0-alpha.56" + }, "devDependencies": { "@jsonforms/core": "^2.4.0", "@jsonforms/react": "^2.4.0", "@material-ui/core": "^4.7.0", "@material-ui/icons": "^4.5.1", + "@material-ui/lab": "^4.0.0-alpha.56", "@types/enzyme": "^3.10.3", "@types/enzyme-adapter-react-16": "^1.0.5", "@types/react-dom": "^16.8.0", diff --git a/packages/material/src/controls/MaterialEnumControl.tsx b/packages/material/src/controls/MaterialEnumControl.tsx index 6167a8d25..1aea1ef24 100644 --- a/packages/material/src/controls/MaterialEnumControl.tsx +++ b/packages/material/src/controls/MaterialEnumControl.tsx @@ -1,7 +1,7 @@ /* The MIT License - Copyright (c) 2018-2019 EclipseSource Munich + Copyright (c) 2018-2020 EclipseSource Munich https://github.com/eclipsesource/jsonforms Permission is hereby granted, free of charge, to any person obtaining a copy @@ -35,10 +35,7 @@ import { MuiSelect } from '../mui-controls/MuiSelect'; import { MaterialInputControl } from './MaterialInputControl'; export const MaterialEnumControl = (props: ControlProps & OwnPropsOfEnum) => ( - + ); export const materialEnumControlTester: RankedTester = rankWith( diff --git a/packages/material/src/controls/MaterialOneOfEnumControl.tsx b/packages/material/src/controls/MaterialOneOfEnumControl.tsx index eeb7b8076..65c20a28e 100644 --- a/packages/material/src/controls/MaterialOneOfEnumControl.tsx +++ b/packages/material/src/controls/MaterialOneOfEnumControl.tsx @@ -35,14 +35,11 @@ import { MuiSelect } from '../mui-controls/MuiSelect'; import { MaterialInputControl } from './MaterialInputControl'; export const MaterialOneOfEnumControl = (props: ControlProps & OwnPropsOfEnum) => ( - + ); export const materialOneOfEnumControlTester: RankedTester = rankWith( - 10, + 5, isOneOfEnumControl ); diff --git a/packages/material/src/controls/index.ts b/packages/material/src/controls/index.ts index 42d4a6214..3b8f9a6e0 100644 --- a/packages/material/src/controls/index.ts +++ b/packages/material/src/controls/index.ts @@ -112,5 +112,5 @@ export { MaterialAnyOfStringOrEnumControl, materialAnyOfStringOrEnumControlTester, MaterialOneOfEnumControl, - materialOneOfEnumControlTester, + materialOneOfEnumControlTester }; diff --git a/packages/material/src/extended/MaterialAutocompleteEnumControl.tsx b/packages/material/src/extended/MaterialAutocompleteEnumControl.tsx new file mode 100644 index 000000000..6738dea77 --- /dev/null +++ b/packages/material/src/extended/MaterialAutocompleteEnumControl.tsx @@ -0,0 +1,55 @@ +/* + The MIT License + + Copyright (c) 2018-2020 EclipseSource Munich + https://github.com/eclipsesource/jsonforms + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +import React from 'react'; +import { + ControlProps, + isEnumControl, + OwnPropsOfEnum, + RankedTester, + rankWith, +} from '@jsonforms/core'; +import { withJsonFormsEnumProps } from '@jsonforms/react'; +import { MuiSelect } from '../mui-controls/MuiSelect'; +import merge from 'lodash/merge'; +import { MaterialInputControl } from '../controls/MaterialInputControl'; +import {MuiAutocomplete} from './MuiAutocomplete'; + +export const MaterialAutocompleteEnumControl = (props: ControlProps & OwnPropsOfEnum) => { + const {config, uischema} = props; + const appliedUiSchemaOptions = merge({}, config, uischema.options); + return ( + + ); +}; + +export const materialAutocompleteEnumControlTester: RankedTester = rankWith( + 10, + isEnumControl +); + +export default withJsonFormsEnumProps(MaterialAutocompleteEnumControl); diff --git a/packages/material/src/extended/MaterialAutocompleteOneOfEnumControl.tsx b/packages/material/src/extended/MaterialAutocompleteOneOfEnumControl.tsx new file mode 100644 index 000000000..1260ae7f3 --- /dev/null +++ b/packages/material/src/extended/MaterialAutocompleteOneOfEnumControl.tsx @@ -0,0 +1,55 @@ +/* + The MIT License + + Copyright (c) 2018-2020 EclipseSource Munich + https://github.com/eclipsesource/jsonforms + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +import React from 'react'; +import { + ControlProps, + isOneOfEnumControl, + OwnPropsOfEnum, + RankedTester, + rankWith, +} from '@jsonforms/core'; +import { withJsonFormsOneOfEnumProps } from '@jsonforms/react'; +import { MuiAutocomplete } from './MuiAutocomplete'; +import { MuiSelect } from '../mui-controls/MuiSelect'; +import { MaterialInputControl } from '../controls/MaterialInputControl'; +import merge from 'lodash/merge'; + +export const MaterialAutocompleteOneOfEnumControl = (props: ControlProps & OwnPropsOfEnum) => { + const {config, uischema} = props; + const appliedUiSchemaOptions = merge({}, config, uischema.options); + return ( + + ); +}; + +export const materialAutocompleteOneOfEnumControlTester: RankedTester = rankWith( + 10, + isOneOfEnumControl +); + +export default withJsonFormsOneOfEnumProps(MaterialAutocompleteOneOfEnumControl); diff --git a/packages/material/src/extended/MuiAutocomplete.tsx b/packages/material/src/extended/MuiAutocomplete.tsx new file mode 100644 index 000000000..ec650d454 --- /dev/null +++ b/packages/material/src/extended/MuiAutocomplete.tsx @@ -0,0 +1,82 @@ +/* + The MIT License + + Copyright (c) 2017-2020 EclipseSource Munich + https://github.com/eclipsesource/jsonforms + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +import React, {ReactNode} from 'react'; +import { EnumCellProps, EnumOption, WithClassname } from '@jsonforms/core'; + +import Input from '@material-ui/core/Input'; +import Autocomplete, { AutocompleteRenderOptionState } from '@material-ui/lab/Autocomplete'; +import { areEqual } from '@jsonforms/react'; +import merge from 'lodash/merge'; + +export interface WithOptionLabel { + getOptionLabel?(option: EnumOption) : string; + renderOption?(option: EnumOption, state: AutocompleteRenderOptionState): ReactNode; +} + +export const MuiAutocomplete = React.memo((props: EnumCellProps & WithClassname & WithOptionLabel) => { + const { + data, + className, + id, + enabled, + uischema, + path, + handleChange, + options, + config, + getOptionLabel, + renderOption + } = props; + const appliedUiSchemaOptions = merge({}, config, uischema.options); + const [inputValue, setInputValue] = React.useState(data); + + const findOption = options.find(o => o.value === data); + return ( + { + handleChange(path, newValue?.value); + }} + inputValue={inputValue} + onInputChange={(_event, newInputValue) => { + setInputValue(newInputValue); + }} + autoHighlight + autoSelect + autoComplete + fullWidth + options={options} + getOptionLabel={getOptionLabel || (option => option?.label)} + style={{ marginTop: 16 }} + renderInput={params => ( + + )} + renderOption={renderOption} + /> + ); +}, areEqual); diff --git a/packages/material/src/extended/index.ts b/packages/material/src/extended/index.ts new file mode 100644 index 000000000..c6a7445f6 --- /dev/null +++ b/packages/material/src/extended/index.ts @@ -0,0 +1,56 @@ +/* + The MIT License + + Copyright (c) 2018-2020 EclipseSource Munich + https://github.com/eclipsesource/jsonforms + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +import MaterialAutocompleteEnumControl, { + materialAutocompleteEnumControlTester, + MaterialAutocompleteEnumControl as MaterialAutocompleteEnumControlUnwrapped +} from './MaterialAutocompleteEnumControl'; + +import MaterialAutocompleteOneOfEnumControl, { + materialAutocompleteOneOfEnumControlTester, + MaterialAutocompleteOneOfEnumControl as MaterialAutocompleteOneOfEnumControlUnwrapped +} from './MaterialAutocompleteOneOfEnumControl'; + +import { JsonFormsRendererRegistryEntry } from '@jsonforms/core'; +import { materialRenderers } from '../'; + +/** + * Includes all 'materialRenderers' and adds additional renderers based on '@material-ui/lab' + */ +export const extendedMaterialRenderers: JsonFormsRendererRegistryEntry[] = [ + { + tester: materialAutocompleteOneOfEnumControlTester, + renderer: MaterialAutocompleteOneOfEnumControl + }, + { + tester: materialAutocompleteEnumControlTester, + renderer: MaterialAutocompleteEnumControl + }, + ...materialRenderers +]; + +export const ExtendedUnwrapped = { + MaterialAutocompleteEnumControl: MaterialAutocompleteEnumControlUnwrapped, + MaterialAutocompleteOneOfEnumControl: MaterialAutocompleteOneOfEnumControlUnwrapped +}; diff --git a/packages/material/src/index.ts b/packages/material/src/index.ts index 65d11b975..f4c26b19b 100644 --- a/packages/material/src/index.ts +++ b/packages/material/src/index.ts @@ -68,7 +68,7 @@ import { MaterialSliderControl, materialSliderControlTester, MaterialTextControl, - materialTextControlTester + materialTextControlTester, } from './controls'; import { MaterialArrayLayout, diff --git a/packages/material/tsconfig.json b/packages/material/tsconfig.json index e6216e859..00793709c 100644 --- a/packages/material/tsconfig.json +++ b/packages/material/tsconfig.json @@ -8,6 +8,7 @@ "node_modules" ], "files": [ - "./src/index.ts" + "./src/index.ts", + "./src/extended/index.ts" ] }