Skip to content

Commit 0985784

Browse files
authored
Merge pull request #807 from data-driven-forms/export-parse-condition
fix(renderer): export parse condition function
2 parents 3b4f31e + 06ec51a commit 0985784

File tree

5 files changed

+94
-84
lines changed

5 files changed

+94
-84
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { AnyObject } from "./common";
2+
import { ConditionDefinition } from "./condition";
3+
4+
export type ParseCondition = (condition: ConditionDefinition, values: AnyObject) => void;
5+
declare const parseCondition: ParseCondition
6+
export default parseCondition;
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import lodashIsEmpty from 'lodash/isEmpty';
2+
import get from 'lodash/get';
3+
4+
const isEmptyValue = (value) => (typeof value === 'number' || value === true ? false : lodashIsEmpty(value));
5+
6+
const fieldCondition = (value, { is, isNotEmpty, isEmpty, pattern, notMatch, flags }) => {
7+
if (isNotEmpty) {
8+
return !isEmptyValue(value);
9+
}
10+
11+
if (isEmpty) {
12+
return isEmptyValue(value);
13+
}
14+
15+
if (pattern) {
16+
const regExpPattern = RegExp(pattern, flags);
17+
18+
return notMatch ? !regExpPattern.test(value) : regExpPattern.test(value);
19+
}
20+
21+
const isMatched = Array.isArray(is) ? !!is.includes(value) : value === is;
22+
23+
return notMatch ? !isMatched : isMatched;
24+
};
25+
26+
export const parseCondition = (condition, values) => {
27+
let positiveResult = {
28+
visible: true,
29+
...condition.then,
30+
result: true
31+
};
32+
33+
let negativeResult = {
34+
visible: false,
35+
...condition.else,
36+
result: false
37+
};
38+
39+
if (Array.isArray(condition)) {
40+
return !condition.map((condition) => parseCondition(condition, values)).some(({ result }) => result === false) ? positiveResult : negativeResult;
41+
}
42+
43+
if (condition.and) {
44+
return !condition.and.map((condition) => parseCondition(condition, values)).some(({ result }) => result === false)
45+
? positiveResult
46+
: negativeResult;
47+
}
48+
49+
if (condition.sequence) {
50+
return condition.sequence.reduce(
51+
(acc, curr) => {
52+
const result = parseCondition(curr, values);
53+
54+
return {
55+
sets: [...acc.sets, ...(result.set ? [result.set] : [])],
56+
visible: acc.visible || result.visible,
57+
result: acc.result || result.result
58+
};
59+
},
60+
{ ...negativeResult, sets: [] }
61+
);
62+
}
63+
64+
if (condition.or) {
65+
return condition.or.map((condition) => parseCondition(condition, values)).some(({ result }) => result === true) ? positiveResult : negativeResult;
66+
}
67+
68+
if (condition.not) {
69+
return !parseCondition(condition.not, values).result ? positiveResult : negativeResult;
70+
}
71+
72+
if (typeof condition.when === 'string') {
73+
return fieldCondition(get(values, condition.when), condition) ? positiveResult : negativeResult;
74+
}
75+
76+
if (Array.isArray(condition.when)) {
77+
return condition.when.map((fieldName) => fieldCondition(get(values, fieldName), condition)).find((condition) => !!condition)
78+
? positiveResult
79+
: negativeResult;
80+
}
81+
82+
return negativeResult;
83+
};
84+
85+
export default parseCondition;

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

Lines changed: 1 addition & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,9 @@
11
import React, { useEffect, useReducer } from 'react';
22
import PropTypes from 'prop-types';
3-
import lodashIsEmpty from 'lodash/isEmpty';
4-
import get from 'lodash/get';
53
import isEqual from 'lodash/isEqual';
64

75
import useFormApi from '../files/use-form-api';
8-
9-
const isEmptyValue = (value) => (typeof value === 'number' || value === true ? false : lodashIsEmpty(value));
10-
11-
const fieldCondition = (value, { is, isNotEmpty, isEmpty, pattern, notMatch, flags }) => {
12-
if (isNotEmpty) {
13-
return !isEmptyValue(value);
14-
}
15-
16-
if (isEmpty) {
17-
return isEmptyValue(value);
18-
}
19-
20-
if (pattern) {
21-
const regExpPattern = RegExp(pattern, flags);
22-
23-
return notMatch ? !regExpPattern.test(value) : regExpPattern.test(value);
24-
}
25-
26-
const isMatched = Array.isArray(is) ? !!is.includes(value) : value === is;
27-
28-
return notMatch ? !isMatched : isMatched;
29-
};
30-
31-
export const parseCondition = (condition, values) => {
32-
let positiveResult = {
33-
visible: true,
34-
...condition.then,
35-
result: true
36-
};
37-
38-
let negativeResult = {
39-
visible: false,
40-
...condition.else,
41-
result: false
42-
};
43-
44-
if (Array.isArray(condition)) {
45-
return !condition.map((condition) => parseCondition(condition, values)).some(({ result }) => result === false) ? positiveResult : negativeResult;
46-
}
47-
48-
if (condition.and) {
49-
return !condition.and.map((condition) => parseCondition(condition, values)).some(({ result }) => result === false)
50-
? positiveResult
51-
: negativeResult;
52-
}
53-
54-
if (condition.sequence) {
55-
return condition.sequence.reduce(
56-
(acc, curr) => {
57-
const result = parseCondition(curr, values);
58-
59-
return {
60-
sets: [...acc.sets, ...(result.set ? [result.set] : [])],
61-
visible: acc.visible || result.visible,
62-
result: acc.result || result.result
63-
};
64-
},
65-
{ ...negativeResult, sets: [] }
66-
);
67-
}
68-
69-
if (condition.or) {
70-
return condition.or.map((condition) => parseCondition(condition, values)).some(({ result }) => result === true) ? positiveResult : negativeResult;
71-
}
72-
73-
if (condition.not) {
74-
return !parseCondition(condition.not, values).result ? positiveResult : negativeResult;
75-
}
76-
77-
if (typeof condition.when === 'string') {
78-
return fieldCondition(get(values, condition.when), condition) ? positiveResult : negativeResult;
79-
}
80-
81-
if (Array.isArray(condition.when)) {
82-
return condition.when.map((fieldName) => fieldCondition(get(values, fieldName), condition)).find((condition) => !!condition)
83-
? positiveResult
84-
: negativeResult;
85-
}
86-
87-
return negativeResult;
88-
};
6+
import parseCondition from '../files/parse-condition';
897

908
export const reducer = (state, { type, sets }) => {
919
switch (type) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ export { default as FieldProvider } from './files/field-provider';
1414
export { default as useFieldApi } from './files/use-field-api';
1515
export { default as DefaultSchemaError } from './files/schema-errors';
1616
export { default as WizardContext } from './files/wizard-context';
17+
export { default as parseCondition } from './files/parse-condition';

packages/react-form-renderer/src/tests/form-renderer/parse-condition.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { parseCondition } from '../../form-renderer/condition';
1+
import parseCondition from '../../files/parse-condition';
22

33
describe('parseCondition', () => {
44
let condition;

0 commit comments

Comments
 (0)