Skip to content

Commit 5402143

Browse files
authored
Merge pull request #11003 from marmelab/fix-autocomplete-input-chips
Fix `<AutocompleteArrayInput>` does not apply `ChipProps` nor `slotProps.chip` in `renderTags`
2 parents c66cd8c + b57d63a commit 5402143

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ import { SimpleForm } from '../form';
1313
import { AutocompleteArrayInput } from './AutocompleteArrayInput';
1414
import { useCreateSuggestionContext } from './useSupportCreateSuggestion';
1515
import {
16+
ChipProps,
1617
CreateItemLabel,
1718
CreateItemLabelRendered,
1819
CreateLabel,
1920
InsideReferenceArrayInput,
2021
InsideReferenceArrayInputOnChange,
2122
OnChange,
2223
OnCreate,
24+
SlotPropsChip,
2325
} from './AutocompleteArrayInput.stories';
2426

2527
describe('<AutocompleteArrayInput />', () => {
@@ -1326,4 +1328,12 @@ describe('<AutocompleteArrayInput />', () => {
13261328
expect(input.value).not.toBe('Create x');
13271329
expect(input.value).toBe('');
13281330
}, 10000);
1331+
it('should allow to customize chips using the ChipProps prop', async () => {
1332+
render(<ChipProps />);
1333+
await screen.findAllByTestId('delete-icon');
1334+
});
1335+
it('should allow to customize chips using the slotProps prop', async () => {
1336+
render(<SlotPropsChip />);
1337+
await screen.findAllByTestId('delete-icon');
1338+
});
13291339
});

packages/ra-ui-materialui/src/input/AutocompleteArrayInput.stories.tsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as React from 'react';
22
import { Admin } from 'react-admin';
33

44
import CloseIcon from '@mui/icons-material/Close';
5+
import BackspaceIcon from '@mui/icons-material/Backspace';
56
import {
67
Button,
78
Chip,
@@ -780,3 +781,40 @@ export const InsideReferenceArrayInputWithCreationSupport = () => (
780781
</Admin>
781782
</TestMemoryRouter>
782783
);
784+
785+
export const ChipProps = () => (
786+
<Wrapper>
787+
<AutocompleteArrayInput
788+
source="roles"
789+
choices={[
790+
{ id: 'admin', name: 'Admin' },
791+
{ id: 'u001', name: 'Editor' },
792+
{ id: 'u002', name: 'Moderator' },
793+
{ id: 'u003', name: 'Reviewer' },
794+
]}
795+
ChipProps={{
796+
deleteIcon: <BackspaceIcon data-testid="delete-icon" />,
797+
}}
798+
/>
799+
</Wrapper>
800+
);
801+
802+
export const SlotPropsChip = () => (
803+
<Wrapper>
804+
<AutocompleteArrayInput
805+
source="roles"
806+
choices={[
807+
{ id: 'admin', name: 'Admin' },
808+
{ id: 'u001', name: 'Editor' },
809+
{ id: 'u002', name: 'Moderator' },
810+
{ id: 'u003', name: 'Reviewer' },
811+
]}
812+
slotProps={{
813+
// @ts-ignore
814+
chip: {
815+
deleteIcon: <BackspaceIcon data-testid="delete-icon" />,
816+
},
817+
}}
818+
/>
819+
</Wrapper>
820+
);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,11 @@ If you provided a React element for the optionText prop, you must also provide t
726726
value.map((option, index) => {
727727
// We have to extract the key because react 19 does not allow to spread the key prop
728728
const { key, ...tagProps } = getTagProps({ index });
729+
// @ts-expect-error slotProps do not yet exist in MUI v5
730+
const mergedSlotProps = props.slotProps?.chip
731+
? // @ts-expect-error slotProps do not yet exist in MUI v5
732+
props.slotProps.chip
733+
: props.ChipProps;
729734
return (
730735
<Chip
731736
label={
@@ -738,6 +743,7 @@ If you provided a React element for the optionText prop, you must also provide t
738743
size="small"
739744
key={key}
740745
{...tagProps}
746+
{...mergedSlotProps}
741747
/>
742748
);
743749
})

0 commit comments

Comments
 (0)