|
1 | 1 | import React from 'react';
|
2 |
| -import FormRenderer, { componentTypes } from '@data-driven-forms/react-form-renderer'; |
3 |
| -import { FormTemplate, componentMapper } from '@data-driven-forms/pf4-component-mapper'; |
| 2 | +import FormRenderer from '@data-driven-forms/react-form-renderer/dist/cjs/form-renderer'; |
| 3 | +import componentTypes from '@data-driven-forms/react-form-renderer/dist/cjs/component-types'; |
| 4 | + |
| 5 | +import TextField from '@data-driven-forms/pf4-component-mapper/dist/cjs/text-field'; |
| 6 | +import FormTemplate from '@data-driven-forms/pf4-component-mapper/dist/cjs/form-template'; |
| 7 | +import Checkbox from '@data-driven-forms/pf4-component-mapper/dist/cjs/checkbox'; |
| 8 | +import PlainText from '@data-driven-forms/pf4-component-mapper/dist/cjs/plain-text'; |
4 | 9 |
|
5 | 10 | const schema = {
|
6 | 11 | title: 'Example of conditions',
|
7 |
| - fields: [{ |
8 |
| - component: componentTypes.TEXT_FIELD, |
9 |
| - name: 'field-1', |
10 |
| - label: 'Field 1', |
11 |
| - helperText: 'To show field 2 type a cat', |
12 |
| - }, { |
13 |
| - component: componentTypes.TEXT_FIELD, |
14 |
| - name: 'field-2', |
15 |
| - label: 'Field 2', |
16 |
| - helperText: 'To show field 3 type a cat as the second word', |
17 |
| - condition: { |
18 |
| - when: 'field-1', |
19 |
| - is: 'cat', |
| 12 | + fields: [ |
| 13 | + { |
| 14 | + component: componentTypes.TEXT_FIELD, |
| 15 | + name: 'field-1', |
| 16 | + label: 'Field 1', |
| 17 | + helperText: 'To show field 2 type a cat' |
| 18 | + }, |
| 19 | + { |
| 20 | + component: componentTypes.TEXT_FIELD, |
| 21 | + name: 'field-2', |
| 22 | + label: 'Field 2', |
| 23 | + helperText: 'To show field 3 type a cat as the second word', |
| 24 | + condition: { |
| 25 | + when: 'field-1', |
| 26 | + is: 'cat' |
| 27 | + } |
| 28 | + }, |
| 29 | + { |
| 30 | + component: componentTypes.TEXT_FIELD, |
| 31 | + name: 'field-3', |
| 32 | + label: 'Field 3', |
| 33 | + condition: { |
| 34 | + when: 'field-2', |
| 35 | + pattern: /^\w+ cat/ |
| 36 | + }, |
| 37 | + initialValue: 'We all love cats!' |
20 | 38 | },
|
21 |
| - }, { |
22 |
| - component: componentTypes.TEXT_FIELD, |
23 |
| - name: 'field-3', |
24 |
| - label: 'Field 3', |
25 |
| - condition: { |
26 |
| - when: 'field-2', |
27 |
| - pattern: /^\w+ cat/, |
| 39 | + { |
| 40 | + component: componentTypes.CHECKBOX, |
| 41 | + name: 'check-1', |
| 42 | + label: 'I want to be a true', |
| 43 | + condition: { when: 'field-3', isNotEmpty: true } |
28 | 44 | },
|
29 |
| - initialValue: 'We all love cats!', |
30 |
| - }, { |
31 |
| - component: componentTypes.CHECKBOX, |
32 |
| - name: 'check-1', |
33 |
| - label: 'I want to be a true', |
34 |
| - condition: { when: 'field-3', isNotEmpty: true }, |
35 |
| - }, { |
36 |
| - component: componentTypes.CHECKBOX, |
37 |
| - name: 'check-2', |
38 |
| - label: 'I also want to be true', |
39 |
| - condition: { when: 'field-3', isNotEmpty: true }, |
40 |
| - }, { |
41 |
| - component: componentTypes.CHECKBOX, |
42 |
| - name: 'check-3', |
43 |
| - label: 'Hmmm, please, uncheck me.', |
44 |
| - condition: { |
45 |
| - and: [ |
46 |
| - { when: 'check-1', is: true }, |
47 |
| - { when: 'check-2', is: true }, |
48 |
| - ], |
| 45 | + { |
| 46 | + component: componentTypes.CHECKBOX, |
| 47 | + name: 'check-2', |
| 48 | + label: 'I also want to be true', |
| 49 | + condition: { when: 'field-3', isNotEmpty: true } |
49 | 50 | },
|
50 |
| - initialValue: true, |
51 |
| - }, { |
52 |
| - component: componentTypes.PLAIN_TEXT, |
53 |
| - name: 'congrats!', |
54 |
| - label: 'You made it!', |
55 |
| - condition: { |
56 |
| - and: [ |
57 |
| - { and: [ |
| 51 | + { |
| 52 | + component: componentTypes.CHECKBOX, |
| 53 | + name: 'check-3', |
| 54 | + label: 'Hmmm, please, uncheck me.', |
| 55 | + condition: { |
| 56 | + and: [ |
58 | 57 | { when: 'check-1', is: true },
|
59 |
| - { when: 'check-2', is: true }, |
60 |
| - ]}, |
61 |
| - { not: { when: 'check-3', isNotEmpty: true }}, |
62 |
| - ], |
| 58 | + { when: 'check-2', is: true } |
| 59 | + ] |
| 60 | + }, |
| 61 | + initialValue: true |
63 | 62 | },
|
64 |
| - }], |
| 63 | + { |
| 64 | + component: componentTypes.PLAIN_TEXT, |
| 65 | + name: 'congrats!', |
| 66 | + label: 'You made it!', |
| 67 | + condition: { |
| 68 | + and: [ |
| 69 | + { |
| 70 | + and: [ |
| 71 | + { when: 'check-1', is: true }, |
| 72 | + { when: 'check-2', is: true } |
| 73 | + ] |
| 74 | + }, |
| 75 | + { not: { when: 'check-3', isNotEmpty: true } } |
| 76 | + ] |
| 77 | + } |
| 78 | + } |
| 79 | + ] |
65 | 80 | };
|
66 | 81 |
|
| 82 | +const componentMapper = { |
| 83 | + [componentTypes.TEXT_FIELD]: TextField, |
| 84 | + [componentTypes.PLAIN_TEXT]: PlainText, |
| 85 | + [componentTypes.CHECKBOX]: Checkbox |
| 86 | +}; |
| 87 | + |
| 88 | +const FormTemplateWrapper = (props) => <FormTemplate {...props} showFormControls={false} />; |
| 89 | + |
67 | 90 | const Condition = () => (
|
68 | 91 | <div className="pf4">
|
69 |
| - <FormRenderer |
70 |
| - FormTemplate={ (props) => <FormTemplate {...props} showFormControls={false} /> } |
71 |
| - componentMapper={ componentMapper } |
72 |
| - schema={ schema } |
73 |
| - onSubmit={ console.log } |
74 |
| - /> |
| 92 | + <FormRenderer FormTemplate={FormTemplateWrapper} componentMapper={componentMapper} schema={schema} onSubmit={console.log} /> |
75 | 93 | </div>
|
76 | 94 | );
|
77 | 95 |
|
|
0 commit comments