Skip to content

Commit af0b59c

Browse files
committed
Add sequence and conditional actions to demo
1 parent 5b711e9 commit af0b59c

File tree

3 files changed

+164
-2
lines changed

3 files changed

+164
-2
lines changed

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

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ 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
37+
### Or
3838

3939
At least one condition must be met.
4040

@@ -85,7 +85,7 @@ Also, you can use a shorthand:
8585

8686
<RawComponent source="conditions/or" />
8787

88-
### AND
88+
### And
8989

9090
All conditions must be met.
9191

@@ -199,6 +199,27 @@ As the value you can use an array (AND) or another condition.
199199

200200
<RawComponent source="conditions/not" />
201201

202+
### Sequence
203+
204+
This special type of condition allows to trigger a sequence of multiple independent conditions. This is useful in combination with [conditional actions](/renderer/condition#conditionalactions). Setters are executed independently. Visibility is set to true, if any of the conditions sets it to true. Sequence has to be currently the root condition, that means the sequence cannot be nested within other types of conditions such as `and`, `or` and `not`.
205+
206+
```jsx
207+
{
208+
fields: [{
209+
name: 'Sequence condition',
210+
component: 'text-field',
211+
condition: {
212+
sequence: [
213+
{ when: ['a'], is: 'x', then: { set: { field: 'value' } } },
214+
{ when: ['b'], is: 'x', then: { set: { field: 'different value' } } }
215+
]
216+
}
217+
}]
218+
}
219+
```
220+
221+
<RawComponent source="conditions/sequence" />
222+
202223
### Nesting
203224

204225
Of course it is possible to nest conditions:
@@ -334,6 +355,44 @@ condition: {
334355

335356
<RawComponent source="conditions/not-match" />
336357

358+
## Conditional actions
359+
360+
There are currently two types of conditionals actions: `visible` and `set`. These actions can be called from `then` or `else` statements in root conditions. (Conditions has to be the root of the condition tree, or they have to be included in a [sequence](/renderer/condition#sequence) array.)
361+
362+
```jsx
363+
condition: { when: 'x', is: 'y', then: { ... }, else: { ... } }
364+
```
365+
366+
### Set
367+
368+
Setter allows to change form values according to selected values in different fields.
369+
370+
```jsx
371+
// Single value
372+
condition: { when: 'x', is: 'y', then: { set: { [field]: value } } }
373+
374+
// Multiple values
375+
condition: {
376+
when: 'x',
377+
is: 'y',
378+
then: { set: { [field1]: value1, [field2]: value2 } }
379+
}
380+
```
381+
382+
Set is a object consists of field names as keys and values as values. You can change any form field value from any conditional action.
383+
384+
### Visible
385+
386+
Visible controls visibility of the field that includes the condition. By default, it is set to `true`.
387+
388+
```jsx
389+
condition: { when: 'x', is: 'y', then: { visible: true } }
390+
```
391+
392+
### Example
393+
394+
<RawComponent source="conditions/set" />
395+
337396
## Clearing values
338397

339398
If you need to clear values after switching fields, please see [here](/renderer/unmounting).
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: 'Sequence condition',
10+
fields: [
11+
{
12+
component: componentTypes.TEXT_FIELD,
13+
name: 'field-1',
14+
label: 'What do you like?',
15+
helperText: 'Write dog or cat here.'
16+
},
17+
{
18+
component: componentTypes.TEXT_FIELD,
19+
name: 'field-2',
20+
label: 'What I like...',
21+
isDisabled: true,
22+
condition: {
23+
sequence: [
24+
{ when: 'field-1', pattern: /^cat/i, then: { set: { 'field-2': 'I love cats!' } }, else: { visible: true } },
25+
{ when: 'field-1', pattern: /^dog/i, then: { set: { 'field-2': 'I love dogs!' } }, else: { visible: true } },
26+
{
27+
when: 'field-1',
28+
notMatch: 'true',
29+
pattern: /^(cat|dog)/i,
30+
then: { set: { 'field-2': 'I love something else!' } },
31+
else: { visible: true }
32+
}
33+
]
34+
}
35+
}
36+
]
37+
};
38+
39+
const componentMapper = {
40+
[componentTypes.TEXT_FIELD]: TextField
41+
};
42+
43+
const SequenceCondition = () => (
44+
<div className="pf4">
45+
<FormRenderer FormTemplate={FormTemplate} componentMapper={componentMapper} schema={schema} onSubmit={console.log} />
46+
</div>
47+
);
48+
49+
export default SequenceCondition;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 Checkbox from '@data-driven-forms/pf4-component-mapper/dist/cjs/checkbox';
6+
import TextField from '@data-driven-forms/pf4-component-mapper/dist/cjs/text-field';
7+
import FormTemplate from '@data-driven-forms/pf4-component-mapper/dist/cjs/form-template';
8+
9+
const schema = {
10+
title: 'Set action',
11+
fields: [
12+
{
13+
component: componentTypes.CHECKBOX,
14+
name: 'useDefaultNickName',
15+
label: 'Do you want to use default nickname?',
16+
condition: {
17+
when: 'nickname',
18+
is: 'User123',
19+
notMatch: true,
20+
then: {
21+
set: { useDefaultNickName: false }
22+
},
23+
else: { visible: true, set: { useDefaultNickName: true } }
24+
},
25+
description: 'set: {} is used to reset the setter'
26+
},
27+
{
28+
component: componentTypes.TEXT_FIELD,
29+
name: 'nickname',
30+
label: 'Nickname',
31+
condition: {
32+
when: 'useDefaultNickName',
33+
is: true,
34+
then: {
35+
set: { nickname: 'User123' }
36+
},
37+
else: { visible: true, set: {} }
38+
}
39+
}
40+
]
41+
};
42+
43+
const componentMapper = {
44+
[componentTypes.CHECKBOX]: Checkbox,
45+
[componentTypes.TEXT_FIELD]: TextField
46+
};
47+
48+
const SetAction = () => (
49+
<div className="pf4">
50+
<FormRenderer FormTemplate={FormTemplate} componentMapper={componentMapper} schema={schema} onSubmit={console.log} />
51+
</div>
52+
);
53+
54+
export default SetAction;

0 commit comments

Comments
 (0)