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

Revert "Fix <SelectArrayInput> does not use recordRepresentation" #9563

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
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
Loading