Skip to content

Commit 8d1c6a8

Browse files
committed
Use TextArrayInput in EditGuesser for scalar array
1 parent 0bb605b commit 8d1c6a8

File tree

4 files changed

+85
-32
lines changed

4 files changed

+85
-32
lines changed

packages/ra-core/src/inference/inferElementFromValues.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,17 @@ const inferElementFromValues = (
158158
)
159159
);
160160
}
161+
if (
162+
typeof values[0][0] === 'string' &&
163+
hasType('scalar_array', types)
164+
) {
165+
return (
166+
types.scalar_array &&
167+
new InferredElement(types.scalar_array, {
168+
source: name,
169+
})
170+
);
171+
}
161172
// FIXME introspect further
162173
return new InferredElement(types.string, { source: name });
163174
}

packages/ra-ui-materialui/src/detail/EditGuesser.spec.tsx

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,35 @@
11
import * as React from 'react';
22
import expect from 'expect';
3-
import { render, screen, waitFor } from '@testing-library/react';
4-
import { CoreAdminContext } from 'ra-core';
3+
import { render, screen } from '@testing-library/react';
54

6-
import { EditGuesser } from './EditGuesser';
7-
import { ThemeProvider } from '../theme/ThemeProvider';
5+
import { EditGuesser } from './EditGuesser.stories';
86

97
describe('<EditGuesser />', () => {
108
it('should log the guessed Edit view based on the fetched record', async () => {
119
const logSpy = jest.spyOn(console, 'log').mockImplementation(() => {});
12-
const dataProvider = {
13-
getOne: () =>
14-
Promise.resolve({
15-
data: {
16-
id: 123,
17-
author: 'john doe',
18-
post_id: 6,
19-
score: 3,
20-
body: "Queen, tossing her head through the wood. 'If it had lost something; and she felt sure it.",
21-
created_at: new Date('2012-08-02'),
22-
tags_ids: [1, 2],
23-
},
24-
}),
25-
getMany: () => Promise.resolve({ data: [] }),
26-
};
27-
render(
28-
<ThemeProvider>
29-
<CoreAdminContext dataProvider={dataProvider as any}>
30-
<EditGuesser resource="comments" id={123} enableLog />
31-
</CoreAdminContext>
32-
</ThemeProvider>
33-
);
34-
await waitFor(() => {
35-
screen.getByDisplayValue('john doe');
36-
});
10+
render(<EditGuesser />);
11+
await screen.findByDisplayValue('john doe');
3712
expect(logSpy).toHaveBeenCalledWith(`Guessed Edit:
3813
39-
import { DateInput, Edit, NumberInput, ReferenceArrayInput, ReferenceInput, SimpleForm, TextInput } from 'react-admin';
14+
import { ArrayInput, BooleanInput, DateInput, Edit, NumberInput, ReferenceArrayInput, ReferenceInput, SimpleForm, SimpleFormIterator, TextArrayInput, TextInput } from 'react-admin';
4015
41-
export const CommentEdit = () => (
16+
export const BookEdit = () => (
4217
<Edit>
4318
<SimpleForm>
4419
<TextInput source="id" />
45-
<TextInput source="author" />
20+
<ArrayInput source="authors"><SimpleFormIterator><TextInput source="id" />
21+
<TextInput source="name" />
22+
<DateInput source="dob" /></SimpleFormIterator></ArrayInput>
4623
<ReferenceInput source="post_id" reference="posts" />
4724
<NumberInput source="score" />
4825
<TextInput source="body" />
26+
<TextInput source="description" />
4927
<DateInput source="created_at" />
5028
<ReferenceArrayInput source="tags_ids" reference="tags" />
29+
<TextInput source="url" />
30+
<TextInput source="email" />
31+
<BooleanInput source="isAlreadyPublished" />
32+
<TextArrayInput source="genres" />
5133
</SimpleForm>
5234
</Edit>
5335
);`);
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import * as React from 'react';
2+
import { Admin } from 'react-admin';
3+
import { Resource, TestMemoryRouter } from 'ra-core';
4+
import fakeRestProvider from 'ra-data-fakerest';
5+
6+
import { EditGuesser as RAEditGuesser } from './EditGuesser';
7+
8+
export default { title: 'ra-ui-materialui/detail/EditGuesser' };
9+
10+
const data = {
11+
books: [
12+
{
13+
id: 123,
14+
authors: [
15+
{ id: 1, name: 'john doe', dob: '1990-01-01' },
16+
{ id: 2, name: 'jane doe', dob: '1992-01-01' },
17+
],
18+
post_id: 6,
19+
score: 3,
20+
body: "Queen, tossing her head through the wood. 'If it had lost something; and she felt sure it.",
21+
description: `<p><strong>War and Peace</strong> is a novel by the Russian author <a href="https://en.wikipedia.org/wiki/Leo_Tolstoy">Leo Tolstoy</a>,
22+
published serially, then in its entirety in 1869.</p>
23+
<p>It is regarded as one of Tolstoy's finest literary achievements and remains a classic of world literature.</p>`,
24+
created_at: new Date('2012-08-02'),
25+
tags_ids: [1, 2],
26+
url: 'https://www.myshop.com/tags/top-seller',
27+
28+
isAlreadyPublished: true,
29+
genres: [
30+
'Fiction',
31+
'Historical Fiction',
32+
'Classic Literature',
33+
'Russian Literature',
34+
],
35+
},
36+
],
37+
tags: [
38+
{ id: 1, name: 'top seller' },
39+
{ id: 2, name: 'new' },
40+
],
41+
posts: [
42+
{ id: 6, title: 'War and Peace', body: 'A great novel by Leo Tolstoy' },
43+
],
44+
};
45+
46+
const EditGuesserWithProdLogs = () => <RAEditGuesser enableLog />;
47+
48+
export const EditGuesser = () => (
49+
<TestMemoryRouter initialEntries={['/books/123']}>
50+
<Admin dataProvider={fakeRestProvider(data)}>
51+
<Resource name="books" edit={EditGuesserWithProdLogs} />
52+
</Admin>
53+
</TestMemoryRouter>
54+
);

packages/ra-ui-materialui/src/detail/editFieldTypes.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
SelectInput,
1414
SimpleFormIterator,
1515
TextInput,
16+
TextArrayInput,
1617
} from '../input';
1718
import { InferredElement, InferredTypeMap, InputProps } from 'ra-core';
1819

@@ -40,6 +41,11 @@ ${children.map(child => ` ${child.getRepresentation()}`).join('\n')}
4041
.map(child => child.getRepresentation())
4142
.join('\n')}</SimpleFormIterator></ArrayInput>`,
4243
},
44+
scalar_array: {
45+
component: TextArrayInput,
46+
representation: (props: InputProps) =>
47+
`<TextArrayInput source="${props.source}" />`,
48+
},
4349
boolean: {
4450
component: BooleanInput,
4551
representation: (props: InputProps) =>

0 commit comments

Comments
 (0)