From a843ac175b86d0f3fdc4f5e563559d2a2c9aed9f Mon Sep 17 00:00:00 2001 From: Moritz Mock Date: Thu, 30 Sep 2021 13:31:36 +0200 Subject: [PATCH 1/2] removed React.forwardRef from SelectItem Component --- src/components/form/async/SelectItem.tsx | 229 ++++++++++------------- 1 file changed, 102 insertions(+), 127 deletions(-) diff --git a/src/components/form/async/SelectItem.tsx b/src/components/form/async/SelectItem.tsx index 9f00798d..e2e1db56 100644 --- a/src/components/form/async/SelectItem.tsx +++ b/src/components/form/async/SelectItem.tsx @@ -1,11 +1,10 @@ -import React, { useMemo, useState } from 'react' +import React, { ReactElement, useMemo, useState } from 'react' import { useField } from 'formik' import classnames from 'classnames' import IconKeyboardArrowDown from '@aboutbits/react-material-icons/dist/IconKeyboardArrowDown' import IconClose from '@aboutbits/react-material-icons/dist/IconClose' - -import { useInternationalization } from '../../../framework' import { useCustomInputCss } from '../useCustomInputCss' +import { useInternationalization } from '../../../framework' import { InputLabel } from '../InputLabel' import { InputError } from '../InputError' import { @@ -13,26 +12,23 @@ import { Props as DialogProps, } from './SelectItemDialogWithSearch' -type Props = React.DetailedHTMLProps< - React.ButtonHTMLAttributes, - HTMLButtonElement -> & { +type Props = { + disabled?: boolean id: string name: string label: string placeholder: string - disabled?: boolean defaultValue: ItemType } & Pick< - DialogProps, - | 'useGetData' - | 'dialogTitle' - | 'dialogLabel' - | 'noSearchResults' - | 'renderListItem' - | 'renderErrorMessage' - | 'paginationConfig' - > + DialogProps, + | 'useGetData' + | 'dialogTitle' + | 'dialogLabel' + | 'noSearchResults' + | 'renderListItem' + | 'renderErrorMessage' + | 'paginationConfig' +> export type ReferenceObject = { id: string | number @@ -40,8 +36,6 @@ export type ReferenceObject = { label?: string } -type ItemType = ReferenceObject - /** * Converts tailwindcss classes from placeholder to text. * @@ -70,120 +64,101 @@ export const replacePlaceholderColorWithTextColor = (css: string): string => { ) } -export const SelectItem = React.forwardRef< - HTMLButtonElement, - Props ->( - ( - { - disabled = false, - id, - name, - label, - placeholder, - defaultValue, - useGetData, - dialogTitle, - dialogLabel, - noSearchResults, - renderListItem, - renderErrorMessage, - paginationConfig, - }, - ref - ) => { - const [field, , helpers] = useField(name) - const [, , helpersId] = useField(name + '.id') - const [showDialog, setShowDialog] = useState(false) - const customCss = useCustomInputCss(`${field.name}.id`, disabled) - const internationalization = useInternationalization() - const customCssInputCss = useMemo( - () => replacePlaceholderColorWithTextColor(customCss.inputCss), - [customCss.inputCss] - ) +export function SelectItem({ + disabled = false, + id, + name, + label, + placeholder, + defaultValue, + useGetData, + dialogTitle, + dialogLabel, + noSearchResults, + renderListItem, + renderErrorMessage, + paginationConfig, +}: Props): ReactElement { + const [field, , helpers] = useField(name) + const [, , helpersId] = useField(name + '.id') + const [showDialog, setShowDialog] = useState(false) + const customCss = useCustomInputCss(`${field.name}.id`, disabled) + const internationalization = useInternationalization() + const customCssInputCss = useMemo( + () => replacePlaceholderColorWithTextColor(customCss.inputCss), + [customCss.inputCss] + ) - return ( - <> -
- - {field.value.id === '' || field.value.id === 0 ? ( + return ( + <> +
+ + {field.value.id === '' || field.value.id === 0 ? ( + + ) : ( +
+ - ) : ( -
- - -
- )} - -
- {showDialog && ( - { - helpersId.setTouched(true) - setShowDialog(false) - }} - isOpen={showDialog} - onConfirm={(item: ItemType) => { - helpersId.setTouched(true) - helpers.setValue(item) - setShowDialog(false) - }} - useGetData={useGetData} - renderListItem={renderListItem} - renderErrorMessage={renderErrorMessage} - dialogTitle={dialogTitle} - dialogLabel={dialogLabel} - noSearchResults={noSearchResults} - paginationConfig={paginationConfig} - /> +
)} - - ) - } -) - -SelectItem.displayName = 'SelectItem' + +
+ {showDialog && ( + { + helpersId.setTouched(true) + setShowDialog(false) + }} + isOpen={showDialog} + onConfirm={(item: ItemType) => { + helpersId.setTouched(true) + helpers.setValue(item) + setShowDialog(false) + }} + useGetData={useGetData} + renderListItem={renderListItem} + renderErrorMessage={renderErrorMessage} + dialogTitle={dialogTitle} + dialogLabel={dialogLabel} + noSearchResults={noSearchResults} + paginationConfig={paginationConfig} + /> + )} + + ) +} From b99a08f2216e9c1e83aea5ca7cca74e3e8515809 Mon Sep 17 00:00:00 2001 From: Moritz Mock Date: Thu, 30 Sep 2021 13:33:51 +0200 Subject: [PATCH 2/2] adjusted ordering of parameters --- src/components/form/async/SelectItem.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/form/async/SelectItem.tsx b/src/components/form/async/SelectItem.tsx index e2e1db56..fdcc3272 100644 --- a/src/components/form/async/SelectItem.tsx +++ b/src/components/form/async/SelectItem.tsx @@ -13,11 +13,11 @@ import { } from './SelectItemDialogWithSearch' type Props = { - disabled?: boolean id: string name: string label: string placeholder: string + disabled?: boolean defaultValue: ItemType } & Pick< DialogProps,