Skip to content

Commit

Permalink
Add enum autocomplete renderers
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
eneufeld authored Jun 22, 2020
1 parent 432af77 commit 60f2af7
Show file tree
Hide file tree
Showing 13 changed files with 350 additions and 20 deletions.
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 75 additions & 5 deletions packages/examples/src/enum.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -23,7 +23,6 @@
THE SOFTWARE.
*/
import { registerExamples } from './register';
import { UISchemaElement } from '@jsonforms/core';

export const schema = {
type: 'object',
Expand All @@ -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([
{
Expand Down
5 changes: 3 additions & 2 deletions packages/material/example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
4 changes: 4 additions & 0 deletions packages/material/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 2 additions & 5 deletions packages/material/src/controls/MaterialEnumControl.tsx
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -35,10 +35,7 @@ import { MuiSelect } from '../mui-controls/MuiSelect';
import { MaterialInputControl } from './MaterialInputControl';

export const MaterialEnumControl = (props: ControlProps & OwnPropsOfEnum) => (
<MaterialInputControl
{...props}
input={MuiSelect}
/>
<MaterialInputControl {...props} input={MuiSelect} />
);

export const materialEnumControlTester: RankedTester = rankWith(
Expand Down
7 changes: 2 additions & 5 deletions packages/material/src/controls/MaterialOneOfEnumControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,11 @@ import { MuiSelect } from '../mui-controls/MuiSelect';
import { MaterialInputControl } from './MaterialInputControl';

export const MaterialOneOfEnumControl = (props: ControlProps & OwnPropsOfEnum) => (
<MaterialInputControl
{...props}
input={MuiSelect}
/>
<MaterialInputControl {...props} input={MuiSelect} />
);

export const materialOneOfEnumControlTester: RankedTester = rankWith(
10,
5,
isOneOfEnumControl
);

Expand Down
2 changes: 1 addition & 1 deletion packages/material/src/controls/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,5 @@ export {
MaterialAnyOfStringOrEnumControl,
materialAnyOfStringOrEnumControlTester,
MaterialOneOfEnumControl,
materialOneOfEnumControlTester,
materialOneOfEnumControlTester
};
55 changes: 55 additions & 0 deletions packages/material/src/extended/MaterialAutocompleteEnumControl.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<MaterialInputControl
{...props}
input={appliedUiSchemaOptions.autocomplete === false ? MuiSelect : MuiAutocomplete}
/>
);
};

export const materialAutocompleteEnumControlTester: RankedTester = rankWith(
10,
isEnumControl
);

export default withJsonFormsEnumProps(MaterialAutocompleteEnumControl);
Original file line number Diff line number Diff line change
@@ -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 (
<MaterialInputControl
{...props}
input={appliedUiSchemaOptions.autocomplete === false ? MuiSelect : MuiAutocomplete}
/>
);
};

export const materialAutocompleteOneOfEnumControlTester: RankedTester = rankWith(
10,
isOneOfEnumControl
);

export default withJsonFormsOneOfEnumProps(MaterialAutocompleteOneOfEnumControl);
82 changes: 82 additions & 0 deletions packages/material/src/extended/MuiAutocomplete.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<Autocomplete
className={className}
id={id}
disabled={!enabled}
value={findOption}
onChange={(_event: any, newValue: EnumOption | null) => {
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 => (
<Input style={{ width: '100%' }} type='text' inputProps={params.inputProps} inputRef={params.InputProps.ref} autoFocus={appliedUiSchemaOptions.focus}/>
)}
renderOption={renderOption}
/>
);
}, areEqual);
Loading

0 comments on commit 60f2af7

Please sign in to comment.