Skip to content

Commit

Permalink
Merge pull request #9563 from marmelab/revert-9532-fix-select-array-i…
Browse files Browse the repository at this point in the history
…nput-does-not-use-record-representation

Revert "fix: SelectArrayInput does not use recordRepresentation"
  • Loading branch information
slax57 authored Jan 5, 2024
2 parents 9561dbc + 844c288 commit 5bac944
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 81 deletions.
17 changes: 0 additions & 17 deletions packages/ra-ui-materialui/src/input/SelectArrayInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
DifferentIdTypes,
TranslateChoice,
InsideArrayInput,
WithRecordRepresentation,
} from './SelectArrayInput.stories';

describe('<SelectArrayInput />', () => {
Expand Down Expand Up @@ -660,20 +659,4 @@ describe('<SelectArrayInput />', () => {
fireEvent.click(screen.getByLabelText('Add'));
expect(await screen.findAllByText('Foo')).toHaveLength(2);
});

describe('record representation', () => {
it('should use record representation if defined', async () => {
render(<WithRecordRepresentation />);
await screen.findByText('resources.tags.fields.tag_ids');
expect(screen.queryByText('1 - Architecture')).not.toBeNull();
expect(screen.queryByText('3 - Painting')).not.toBeNull();
});

it('should use option text instead of record representation if defined', async () => {
render(<WithRecordRepresentation setOptionText />);
await screen.findByText('resources.tags.fields.tag_ids');
expect(screen.queryByText('Architecture')).not.toBeNull();
expect(screen.queryByText('Painting')).not.toBeNull();
});
});
});
59 changes: 1 addition & 58 deletions packages/ra-ui-materialui/src/input/SelectArrayInput.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ import { ReferenceArrayInput } from './ReferenceArrayInput';
import { useCreateSuggestionContext } from './useSupportCreateSuggestion';
import { TextInput } from './TextInput';
import { ArrayInput, SimpleFormIterator } from './ArrayInput';
import {
DataProvider,
FormDataConsumer,
ResourceDefinitionContextProvider,
} from 'ra-core';
import { FormDataConsumer } from 'ra-core';
import { useWatch } from 'react-hook-form';

export default { title: 'ra-ui-materialui/input/SelectArrayInput' };
Expand Down Expand Up @@ -355,56 +351,3 @@ export const TranslateChoice = () => {
</AdminContext>
);
};

export const WithRecordRepresentation = ({ setOptionText = false }) => {
const tags = [
{ id: 0, name: '3D' },
{ id: 1, name: 'Architecture' },
{ id: 2, name: 'Design' },
{ id: 3, name: 'Painting' },
{ id: 4, name: 'Photography' },
];
const resouceDefs = {
tags: {
name: 'tags',
recordRepresentation: record => `${record.id} - ${record.name}`,
},
};
return (
<AdminContext
dataProvider={
({
getList: () =>
Promise.resolve({
data: tags,
total: tags.length,
}),
getMany: (_, params) => {
return Promise.resolve({
data: params.ids.map(id =>
tags.find(tag => tag.id === id)
),
});
},
} as unknown) as DataProvider
}
>
<ResourceDefinitionContextProvider definitions={resouceDefs}>
<SimpleForm
defaultValues={{ tag_ids: [1, 3] }}
onSubmit={() => {}}
>
<ReferenceArrayInput reference="tags" source="tag_ids">
<SelectArrayInput
optionText={setOptionText ? 'name' : undefined}
/>
</ReferenceArrayInput>
</SimpleForm>
</ResourceDefinitionContextProvider>
</AdminContext>
);
};

export const WithRecordRepresentationAndOptionText = () => (
<WithRecordRepresentation setOptionText />
);
8 changes: 2 additions & 6 deletions packages/ra-ui-materialui/src/input/SelectArrayInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
useChoicesContext,
useChoices,
RaRecord,
useGetRecordRepresentation,
} from 'ra-core';
import { InputHelperText } from './InputHelperText';
import { FormControlProps } from '@mui/material/FormControl';
Expand Down Expand Up @@ -106,7 +105,7 @@ export const SelectArrayInput = (props: SelectArrayInputProps) => {
onChange,
onCreate,
options = defaultOptions,
optionText,
optionText = 'name',
optionValue = 'id',
parse,
resource: resourceProp,
Expand Down Expand Up @@ -135,11 +134,8 @@ export const SelectArrayInput = (props: SelectArrayInputProps) => {
source: sourceProp,
});

const getRecordRepresentation = useGetRecordRepresentation(resource);
const { getChoiceText, getChoiceValue, getDisableValue } = useChoices({
optionText:
optionText ??
(isFromReference ? getRecordRepresentation : undefined),
optionText,
optionValue,
disableValue,
translateChoice: translateChoice ?? !isFromReference,
Expand Down

0 comments on commit 5bac944

Please sign in to comment.