Skip to content

Commit 544c05b

Browse files
authored
Extend AutocompleteControl to allow custom filterOptions
* Extend AutocompleteControl * Extend enum example * Add custom autocomplete renderer for example
1 parent 34b1e43 commit 544c05b

File tree

5 files changed

+85
-6
lines changed

5 files changed

+85
-6
lines changed

packages/example/src/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,13 @@ const setupStore = (
115115
export const renderExample = (
116116
renderers: { tester: RankedTester; renderer: any }[],
117117
cells: { tester: RankedTester; cell: any }[],
118+
enhancer?: (examples: ReactExampleDescription[]) => ReactExampleDescription[],
118119
...additionalStoreParams: AdditionalStoreParams[]
119120
) => {
120121
const exampleData = enhanceExample(getExamples());
122+
const enhancedExampleData = enhancer ? enhancer(exampleData) : exampleData;
121123
const store = setupStore(
122-
exampleData,
124+
enhancedExampleData,
123125
cells,
124126
renderers,
125127
additionalStoreParams

packages/examples/src/enum.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,16 @@ export const schema = {
3939
type: 'string',
4040
oneOf: [
4141
{const: 'foo', title: 'Foo'},
42-
{const: 'bar', title: 'Bar'}
42+
{const: 'bar', title: 'Bar'},
43+
{const: 'foobar', title: 'FooBar'}
4344
]
4445
},
4546
oneOfEnumSet: {
4647
type: 'string',
4748
oneOf: [
4849
{const: 'foo', title: 'Foo'},
49-
{const: 'bar', title: 'Bar'}
50+
{const: 'bar', title: 'Bar'},
51+
{const: 'foobar', title: 'FooBar'}
5052
]
5153
}
5254
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import {
2+
ControlProps,
3+
OwnPropsOfEnum,
4+
RankedTester,
5+
withIncreasedRank,
6+
Actions
7+
} from '@jsonforms/core';
8+
import { ExtendedUnwrapped } from '../src/extended';
9+
import { materialAutocompleteOneOfEnumControlTester } from '../src/extended/MaterialAutocompleteOneOfEnumControl';
10+
import { withJsonFormsOneOfEnumProps } from '@jsonforms/react';
11+
import React from 'react';
12+
import Typography from '@material-ui/core/Typography';
13+
import { AnyAction, Dispatch } from 'redux';
14+
const MyAutocompleteControl = (props: ControlProps & OwnPropsOfEnum) => {
15+
return (
16+
<ExtendedUnwrapped.MaterialAutocompleteOneOfEnumControl
17+
{...props}
18+
renderOption={option => (<Typography>{`${option?.value}\t-\t${option?.label}`}</Typography>)}
19+
filterOptions={(options, state) => options.filter(o => o.label.includes(state.inputValue) || o.value.includes(state.inputValue))}
20+
/>
21+
);
22+
};
23+
24+
const myAutocompleteTester: RankedTester = withIncreasedRank(
25+
1,
26+
materialAutocompleteOneOfEnumControlTester
27+
);
28+
29+
const ConnectedControl = withJsonFormsOneOfEnumProps(MyAutocompleteControl);
30+
31+
32+
export const ExampleExtension = (dispatch: Dispatch<AnyAction>) => (
33+
<div>
34+
<button
35+
onClick={() =>
36+
dispatch(
37+
Actions.registerRenderer(myAutocompleteTester, ConnectedControl)
38+
)
39+
}
40+
>
41+
Register Custom OneOf Autocomplete
42+
</button>
43+
<button
44+
onClick={() =>
45+
dispatch(
46+
Actions.unregisterRenderer(myAutocompleteTester, ConnectedControl)
47+
)
48+
}
49+
>
50+
Unregister Custom OneOf Autocomplete
51+
</button>
52+
</div>
53+
);

packages/material/example/index.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,23 @@
2525
import { renderExample } from '../../example/src/index';
2626
import { materialCells } from '../src';
2727
import { extendedMaterialRenderers } from '../src/extended';
28+
import { ExampleExtension } from './CustomAutocomplete';
29+
import { ReactExampleDescription } from '../../example/src/util';
2830

29-
renderExample(extendedMaterialRenderers, materialCells);
31+
const addCustomAutocompleteControl = (examples: ReactExampleDescription[]) => {
32+
return examples.map(example => {
33+
if(example.name === 'enum'){
34+
const adjustedExample = Object.assign({}, example, {
35+
customReactExtension: ExampleExtension
36+
})
37+
return adjustedExample;
38+
}
39+
return example;
40+
});
41+
};
42+
43+
renderExample(
44+
extendedMaterialRenderers,
45+
materialCells,
46+
addCustomAutocompleteControl
47+
);

packages/material/src/extended/MuiAutocomplete.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,19 @@
2222
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2323
THE SOFTWARE.
2424
*/
25-
import React, {ReactNode} from 'react';
25+
import React, { ReactNode } from 'react';
2626
import { EnumCellProps, EnumOption, WithClassname } from '@jsonforms/core';
2727

2828
import Input from '@material-ui/core/Input';
2929
import Autocomplete, { AutocompleteRenderOptionState } from '@material-ui/lab/Autocomplete';
3030
import { areEqual } from '@jsonforms/react';
3131
import merge from 'lodash/merge';
32+
import { FilterOptionsState } from '@material-ui/lab/useAutocomplete';
3233

3334
export interface WithOptionLabel {
3435
getOptionLabel?(option: EnumOption) : string;
3536
renderOption?(option: EnumOption, state: AutocompleteRenderOptionState): ReactNode;
37+
filterOptions?(options: EnumOption[], state: FilterOptionsState<EnumOption>) : EnumOption[];
3638
}
3739

3840
export const MuiAutocomplete = React.memo((props: EnumCellProps & WithClassname & WithOptionLabel) => {
@@ -47,7 +49,8 @@ export const MuiAutocomplete = React.memo((props: EnumCellProps & WithClassname
4749
options,
4850
config,
4951
getOptionLabel,
50-
renderOption
52+
renderOption,
53+
filterOptions
5154
} = props;
5255
const appliedUiSchemaOptions = merge({}, config, uischema.options);
5356
const [inputValue, setInputValue] = React.useState(data ?? '');
@@ -77,6 +80,7 @@ export const MuiAutocomplete = React.memo((props: EnumCellProps & WithClassname
7780
<Input style={{ width: '100%' }} type='text' inputProps={params.inputProps} inputRef={params.InputProps.ref} autoFocus={appliedUiSchemaOptions.focus}/>
7881
)}
7982
renderOption={renderOption}
83+
filterOptions={filterOptions}
8084
/>
8185
);
8286
}, areEqual);

0 commit comments

Comments
 (0)