Skip to content

Commit 1475f86

Browse files
committed
fix
1 parent 122fc0b commit 1475f86

File tree

5 files changed

+20
-5
lines changed

5 files changed

+20
-5
lines changed

packages/ra-core/src/form/choices/useChoices.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export interface UseChoicesOptions {
2727
optionText?: OptionText;
2828
disableValue?: string;
2929
translateChoice?: boolean;
30+
createValue?: string;
31+
createHintValue?: string;
3032
}
3133

3234
/*
@@ -45,11 +47,18 @@ export const useChoices = ({
4547
optionValue = 'id',
4648
disableValue = 'disabled',
4749
translateChoice = true,
50+
createValue = '@@ra-create',
51+
createHintValue = '@@ra-create-hint',
4852
}: UseChoicesOptions) => {
4953
const translate = useTranslate();
5054

5155
const getChoiceText = useCallback(
5256
choice => {
57+
if (choice?.id === createValue || choice?.id === createHintValue) {
58+
// For the create choice, we always use the name prop as text
59+
return get(choice, 'name');
60+
}
61+
5362
if (isValidElement<{ record: any }>(optionText)) {
5463
return (
5564
<RecordContextProvider value={choice}>
@@ -68,7 +77,7 @@ export const useChoices = ({
6877
? translate(String(choiceName), { _: choiceName })
6978
: String(choiceName);
7079
},
71-
[optionText, translate, translateChoice]
80+
[createHintValue, createValue, optionText, translate, translateChoice]
7281
);
7382

7483
const getChoiceValue = useCallback(

packages/ra-core/src/form/useSuggestions.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export const useSuggestions = ({
3030
choices,
3131
createText = 'ra.action.create',
3232
createValue = '@@create',
33+
createHintValue = '@@ra-create-hint',
3334
limitChoicesToValue,
3435
matchSuggestion,
3536
optionText,
@@ -43,6 +44,8 @@ export const useSuggestions = ({
4344
optionText,
4445
optionValue,
4546
translateChoice,
47+
createValue,
48+
createHintValue,
4649
});
4750

4851
// eslint-disable-next-line react-hooks/exhaustive-deps
@@ -93,7 +96,6 @@ export interface UseSuggestionsOptions extends UseChoicesOptions {
9396
allowDuplicates?: boolean;
9497
choices?: any[];
9598
createText?: string;
96-
createValue?: any;
9799
limitChoicesToValue?: boolean;
98100
matchSuggestion?: (
99101
filter: string,

packages/ra-ui-materialui/src/input/AutocompleteInput.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ If you provided a React element for the optionText prop, you must also provide t
325325
optionText:
326326
optionText ??
327327
(isFromReference ? getRecordRepresentation : undefined),
328-
optionValue,
328+
createValue,
329+
createHintValue,
329330
selectedItem: selectedChoice,
330331
suggestionLimit,
331332
translateChoice: translateChoice ?? !isFromReference,

packages/ra-ui-materialui/src/input/SelectInput.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ export const SelectInput = (inProps: SelectInputProps) => {
122122
create,
123123
createLabel,
124124
createValue,
125+
createHintValue,
125126
defaultValue,
126127
disableValue = 'disabled',
127128
emptyText = '',
@@ -192,6 +193,8 @@ export const SelectInput = (inProps: SelectInputProps) => {
192193
optionValue,
193194
disableValue,
194195
translateChoice: translateChoice ?? !isFromReference,
196+
createValue,
197+
createHintValue,
195198
});
196199
const { field, fieldState, id, isRequired } = useInput({
197200
defaultValue,
@@ -249,6 +252,7 @@ export const SelectInput = (inProps: SelectInputProps) => {
249252
create,
250253
createLabel,
251254
createValue,
255+
createHintValue,
252256
handleChange,
253257
onCreate,
254258
optionText,

packages/ra-ui-materialui/src/input/useSupportCreateSuggestion.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ export const useSupportCreateSuggestion = (
3939
createItemLabel,
4040
createValue = '@@ra-create',
4141
createHintValue = '@@ra-create-hint',
42-
optionText = 'name',
4342
filter,
4443
handleChange,
4544
onCreate,
@@ -62,7 +61,7 @@ export const useSupportCreateSuggestion = (
6261
? createHintValue
6362
: createValue,
6463
},
65-
typeof optionText === 'string' ? optionText : 'name',
64+
'name',
6665
filter && createItemLabel
6766
? translate(createItemLabel, {
6867
item: filter,

0 commit comments

Comments
 (0)