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

Fix SelectArrayInput create optionText #7030

Merged
merged 2 commits into from
Dec 24, 2021
Merged
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
43 changes: 43 additions & 0 deletions packages/ra-ui-materialui/src/input/SelectArrayInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,49 @@ describe('<SelectArrayInput />', () => {
});
});

it('should support creation of a new choice with nested optionText', async () => {
const choices = [
{ id: 'programming', name: { en: 'Programming' } },
{ id: 'lifestyle', name: { en: 'Lifestyle' } },
{ id: 'photography', name: { en: 'Photography' } },
];
const newChoice = {
id: 'js_fatigue',
name: { en: 'New Kid On The Block' },
};

const { getByLabelText, getByRole, getByText, queryAllByText } = render(
<Form
validateOnBlur
onSubmit={jest.fn()}
render={() => (
<SelectArrayInput
{...defaultProps}
choices={choices}
onCreate={() => {
choices.push(newChoice);
return newChoice;
}}
optionText="name.en"
/>
)}
/>
);

const input = getByLabelText(
'resources.posts.fields.categories'
) as HTMLInputElement;
input.focus();
const select = getByRole('button');
fireEvent.mouseDown(select);

fireEvent.click(getByText('ra.action.create'));
await new Promise(resolve => setTimeout(resolve));
input.blur();
// 2 because there is both the chip for the new selected item and the option (event if hidden)
expect(queryAllByText(newChoice.name.en).length).toEqual(2);
});

it('should support creation of a new choice through the create element', async () => {
const choices = [...defaultProps.choices];
const newChoice = { id: 'js_fatigue', name: 'New Kid On The Block' };
Expand Down
8 changes: 5 additions & 3 deletions packages/ra-ui-materialui/src/input/SelectArrayInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,11 @@ const SelectArrayInput = (props: SelectArrayInputProps) => {
value={getChoiceValue(choice)}
disabled={getDisableValue(choice)}
>
{!!createItem && choice?.id === createItem.id
? createItem.name
: renderMenuItemOption(choice)}
{renderMenuItemOption(
!!createItem && choice?.id === createItem.id
? createItem
: choice
)}
</MenuItem>
) : null;
},
Expand Down