-
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.
Merge pull request #25 from dshk0718/main
Wrap `text-field` with `Autocomplete` to provide a way to add text suggestions
- Loading branch information
Showing
1 changed file
with
21 additions
and
12 deletions.
There are no files selected for viewing
33 changes: 21 additions & 12 deletions
33
packages/evergreen-component-mapper/src/text-field/text-field.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 |
---|---|---|
@@ -1,25 +1,34 @@ | ||
import React from 'react'; | ||
|
||
import { TextInputField } from 'evergreen-ui'; | ||
import { Autocomplete, TextInputField } from 'evergreen-ui'; | ||
|
||
import useFieldApi from '@data-driven-forms/react-form-renderer/use-field-api'; | ||
import { UseFieldApiProps } from '@data-driven-forms/react-form-renderer'; | ||
|
||
export interface TextFieldProps extends UseFieldApiProps<string> { | ||
name: string; | ||
isRequired?: boolean; | ||
}; | ||
name: string; | ||
items?: string[]; | ||
isRequired?: boolean; | ||
} | ||
|
||
const TextField: React.FC<TextFieldProps> = (props) => { | ||
const { input, meta, isRequired, ...rest } = useFieldApi(props); | ||
const { input, meta, isRequired, items, ...rest } = useFieldApi(props) as TextFieldProps; | ||
|
||
return <TextInputField | ||
{...input} | ||
required={isRequired} | ||
isInvalid={Boolean(meta.error)} | ||
validationMessage={meta.error} | ||
{...rest} | ||
/>; | ||
return ( | ||
<Autocomplete {...input} items={items || []}> | ||
{({ getInputProps, getRef, inputValue, openMenu }) => ( | ||
<TextInputField | ||
ref={getRef} | ||
required={isRequired} | ||
isInvalid={Boolean(meta.error)} | ||
validationMessage={meta.error} | ||
{...getInputProps({ onFocus: () => openMenu() })} | ||
value={inputValue} | ||
{...rest} | ||
/> | ||
)} | ||
</Autocomplete> | ||
); | ||
}; | ||
|
||
export default TextField; |