Skip to content

Commit 0ebc452

Browse files
committed
Add conditions example
1 parent 86af897 commit 0ebc452

File tree

9 files changed

+358
-6
lines changed

9 files changed

+358
-6
lines changed

packages/react-renderer-demo/src/app/pages/renderer/condition.md

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ You can show a field only if it meets a condition:
3434

3535
`when` - is name of field where the value is stored, **always required!**. It can be either string `'field-name'` or array of strings `['field-1', 'field-2']`.
3636

37-
### OR condition. At least one condition must be met
37+
### OR
38+
39+
At least one condition must be met.
3840

3941
You can use `or` object to connect two conditions. If either of of fields with name `a` and `b` will have value `x` condition is met.
4042

@@ -81,7 +83,11 @@ Also, you can use a shorthand:
8183
}
8284
```
8385

84-
### AND condition. All conditions must be met
86+
<RawComponent source="conditions/or" />
87+
88+
### AND
89+
90+
All conditions must be met.
8591

8692
Field `controlled-field-1` must have value `Bar` and field `controlled-field-2` must include `FooBar` somewhere in its value to display field `BarFoo`.
8793

@@ -154,6 +160,8 @@ Or you can use a shorthand:
154160
}
155161
```
156162

163+
<RawComponent source="conditions/and" />
164+
157165
### Not
158166

159167
You can simple negate a condition by using a `not`. Following condition is a true, when both of values are not a `x`.
@@ -189,9 +197,11 @@ You can simple negate a condition by using a `not`. Following condition is a tru
189197

190198
As the value you can use an array (AND) or another condition.
191199

200+
<RawComponent source="conditions/not" />
201+
192202
### Nesting
193203

194-
Of course it is possible to neste conditions:
204+
Of course it is possible to nest conditions:
195205

196206
```jsx
197207
condition = {
@@ -202,6 +212,8 @@ condition = {
202212
};
203213
```
204214

215+
<RawComponent source="condition" />
216+
205217
## Conditions
206218

207219
### Is
@@ -217,6 +229,9 @@ condition: {
217229
// Foo == 'Bar' => true
218230
// Foo == 'Not a Bar' => false
219231
```
232+
233+
<RawComponent source="conditions/is" />
234+
220235
### Is empty
221236

222237
`isEmpty` - tests if the value is empty (using [lodash function](https://lodash.com/docs/4.17.11#isEmpty))
@@ -237,6 +252,9 @@ condition: {
237252
// Foo = false => true
238253
// Foo = true => false
239254
```
255+
256+
<RawComponent source="conditions/is-empty" />
257+
240258
### Is not empty
241259

242260
`isNotEmpty` - tests if the value is not empty (using [lodash function](https://lodash.com/docs/4.17.11#isEmpty))
@@ -257,6 +275,9 @@ condition: {
257275
// Foo = true => false
258276
// Foo = true => true
259277
```
278+
279+
<RawComponent source="conditions/is-not-empty" />
280+
260281
### Pattern
261282

262283
`pattern` - tests if the value matches the pattern
@@ -284,6 +305,8 @@ condition: {
284305
// Foo = 'bAr!' => true
285306
```
286307

308+
<RawComponent source="conditions/pattern" />
309+
287310
### Not match
288311

289312
`notMatch` - reverse `is`/`pattern` condition
@@ -309,9 +332,7 @@ condition: {
309332
// Foo = 'bar' => false
310333
```
311334

312-
## Example
313-
314-
<RawComponent source="condition" />
335+
<RawComponent source="conditions/not-match" />
315336

316337
## Clearing values
317338

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import React from 'react';
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+
8+
const schema = {
9+
title: 'And condition',
10+
fields: [
11+
{
12+
component: componentTypes.TEXT_FIELD,
13+
name: 'field-1',
14+
label: 'Field 1',
15+
helperText: 'To show field 3 type a cat here...'
16+
},
17+
{
18+
component: componentTypes.TEXT_FIELD,
19+
name: 'field-2',
20+
label: 'Field 1',
21+
helperText: '...and dog here.'
22+
},
23+
{
24+
component: componentTypes.TEXT_FIELD,
25+
name: 'field-3',
26+
label: 'Field 3',
27+
initialValue: 'I am shown!',
28+
isDisabled: true,
29+
condition: {
30+
and: [
31+
{ when: 'field-1', is: 'cat' },
32+
{ when: 'field-2', is: 'dog' }
33+
]
34+
}
35+
}
36+
]
37+
};
38+
39+
const componentMapper = {
40+
[componentTypes.TEXT_FIELD]: TextField
41+
};
42+
43+
const AndCondition = () => (
44+
<div className="pf4">
45+
<FormRenderer FormTemplate={FormTemplate} componentMapper={componentMapper} schema={schema} onSubmit={console.log} />
46+
</div>
47+
);
48+
49+
export default AndCondition;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import React from 'react';
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+
8+
const schema = {
9+
title: 'IsEmpty condition',
10+
fields: [
11+
{
12+
component: componentTypes.TEXT_FIELD,
13+
name: 'field-1',
14+
label: 'Field 1',
15+
helperText: 'To show field 2 remove cat here!',
16+
initialValue: 'cat'
17+
},
18+
{
19+
component: componentTypes.TEXT_FIELD,
20+
name: 'field-2',
21+
label: 'Field 2',
22+
initialValue: 'I am shown!',
23+
isDisabled: true,
24+
condition: { when: 'field-1', isEmpty: true }
25+
}
26+
]
27+
};
28+
29+
const componentMapper = {
30+
[componentTypes.TEXT_FIELD]: TextField
31+
};
32+
33+
const IsEmpty = () => (
34+
<div className="pf4">
35+
<FormRenderer FormTemplate={FormTemplate} componentMapper={componentMapper} schema={schema} onSubmit={console.log} />
36+
</div>
37+
);
38+
39+
export default IsEmpty;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React from 'react';
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+
8+
const schema = {
9+
title: 'IsNotEmpty condition',
10+
fields: [
11+
{
12+
component: componentTypes.TEXT_FIELD,
13+
name: 'field-1',
14+
label: 'Field 1',
15+
helperText: 'To show field 2 write anything here!'
16+
},
17+
{
18+
component: componentTypes.TEXT_FIELD,
19+
name: 'field-2',
20+
label: 'Field 2',
21+
initialValue: 'I am shown!',
22+
isDisabled: true,
23+
condition: { when: 'field-1', isNotEmpty: true }
24+
}
25+
]
26+
};
27+
28+
const componentMapper = {
29+
[componentTypes.TEXT_FIELD]: TextField
30+
};
31+
32+
const IsNotEmpty = () => (
33+
<div className="pf4">
34+
<FormRenderer FormTemplate={FormTemplate} componentMapper={componentMapper} schema={schema} onSubmit={console.log} />
35+
</div>
36+
);
37+
38+
export default IsNotEmpty;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React from 'react';
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+
8+
const schema = {
9+
title: 'Is condition',
10+
fields: [
11+
{
12+
component: componentTypes.TEXT_FIELD,
13+
name: 'field-1',
14+
label: 'Field 1',
15+
helperText: 'To show field 2 type cat here!'
16+
},
17+
{
18+
component: componentTypes.TEXT_FIELD,
19+
name: 'field-2',
20+
label: 'Field 2',
21+
initialValue: 'I am shown!',
22+
isDisabled: true,
23+
condition: { when: 'field-1', is: 'cat' }
24+
}
25+
]
26+
};
27+
28+
const componentMapper = {
29+
[componentTypes.TEXT_FIELD]: TextField
30+
};
31+
32+
const IsCondition = () => (
33+
<div className="pf4">
34+
<FormRenderer FormTemplate={FormTemplate} componentMapper={componentMapper} schema={schema} onSubmit={console.log} />
35+
</div>
36+
);
37+
38+
export default IsCondition;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import React from 'react';
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+
8+
const schema = {
9+
title: 'notMatch condition',
10+
fields: [
11+
{
12+
component: componentTypes.TEXT_FIELD,
13+
name: 'field-1',
14+
label: 'Field 1',
15+
helperText: 'To show field 2 remove the cat!',
16+
initialValue: 'blablacatbla'
17+
},
18+
{
19+
component: componentTypes.TEXT_FIELD,
20+
name: 'field-2',
21+
label: 'Field 2',
22+
initialValue: 'I am shown!',
23+
isDisabled: true,
24+
condition: { when: 'field-1', pattern: /cat/, notMatch: true }
25+
}
26+
]
27+
};
28+
29+
const componentMapper = {
30+
[componentTypes.TEXT_FIELD]: TextField
31+
};
32+
33+
const NotMatch = () => (
34+
<div className="pf4">
35+
<FormRenderer FormTemplate={FormTemplate} componentMapper={componentMapper} schema={schema} onSubmit={console.log} />
36+
</div>
37+
);
38+
39+
export default NotMatch;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import React from 'react';
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+
8+
const schema = {
9+
title: 'Not condition',
10+
fields: [
11+
{
12+
component: componentTypes.TEXT_FIELD,
13+
name: 'field-1',
14+
label: 'Field 1',
15+
helperText: 'To show field 2 type anything else than cat here!',
16+
initialValue: 'cat'
17+
},
18+
{
19+
component: componentTypes.TEXT_FIELD,
20+
name: 'field-2',
21+
label: 'Field 2',
22+
initialValue: 'I am shown!',
23+
isDisabled: true,
24+
condition: {
25+
not: { when: 'field-1', is: 'cat' }
26+
}
27+
}
28+
]
29+
};
30+
31+
const componentMapper = {
32+
[componentTypes.TEXT_FIELD]: TextField
33+
};
34+
35+
const NotCondition = () => (
36+
<div className="pf4">
37+
<FormRenderer FormTemplate={FormTemplate} componentMapper={componentMapper} schema={schema} onSubmit={console.log} />
38+
</div>
39+
);
40+
41+
export default NotCondition;

0 commit comments

Comments
 (0)