Skip to content

Commit cbe0a35

Browse files
authored
Merge pull request #7030 from marmelab/fix-select-array-input-create-choice
Fix `SelectArrayInput` create optionText
2 parents ce003d9 + b570204 commit cbe0a35

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

packages/ra-ui-materialui/src/input/SelectArrayInput.spec.tsx

+43
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,49 @@ describe('<SelectArrayInput />', () => {
390390
});
391391
});
392392

393+
it('should support creation of a new choice with nested optionText', async () => {
394+
const choices = [
395+
{ id: 'programming', name: { en: 'Programming' } },
396+
{ id: 'lifestyle', name: { en: 'Lifestyle' } },
397+
{ id: 'photography', name: { en: 'Photography' } },
398+
];
399+
const newChoice = {
400+
id: 'js_fatigue',
401+
name: { en: 'New Kid On The Block' },
402+
};
403+
404+
const { getByLabelText, getByRole, getByText, queryAllByText } = render(
405+
<Form
406+
validateOnBlur
407+
onSubmit={jest.fn()}
408+
render={() => (
409+
<SelectArrayInput
410+
{...defaultProps}
411+
choices={choices}
412+
onCreate={() => {
413+
choices.push(newChoice);
414+
return newChoice;
415+
}}
416+
optionText="name.en"
417+
/>
418+
)}
419+
/>
420+
);
421+
422+
const input = getByLabelText(
423+
'resources.posts.fields.categories'
424+
) as HTMLInputElement;
425+
input.focus();
426+
const select = getByRole('button');
427+
fireEvent.mouseDown(select);
428+
429+
fireEvent.click(getByText('ra.action.create'));
430+
await new Promise(resolve => setTimeout(resolve));
431+
input.blur();
432+
// 2 because there is both the chip for the new selected item and the option (event if hidden)
433+
expect(queryAllByText(newChoice.name.en).length).toEqual(2);
434+
});
435+
393436
it('should support creation of a new choice through the create element', async () => {
394437
const choices = [...defaultProps.choices];
395438
const newChoice = { id: 'js_fatigue', name: 'New Kid On The Block' };

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,11 @@ const SelectArrayInput = (props: SelectArrayInputProps) => {
184184
value={getChoiceValue(choice)}
185185
disabled={getDisableValue(choice)}
186186
>
187-
{!!createItem && choice?.id === createItem.id
188-
? createItem.name
189-
: renderMenuItemOption(choice)}
187+
{renderMenuItemOption(
188+
!!createItem && choice?.id === createItem.id
189+
? createItem
190+
: choice
191+
)}
190192
</MenuItem>
191193
) : null;
192194
},

0 commit comments

Comments
 (0)