Skip to content

Commit f1818eb

Browse files
committed
WIP: Simple legacyConditionsMapper implemented, only handles visible-conditions for now
1 parent f524ca4 commit f1818eb

File tree

4 files changed

+101
-12
lines changed

4 files changed

+101
-12
lines changed

packages/react-form-renderer/demo/index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const fileSchema = {
1616
component: 'text-field',
1717
name: 'field2',
1818
label: 'Field2 (try "xyz")',
19+
hideField: true,
1920
},
2021
{
2122
component: 'text-field',
@@ -27,10 +28,20 @@ const fileSchema = {
2728
name: 'field4',
2829
label: 'Field4',
2930
},
31+
32+
{
33+
component: 'text-field',
34+
name: 'field5',
35+
label: 'Field5',
36+
condition: {
37+
when: 'field1',
38+
is: 'cba',
39+
},
40+
},
3041
],
3142
conditions: {
3243
cond1: {
33-
when: 'field1',
44+
when: 'fieldx',
3445
is: 'abc',
3546
then: {
3647
field4: {
@@ -40,6 +51,9 @@ const fileSchema = {
4051
field3: {
4152
disabled: true,
4253
},
54+
field2: {
55+
visible: true,
56+
},
4357
},
4458
},
4559
cond2: {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
We need to collect all field-level conditions (legacy conditions) and remap those to the new
3+
conditions structure.
4+
5+
Legacy example:
6+
fields: [
7+
{
8+
name: 'Foo', // controlled field
9+
component: 'text-field',
10+
}, {
11+
name: 'BarFoo',
12+
label: 'Foo is Bar!',
13+
component: 'text-field',
14+
condition: {
15+
when: 'Foo', // name of controlled field
16+
is: 'Bar', // condition
17+
},
18+
},
19+
]
20+
21+
Remapped condition
22+
- unique key for each condition needs to be generated
23+
- fieldName needed as key under then/else:
24+
25+
"legacy-BarFoo-0": {
26+
when: 'Foo',
27+
is: 'Bar',
28+
then: {
29+
BarFoo: {
30+
visible: true
31+
}
32+
}
33+
else: {
34+
BarFoo: {
35+
visible: false
36+
}
37+
}
38+
},
39+
40+
41+
*/
42+
43+
export const collectLegacyConditions = ({fields}) => {
44+
const conditions = {};
45+
let counter = 0;
46+
let prevName;
47+
48+
fields.forEach(field => {
49+
const {name, condition} = field;
50+
if (name !== prevName) counter = 0;
51+
if (!condition) return;
52+
53+
const key = `legacy-${name}-${counter}`;
54+
55+
conditions[key] = {
56+
when: condition.when,
57+
is: condition.is,
58+
then: {
59+
[name]: {visible: false},
60+
},
61+
else: {
62+
[name]: {visible: true},
63+
},
64+
};
65+
66+
prevName = name;
67+
counter++;
68+
});
69+
70+
return conditions;
71+
};

packages/react-form-renderer/src/files/register-conditions.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ import {Field} from 'react-final-form';
44

55
import {conditionsMapper} from './conditions-mapper';
66
import {parseCondition} from '../form-renderer/condition';
7+
import {collectLegacyConditions} from './legacy-conditions';
78

89
const RegisterConditions = ({schema}) => {
910
const {getState, registerField, dispatchUIState} = useFormApi();
1011

1112
useEffect(() => {
12-
const indexedConditions = conditionsMapper({conditions: schema.conditions});
13+
const legacyConditions = collectLegacyConditions({fields: schema.fields});
14+
const mergedConditions = {...legacyConditions, ...schema.conditions};
15+
const indexedConditions = conditionsMapper({conditions: mergedConditions});
1316

1417
//We need an array of conditions, including the fieldName
1518
const unsubscribeFields = Object.entries(indexedConditions)
@@ -29,25 +32,26 @@ const RegisterConditions = ({schema}) => {
2932

3033
console.log('Parsing conditions for field ' + field.fieldName);
3134

35+
const values = getState().values;
3236
fieldState.data.conditions.map(condition => {
33-
const conditionResult = parseCondition(condition, getState().values);
37+
const conditionResult = parseCondition(condition, values);
3438
const {
3539
uiState: {add, remove},
3640
} = conditionResult;
3741

38-
if (add) {
42+
//remove needs to happen before add. Otherwise an added "then" will be overwritten by a removed "else"
43+
if (remove) {
3944
dispatchUIState({
40-
type: 'addUIState',
45+
type: 'removeUIState',
4146
source: condition.key,
42-
uiState: add,
47+
uiState: remove,
4348
});
4449
}
45-
46-
if (remove) {
50+
if (add) {
4751
dispatchUIState({
48-
type: 'removeUIState',
52+
type: 'addUIState',
4953
source: condition.key,
50-
uiState: remove,
54+
uiState: add,
5155
});
5256
}
5357
});

packages/react-form-renderer/src/form-renderer/condition.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ const fieldCondition = (value, {is, isNotEmpty, isEmpty, pattern, notMatch, flag
2828
};
2929

3030
export const parseCondition = (condition, values) => {
31-
//Positive result is alwaus a triggering condition
32-
//since a the clause always exists
31+
//Positive result is always a triggering condition
32+
//since a then clause always exists
3333
let positiveResult = {
3434
uiState: {
3535
add: condition.then,

0 commit comments

Comments
 (0)