Skip to content

Commit 60f2af7

Browse files
authored
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.
1 parent 432af77 commit 60f2af7

File tree

13 files changed

+350
-20
lines changed

13 files changed

+350
-20
lines changed

package-lock.json

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/examples/src/enum.ts

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
The MIT License
33
4-
Copyright (c) 2017-2019 EclipseSource Munich
4+
Copyright (c) 2017-2020 EclipseSource Munich
55
https://github.com/eclipsesource/jsonforms
66
77
Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -23,7 +23,6 @@
2323
THE SOFTWARE.
2424
*/
2525
import { registerExamples } from './register';
26-
import { UISchemaElement } from '@jsonforms/core';
2726

2827
export const schema = {
2928
type: 'object',
@@ -32,19 +31,90 @@ export const schema = {
3231
type: 'string',
3332
enum: ['foo', 'bar']
3433
},
34+
plainEnumSet: {
35+
type: 'string',
36+
enum: ['foo', 'bar']
37+
},
3538
oneOfEnum: {
3639
type: 'string',
3740
oneOf: [
3841
{const: 'foo', title: 'Foo'},
3942
{const: 'bar', title: 'Bar'}
4043
]
41-
}
44+
},
45+
oneOfEnumSet: {
46+
type: 'string',
47+
oneOf: [
48+
{const: 'foo', title: 'Foo'},
49+
{const: 'bar', title: 'Bar'}
50+
]
51+
}
4252
}
4353
};
4454

45-
export const uischema: UISchemaElement = undefined;
55+
export const uischema = {
56+
type: 'VerticalLayout',
57+
elements: [
58+
{
59+
type: 'Group',
60+
label: 'Enums',
61+
elements: [
62+
{
63+
type: 'Control',
64+
scope: '#/properties/plainEnum'
65+
},
66+
{
67+
type: 'Control',
68+
scope: '#/properties/plainEnumSet'
69+
},
70+
{
71+
type: 'Control',
72+
scope: '#/properties/plainEnum',
73+
options: {
74+
autocomplete: false
75+
}
76+
},
77+
{
78+
type: 'Control',
79+
scope: '#/properties/plainEnumSet',
80+
options: {
81+
autocomplete: false
82+
}
83+
},
84+
]
85+
},
86+
{
87+
type: 'Group',
88+
label: 'One of Enums',
89+
elements: [
90+
{
91+
type: 'Control',
92+
scope: '#/properties/oneOfEnum'
93+
},
94+
{
95+
type: 'Control',
96+
scope: '#/properties/oneOfEnumSet'
97+
},
98+
{
99+
type: 'Control',
100+
scope: '#/properties/oneOfEnum',
101+
options: {
102+
autocomplete: false
103+
}
104+
},
105+
{
106+
type: 'Control',
107+
scope: '#/properties/oneOfEnumSet',
108+
options: {
109+
autocomplete: false
110+
}
111+
},
112+
]
113+
}
114+
]
115+
};
46116

47-
export const data = {};
117+
export const data = {plainEnumSet: 'foo', oneOfEnumSet: 'bar'};
48118

49119
registerExamples([
50120
{

packages/material/example/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
THE SOFTWARE.
2424
*/
2525
import { renderExample } from '../../example/src/index';
26-
import { materialCells, materialRenderers } from '../src';
26+
import { materialCells } from '../src';
27+
import { extendedMaterialRenderers } from '../src/extended';
2728

28-
renderExample(materialRenderers, materialCells);
29+
renderExample(extendedMaterialRenderers, materialCells);

packages/material/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,15 @@
7878
"@material-ui/icons": "^4.5.1",
7979
"react-redux": "^7.1.3"
8080
},
81+
"optionalPeerDependencies": {
82+
"@material-ui/lab": "^4.0.0-alpha.56"
83+
},
8184
"devDependencies": {
8285
"@jsonforms/core": "^2.4.0",
8386
"@jsonforms/react": "^2.4.0",
8487
"@material-ui/core": "^4.7.0",
8588
"@material-ui/icons": "^4.5.1",
89+
"@material-ui/lab": "^4.0.0-alpha.56",
8690
"@types/enzyme": "^3.10.3",
8791
"@types/enzyme-adapter-react-16": "^1.0.5",
8892
"@types/react-dom": "^16.8.0",

packages/material/src/controls/MaterialEnumControl.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
The MIT License
33
4-
Copyright (c) 2018-2019 EclipseSource Munich
4+
Copyright (c) 2018-2020 EclipseSource Munich
55
https://github.com/eclipsesource/jsonforms
66
77
Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -35,10 +35,7 @@ import { MuiSelect } from '../mui-controls/MuiSelect';
3535
import { MaterialInputControl } from './MaterialInputControl';
3636

3737
export const MaterialEnumControl = (props: ControlProps & OwnPropsOfEnum) => (
38-
<MaterialInputControl
39-
{...props}
40-
input={MuiSelect}
41-
/>
38+
<MaterialInputControl {...props} input={MuiSelect} />
4239
);
4340

4441
export const materialEnumControlTester: RankedTester = rankWith(

packages/material/src/controls/MaterialOneOfEnumControl.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,11 @@ import { MuiSelect } from '../mui-controls/MuiSelect';
3535
import { MaterialInputControl } from './MaterialInputControl';
3636

3737
export const MaterialOneOfEnumControl = (props: ControlProps & OwnPropsOfEnum) => (
38-
<MaterialInputControl
39-
{...props}
40-
input={MuiSelect}
41-
/>
38+
<MaterialInputControl {...props} input={MuiSelect} />
4239
);
4340

4441
export const materialOneOfEnumControlTester: RankedTester = rankWith(
45-
10,
42+
5,
4643
isOneOfEnumControl
4744
);
4845

packages/material/src/controls/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,5 @@ export {
112112
MaterialAnyOfStringOrEnumControl,
113113
materialAnyOfStringOrEnumControlTester,
114114
MaterialOneOfEnumControl,
115-
materialOneOfEnumControlTester,
115+
materialOneOfEnumControlTester
116116
};
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
The MIT License
3+
4+
Copyright (c) 2018-2020 EclipseSource Munich
5+
https://github.com/eclipsesource/jsonforms
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
*/
25+
import React from 'react';
26+
import {
27+
ControlProps,
28+
isEnumControl,
29+
OwnPropsOfEnum,
30+
RankedTester,
31+
rankWith,
32+
} from '@jsonforms/core';
33+
import { withJsonFormsEnumProps } from '@jsonforms/react';
34+
import { MuiSelect } from '../mui-controls/MuiSelect';
35+
import merge from 'lodash/merge';
36+
import { MaterialInputControl } from '../controls/MaterialInputControl';
37+
import {MuiAutocomplete} from './MuiAutocomplete';
38+
39+
export const MaterialAutocompleteEnumControl = (props: ControlProps & OwnPropsOfEnum) => {
40+
const {config, uischema} = props;
41+
const appliedUiSchemaOptions = merge({}, config, uischema.options);
42+
return (
43+
<MaterialInputControl
44+
{...props}
45+
input={appliedUiSchemaOptions.autocomplete === false ? MuiSelect : MuiAutocomplete}
46+
/>
47+
);
48+
};
49+
50+
export const materialAutocompleteEnumControlTester: RankedTester = rankWith(
51+
10,
52+
isEnumControl
53+
);
54+
55+
export default withJsonFormsEnumProps(MaterialAutocompleteEnumControl);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
The MIT License
3+
4+
Copyright (c) 2018-2020 EclipseSource Munich
5+
https://github.com/eclipsesource/jsonforms
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
*/
25+
import React from 'react';
26+
import {
27+
ControlProps,
28+
isOneOfEnumControl,
29+
OwnPropsOfEnum,
30+
RankedTester,
31+
rankWith,
32+
} from '@jsonforms/core';
33+
import { withJsonFormsOneOfEnumProps } from '@jsonforms/react';
34+
import { MuiAutocomplete } from './MuiAutocomplete';
35+
import { MuiSelect } from '../mui-controls/MuiSelect';
36+
import { MaterialInputControl } from '../controls/MaterialInputControl';
37+
import merge from 'lodash/merge';
38+
39+
export const MaterialAutocompleteOneOfEnumControl = (props: ControlProps & OwnPropsOfEnum) => {
40+
const {config, uischema} = props;
41+
const appliedUiSchemaOptions = merge({}, config, uischema.options);
42+
return (
43+
<MaterialInputControl
44+
{...props}
45+
input={appliedUiSchemaOptions.autocomplete === false ? MuiSelect : MuiAutocomplete}
46+
/>
47+
);
48+
};
49+
50+
export const materialAutocompleteOneOfEnumControlTester: RankedTester = rankWith(
51+
10,
52+
isOneOfEnumControl
53+
);
54+
55+
export default withJsonFormsOneOfEnumProps(MaterialAutocompleteOneOfEnumControl);
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
The MIT License
3+
4+
Copyright (c) 2017-2020 EclipseSource Munich
5+
https://github.com/eclipsesource/jsonforms
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
*/
25+
import React, {ReactNode} from 'react';
26+
import { EnumCellProps, EnumOption, WithClassname } from '@jsonforms/core';
27+
28+
import Input from '@material-ui/core/Input';
29+
import Autocomplete, { AutocompleteRenderOptionState } from '@material-ui/lab/Autocomplete';
30+
import { areEqual } from '@jsonforms/react';
31+
import merge from 'lodash/merge';
32+
33+
export interface WithOptionLabel {
34+
getOptionLabel?(option: EnumOption) : string;
35+
renderOption?(option: EnumOption, state: AutocompleteRenderOptionState): ReactNode;
36+
}
37+
38+
export const MuiAutocomplete = React.memo((props: EnumCellProps & WithClassname & WithOptionLabel) => {
39+
const {
40+
data,
41+
className,
42+
id,
43+
enabled,
44+
uischema,
45+
path,
46+
handleChange,
47+
options,
48+
config,
49+
getOptionLabel,
50+
renderOption
51+
} = props;
52+
const appliedUiSchemaOptions = merge({}, config, uischema.options);
53+
const [inputValue, setInputValue] = React.useState(data);
54+
55+
const findOption = options.find(o => o.value === data);
56+
return (
57+
<Autocomplete
58+
className={className}
59+
id={id}
60+
disabled={!enabled}
61+
value={findOption}
62+
onChange={(_event: any, newValue: EnumOption | null) => {
63+
handleChange(path, newValue?.value);
64+
}}
65+
inputValue={inputValue}
66+
onInputChange={(_event, newInputValue) => {
67+
setInputValue(newInputValue);
68+
}}
69+
autoHighlight
70+
autoSelect
71+
autoComplete
72+
fullWidth
73+
options={options}
74+
getOptionLabel={getOptionLabel || (option => option?.label)}
75+
style={{ marginTop: 16 }}
76+
renderInput={params => (
77+
<Input style={{ width: '100%' }} type='text' inputProps={params.inputProps} inputRef={params.InputProps.ref} autoFocus={appliedUiSchemaOptions.focus}/>
78+
)}
79+
renderOption={renderOption}
80+
/>
81+
);
82+
}, areEqual);

0 commit comments

Comments
 (0)