From 8669f11328675d9585b6aee60be58ec2f60cb479 Mon Sep 17 00:00:00 2001 From: gitstart Date: Mon, 7 Aug 2023 10:29:05 +0000 Subject: [PATCH 1/2] resolve generic type Co-authored-by: passy4ucj Co-authored-by: GitStart <1501599+gitstart@users.noreply.github.com> Co-authored-by: gitstart_bot --- packages/ra-core/src/form/useChoices.tsx | 4 ++-- packages/ra-ui-materialui/src/field/SelectField.tsx | 2 +- packages/ra-ui-materialui/src/input/AutocompleteInput.tsx | 2 +- packages/ra-ui-materialui/src/input/CheckboxGroupInput.tsx | 2 +- packages/ra-ui-materialui/src/input/DatagridInput.tsx | 2 +- packages/ra-ui-materialui/src/input/RadioButtonGroupInput.tsx | 2 +- packages/ra-ui-materialui/src/input/SelectArrayInput.tsx | 2 +- packages/ra-ui-materialui/src/input/SelectInput.tsx | 2 +- 8 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/ra-core/src/form/useChoices.tsx b/packages/ra-core/src/form/useChoices.tsx index 9c6033c023d..0e07e877708 100644 --- a/packages/ra-core/src/form/useChoices.tsx +++ b/packages/ra-core/src/form/useChoices.tsx @@ -12,8 +12,8 @@ export type OptionTextElement = ReactElement<{ export type OptionTextFunc = (choice: any) => React.ReactNode; export type OptionText = OptionTextElement | OptionTextFunc | string; -export interface ChoicesProps { - choices?: ReadonlyArray; +export interface ChoicesProps { + choices?: T[]; isFetching?: boolean; isLoading?: boolean; optionValue?: string; diff --git a/packages/ra-ui-materialui/src/field/SelectField.tsx b/packages/ra-ui-materialui/src/field/SelectField.tsx index e9ecc058b15..1a8d8acdb8e 100644 --- a/packages/ra-ui-materialui/src/field/SelectField.tsx +++ b/packages/ra-ui-materialui/src/field/SelectField.tsx @@ -148,6 +148,6 @@ export const SelectField = genericMemo(SelectFieldImpl); export interface SelectFieldProps< RecordType extends Record = Record -> extends ChoicesProps, +> extends ChoicesProps, FieldProps, Omit {} diff --git a/packages/ra-ui-materialui/src/input/AutocompleteInput.tsx b/packages/ra-ui-materialui/src/input/AutocompleteInput.tsx index 7496f2d8522..958419dca1e 100644 --- a/packages/ra-ui-materialui/src/input/AutocompleteInput.tsx +++ b/packages/ra-ui-materialui/src/input/AutocompleteInput.tsx @@ -682,7 +682,7 @@ export interface AutocompleteInputProps< DisableClearable extends boolean | undefined = false, SupportCreate extends boolean | undefined = false > extends Omit, - ChoicesProps, + ChoicesProps, UseSuggestionsOptions, Omit, Omit< diff --git a/packages/ra-ui-materialui/src/input/CheckboxGroupInput.tsx b/packages/ra-ui-materialui/src/input/CheckboxGroupInput.tsx index f643d5d659b..e60926d6cf6 100644 --- a/packages/ra-ui-materialui/src/input/CheckboxGroupInput.tsx +++ b/packages/ra-ui-materialui/src/input/CheckboxGroupInput.tsx @@ -284,7 +284,7 @@ CheckboxGroupInput.propTypes = { }; export type CheckboxGroupInputProps = Omit & - ChoicesProps & + ChoicesProps & CheckboxProps & FormControlProps & { options?: CheckboxProps; diff --git a/packages/ra-ui-materialui/src/input/DatagridInput.tsx b/packages/ra-ui-materialui/src/input/DatagridInput.tsx index a1232431017..8c38b234941 100644 --- a/packages/ra-ui-materialui/src/input/DatagridInput.tsx +++ b/packages/ra-ui-materialui/src/input/DatagridInput.tsx @@ -160,7 +160,7 @@ export const DatagridInput = (props: DatagridInputProps) => { }; export type DatagridInputProps = Omit & - ChoicesProps & + ChoicesProps & Omit & DatagridProps & { children?: ReactNode; diff --git a/packages/ra-ui-materialui/src/input/RadioButtonGroupInput.tsx b/packages/ra-ui-materialui/src/input/RadioButtonGroupInput.tsx index 3e75a4c25ce..89e3e3668d9 100644 --- a/packages/ra-ui-materialui/src/input/RadioButtonGroupInput.tsx +++ b/packages/ra-ui-materialui/src/input/RadioButtonGroupInput.tsx @@ -269,7 +269,7 @@ const sanitizeRestProps = ({ }: any) => sanitizeInputRestProps(rest); export type RadioButtonGroupInputProps = Omit & - ChoicesProps & + ChoicesProps & FormControlProps & RadioGroupProps & { options?: RadioGroupProps; diff --git a/packages/ra-ui-materialui/src/input/SelectArrayInput.tsx b/packages/ra-ui-materialui/src/input/SelectArrayInput.tsx index f4baad6e260..b06c28fceaa 100644 --- a/packages/ra-ui-materialui/src/input/SelectArrayInput.tsx +++ b/packages/ra-ui-materialui/src/input/SelectArrayInput.tsx @@ -358,7 +358,7 @@ export const SelectArrayInput = (props: SelectArrayInputProps) => { ); }; -export type SelectArrayInputProps = ChoicesProps & +export type SelectArrayInputProps = ChoicesProps & Omit & Omit & Omit & { diff --git a/packages/ra-ui-materialui/src/input/SelectInput.tsx b/packages/ra-ui-materialui/src/input/SelectInput.tsx index 97f5701473e..f6ee710eb33 100644 --- a/packages/ra-ui-materialui/src/input/SelectInput.tsx +++ b/packages/ra-ui-materialui/src/input/SelectInput.tsx @@ -423,7 +423,7 @@ const StyledResettableTextField = styled(ResettableTextField, { })); export type SelectInputProps = Omit & - ChoicesProps & + ChoicesProps & Omit & Omit & { disableValue?: string; From f2d5f25b7936895a892d0606e001ce2b8435ee9a Mon Sep 17 00:00:00 2001 From: gitstart Date: Wed, 8 Nov 2023 09:30:01 +0000 Subject: [PATCH 2/2] refactor: use readonly array type for choices Co-authored-by: Azerii <32432927+Azerii@users.noreply.github.com> --- packages/ra-core/src/controller/list/useList.ts | 6 ++++-- .../ra-core/src/form/choices/useChoicesContext.ts | 4 +++- packages/ra-core/src/form/useChoices.tsx | 2 +- .../src/input/AutocompleteInput.tsx | 13 ++++--------- .../src/input/CheckboxGroupInput.tsx | 2 +- .../ra-ui-materialui/src/input/DatagridInput.tsx | 2 +- .../src/input/RadioButtonGroupInput.tsx | 2 +- .../ra-ui-materialui/src/input/SelectArrayInput.tsx | 2 +- packages/ra-ui-materialui/src/input/SelectInput.tsx | 2 +- 9 files changed, 17 insertions(+), 18 deletions(-) diff --git a/packages/ra-core/src/controller/list/useList.ts b/packages/ra-core/src/controller/list/useList.ts index 9edeb317010..746ec861e0e 100644 --- a/packages/ra-core/src/controller/list/useList.ts +++ b/packages/ra-core/src/controller/list/useList.ts @@ -55,7 +55,6 @@ export const useList = ( props: UseListOptions ): UseListValue => { const { - data, error, filter = defaultFilter, isFetching = false, @@ -67,6 +66,9 @@ export const useList = ( } = props; const resource = useResourceContext(props); + // Make copy of data so it becomes mutable + const data = props.data ? [...props.data] : undefined; + const [fetchingState, setFetchingState] = useSafeSetState( isFetching ) as [boolean, (isFetching: boolean) => void]; @@ -276,7 +278,7 @@ export const useList = ( }; export interface UseListOptions { - data?: RecordType[]; + data?: readonly RecordType[]; error?: any; filter?: FilterPayload; isFetching?: boolean; diff --git a/packages/ra-core/src/form/choices/useChoicesContext.ts b/packages/ra-core/src/form/choices/useChoicesContext.ts index 93df8d3ccc8..9365bcff340 100644 --- a/packages/ra-core/src/form/choices/useChoicesContext.ts +++ b/packages/ra-core/src/form/choices/useChoicesContext.ts @@ -4,7 +4,9 @@ import { useList } from '../../controller'; import { ChoicesContext, ChoicesContextValue } from './ChoicesContext'; export const useChoicesContext = ( - options: Partial & { choices?: ChoicesType[] } = {} + options: Partial & { + choices?: readonly ChoicesType[]; + } = {} ): ChoicesContextValue => { const context = useContext(ChoicesContext) as ChoicesContextValue< ChoicesType diff --git a/packages/ra-core/src/form/useChoices.tsx b/packages/ra-core/src/form/useChoices.tsx index ee77d31e337..9c6033c023d 100644 --- a/packages/ra-core/src/form/useChoices.tsx +++ b/packages/ra-core/src/form/useChoices.tsx @@ -13,7 +13,7 @@ export type OptionTextFunc = (choice: any) => React.ReactNode; export type OptionText = OptionTextElement | OptionTextFunc | string; export interface ChoicesProps { - choices?: readonly any[]; + choices?: ReadonlyArray; isFetching?: boolean; isLoading?: boolean; optionValue?: string; diff --git a/packages/ra-ui-materialui/src/input/AutocompleteInput.tsx b/packages/ra-ui-materialui/src/input/AutocompleteInput.tsx index 0e1a2b33f3d..684b2f9c632 100644 --- a/packages/ra-ui-materialui/src/input/AutocompleteInput.tsx +++ b/packages/ra-ui-materialui/src/input/AutocompleteInput.tsx @@ -188,7 +188,7 @@ export const AutocompleteInput = < setFilters, isFromReference, } = useChoicesContext({ - choices: choicesProp as any[], + choices: choicesProp, isFetching: isFetchingProp, isLoading: isLoadingProp, resource: resourceProp, @@ -763,23 +763,18 @@ const useSelectedChoice = < return selectedChoice || null; }; -const getSelectedItems = ( - choices: readonly any[] = [], - value, - optionValue = 'id', - multiple -) => { +const getSelectedItems = (choices, value, optionValue = 'id', multiple) => { if (multiple) { return (Array.isArray(value ?? []) ? value : [value]) .map(item => - choices.find( + choices?.find( choice => String(item) === String(get(choice, optionValue)) ) ) .filter(item => !!item); } return ( - choices.find( + choices?.find( choice => String(get(choice, optionValue)) === String(value) ) || '' ); diff --git a/packages/ra-ui-materialui/src/input/CheckboxGroupInput.tsx b/packages/ra-ui-materialui/src/input/CheckboxGroupInput.tsx index ced509a17b3..b1eedfdfcad 100644 --- a/packages/ra-ui-materialui/src/input/CheckboxGroupInput.tsx +++ b/packages/ra-ui-materialui/src/input/CheckboxGroupInput.tsx @@ -118,7 +118,7 @@ export const CheckboxGroupInput: FunctionComponent = pr source, isFromReference, } = useChoicesContext({ - choices: choicesProp as any[], + choices: choicesProp, isFetching: isFetchingProp, isLoading: isLoadingProp, resource: resourceProp, diff --git a/packages/ra-ui-materialui/src/input/DatagridInput.tsx b/packages/ra-ui-materialui/src/input/DatagridInput.tsx index 7cb55cfa830..a1232431017 100644 --- a/packages/ra-ui-materialui/src/input/DatagridInput.tsx +++ b/packages/ra-ui-materialui/src/input/DatagridInput.tsx @@ -68,7 +68,7 @@ export const DatagridInput = (props: DatagridInputProps) => { source, ...choicesContext } = useChoicesContext({ - choices: choices as any[], + choices, resource: resourceProp, source: sourceProp, }); diff --git a/packages/ra-ui-materialui/src/input/RadioButtonGroupInput.tsx b/packages/ra-ui-materialui/src/input/RadioButtonGroupInput.tsx index 9efd17d34a7..109ff69a1c0 100644 --- a/packages/ra-ui-materialui/src/input/RadioButtonGroupInput.tsx +++ b/packages/ra-ui-materialui/src/input/RadioButtonGroupInput.tsx @@ -112,7 +112,7 @@ export const RadioButtonGroupInput = (props: RadioButtonGroupInputProps) => { source, isFromReference, } = useChoicesContext({ - choices: choicesProp as any[], + choices: choicesProp, isFetching: isFetchingProp, isLoading: isLoadingProp, resource: resourceProp, diff --git a/packages/ra-ui-materialui/src/input/SelectArrayInput.tsx b/packages/ra-ui-materialui/src/input/SelectArrayInput.tsx index 2301bde8ef5..5b0c0f5cfbe 100644 --- a/packages/ra-ui-materialui/src/input/SelectArrayInput.tsx +++ b/packages/ra-ui-materialui/src/input/SelectArrayInput.tsx @@ -127,7 +127,7 @@ export const SelectArrayInput = (props: SelectArrayInputProps) => { resource, isFromReference, } = useChoicesContext({ - choices: choicesProp as any[], + choices: choicesProp, isLoading: isLoadingProp, isFetching: isFetchingProp, resource: resourceProp, diff --git a/packages/ra-ui-materialui/src/input/SelectInput.tsx b/packages/ra-ui-materialui/src/input/SelectInput.tsx index 685d82258f1..17962aa9995 100644 --- a/packages/ra-ui-materialui/src/input/SelectInput.tsx +++ b/packages/ra-ui-materialui/src/input/SelectInput.tsx @@ -153,7 +153,7 @@ export const SelectInput = (props: SelectInputProps) => { resource, isFromReference, } = useChoicesContext({ - choices: choicesProp as any[], + choices: choicesProp, isLoading: isLoadingProp, isFetching: isFetchingProp, resource: resourceProp,