-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(evergreen): add combo-box component to evergreen component mapper
combo-box will be treated similar to a select component, with a required format for an array of objects to populate items
- Loading branch information
Daniel Warke
committed
Aug 23, 2023
1 parent
663f904
commit d913ef3
Showing
5 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
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
68 changes: 68 additions & 0 deletions
68
packages/evergreen-component-mapper/src/combo-box/combo-box.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,68 @@ | ||
import React, { useMemo } from 'react'; | ||
|
||
import { Combobox as EvergreenCombobox, FormField } from 'evergreen-ui'; | ||
|
||
import useFieldApi from '@data-driven-forms/react-form-renderer/use-field-api'; | ||
import { | ||
AnyObject, | ||
UseFieldApiProps, | ||
} from '@data-driven-forms/react-form-renderer'; | ||
|
||
export interface ComboBoxItem extends AnyObject { | ||
value?: any; | ||
label: string; | ||
} | ||
|
||
export interface ComboBoxProps extends UseFieldApiProps<string> { | ||
name: string; | ||
isRequired?: boolean; | ||
options: ComboBoxItem[]; | ||
} | ||
|
||
const ComboBox: React.FC<ComboBoxProps> = (props) => { | ||
const { | ||
id, | ||
input, | ||
meta, | ||
isDisabled, | ||
options, | ||
isRequired, | ||
label, | ||
description, | ||
inputProps, | ||
...rest | ||
} = useFieldApi(props); | ||
|
||
const selectedItem = useMemo(() => { | ||
return options.find((item: ComboBoxItem) => item.value === input.value); | ||
}, [input.value, options]); | ||
|
||
return ( | ||
<FormField | ||
labelFor={id} | ||
label={label} | ||
description={description} | ||
isRequired={isRequired} | ||
validationMessage={meta.error} | ||
marginBottom={24} | ||
> | ||
<EvergreenCombobox | ||
id={id} | ||
width="100%" | ||
selectedItem={selectedItem} | ||
onChange={(item?: ComboBoxItem) => input.onChange(item?.value)} | ||
disabled={isDisabled} | ||
items={options} | ||
itemToString={(item?: ComboBoxItem) => (item ? item.label : '')} | ||
inputProps={{ | ||
required: isRequired, | ||
isInvalid: Boolean(meta.error), | ||
...inputProps, | ||
}} | ||
{...rest} | ||
/> | ||
</FormField> | ||
); | ||
}; | ||
|
||
export default ComboBox; |
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,2 @@ | ||
export { default } from './combo-box'; | ||
export * from './combo-box'; |
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