Skip to content

Commit 75e34b0

Browse files
committed
Adds tests and native support for unserialized unrelated arrays
| Q | A | ------------- | --- | Bug fix? | yes | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #122 | License | MIT | Doc PR | api-platform/docs#664 This PR supports a simple strategy to edit serialized array inputs from DB entities. While it may not currently address more comlicated array structures, this solution supports simple one-dimensional, keyless(numerically indexed), arrays
1 parent d90632e commit 75e34b0

File tree

3 files changed

+82
-19
lines changed

3 files changed

+82
-19
lines changed

src/Create.test.js

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Api from '@api-platform/api-doc-parser/lib/Api';
22
import Field from '@api-platform/api-doc-parser/lib/Field';
33
import Resource from '@api-platform/api-doc-parser/lib/Resource';
4-
import {TextInput} from 'react-admin';
4+
import {ArrayInput, SimpleFormIterator, TextInput} from 'react-admin';
55
import {shallow} from 'enzyme';
66
import React from 'react';
77
import Create from './Create';
@@ -36,6 +36,12 @@ const resourceData = {
3636
required: true,
3737
deprecated: true,
3838
}),
39+
new Field('fieldD', {
40+
id: 'http://schema.org/fieldD',
41+
range: 'http://www.w3.org/2001/XMLSchema#array',
42+
reference: null,
43+
required: true,
44+
}),
3945
],
4046
};
4147

@@ -67,8 +73,11 @@ describe('<Create />', () => {
6773
/>,
6874
);
6975

70-
expect(defaultInputFactory).toHaveBeenCalledTimes(2);
71-
expect(render.find(TextInput).length).toEqual(2);
76+
expect(defaultInputFactory).toHaveBeenCalledTimes(3);
77+
expect(render.find(ArrayInput).length).toEqual(1);
78+
expect(render.find(SimpleFormIterator).length).toEqual(1);
79+
expect(render.find(TextInput).length).toEqual(3);
80+
expect(render.find(ArrayInput).get(0).props.source).toEqual('fieldD');
7281
expect(render.find(TextInput).get(0).props.source).toEqual('fieldA');
7382
expect(render.find(TextInput).get(1).props.source).toEqual('fieldB');
7483
});
@@ -104,9 +113,12 @@ describe('<Create />', () => {
104113
/>,
105114
);
106115

107-
expect(customInputFactory).toHaveBeenCalledTimes(2);
116+
expect(customInputFactory).toHaveBeenCalledTimes(3);
108117
expect(defaultInputFactory).toHaveBeenCalledTimes(0);
109-
expect(render.find(TextInput).length).toEqual(2);
118+
expect(render.find(ArrayInput).length).toEqual(1);
119+
expect(render.find(SimpleFormIterator).length).toEqual(1);
120+
expect(render.find(TextInput).length).toEqual(3);
121+
expect(render.find(ArrayInput).get(0).props.source).toEqual('fieldD');
110122
expect(render.find(TextInput).get(0).props.source).toEqual('fieldA');
111123
expect(render.find(TextInput).get(1).props.source).toEqual('fieldB');
112124
});
@@ -123,6 +135,12 @@ describe('<Create />', () => {
123135
reference: null,
124136
required: true,
125137
}),
138+
new Field('fieldE', {
139+
id: 'http://schema.org/fieldE',
140+
range: 'http://www.w3.org/2001/XMLSchema#array',
141+
reference: null,
142+
required: true,
143+
}),
126144
],
127145
});
128146

@@ -144,8 +162,11 @@ describe('<Create />', () => {
144162
/>,
145163
);
146164

147-
expect(defaultInputFactory).toHaveBeenCalledTimes(1);
148-
expect(render.find(TextInput).length).toEqual(1);
165+
expect(defaultInputFactory).toHaveBeenCalledTimes(2);
166+
expect(render.find(ArrayInput).length).toEqual(1);
167+
expect(render.find(SimpleFormIterator).length).toEqual(1);
168+
expect(render.find(TextInput).length).toEqual(2);
169+
expect(render.find(ArrayInput).get(0).props.source).toEqual('fieldE');
149170
expect(render.find(TextInput).get(0).props.source).toEqual('fieldC');
150171
});
151172
});

src/Edit.test.js

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import Api from '@api-platform/api-doc-parser/lib/Api';
22
import Field from '@api-platform/api-doc-parser/lib/Field';
33
import Resource from '@api-platform/api-doc-parser/lib/Resource';
4-
import {DisabledInput, TextInput} from 'react-admin';
4+
import {
5+
ArrayInput,
6+
DisabledInput,
7+
SimpleFormIterator,
8+
TextInput,
9+
} from 'react-admin';
510
import {shallow} from 'enzyme';
611
import React from 'react';
712
import Edit from './Edit';
@@ -36,6 +41,12 @@ const resourceData = {
3641
required: true,
3742
deprecated: true,
3843
}),
44+
new Field('fieldD', {
45+
id: 'http://schema.org/fieldD',
46+
range: 'http://www.w3.org/2001/XMLSchema#array',
47+
reference: null,
48+
required: true,
49+
}),
3950
],
4051
};
4152

@@ -67,10 +78,13 @@ describe('<Edit />', () => {
6778
/>,
6879
);
6980

70-
expect(defaultInputFactory).toHaveBeenCalledTimes(2);
81+
expect(defaultInputFactory).toHaveBeenCalledTimes(3);
82+
expect(render.find(ArrayInput).length).toEqual(1);
7183
expect(render.find(DisabledInput).length).toEqual(1);
7284
expect(render.find(DisabledInput).get(0).props.source).toEqual('id');
73-
expect(render.find(TextInput).length).toEqual(2);
85+
expect(render.find(SimpleFormIterator).length).toEqual(1);
86+
expect(render.find(TextInput).length).toEqual(3);
87+
expect(render.find(ArrayInput).get(0).props.source).toEqual('fieldD');
7488
expect(render.find(TextInput).get(0).props.source).toEqual('fieldA');
7589
expect(render.find(TextInput).get(1).props.source).toEqual('fieldB');
7690
});
@@ -103,9 +117,11 @@ describe('<Edit />', () => {
103117
/>,
104118
);
105119

106-
expect(defaultInputFactory).toHaveBeenCalledTimes(2);
120+
expect(defaultInputFactory).toHaveBeenCalledTimes(3);
121+
expect(render.find(ArrayInput).length).toEqual(1);
107122
expect(render.find(DisabledInput).length).toEqual(0);
108-
expect(render.find(TextInput).length).toEqual(2);
123+
expect(render.find(TextInput).length).toEqual(3);
124+
expect(render.find(ArrayInput).get(0).props.source).toEqual('fieldD');
109125
expect(render.find(TextInput).get(0).props.source).toEqual('fieldA');
110126
expect(render.find(TextInput).get(1).props.source).toEqual('fieldB');
111127
});
@@ -141,11 +157,14 @@ describe('<Edit />', () => {
141157
/>,
142158
);
143159

144-
expect(customInputFactory).toHaveBeenCalledTimes(2);
160+
expect(customInputFactory).toHaveBeenCalledTimes(3);
145161
expect(defaultInputFactory).toHaveBeenCalledTimes(0);
162+
expect(render.find(ArrayInput).length).toEqual(1);
146163
expect(render.find(DisabledInput).length).toEqual(1);
147164
expect(render.find(DisabledInput).get(0).props.source).toEqual('id');
148-
expect(render.find(TextInput).length).toEqual(2);
165+
expect(render.find(SimpleFormIterator).length).toEqual(1);
166+
expect(render.find(TextInput).length).toEqual(3);
167+
expect(render.find(ArrayInput).get(0).props.source).toEqual('fieldD');
149168
expect(render.find(TextInput).get(0).props.source).toEqual('fieldA');
150169
expect(render.find(TextInput).get(1).props.source).toEqual('fieldB');
151170
});
@@ -162,6 +181,12 @@ describe('<Edit />', () => {
162181
reference: null,
163182
required: true,
164183
}),
184+
new Field('fieldE', {
185+
id: 'http://schema.org/fieldE',
186+
range: 'http://www.w3.org/2001/XMLSchema#array',
187+
reference: null,
188+
required: true,
189+
}),
165190
],
166191
});
167192

@@ -183,10 +208,13 @@ describe('<Edit />', () => {
183208
/>,
184209
);
185210

186-
expect(defaultInputFactory).toHaveBeenCalledTimes(1);
211+
expect(defaultInputFactory).toHaveBeenCalledTimes(2);
212+
expect(render.find(ArrayInput).length).toEqual(1);
187213
expect(render.find(DisabledInput).length).toEqual(1);
188214
expect(render.find(DisabledInput).get(0).props.source).toEqual('id');
189-
expect(render.find(TextInput).length).toEqual(1);
215+
expect(render.find(SimpleFormIterator).length).toEqual(1);
216+
expect(render.find(TextInput).length).toEqual(2);
217+
expect(render.find(ArrayInput).get(0).props.source).toEqual('fieldE');
190218
expect(render.find(TextInput).get(0).props.source).toEqual('fieldC');
191219
});
192220

@@ -227,11 +255,14 @@ describe('<Edit />', () => {
227255
/>,
228256
);
229257

230-
expect(defaultInputFactory).toHaveBeenCalledTimes(3);
258+
expect(defaultInputFactory).toHaveBeenCalledTimes(4);
259+
expect(render.find(ArrayInput).length).toEqual(1);
231260
expect(render.find(DisabledInput).length).toEqual(0);
232-
expect(render.find(TextInput).length).toEqual(3);
261+
expect(render.find(TextInput).length).toEqual(4);
262+
expect(render.find(ArrayInput).get(0).props.source).toEqual('fieldD');
233263
expect(render.find(TextInput).get(0).props.source).toEqual('fieldA');
234264
expect(render.find(TextInput).get(1).props.source).toEqual('fieldB');
235-
expect(render.find(TextInput).get(2).props.source).toEqual('id');
265+
expect(render.find(TextInput).get(2).props.source).toEqual(undefined);
266+
expect(render.find(TextInput).get(3).props.source).toEqual('id');
236267
});
237268
});

src/inputFactory.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
ArrayInput,
23
BooleanInput,
34
DateInput,
45
NumberInput,
@@ -7,6 +8,7 @@ import {
78
required,
89
SelectArrayInput,
910
SelectInput,
11+
SimpleFormIterator,
1012
TextInput,
1113
} from 'react-admin';
1214
import React from 'react';
@@ -72,6 +74,15 @@ export default (field, options) => {
7274
}
7375

7476
switch (field.range) {
77+
case 'http://www.w3.org/2001/XMLSchema#array':
78+
return (
79+
<ArrayInput key={field.name} source={field.name} {...props}>
80+
<SimpleFormIterator>
81+
<TextInput />
82+
</SimpleFormIterator>
83+
</ArrayInput>
84+
);
85+
7586
case 'http://www.w3.org/2001/XMLSchema#integer':
7687
return <NumberInput key={field.name} source={field.name} {...props} />;
7788

0 commit comments

Comments
 (0)