Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RA-OSS-20 - Some Inputs with choices in their props do not accept ReadonlyArray<T> #9162

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/ra-core/src/controller/list/useList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ export const useList = <RecordType extends RaRecord = any>(
props: UseListOptions<RecordType>
): UseListValue<RecordType> => {
const {
data,
error,
filter = defaultFilter,
isFetching = false,
Expand All @@ -67,6 +66,9 @@ export const useList = <RecordType extends RaRecord = any>(
} = props;
const resource = useResourceContext(props);

// Make copy of data so it becomes mutable
const data = props.data ? [...props.data] : undefined;

const [fetchingState, setFetchingState] = useSafeSetState<boolean>(
isFetching
) as [boolean, (isFetching: boolean) => void];
Expand Down Expand Up @@ -276,7 +278,7 @@ export const useList = <RecordType extends RaRecord = any>(
};

export interface UseListOptions<RecordType extends RaRecord = any> {
data?: RecordType[];
data?: readonly RecordType[];
error?: any;
filter?: FilterPayload;
isFetching?: boolean;
Expand Down
4 changes: 3 additions & 1 deletion packages/ra-core/src/form/choices/useChoicesContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { useList } from '../../controller';
import { ChoicesContext, ChoicesContextValue } from './ChoicesContext';

export const useChoicesContext = <ChoicesType extends RaRecord = RaRecord>(
options: Partial<ChoicesContextValue> & { choices?: ChoicesType[] } = {}
options: Partial<ChoicesContextValue> & {
choices?: readonly ChoicesType[];
} = {}
): ChoicesContextValue => {
const context = useContext(ChoicesContext) as ChoicesContextValue<
ChoicesType
Expand Down
2 changes: 1 addition & 1 deletion packages/ra-core/src/form/useChoices.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type OptionTextFunc = (choice: any) => React.ReactNode;
export type OptionText = OptionTextElement | OptionTextFunc | string;

export interface ChoicesProps {
choices?: any[];
choices?: ReadonlyArray<any>;
isFetching?: boolean;
isLoading?: boolean;
optionValue?: string;
Expand Down
11 changes: 3 additions & 8 deletions packages/ra-ui-materialui/src/input/AutocompleteInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
) || ''
);
Expand Down