From 54fd043a33c512f8909f5e39383eeb594dd27e7a Mon Sep 17 00:00:00 2001 From: Ivan Gabriele Date: Wed, 14 Dec 2022 11:55:57 +0100 Subject: [PATCH] feat(formiks): add FormikAutoComplete --- .eslintrc.js | 1 + src/fields/AutoComplete.tsx | 34 +- src/formiks/FormikAutoComplete.tsx | 27 + src/index.ts | 2 + stories/Icon.stories.tsx | 754 +++++++++--------- stories/components/Dropdown.stories.tsx | 2 +- stories/elements/Button.stories.tsx | 640 +++++++-------- stories/elements/Field.stories.tsx | 6 +- stories/elements/Fieldset.stories.tsx | 12 +- stories/elements/IconButton.stories.tsx | 20 +- stories/elements/Label.stories.tsx | 4 +- stories/elements/Legend.stories.tsx | 4 +- stories/elements/Tag.stories.tsx | 12 +- .../AutoComplete/WithOptions.stories.tsx | 9 +- .../fields/AutoComplete/WithQuery.stories.tsx | 1 + stories/fields/Checkbox.stories.tsx | 2 +- stories/fields/DatePicker.stories.tsx | 2 +- stories/fields/DateRangePicker.stories.tsx | 2 +- stories/fields/Select.stories.tsx | 4 +- .../AutoComplete/WithOptions.stories.tsx | 54 ++ .../AutoComplete/WithQuery.stories.tsx | 53 ++ stories/formiks/FormikCheckbox.stories.tsx | 2 +- stories/formiks/FormikDatePicker.stories.tsx | 2 +- .../formiks/FormikDateRangePicker.stories.tsx | 2 +- stories/formiks/FormikEffect.stories.tsx | 2 +- .../formiks/FormikMultiCheckbox.stories.tsx | 2 +- stories/formiks/FormikMultiRadio.stories.tsx | 2 +- stories/formiks/FormikMultiSelect.stories.tsx | 2 +- stories/formiks/FormikSelect.stories.tsx | 6 +- stories/formiks/FormikTextInput.stories.tsx | 2 +- stories/formiks/FormikTextarea.stories.tsx | 2 +- 31 files changed, 920 insertions(+), 749 deletions(-) create mode 100644 src/formiks/FormikAutoComplete.tsx create mode 100644 stories/formiks/AutoComplete/WithOptions.stories.tsx create mode 100644 stories/formiks/AutoComplete/WithQuery.stories.tsx diff --git a/.eslintrc.js b/.eslintrc.js index 66f63669d..b10984954 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -123,6 +123,7 @@ module.exports = { 'import/no-default-export': 'off', + 'react/destructuring-assignment': 'off', 'react/function-component-definition': 'off', 'sort-keys-fix/sort-keys-fix': 'off' diff --git a/src/fields/AutoComplete.tsx b/src/fields/AutoComplete.tsx index 89961e7bb..18fee195e 100644 --- a/src/fields/AutoComplete.tsx +++ b/src/fields/AutoComplete.tsx @@ -1,5 +1,6 @@ import ky from 'ky' -import { useCallback, useMemo, useState } from 'react' +import { propEq } from 'ramda' +import { useCallback, useEffect, useMemo, useRef, useState } from 'react' import { AutoComplete as RsuiteAutoComplete } from 'rsuite' import styled from 'styled-components' @@ -25,6 +26,7 @@ export type AutoCompleteProps = Omit< queryUrl?: string } export function AutoComplete({ + defaultValue, isLabelHidden, isLight = false, label, @@ -35,19 +37,24 @@ export function AutoComplete({ queryUrl, ...originalProps }: AutoCompleteProps) { + const prevDefaultValuePropRef = useRef(defaultValue) + + const [controlledDefaultValue, setDefaultControlledValue] = useState(defaultValue) const [autoGeneratedOptions, setAutoGeneratedOptions] = useState([]) const controlledOptions = useMemo(() => options ?? autoGeneratedOptions, [autoGeneratedOptions, options]) + const controlledValueAsLabel = useMemo(() => { + const foundOption = controlledOptions.find(propEq('value', controlledDefaultValue)) + + return foundOption ? foundOption.label : undefined + }, [controlledDefaultValue, controlledOptions]) const key = useMemo( - () => `${originalProps.name}-${JSON.stringify(originalProps.defaultValue)}`, - [originalProps.defaultValue, originalProps.name] + () => `${originalProps.name}-${JSON.stringify(controlledDefaultValue)}`, + [controlledDefaultValue, originalProps.name] ) const handleChange = useCallback( async (nextQuery: string) => { - // console.log(nextQuery) - // console.log(nextQuery.length) - if (queryUrl && queryMap) { const url = queryUrl.replace('%s', nextQuery) const rawData: Record[] = await ky.get(url).json() @@ -71,15 +78,23 @@ export function AutoComplete({ const handleSelect = useCallback( (nextValue: string) => { - if (!onChange) { - return + if (onChange) { + onChange(nextValue) } - onChange(nextValue) + setDefaultControlledValue(nextValue) }, [onChange] ) + useEffect(() => { + if (defaultValue === prevDefaultValuePropRef.current) { + return + } + + setDefaultControlledValue(defaultValue) + }, [defaultValue]) + return (