|
1 | 1 | /* eslint-disable camelcase */ |
2 | | -import React, { useState } from 'react'; |
| 2 | +import React from 'react'; |
3 | 3 | import ReactDOM from 'react-dom'; |
4 | | -import FormRenderer from '../src'; |
| 4 | +import FormRenderer, { validatorTypes } from '../src'; |
5 | 5 | import componentMapper from './form-fields-mapper'; |
6 | 6 | import FormTemplate from './form-template'; |
7 | | -import sandboxSchema from './sandbox'; |
| 7 | +// import sandboxSchema from './sandbox'; |
8 | 8 |
|
9 | 9 | const intl = (name) => `translated ${name}`; |
10 | 10 |
|
11 | 11 | const actionMapper = { |
12 | | - loadData: (data) => () => new Promise((resolve) => setTimeout(() => resolve({ custom: 'ererewr', ...data }), 1700)), |
| 12 | + loadData: (data) => (...args) => |
| 13 | + new Promise((resolve) => { |
| 14 | + setTimeout(() => resolve({ custom: 'ererewr', ...data }), 1700); |
| 15 | + }), |
13 | 16 | loadLabel: intl |
14 | 17 | }; |
15 | 18 |
|
| 19 | +const validatorMapper = { |
| 20 | + asyncValidator: (url, attributes) => (value, allValues) => |
| 21 | + new Promise((resolve, reject) => |
| 22 | + setTimeout(() => { |
| 23 | + if (value === 'error') { |
| 24 | + reject('Async validation failed'); |
| 25 | + } |
| 26 | + |
| 27 | + resolve('hola'); |
| 28 | + }, 1700) |
| 29 | + ) |
| 30 | +}; |
| 31 | + |
| 32 | +const asyncValidatorSchema = { |
| 33 | + fields: [ |
| 34 | + { |
| 35 | + component: 'text-field', |
| 36 | + name: 'async-validation-field', |
| 37 | + label: 'Async validation field', |
| 38 | + validate: [ |
| 39 | + { type: 'asyncValidator' }, |
| 40 | + { type: 'required-validator' }, |
| 41 | + { |
| 42 | + type: validatorTypes.PATTERN_VALIDATOR, |
| 43 | + pattern: '^Foo$', |
| 44 | + flags: 'i' |
| 45 | + } |
| 46 | + ] |
| 47 | + } |
| 48 | + ] |
| 49 | +}; |
| 50 | + |
16 | 51 | const App = () => { |
17 | | - const [values, setValues] = useState({}); |
| 52 | + // const [values, setValues] = useState({}); |
18 | 53 | return ( |
19 | 54 | <div style={{ padding: 20 }}> |
20 | 55 | <FormRenderer |
21 | | - initialValues={{ |
22 | | - text_box_1: 'hue', |
23 | | - text_box_3: 'initial' |
24 | | - }} |
25 | | - clearedValue={'bla'} |
| 56 | + validatorMapper={validatorMapper} |
26 | 57 | componentMapper={componentMapper} |
27 | 58 | onSubmit={(values) => console.log(values)} |
28 | | - onCancel={console.log} |
29 | | - canReset |
30 | | - onReset={() => console.log('i am resseting')} |
31 | | - schema={sandboxSchema} |
32 | | - debug={(state) => setValues(state.values)} |
| 59 | + schema={asyncValidatorSchema} |
33 | 60 | FormTemplate={FormTemplate} |
34 | 61 | actionMapper={actionMapper} |
35 | 62 | /> |
36 | | - <div>{JSON.stringify(values, null, 2)}</div> |
37 | 63 | </div> |
38 | 64 | ); |
39 | 65 | }; |
|
0 commit comments