1
1
import * as React from 'react' ;
2
- import { required } from 'ra-core' ;
2
+ import { required , Resource } from 'ra-core' ;
3
3
import { useFormState , useFormContext } from 'react-hook-form' ;
4
4
5
5
import { TextInput } from './TextInput' ;
@@ -9,6 +9,8 @@ import { Edit } from '../detail';
9
9
import { SimpleForm , Toolbar } from '../form' ;
10
10
import { SaveButton } from '../button' ;
11
11
import { FormInspector } from './common' ;
12
+ import { Admin } from 'react-admin' ;
13
+ import { MemoryRouter } from 'react-router' ;
12
14
13
15
export default { title : 'ra-ui-materialui/input/TextInput' } ;
14
16
@@ -176,6 +178,71 @@ export const Error = () => (
176
178
</ AdminContext >
177
179
) ;
178
180
181
+ export const ServerError = ( ) => (
182
+ < MemoryRouter initialEntries = { [ '/posts/create' ] } >
183
+ < Admin
184
+ dataProvider = {
185
+ {
186
+ create : ( resource , { data } ) => {
187
+ console . log ( `reject create on ${ resource } : ` , data ) ;
188
+ return Promise . reject ( {
189
+ data,
190
+ message :
191
+ 'An article with this title already exists. The title must be unique.' ,
192
+ } ) ;
193
+ } ,
194
+ } as any
195
+ }
196
+ >
197
+ < Resource
198
+ name = "posts"
199
+ create = { ( ) => (
200
+ < Create resource = "posts" record = { { title : 'Lorem ipsum' } } >
201
+ < SimpleForm toolbar = { AlwaysOnToolbar } >
202
+ < TextInput source = "title" />
203
+ < FormInspector />
204
+ </ SimpleForm >
205
+ </ Create >
206
+ ) }
207
+ />
208
+ </ Admin >
209
+ </ MemoryRouter >
210
+ ) ;
211
+
212
+ export const ServerValidationError = ( ) => (
213
+ < MemoryRouter initialEntries = { [ '/posts/create' ] } >
214
+ < Admin
215
+ dataProvider = {
216
+ {
217
+ create : ( resource , { data } ) => {
218
+ console . log ( `reject create on ${ resource } : ` , data ) ;
219
+ return Promise . reject ( {
220
+ data,
221
+ body : {
222
+ errors : {
223
+ title : 'An article with this title already exists. The title must be unique.' ,
224
+ } ,
225
+ } ,
226
+ } ) ;
227
+ } ,
228
+ } as any
229
+ }
230
+ >
231
+ < Resource
232
+ name = "posts"
233
+ create = { ( ) => (
234
+ < Create resource = "posts" record = { { title : 'Lorem ipsum' } } >
235
+ < SimpleForm toolbar = { AlwaysOnToolbar } >
236
+ < TextInput source = "title" />
237
+ < FormInspector />
238
+ </ SimpleForm >
239
+ </ Create >
240
+ ) }
241
+ />
242
+ </ Admin >
243
+ </ MemoryRouter >
244
+ ) ;
245
+
179
246
export const Sx = ( ) => (
180
247
< Wrapper >
181
248
< TextInput
0 commit comments