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 16779ded22d..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?: 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 de43eb1f532..684b2f9c632 100644 --- a/packages/ra-ui-materialui/src/input/AutocompleteInput.tsx +++ b/packages/ra-ui-materialui/src/input/AutocompleteInput.tsx @@ -763,23 +763,18 @@ const useSelectedChoice = < return selectedChoice || null; }; -const getSelectedItems = ( - choices = [], - 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) ) || '' );