-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
13 changed files
with
350 additions
and
20 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
packages/material/src/extended/MaterialAutocompleteEnumControl.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
55 changes: 55 additions & 0 deletions
55
packages/material/src/extended/MaterialAutocompleteOneOfEnumControl.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Oops, something went wrong.