-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathArrayInput.stories.tsx
130 lines (121 loc) · 3.98 KB
/
ArrayInput.stories.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import * as React from 'react';
import { Admin } from 'react-admin';
import { Resource } from 'ra-core';
import { createMemoryHistory } from 'history';
import { Edit } from '../../detail';
import { SimpleForm } from '../../form';
import { ArrayInput } from './ArrayInput';
import { SimpleFormIterator } from './SimpleFormIterator';
import { TextInput } from '../TextInput';
import { AutocompleteInput } from '../AutocompleteInput';
export default { title: 'ra-ui-materialui/input/ArrayInput' };
const dataProvider = {
getOne: (resource, params) =>
Promise.resolve({
data: {
id: 1,
title: 'War and Peace',
authors: [
{
name: 'Leo Tolstoy',
role: 'head_writer',
},
{
name: 'Alexander Pushkin',
role: 'co_writer',
},
],
tags: ['novel', 'war', 'classic'],
},
}),
update: (resource, params) => Promise.resolve(params),
} as any;
const history = createMemoryHistory({ initialEntries: ['/books/1'] });
const BookEdit = () => {
return (
<Edit
mutationMode="pessimistic"
mutationOptions={{
onSuccess: data => {
console.log(data);
},
}}
>
<SimpleForm>
<ArrayInput source="authors" fullWidth>
<SimpleFormIterator>
<TextInput source="name" />
<TextInput source="role" />
</SimpleFormIterator>
</ArrayInput>
</SimpleForm>
</Edit>
);
};
export const Basic = () => (
<Admin dataProvider={dataProvider} history={history}>
<Resource name="books" edit={BookEdit} />
</Admin>
);
const BookEditWithAutocomplete = () => {
return (
<Edit
mutationMode="pessimistic"
mutationOptions={{
onSuccess: data => {
console.log(data);
},
}}
>
<SimpleForm>
<ArrayInput source="authors" fullWidth>
<SimpleFormIterator>
<AutocompleteInput
source="role"
choices={[
{ id: 'head_writer', name: 'Head Writer' },
{ id: 'co_writer', name: 'Co-Writer' },
]}
/>
<TextInput source="name" />
</SimpleFormIterator>
</ArrayInput>
</SimpleForm>
</Edit>
);
};
export const AutocompleteFirst = () => (
<Admin dataProvider={dataProvider} history={history}>
<Resource name="books" edit={BookEditWithAutocomplete} />
</Admin>
);
export const Scalar = () => (
<Admin dataProvider={dataProvider} history={history}>
<Resource
name="books"
edit={() => (
<Edit
mutationMode="pessimistic"
mutationOptions={{
onSuccess: data => {
console.log(data);
},
}}
>
<SimpleForm>
<TextInput source="title" />
<ArrayInput source="tags" fullWidth>
<SimpleFormIterator disableReordering>
<TextInput
source=""
label="tag"
helperText={false}
/>
</SimpleFormIterator>
</ArrayInput>
</SimpleForm>
</Edit>
)}
/>
</Admin>
);