@pega/cosmos-react-condition-builder 5.0.0-dev.4.6 → 5.0.0-dev.4.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/lib/components/ConditionBuilder/AtomicCondition.d.ts +0 -57
- package/lib/components/ConditionBuilder/AtomicCondition.d.ts.map +0 -1
- package/lib/components/ConditionBuilder/AtomicCondition.js +0 -164
- package/lib/components/ConditionBuilder/AtomicCondition.js.map +0 -1
- package/lib/components/ConditionBuilder/ConditionBuilder.d.ts +0 -6
- package/lib/components/ConditionBuilder/ConditionBuilder.d.ts.map +0 -1
- package/lib/components/ConditionBuilder/ConditionBuilder.js +0 -271
- package/lib/components/ConditionBuilder/ConditionBuilder.js.map +0 -1
- package/lib/components/ConditionBuilder/ConditionBuilder.styles.d.ts +0 -7
- package/lib/components/ConditionBuilder/ConditionBuilder.styles.d.ts.map +0 -1
- package/lib/components/ConditionBuilder/ConditionBuilder.styles.js +0 -59
- package/lib/components/ConditionBuilder/ConditionBuilder.styles.js.map +0 -1
- package/lib/components/ConditionBuilder/ConditionBuilder.types.d.ts +0 -136
- package/lib/components/ConditionBuilder/ConditionBuilder.types.d.ts.map +0 -1
- package/lib/components/ConditionBuilder/ConditionBuilder.types.js +0 -2
- package/lib/components/ConditionBuilder/ConditionBuilder.types.js.map +0 -1
- package/lib/components/ConditionBuilder/FieldCondition.d.ts +0 -7
- package/lib/components/ConditionBuilder/FieldCondition.d.ts.map +0 -1
- package/lib/components/ConditionBuilder/FieldCondition.js +0 -26
- package/lib/components/ConditionBuilder/FieldCondition.js.map +0 -1
- package/lib/components/ConditionBuilder/FieldCondition.types.d.ts +0 -30
- package/lib/components/ConditionBuilder/FieldCondition.types.d.ts.map +0 -1
- package/lib/components/ConditionBuilder/FieldCondition.types.js +0 -2
- package/lib/components/ConditionBuilder/FieldCondition.types.js.map +0 -1
- package/lib/components/ConditionBuilder/RhsControls/DateFunctionMenu.d.ts +0 -18
- package/lib/components/ConditionBuilder/RhsControls/DateFunctionMenu.d.ts.map +0 -1
- package/lib/components/ConditionBuilder/RhsControls/DateFunctionMenu.js +0 -155
- package/lib/components/ConditionBuilder/RhsControls/DateFunctionMenu.js.map +0 -1
- package/lib/components/ConditionBuilder/RhsControls/FieldSelector.d.ts +0 -21
- package/lib/components/ConditionBuilder/RhsControls/FieldSelector.d.ts.map +0 -1
- package/lib/components/ConditionBuilder/RhsControls/FieldSelector.js +0 -60
- package/lib/components/ConditionBuilder/RhsControls/FieldSelector.js.map +0 -1
- package/lib/components/ConditionBuilder/RhsControls/NumericInput.d.ts +0 -18
- package/lib/components/ConditionBuilder/RhsControls/NumericInput.d.ts.map +0 -1
- package/lib/components/ConditionBuilder/RhsControls/NumericInput.js +0 -22
- package/lib/components/ConditionBuilder/RhsControls/NumericInput.js.map +0 -1
- package/lib/components/ConditionBuilder/RhsControls/RelativeDateMenu.d.ts +0 -18
- package/lib/components/ConditionBuilder/RhsControls/RelativeDateMenu.d.ts.map +0 -1
- package/lib/components/ConditionBuilder/RhsControls/RelativeDateMenu.js +0 -119
- package/lib/components/ConditionBuilder/RhsControls/RelativeDateMenu.js.map +0 -1
- package/lib/components/ConditionBuilder/RhsControls/RhsModeSwitch.d.ts +0 -26
- package/lib/components/ConditionBuilder/RhsControls/RhsModeSwitch.d.ts.map +0 -1
- package/lib/components/ConditionBuilder/RhsControls/RhsModeSwitch.js +0 -156
- package/lib/components/ConditionBuilder/RhsControls/RhsModeSwitch.js.map +0 -1
- package/lib/components/ConditionBuilder/RhsControls/TimePeriodInput.d.ts +0 -6
- package/lib/components/ConditionBuilder/RhsControls/TimePeriodInput.d.ts.map +0 -1
- package/lib/components/ConditionBuilder/RhsControls/TimePeriodInput.js +0 -112
- package/lib/components/ConditionBuilder/RhsControls/TimePeriodInput.js.map +0 -1
- package/lib/components/ConditionBuilder/RhsControls/TimePeriodMenu.d.ts +0 -20
- package/lib/components/ConditionBuilder/RhsControls/TimePeriodMenu.d.ts.map +0 -1
- package/lib/components/ConditionBuilder/RhsControls/TimePeriodMenu.js +0 -12
- package/lib/components/ConditionBuilder/RhsControls/TimePeriodMenu.js.map +0 -1
- package/lib/components/ConditionBuilder/RhsControls/ValueSelector.d.ts +0 -45
- package/lib/components/ConditionBuilder/RhsControls/ValueSelector.d.ts.map +0 -1
- package/lib/components/ConditionBuilder/RhsControls/ValueSelector.js +0 -196
- package/lib/components/ConditionBuilder/RhsControls/ValueSelector.js.map +0 -1
- package/lib/components/ConditionBuilder/RhsControls/index.d.ts +0 -42
- package/lib/components/ConditionBuilder/RhsControls/index.d.ts.map +0 -1
- package/lib/components/ConditionBuilder/RhsControls/index.js +0 -321
- package/lib/components/ConditionBuilder/RhsControls/index.js.map +0 -1
- package/lib/components/ConditionBuilder/core/comparators.d.ts +0 -12
- package/lib/components/ConditionBuilder/core/comparators.d.ts.map +0 -1
- package/lib/components/ConditionBuilder/core/comparators.js +0 -78
- package/lib/components/ConditionBuilder/core/comparators.js.map +0 -1
- package/lib/components/ConditionBuilder/core/evaluator.d.ts +0 -17
- package/lib/components/ConditionBuilder/core/evaluator.d.ts.map +0 -1
- package/lib/components/ConditionBuilder/core/evaluator.js +0 -314
- package/lib/components/ConditionBuilder/core/evaluator.js.map +0 -1
- package/lib/components/ConditionBuilder/core/extendDayJs.d.ts +0 -2
- package/lib/components/ConditionBuilder/core/extendDayJs.d.ts.map +0 -1
- package/lib/components/ConditionBuilder/core/extendDayJs.js +0 -4
- package/lib/components/ConditionBuilder/core/extendDayJs.js.map +0 -1
- package/lib/components/ConditionBuilder/core/formatter.d.ts +0 -17
- package/lib/components/ConditionBuilder/core/formatter.d.ts.map +0 -1
- package/lib/components/ConditionBuilder/core/formatter.js +0 -262
- package/lib/components/ConditionBuilder/core/formatter.js.map +0 -1
- package/lib/components/ConditionBuilder/core/rows-reducer.d.ts +0 -27
- package/lib/components/ConditionBuilder/core/rows-reducer.d.ts.map +0 -1
- package/lib/components/ConditionBuilder/core/rows-reducer.js +0 -50
- package/lib/components/ConditionBuilder/core/rows-reducer.js.map +0 -1
- package/lib/components/ConditionBuilder/core/time-utils.d.ts +0 -5
- package/lib/components/ConditionBuilder/core/time-utils.d.ts.map +0 -1
- package/lib/components/ConditionBuilder/core/time-utils.js +0 -23
- package/lib/components/ConditionBuilder/core/time-utils.js.map +0 -1
- package/lib/components/ConditionBuilder/core/transformer.d.ts +0 -7
- package/lib/components/ConditionBuilder/core/transformer.d.ts.map +0 -1
- package/lib/components/ConditionBuilder/core/transformer.js +0 -210
- package/lib/components/ConditionBuilder/core/transformer.js.map +0 -1
- package/lib/components/ConditionBuilder/core/types.d.ts +0 -47
- package/lib/components/ConditionBuilder/core/types.d.ts.map +0 -1
- package/lib/components/ConditionBuilder/core/types.js +0 -2
- package/lib/components/ConditionBuilder/core/types.js.map +0 -1
- package/lib/components/ConditionBuilder/core/utils.d.ts +0 -37
- package/lib/components/ConditionBuilder/core/utils.d.ts.map +0 -1
- package/lib/components/ConditionBuilder/core/utils.js +0 -454
- package/lib/components/ConditionBuilder/core/utils.js.map +0 -1
- package/lib/components/ConditionBuilder/index.d.ts +0 -11
- package/lib/components/ConditionBuilder/index.d.ts.map +0 -1
- package/lib/components/ConditionBuilder/index.js +0 -8
- package/lib/components/ConditionBuilder/index.js.map +0 -1
- package/lib/components/ConditionInput/ConditionInput.d.ts +0 -6
- package/lib/components/ConditionInput/ConditionInput.d.ts.map +0 -1
- package/lib/components/ConditionInput/ConditionInput.js +0 -302
- package/lib/components/ConditionInput/ConditionInput.js.map +0 -1
- package/lib/components/ConditionInput/ConditionInput.styles.d.ts +0 -5
- package/lib/components/ConditionInput/ConditionInput.styles.d.ts.map +0 -1
- package/lib/components/ConditionInput/ConditionInput.styles.js +0 -27
- package/lib/components/ConditionInput/ConditionInput.styles.js.map +0 -1
- package/lib/components/ConditionInput/ConditionInput.types.d.ts +0 -32
- package/lib/components/ConditionInput/ConditionInput.types.d.ts.map +0 -1
- package/lib/components/ConditionInput/ConditionInput.types.js +0 -2
- package/lib/components/ConditionInput/ConditionInput.types.js.map +0 -1
- package/lib/components/ConditionInput/index.d.ts +0 -3
- package/lib/components/ConditionInput/index.d.ts.map +0 -1
- package/lib/components/ConditionInput/index.js +0 -2
- package/lib/components/ConditionInput/index.js.map +0 -1
- package/lib/components/ConditionInput/utils.d.ts +0 -17
- package/lib/components/ConditionInput/utils.d.ts.map +0 -1
- package/lib/components/ConditionInput/utils.js +0 -121
- package/lib/components/ConditionInput/utils.js.map +0 -1
- package/lib/components/PromotedFilters/ConditionSelector.d.ts +0 -31
- package/lib/components/PromotedFilters/ConditionSelector.d.ts.map +0 -1
- package/lib/components/PromotedFilters/ConditionSelector.js +0 -44
- package/lib/components/PromotedFilters/ConditionSelector.js.map +0 -1
- package/lib/components/PromotedFilters/PromotedFilters.d.ts +0 -11
- package/lib/components/PromotedFilters/PromotedFilters.d.ts.map +0 -1
- package/lib/components/PromotedFilters/PromotedFilters.js +0 -153
- package/lib/components/PromotedFilters/PromotedFilters.js.map +0 -1
- package/lib/components/PromotedFilters/PromotedFilters.styles.d.ts +0 -4
- package/lib/components/PromotedFilters/PromotedFilters.styles.d.ts.map +0 -1
- package/lib/components/PromotedFilters/PromotedFilters.styles.js +0 -38
- package/lib/components/PromotedFilters/PromotedFilters.styles.js.map +0 -1
- package/lib/components/PromotedFilters/PromotedFilters.types.d.ts +0 -33
- package/lib/components/PromotedFilters/PromotedFilters.types.d.ts.map +0 -1
- package/lib/components/PromotedFilters/PromotedFilters.types.js +0 -2
- package/lib/components/PromotedFilters/PromotedFilters.types.js.map +0 -1
- package/lib/components/PromotedFilters/index.d.ts +0 -3
- package/lib/components/PromotedFilters/index.d.ts.map +0 -1
- package/lib/components/PromotedFilters/index.js +0 -2
- package/lib/components/PromotedFilters/index.js.map +0 -1
- package/lib/index.d.ts +0 -7
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js +0 -8
- package/lib/index.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pega/cosmos-react-condition-builder",
|
|
3
|
-
"version": "5.0.0-dev.4.
|
|
3
|
+
"version": "5.0.0-dev.4.8",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/pegasystems/cosmos-react.git",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"build": "tsc -b"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@pega/cosmos-react-core": "5.0.0-dev.4.
|
|
26
|
+
"@pega/cosmos-react-core": "5.0.0-dev.4.8",
|
|
27
27
|
"@types/react": "^17.0.62",
|
|
28
28
|
"@types/react-dom": "^17.0.20",
|
|
29
29
|
"@types/styled-components": "^5.1.26",
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { FunctionComponent, Ref } from 'react';
|
|
2
|
-
import { BaseProps, HandleValue, ForwardProps } from '@pega/cosmos-react-core';
|
|
3
|
-
import { LeafCondition, Field, DateFunctionsByType } from './ConditionBuilder.types';
|
|
4
|
-
import { ComparatorsByType, RHSType } from './core/types';
|
|
5
|
-
/** Determines the default condition object for a given LHS value */
|
|
6
|
-
export declare function getInitConditionForLhs(lhs: LeafCondition['lhs'], fields: Field[], validRhsTypes: Set<RHSType>, validComparators?: ComparatorsByType[], dateFunctions?: DateFunctionsByType): LeafCondition;
|
|
7
|
-
/**
|
|
8
|
-
* Verifies if all the values in the condition are valid.
|
|
9
|
-
* NOTES:
|
|
10
|
-
* - This is intended to be used only on conditions emitted by the components in this package.
|
|
11
|
-
* - If a condition is manually constructed outside these components, this utility could miss some malformed conditions.
|
|
12
|
-
*/
|
|
13
|
-
export declare function isValidCondition(condition: LeafCondition, fields: Field[], trimValues?: boolean): boolean;
|
|
14
|
-
/** Type definition for the input props of AtomicCondition */
|
|
15
|
-
export interface AtomicConditionProps extends BaseProps {
|
|
16
|
-
/** Condition to use in the component */
|
|
17
|
-
condition: LeafCondition;
|
|
18
|
-
/** Metadata (data-model) for the Fields to use in the Condition */
|
|
19
|
-
fields: Field[];
|
|
20
|
-
/** Used to enable inline error indicators on invalid inputs */
|
|
21
|
-
indicateErrors: boolean;
|
|
22
|
-
/** (Optional) Lookup used to limit the Comparators allowed in the Condition. */
|
|
23
|
-
validComparators?: ComparatorsByType[];
|
|
24
|
-
/** Callback for any modifications to the Condition */
|
|
25
|
-
onChange?: (newCondition: LeafCondition, status?: 'error') => void;
|
|
26
|
-
/** Callback fired when the condition changes. */
|
|
27
|
-
onBlur?: (newCondition?: LeafCondition) => void;
|
|
28
|
-
/** Item flow direction. Defaults to 'row' */
|
|
29
|
-
itemDirection?: 'row' | 'column';
|
|
30
|
-
/**
|
|
31
|
-
* Allows presenting the LHS as a readonly text, input or hidden. Defaults presenting as 'input'.
|
|
32
|
-
* NOTE: Make sure that a valid LHS is passed in the 'condition'
|
|
33
|
-
*/
|
|
34
|
-
lhsMode?: 'input' | 'label' | 'hidden';
|
|
35
|
-
/** Restricts RHS values to only specified types of values.
|
|
36
|
-
* DEFAULT: Set(['LITERAL', 'FIELD']).
|
|
37
|
-
* WARNING: There are LHS/Comparator pairings that require specific RHS types. Please be mindful of
|
|
38
|
-
* these incompatibilities when limiting RHS value types using this prop. We do not support
|
|
39
|
-
* limiting RHS field types to incompatible types and it will result in a validation error
|
|
40
|
-
* if you do so. The pairings referred to here are as follows:
|
|
41
|
-
* - BOOLEAN LHS Fields coupled with "EQ" or "NEQ" comparators, will always yield a Field RHS
|
|
42
|
-
* - Any LHS Fields coupled with "IN" or "NOT_IN" comparators, will always yield a Literal RHS (Comma separated list)
|
|
43
|
-
* If you would like to limit RHS value types and avoid these incompatibilities, you will need to exclude these
|
|
44
|
-
* comparators using the validComparators prop.
|
|
45
|
-
*/
|
|
46
|
-
validRhsTypes?: Set<RHSType>;
|
|
47
|
-
/** (Optional) When enabling 'DATE_FUNCTION's via `validRhsTypes`, pass this to specify which all date functions are applicable per field type */
|
|
48
|
-
dateFunctions?: DateFunctionsByType;
|
|
49
|
-
/** Ref forwarded to the wrapping element. */
|
|
50
|
-
ref?: Ref<HTMLDivElement>;
|
|
51
|
-
/** Imperative handle */
|
|
52
|
-
handle?: Ref<HandleValue>;
|
|
53
|
-
}
|
|
54
|
-
/** A controlled component for Atomic Conditions */
|
|
55
|
-
declare const AtomicCondition: FunctionComponent<AtomicConditionProps & ForwardProps>;
|
|
56
|
-
export default AtomicCondition;
|
|
57
|
-
//# sourceMappingURL=AtomicCondition.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"AtomicCondition.d.ts","sourceRoot":"","sources":["../../../src/components/ConditionBuilder/AtomicCondition.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EAGjB,GAAG,EAKJ,MAAM,OAAO,CAAC;AAEf,OAAO,EAML,SAAS,EACT,WAAW,EACX,YAAY,EAIb,MAAM,yBAAyB,CAAC;AAGjC,OAAO,EAAE,aAAa,EAAE,KAAK,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAIrF,OAAO,EAAc,iBAAiB,EAAa,OAAO,EAAE,MAAM,cAAc,CAAC;AAoDjF,oEAAoE;AACpE,wBAAgB,sBAAsB,CACpC,GAAG,EAAE,aAAa,CAAC,KAAK,CAAC,EACzB,MAAM,EAAE,KAAK,EAAE,EACf,aAAa,EAAE,GAAG,CAAC,OAAO,CAAC,EAC3B,gBAAgB,CAAC,EAAE,iBAAiB,EAAE,EACtC,aAAa,CAAC,EAAE,mBAAmB,GAClC,aAAa,CAcf;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAC9B,SAAS,EAAE,aAAa,EACxB,MAAM,EAAE,KAAK,EAAE,EACf,UAAU,GAAE,OAAe,GAC1B,OAAO,CAST;AAED,6DAA6D;AAC7D,MAAM,WAAW,oBAAqB,SAAQ,SAAS;IACrD,wCAAwC;IACxC,SAAS,EAAE,aAAa,CAAC;IAEzB,mEAAmE;IACnE,MAAM,EAAE,KAAK,EAAE,CAAC;IAEhB,+DAA+D;IAC/D,cAAc,EAAE,OAAO,CAAC;IAExB,gFAAgF;IAChF,gBAAgB,CAAC,EAAE,iBAAiB,EAAE,CAAC;IAEvC,sDAAsD;IACtD,QAAQ,CAAC,EAAE,CAAC,YAAY,EAAE,aAAa,EAAE,MAAM,CAAC,EAAE,OAAO,KAAK,IAAI,CAAC;IAEnE,iDAAiD;IACjD,MAAM,CAAC,EAAE,CAAC,YAAY,CAAC,EAAE,aAAa,KAAK,IAAI,CAAC;IAEhD,6CAA6C;IAC7C,aAAa,CAAC,EAAE,KAAK,GAAG,QAAQ,CAAC;IAEjC;;;OAGG;IACH,OAAO,CAAC,EAAE,OAAO,GAAG,OAAO,GAAG,QAAQ,CAAC;IAEvC;;;;;;;;;;OAUG;IACH,aAAa,CAAC,EAAE,GAAG,CAAC,OAAO,CAAC,CAAC;IAE7B,iJAAiJ;IACjJ,aAAa,CAAC,EAAE,mBAAmB,CAAC;IAEpC,6CAA6C;IAC7C,GAAG,CAAC,EAAE,GAAG,CAAC,cAAc,CAAC,CAAC;IAC1B,wBAAwB;IACxB,MAAM,CAAC,EAAE,GAAG,CAAC,WAAW,CAAC,CAAC;CAC3B;AAED,mDAAmD;AACnD,QAAA,MAAM,eAAe,EAAE,iBAAiB,CAAC,oBAAoB,GAAG,YAAY,CAiL3E,CAAC;AAEF,eAAe,eAAe,CAAC"}
|
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import { forwardRef, useRef, useState, useImperativeHandle } from 'react';
|
|
3
|
-
import { Grid, Select, Option, useI18n, hasProp, useConsolidatedRef, useFocusWithin, useAfterInitialEffect } from '@pega/cosmos-react-core';
|
|
4
|
-
import { getItem } from './core/utils';
|
|
5
|
-
import FieldSelector from './RhsControls/FieldSelector';
|
|
6
|
-
import RhsControls, { getDefaultRhs, isValidRhs } from './RhsControls';
|
|
7
|
-
import getComparatorOptions from './core/comparators';
|
|
8
|
-
import { StyledAtomicCondition } from './ConditionBuilder.styles';
|
|
9
|
-
// Returns field-type for a given field-name
|
|
10
|
-
function getFieldType(fieldName, fields) {
|
|
11
|
-
const matchingField = getItem(fields, fieldName);
|
|
12
|
-
return matchingField && hasProp(matchingField, 'type') ? matchingField.type : undefined;
|
|
13
|
-
}
|
|
14
|
-
// Returns display label for a given field-name
|
|
15
|
-
function getFieldLabel(fieldName, fields) {
|
|
16
|
-
const matchingField = getItem(fields, fieldName);
|
|
17
|
-
return matchingField?.primary;
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Upon changing the comparator, checks if the old RHS is valid for the new comparator
|
|
21
|
-
* NOTE: This works under the assumption that the LHS doesn't change
|
|
22
|
-
*/
|
|
23
|
-
function isRhsValidForComparator(rhs, comparator) {
|
|
24
|
-
if (rhs === undefined) {
|
|
25
|
-
return ['IS_NULL', 'IS_NOT_NULL', 'IS_TRUE', 'IS_FALSE'].includes(comparator);
|
|
26
|
-
}
|
|
27
|
-
if (hasProp(rhs, 'values')) {
|
|
28
|
-
return ['IN', 'NOT_IN'].includes(comparator);
|
|
29
|
-
}
|
|
30
|
-
if (hasProp(rhs, 'parameterId')) {
|
|
31
|
-
return ['EQ', 'NEQ'].includes(comparator);
|
|
32
|
-
}
|
|
33
|
-
if (hasProp(rhs, 'start')) {
|
|
34
|
-
return ['BTW', 'NOT_BTW'].includes(comparator);
|
|
35
|
-
}
|
|
36
|
-
// `rhs.field` and `rhs.value` cases
|
|
37
|
-
return [
|
|
38
|
-
'EQ',
|
|
39
|
-
'NEQ',
|
|
40
|
-
'GT',
|
|
41
|
-
'GTE',
|
|
42
|
-
'LT',
|
|
43
|
-
'LTE',
|
|
44
|
-
'STARTS_WITH',
|
|
45
|
-
'NOT_STARTS_WITH',
|
|
46
|
-
'ENDS_WITH',
|
|
47
|
-
'NOT_ENDS_WITH',
|
|
48
|
-
'CONTAINS',
|
|
49
|
-
'NOT_CONTAINS'
|
|
50
|
-
].includes(comparator);
|
|
51
|
-
}
|
|
52
|
-
/** Determines the default condition object for a given LHS value */
|
|
53
|
-
export function getInitConditionForLhs(lhs, fields, validRhsTypes, validComparators, dateFunctions) {
|
|
54
|
-
let comparator;
|
|
55
|
-
let rhs;
|
|
56
|
-
if (lhs.field === '') {
|
|
57
|
-
// If the empty option is selected, reset the comparator and rhs to their default values
|
|
58
|
-
comparator = 'IS_NOT_NULL';
|
|
59
|
-
rhs = undefined;
|
|
60
|
-
}
|
|
61
|
-
else {
|
|
62
|
-
const newFieldType = getFieldType(lhs.field, fields);
|
|
63
|
-
comparator = getComparatorOptions(newFieldType, validComparators)[0].id; // Find the applicable comparators and select the first among them
|
|
64
|
-
rhs = getDefaultRhs(newFieldType, comparator, validRhsTypes, dateFunctions);
|
|
65
|
-
}
|
|
66
|
-
return { lhs, comparator, rhs };
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Verifies if all the values in the condition are valid.
|
|
70
|
-
* NOTES:
|
|
71
|
-
* - This is intended to be used only on conditions emitted by the components in this package.
|
|
72
|
-
* - If a condition is manually constructed outside these components, this utility could miss some malformed conditions.
|
|
73
|
-
*/
|
|
74
|
-
export function isValidCondition(condition, fields, trimValues = false) {
|
|
75
|
-
// If no field is yet selected on the LHS
|
|
76
|
-
if (condition.lhs.field === '') {
|
|
77
|
-
return false;
|
|
78
|
-
}
|
|
79
|
-
// If a field is present on the LHS, check if the RHS is valid
|
|
80
|
-
// No need to check the comparator since the comparator is always assigned a valid value
|
|
81
|
-
const fieldType = getFieldType(condition.lhs.field, fields);
|
|
82
|
-
return isValidRhs(condition.rhs, condition.lhs, fields, fieldType, trimValues);
|
|
83
|
-
}
|
|
84
|
-
/** A controlled component for Atomic Conditions */
|
|
85
|
-
const AtomicCondition = forwardRef(function AtomicCondition({ condition, fields, indicateErrors, validComparators, onChange, onBlur, itemDirection, lhsMode = 'input', validRhsTypes = new Set(['LITERAL', 'FIELD']), dateFunctions, handle, ...restProps }, ref) {
|
|
86
|
-
const t = useI18n();
|
|
87
|
-
// stores condition form state
|
|
88
|
-
const [state, setState] = useState(condition);
|
|
89
|
-
// ref needed to handle use case of onChange and onBlur being triggered at the same time
|
|
90
|
-
// state is not updated by the time blur is invoked, ref is always set to newest value
|
|
91
|
-
const newCondition = useRef(condition);
|
|
92
|
-
const fieldType = getFieldType(condition.lhs.field, fields); // Can be `undefined` if no match or if no previous field
|
|
93
|
-
const comparatorOptions = fieldType
|
|
94
|
-
? getComparatorOptions(fieldType, validComparators)
|
|
95
|
-
: [{ id: '', label: t('condition_builder_select_placeholder') }];
|
|
96
|
-
useAfterInitialEffect(() => {
|
|
97
|
-
setState(condition);
|
|
98
|
-
}, [condition]);
|
|
99
|
-
const onFocusChange = (focused) => {
|
|
100
|
-
if (!focused) {
|
|
101
|
-
// newCondition is returned, as it stores newest value
|
|
102
|
-
onBlur?.(newCondition.current && isValidCondition(newCondition.current, fields, true)
|
|
103
|
-
? newCondition.current
|
|
104
|
-
: undefined);
|
|
105
|
-
}
|
|
106
|
-
};
|
|
107
|
-
const containerRef = useConsolidatedRef(ref);
|
|
108
|
-
useFocusWithin([containerRef], onFocusChange);
|
|
109
|
-
// Handle condition update
|
|
110
|
-
const handleConditionChange = (updatedCondition, status) => {
|
|
111
|
-
newCondition.current = updatedCondition;
|
|
112
|
-
// new condition is returned with status to allow recognizing errors and avoiding clearing wrong values from input
|
|
113
|
-
onChange?.(updatedCondition, status);
|
|
114
|
-
};
|
|
115
|
-
// Handle change in the field-selector
|
|
116
|
-
const handleFieldChange = (id) => {
|
|
117
|
-
const lhs = { field: id };
|
|
118
|
-
const initCondition = getInitConditionForLhs(lhs, fields, validRhsTypes, validComparators, dateFunctions); // Determine comparator and rhs for the new lhs
|
|
119
|
-
handleConditionChange(initCondition);
|
|
120
|
-
};
|
|
121
|
-
// Handle change in the comparator
|
|
122
|
-
const handleComparatorChange = (e) => {
|
|
123
|
-
if (e.target.value === '') {
|
|
124
|
-
return; // The option is shown only when an lhs is not yet selected. Ignore this selection for those cases
|
|
125
|
-
}
|
|
126
|
-
const comparator = e.target.value;
|
|
127
|
-
const rhs = isRhsValidForComparator(condition.rhs, comparator)
|
|
128
|
-
? { ...condition.rhs } // Duplicating rhs to avoid using source object as it can be mutated later.
|
|
129
|
-
: getDefaultRhs(fieldType, comparator, validRhsTypes, dateFunctions);
|
|
130
|
-
handleConditionChange({ ...condition, comparator, rhs });
|
|
131
|
-
};
|
|
132
|
-
// Handle change in the RHS
|
|
133
|
-
const handleRHSChange = (rhs, status) => {
|
|
134
|
-
handleConditionChange({ ...condition, rhs }, status);
|
|
135
|
-
};
|
|
136
|
-
const rhsHandle = useRef(null);
|
|
137
|
-
const clear = () => {
|
|
138
|
-
rhsHandle.current?.clear();
|
|
139
|
-
if (newCondition.current) {
|
|
140
|
-
handleConditionChange(getInitConditionForLhs(state.lhs, fields, validRhsTypes, validComparators, dateFunctions));
|
|
141
|
-
}
|
|
142
|
-
};
|
|
143
|
-
useImperativeHandle(handle, () => ({
|
|
144
|
-
clear
|
|
145
|
-
}), [clear]);
|
|
146
|
-
const lhsLabel = getFieldLabel(condition.lhs.field, fields);
|
|
147
|
-
return (
|
|
148
|
-
// Using minmax() to get equal widths: https://stackoverflow.com/a/61240964/4247448
|
|
149
|
-
_jsx(StyledAtomicCondition, { ...restProps, name: lhsMode === 'label' ? lhsLabel : undefined, "aria-label": lhsMode === 'hidden' ? lhsLabel : undefined, children: _jsxs(Grid, { sm: {
|
|
150
|
-
container: {
|
|
151
|
-
cols: itemDirection === 'column'
|
|
152
|
-
? 'minmax(0, 1fr)'
|
|
153
|
-
: `repeat(${lhsMode !== 'input' ? 2 : 3}, minmax(0,1fr))`
|
|
154
|
-
}
|
|
155
|
-
}, container: {
|
|
156
|
-
justifyItems: 'stretch',
|
|
157
|
-
colGap: 0.5,
|
|
158
|
-
rowGap: 0.5
|
|
159
|
-
}, ref: containerRef, children: [lhsMode === 'input' && (_jsx(FieldSelector, { value: state.lhs.field, onChange: handleFieldChange, fields: fields, status: indicateErrors && state.lhs.field === '' ? 'error' : undefined, info: indicateErrors && state.lhs.field === ''
|
|
160
|
-
? t('condition_builder_condition_select_info_default')
|
|
161
|
-
: undefined })), _jsx(Select, { value: state.comparator, onChange: handleComparatorChange, name: 'comparator', label: t('condition_builder_comparator_label'), children: comparatorOptions.map(option => (_jsx(Option, { value: option.id, children: option.label }, option.id))) }), _jsx(RhsControls, { lhs: state.lhs, comparator: state.comparator, rhs: state.rhs, fields: fields, onChange: handleRHSChange, indicateErrors: indicateErrors, validRhsTypes: validRhsTypes, dateFunctions: dateFunctions, itemDirection: itemDirection || 'row', handle: rhsHandle })] }) }));
|
|
162
|
-
});
|
|
163
|
-
export default AtomicCondition;
|
|
164
|
-
//# sourceMappingURL=AtomicCondition.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"AtomicCondition.js","sourceRoot":"","sources":["../../../src/components/ConditionBuilder/AtomicCondition.tsx"],"names":[],"mappings":";AAAA,OAAO,EAGL,UAAU,EAEV,MAAM,EACN,QAAQ,EAER,mBAAmB,EACpB,MAAM,OAAO,CAAC;AAEf,OAAO,EACL,IAAI,EACJ,MAAM,EACN,MAAM,EACN,OAAO,EACP,OAAO,EAIP,kBAAkB,EAClB,cAAc,EACd,qBAAqB,EACtB,MAAM,yBAAyB,CAAC;AAEjC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAEvC,OAAO,aAAa,MAAM,6BAA6B,CAAC;AACxD,OAAO,WAAW,EAAE,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AACvE,OAAO,oBAAoB,MAAM,oBAAoB,CAAC;AAEtD,OAAO,EAAE,qBAAqB,EAAE,MAAM,2BAA2B,CAAC;AAElE,4CAA4C;AAC5C,SAAS,YAAY,CAAC,SAAiB,EAAE,MAAe;IACtD,MAAM,aAAa,GAAG,OAAO,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IACjD,OAA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KAAK,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,EACtE,IAAI,EACF,cAAc,IAAI,KAAK,CAAC,GAAG,CAAC,KAAK,KAAK,EAAE;wBACtC,CAAC,CAAC,CAAC,CAAC,iDAAiD,CAAC;wBACtD,CAAC,CAAC,SAAS,GAEf,CACH,EAGD,KAAC,MAAM,IACL,KAAK,EAAE,KAAK,CAAC,UAAU,EACvB,QAAQ,EAAE,sBAAsB,EAChC,IAAI,EAAC,YAAY,EACjB,KAAK,EAAE,CAAC,CAAC,oCAAoC,CAAC,YAE7C,iBAAiB,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,CAC/B,KAAC,MAAM,IAAiB,KAAK,EAAE,MAAM,CAAC,EAAE,YACrC,MAAM,CAAC,KAAK,IADF,MAAM,CAAC,EAAE,CAEb,CACV,CAAC,GACK,EAGT,KAAC,WAAW,IACV,GAAG,EAAE,KAAK,CAAC,GAAG,EACd,UAAU,EAAE,KAAK,CAAC,UAAU,EAC5B,GAAG,EAAE,KAAK,CAAC,GAAG,EACd,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,eAAe,EACzB,cAAc,EAAE,cAAc,EAC9B,aAAa,EAAE,aAAa,EAC5B,aAAa,EAAE,aAAa,EAC5B,aAAa,EAAE,aAAa,IAAI,KAAK,EACrC,MAAM,EAAE,SAAS,GACjB,IACG,GACe,CACzB,CAAC;AACJ,CAAC,CACF,CAAC;AAEF,eAAe,eAAe,CAAC","sourcesContent":["import {\n FunctionComponent,\n ChangeEvent,\n forwardRef,\n Ref,\n useRef,\n useState,\n PropsWithoutRef,\n useImperativeHandle\n} from 'react';\n\nimport {\n Grid,\n Select,\n Option,\n useI18n,\n hasProp,\n BaseProps,\n HandleValue,\n ForwardProps,\n useConsolidatedRef,\n useFocusWithin,\n useAfterInitialEffect\n} from '@pega/cosmos-react-core';\n\nimport { getItem } from './core/utils';\nimport { LeafCondition, Field, DateFunctionsByType } from './ConditionBuilder.types';\nimport FieldSelector from './RhsControls/FieldSelector';\nimport RhsControls, { getDefaultRhs, isValidRhs } from './RhsControls';\nimport getComparatorOptions from './core/comparators';\nimport { Comparator, ComparatorsByType, FieldType, RHSType } from './core/types';\nimport { StyledAtomicCondition } from './ConditionBuilder.styles';\n\n// Returns field-type for a given field-name\nfunction getFieldType(fieldName: string, fields: Field[]): FieldType | undefined {\n const matchingField = getItem(fields, fieldName);\n return matchingField && hasProp(matchingField, 'type') ? matchingField.type : undefined;\n}\n\n// Returns display label for a given field-name\nfunction getFieldLabel(fieldName: string, fields: Field[]): string | undefined {\n const matchingField = getItem(fields, fieldName);\n return matchingField?.primary;\n}\n\n/**\n * Upon changing the comparator, checks if the old RHS is valid for the new comparator\n * NOTE: This works under the assumption that the LHS doesn't change\n */\nfunction isRhsValidForComparator(rhs: LeafCondition['rhs'], comparator: Comparator): boolean {\n if (rhs === undefined) {\n return ['IS_NULL', 'IS_NOT_NULL', 'IS_TRUE', 'IS_FALSE'].includes(comparator);\n }\n if (hasProp(rhs, 'values')) {\n return ['IN', 'NOT_IN'].includes(comparator);\n }\n\n if (hasProp(rhs, 'parameterId')) {\n return ['EQ', 'NEQ'].includes(comparator);\n }\n\n if (hasProp(rhs, 'start')) {\n return ['BTW', 'NOT_BTW'].includes(comparator);\n }\n\n // `rhs.field` and `rhs.value` cases\n return [\n 'EQ',\n 'NEQ',\n 'GT',\n 'GTE',\n 'LT',\n 'LTE',\n 'STARTS_WITH',\n 'NOT_STARTS_WITH',\n 'ENDS_WITH',\n 'NOT_ENDS_WITH',\n 'CONTAINS',\n 'NOT_CONTAINS'\n ].includes(comparator);\n}\n\n/** Determines the default condition object for a given LHS value */\nexport function getInitConditionForLhs(\n lhs: LeafCondition['lhs'],\n fields: Field[],\n validRhsTypes: Set<RHSType>,\n validComparators?: ComparatorsByType[],\n dateFunctions?: DateFunctionsByType\n): LeafCondition {\n let comparator: Comparator;\n let rhs: LeafCondition['rhs'];\n if (lhs.field === '') {\n // If the empty option is selected, reset the comparator and rhs to their default values\n comparator = 'IS_NOT_NULL';\n rhs = undefined;\n } else {\n const newFieldType = getFieldType(lhs.field, fields) as FieldType;\n comparator = getComparatorOptions(newFieldType, validComparators)[0].id; // Find the applicable comparators and select the first among them\n rhs = getDefaultRhs(newFieldType, comparator, validRhsTypes, dateFunctions);\n }\n\n return { lhs, comparator, rhs };\n}\n\n/**\n * Verifies if all the values in the condition are valid.\n * NOTES:\n * - This is intended to be used only on conditions emitted by the components in this package.\n * - If a condition is manually constructed outside these components, this utility could miss some malformed conditions.\n */\nexport function isValidCondition(\n condition: LeafCondition,\n fields: Field[],\n trimValues: boolean = false\n): boolean {\n // If no field is yet selected on the LHS\n if (condition.lhs.field === '') {\n return false;\n }\n // If a field is present on the LHS, check if the RHS is valid\n // No need to check the comparator since the comparator is always assigned a valid value\n const fieldType = getFieldType(condition.lhs.field, fields);\n return isValidRhs(condition.rhs, condition.lhs, fields, fieldType, trimValues);\n}\n\n/** Type definition for the input props of AtomicCondition */\nexport interface AtomicConditionProps extends BaseProps {\n /** Condition to use in the component */\n condition: LeafCondition;\n\n /** Metadata (data-model) for the Fields to use in the Condition */\n fields: Field[];\n\n /** Used to enable inline error indicators on invalid inputs */\n indicateErrors: boolean;\n\n /** (Optional) Lookup used to limit the Comparators allowed in the Condition. */\n validComparators?: ComparatorsByType[];\n\n /** Callback for any modifications to the Condition */\n onChange?: (newCondition: LeafCondition, status?: 'error') => void;\n\n /** Callback fired when the condition changes. */\n onBlur?: (newCondition?: LeafCondition) => void;\n\n /** Item flow direction. Defaults to 'row' */\n itemDirection?: 'row' | 'column';\n\n /**\n * Allows presenting the LHS as a readonly text, input or hidden. Defaults presenting as 'input'.\n * NOTE: Make sure that a valid LHS is passed in the 'condition'\n */\n lhsMode?: 'input' | 'label' | 'hidden';\n\n /** Restricts RHS values to only specified types of values.\n * DEFAULT: Set(['LITERAL', 'FIELD']).\n * WARNING: There are LHS/Comparator pairings that require specific RHS types. Please be mindful of\n * these incompatibilities when limiting RHS value types using this prop. We do not support\n * limiting RHS field types to incompatible types and it will result in a validation error\n * if you do so. The pairings referred to here are as follows:\n * - BOOLEAN LHS Fields coupled with \"EQ\" or \"NEQ\" comparators, will always yield a Field RHS\n * - Any LHS Fields coupled with \"IN\" or \"NOT_IN\" comparators, will always yield a Literal RHS (Comma separated list)\n * If you would like to limit RHS value types and avoid these incompatibilities, you will need to exclude these\n * comparators using the validComparators prop.\n */\n validRhsTypes?: Set<RHSType>;\n\n /** (Optional) When enabling 'DATE_FUNCTION's via `validRhsTypes`, pass this to specify which all date functions are applicable per field type */\n dateFunctions?: DateFunctionsByType;\n\n /** Ref forwarded to the wrapping element. */\n ref?: Ref<HTMLDivElement>;\n /** Imperative handle */\n handle?: Ref<HandleValue>;\n}\n\n/** A controlled component for Atomic Conditions */\nconst AtomicCondition: FunctionComponent<AtomicConditionProps & ForwardProps> = forwardRef(\n function AtomicCondition(\n {\n condition,\n fields,\n indicateErrors,\n validComparators,\n onChange,\n onBlur,\n itemDirection,\n lhsMode = 'input',\n validRhsTypes = new Set<RHSType>(['LITERAL', 'FIELD']),\n dateFunctions,\n handle,\n ...restProps\n }: PropsWithoutRef<AtomicConditionProps>,\n ref: AtomicConditionProps['ref']\n ) {\n const t = useI18n();\n\n // stores condition form state\n const [state, setState] = useState<LeafCondition>(condition);\n // ref needed to handle use case of onChange and onBlur being triggered at the same time\n // state is not updated by the time blur is invoked, ref is always set to newest value\n const newCondition = useRef(condition);\n\n const fieldType = getFieldType(condition.lhs.field, fields); // Can be `undefined` if no match or if no previous field\n const comparatorOptions = fieldType\n ? getComparatorOptions(fieldType, validComparators)\n : [{ id: '', label: t('condition_builder_select_placeholder') }];\n\n useAfterInitialEffect(() => {\n setState(condition);\n }, [condition]);\n\n const onFocusChange = (focused: boolean) => {\n if (!focused) {\n // newCondition is returned, as it stores newest value\n onBlur?.(\n newCondition.current && isValidCondition(newCondition.current, fields, true)\n ? newCondition.current\n : undefined\n );\n }\n };\n\n const containerRef = useConsolidatedRef(ref);\n useFocusWithin([containerRef], onFocusChange);\n\n // Handle condition update\n const handleConditionChange = (updatedCondition: LeafCondition, status?: 'error') => {\n newCondition.current = updatedCondition;\n // new condition is returned with status to allow recognizing errors and avoiding clearing wrong values from input\n onChange?.(updatedCondition, status);\n };\n\n // Handle change in the field-selector\n const handleFieldChange = (id: string) => {\n const lhs = { field: id };\n const initCondition = getInitConditionForLhs(\n lhs,\n fields,\n validRhsTypes,\n validComparators,\n dateFunctions\n ); // Determine comparator and rhs for the new lhs\n handleConditionChange(initCondition);\n };\n\n // Handle change in the comparator\n const handleComparatorChange = (e: ChangeEvent<HTMLSelectElement>) => {\n if (e.target.value === '') {\n return; // The option is shown only when an lhs is not yet selected. Ignore this selection for those cases\n }\n const comparator = e.target.value as Comparator;\n const rhs = isRhsValidForComparator(condition.rhs, comparator)\n ? ({ ...condition.rhs } as LeafCondition['rhs']) // Duplicating rhs to avoid using source object as it can be mutated later.\n : getDefaultRhs(fieldType as FieldType, comparator, validRhsTypes, dateFunctions);\n\n handleConditionChange({ ...condition, comparator, rhs });\n };\n\n // Handle change in the RHS\n const handleRHSChange = (rhs: LeafCondition['rhs'], status?: 'error') => {\n handleConditionChange({ ...condition, rhs }, status);\n };\n\n const rhsHandle = useRef<HandleValue>(null);\n\n const clear = () => {\n rhsHandle.current?.clear();\n if (newCondition.current) {\n handleConditionChange(\n getInitConditionForLhs(state.lhs, fields, validRhsTypes, validComparators, dateFunctions)\n );\n }\n };\n\n useImperativeHandle(\n handle,\n () => ({\n clear\n }),\n [clear]\n );\n\n const lhsLabel = getFieldLabel(condition.lhs.field, fields);\n\n return (\n // Using minmax() to get equal widths: https://stackoverflow.com/a/61240964/4247448\n <StyledAtomicCondition\n {...restProps}\n name={lhsMode === 'label' ? lhsLabel : undefined}\n aria-label={lhsMode === 'hidden' ? lhsLabel : undefined}\n >\n <Grid\n sm={{\n container: {\n cols:\n itemDirection === 'column'\n ? 'minmax(0, 1fr)'\n : `repeat(${lhsMode !== 'input' ? 2 : 3}, minmax(0,1fr))`\n }\n }}\n container={{\n justifyItems: 'stretch',\n colGap: 0.5,\n rowGap: 0.5\n }}\n ref={containerRef}\n >\n {/* LHS selector */}\n {lhsMode === 'input' && (\n <FieldSelector\n value={state.lhs.field}\n onChange={handleFieldChange}\n fields={fields}\n status={indicateErrors && state.lhs.field === '' ? 'error' : undefined}\n info={\n indicateErrors && state.lhs.field === ''\n ? t('condition_builder_condition_select_info_default')\n : undefined\n }\n />\n )}\n\n {/* Comparator selector */}\n <Select\n value={state.comparator}\n onChange={handleComparatorChange}\n name='comparator'\n label={t('condition_builder_comparator_label')}\n >\n {comparatorOptions.map(option => (\n <Option key={option.id} value={option.id}>\n {option.label}\n </Option>\n ))}\n </Select>\n\n {/* RHS controls */}\n <RhsControls\n lhs={state.lhs}\n comparator={state.comparator}\n rhs={state.rhs}\n fields={fields}\n onChange={handleRHSChange}\n indicateErrors={indicateErrors}\n validRhsTypes={validRhsTypes}\n dateFunctions={dateFunctions}\n itemDirection={itemDirection || 'row'}\n handle={rhsHandle}\n />\n </Grid>\n </StyledAtomicCondition>\n );\n }\n);\n\nexport default AtomicCondition;\n"]}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { FunctionComponent } from 'react';
|
|
2
|
-
import { ForwardProps } from '@pega/cosmos-react-core';
|
|
3
|
-
import ConditionBuilderProps from './ConditionBuilder.types';
|
|
4
|
-
declare const ConditionBuilder: FunctionComponent<ConditionBuilderProps & ForwardProps>;
|
|
5
|
-
export default ConditionBuilder;
|
|
6
|
-
//# sourceMappingURL=ConditionBuilder.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ConditionBuilder.d.ts","sourceRoot":"","sources":["../../../src/components/ConditionBuilder/ConditionBuilder.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EAQlB,MAAM,OAAO,CAAC;AAEf,OAAO,EAOL,YAAY,EASb,MAAM,yBAAyB,CAAC;AAGjC,OAAO,qBAIN,MAAM,0BAA0B,CAAC;AA6BlC,QAAA,MAAM,gBAAgB,EAAE,iBAAiB,CAAC,qBAAqB,GAAG,YAAY,CA6Z7E,CAAC;AAGF,eAAe,gBAAgB,CAAC"}
|
|
@@ -1,271 +0,0 @@
|
|
|
1
|
-
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import { Fragment, useState, useReducer, useImperativeHandle, useRef, useEffect } from 'react';
|
|
3
|
-
import { Banner, Button, Flex, Icon, Input, MenuButton, hasProp, useI18n, getFocusables, Label, useUID, useLiveLog, AdditionalInfo } from '@pega/cosmos-react-core';
|
|
4
|
-
import AtomicCondition, { isValidCondition } from './AtomicCondition';
|
|
5
|
-
import { splitConditionForBuilder, parseLogicString, disambiguateLogic, isBasicModeApplicable, getBasicModeOperators, getLogicFromBasicMode } from './core/utils';
|
|
6
|
-
import rowsReducer, { INSERT_ROW, UPDATE_ROW, REMOVE_ROW } from './core/rows-reducer';
|
|
7
|
-
import { StyledRowWithSeparator, StyledRow, StyledLabel, StyledConditionBuilder } from './ConditionBuilder.styles';
|
|
8
|
-
// Default props for the builder
|
|
9
|
-
const defaultProps = {
|
|
10
|
-
condition: {
|
|
11
|
-
condition: {
|
|
12
|
-
comparator: 'IS_NOT_NULL',
|
|
13
|
-
lhs: { field: '' }
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
// Component for the builder
|
|
18
|
-
const ConditionBuilder = (props) => {
|
|
19
|
-
const { fields, condition: seedCondition, validComparators, validRhsTypes = new Set(['LITERAL', 'FIELD']), dateFunctions, disallowNOT, handle } = props;
|
|
20
|
-
// Split the condition tree into a logic-string and a list of condition-rows.
|
|
21
|
-
const clonedCondition = JSON.parse(JSON.stringify(seedCondition)); // Cloning to avoid modifying the original
|
|
22
|
-
const { logic: seedLogic, rows: seedRows } = splitConditionForBuilder(clonedCondition);
|
|
23
|
-
// On load, look to use the Basic-mode if the seed-condition is simple enough
|
|
24
|
-
const [isBasicMode, setBasicMode] = useState(() => isBasicModeApplicable(clonedCondition));
|
|
25
|
-
const [operatorsList, setOperatorsList] = useState(() => getBasicModeOperators(seedLogic)); // Operators-list used in Basic-mode to render dropdowns between the rows. NOTE: When in Advanced-mode, this can contain stale values.
|
|
26
|
-
const [logicString, setLogicString] = useState(seedLogic); // Logic-string used in Advanced-mode. NOTE: When in Basic-mode, this can contain stale values.
|
|
27
|
-
const [conditionRows, dispatch] = useReducer(rowsReducer, seedRows); // Conditions-rows
|
|
28
|
-
const [logicError, setLogicError] = useState(null); // Used to indicate errors in the logic string
|
|
29
|
-
const [showModeWarning, setModeWarning] = useState(false); // Used to warn user about losing information when moving away from Advanced mode
|
|
30
|
-
const [showErrorIndicators, setErrorIndicators] = useState(false); // Used to enable inline error indicators in the condition rows
|
|
31
|
-
const enableShading = isBasicMode && operatorsList.includes('AND') && operatorsList.includes('OR'); // In Basic mode, enable shading to indicate grouping only when both operators are present
|
|
32
|
-
const labelId = useUID();
|
|
33
|
-
const newOperatorButtonRef = useRef(null);
|
|
34
|
-
const focusRowRef = useRef(null);
|
|
35
|
-
const [focusRowIndex, setFocusRowIndex] = useState(null);
|
|
36
|
-
const bannerHandleRef = useRef(null);
|
|
37
|
-
const buttonRef = useRef(null);
|
|
38
|
-
// Upon adding/removing rows in Advanced mode, update the logic string to reflect the change in labels
|
|
39
|
-
// NOTE: The input is updated only if the previous logicString was simple enough
|
|
40
|
-
const updateLogicString = (actionType) => {
|
|
41
|
-
const numNewRows = actionType === INSERT_ROW ? conditionRows.length + 1 : conditionRows.length - 1;
|
|
42
|
-
// If only one row left after the action, simply use its label as logicString
|
|
43
|
-
// NOTE: If none left, the rowsReducer adds a new empty row
|
|
44
|
-
if (numNewRows <= 1) {
|
|
45
|
-
setLogicString('1');
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
// If the previous string was a simple AND/OR between all the rows, update the logicString to follow the same for the new list of rows.
|
|
49
|
-
// If the previous string was a single token like `1` and a new row is inserted, do an AND between the new rows, i.e. `1 AND 2`
|
|
50
|
-
// For all other complex cases, skip automatically updating the string and wait for the user to change it
|
|
51
|
-
// To check this, split the previous string by `OR/AND` and check if each of the resultant token is a simple numeric token
|
|
52
|
-
const splitter = logicString.includes('OR') ? 'OR' : 'AND';
|
|
53
|
-
const tokens = logicString.split(splitter);
|
|
54
|
-
const isEveryTokenInteger = tokens.every(w => /^\d+$/.test(w.trim()));
|
|
55
|
-
if (isEveryTokenInteger) {
|
|
56
|
-
setLogicString(new Array(numNewRows)
|
|
57
|
-
.fill(0)
|
|
58
|
-
.map((w, i) => i + 1)
|
|
59
|
-
.join(` ${splitter} `));
|
|
60
|
-
}
|
|
61
|
-
};
|
|
62
|
-
// Manages the focus behavior when inserting/removing a row
|
|
63
|
-
useEffect(() => {
|
|
64
|
-
if (focusRowIndex === null)
|
|
65
|
-
return;
|
|
66
|
-
if (newOperatorButtonRef.current) {
|
|
67
|
-
newOperatorButtonRef.current.focus();
|
|
68
|
-
newOperatorButtonRef.current = null;
|
|
69
|
-
}
|
|
70
|
-
else if (focusRowIndex !== null) {
|
|
71
|
-
getFocusables(focusRowRef)[0]?.focus();
|
|
72
|
-
}
|
|
73
|
-
setFocusRowIndex(null);
|
|
74
|
-
}, [focusRowIndex]);
|
|
75
|
-
// Insert a new row at the given index
|
|
76
|
-
const insertRow = (insertAt) => {
|
|
77
|
-
dispatch({ type: INSERT_ROW, payload: { insertAt } }); // Insert an entry in conditionRows
|
|
78
|
-
if (isBasicMode) {
|
|
79
|
-
// Set up a new AND/OR selector
|
|
80
|
-
const insertOpAt = insertAt - 1; // Using `insertAt-1` since the operatorsList has one less entry compared to the conditionRows
|
|
81
|
-
setOperatorsList([
|
|
82
|
-
...operatorsList.slice(0, insertOpAt),
|
|
83
|
-
'AND',
|
|
84
|
-
...operatorsList.slice(insertOpAt)
|
|
85
|
-
]);
|
|
86
|
-
}
|
|
87
|
-
else {
|
|
88
|
-
updateLogicString(INSERT_ROW); // Update the logicString input to reflect the insertion
|
|
89
|
-
}
|
|
90
|
-
setFocusRowIndex(insertAt);
|
|
91
|
-
};
|
|
92
|
-
// Remove the row at the given index
|
|
93
|
-
const removeRow = (removeAt) => {
|
|
94
|
-
dispatch({ type: REMOVE_ROW, payload: { removeAt } }); // Remove an entry in conditionRows
|
|
95
|
-
if (isBasicMode) {
|
|
96
|
-
// Remove an AND/OR selector
|
|
97
|
-
if (removeAt === 0) {
|
|
98
|
-
// If removing the first of the condition rows, remove the extraneous operator
|
|
99
|
-
setOperatorsList(operatorsList.slice(1));
|
|
100
|
-
}
|
|
101
|
-
else {
|
|
102
|
-
let removeOpAt = removeAt - 1; // Using `removeAt-1` since the operatorsList has one less entry compared to the conditionRows
|
|
103
|
-
if (operatorsList[removeOpAt] === 'OR' && operatorsList[removeOpAt + 1] === 'AND') {
|
|
104
|
-
// If removing a condition at the start of a nested group, remove the first operator from that group. e.g. Take `(1 AND 2) OR (3 AND 4 AND 5)`. If `3` is being deleted, it should become `(1 AND 2) OR (4 AND 5)` instead of `1 AND 2 AND 4 AND 5`
|
|
105
|
-
removeOpAt += 1;
|
|
106
|
-
}
|
|
107
|
-
setOperatorsList([
|
|
108
|
-
...operatorsList.slice(0, removeOpAt),
|
|
109
|
-
...operatorsList.slice(removeOpAt + 1)
|
|
110
|
-
]);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
else {
|
|
114
|
-
updateLogicString(REMOVE_ROW); // Update the logicString input to reflect the removal
|
|
115
|
-
}
|
|
116
|
-
setFocusRowIndex(() => {
|
|
117
|
-
if (removeAt !== 0 && removeAt === conditionRows.length - 1) {
|
|
118
|
-
return removeAt - 1;
|
|
119
|
-
}
|
|
120
|
-
return removeAt;
|
|
121
|
-
});
|
|
122
|
-
};
|
|
123
|
-
// Update a Basic-mode operator at the given index
|
|
124
|
-
const updateOperator = (updateAt, newValue) => {
|
|
125
|
-
setOperatorsList([
|
|
126
|
-
...operatorsList.slice(0, updateAt),
|
|
127
|
-
newValue,
|
|
128
|
-
...operatorsList.slice(updateAt + 1)
|
|
129
|
-
]);
|
|
130
|
-
};
|
|
131
|
-
// Validate the logic input. Called upon clicking outside the textBox or upon submission.
|
|
132
|
-
// If the expression is invalid, shows appropriate feedback and returns null.
|
|
133
|
-
// If the expression is valid, formats it by setting up brackets at necessary places (to clear any ambiguities e.g. `1 AND 2 OR 3` is formatted as `(1 AND 2) OR 3` ). Returns a save-ready condition
|
|
134
|
-
const validateLogicString = (logicToUse) => {
|
|
135
|
-
// Set up a lookup with row-labels as keys and rows as values
|
|
136
|
-
const conditionLookup = {};
|
|
137
|
-
conditionRows.forEach(row => {
|
|
138
|
-
const { id, label, ...leaf } = row; // Discard the extra properties added by ConditionRow to the LeafCondition interface
|
|
139
|
-
conditionLookup[row.label] = leaf;
|
|
140
|
-
});
|
|
141
|
-
// Convert the logic string into a condition tree
|
|
142
|
-
const parseResult = parseLogicString(logicToUse, conditionLookup, disallowNOT);
|
|
143
|
-
// Indicate any parse error
|
|
144
|
-
if (hasProp(parseResult, 'error')) {
|
|
145
|
-
setLogicError(parseResult.error);
|
|
146
|
-
return null;
|
|
147
|
-
}
|
|
148
|
-
// If no errors
|
|
149
|
-
setLogicError(null); // Clear any previous error
|
|
150
|
-
setLogicString(disambiguateLogic(logicToUse)); // Disambiguating the logic string by placing parentheses at appropriate places
|
|
151
|
-
return parseResult.condition;
|
|
152
|
-
};
|
|
153
|
-
// Toggle between the Basic and Advanced modes
|
|
154
|
-
// `forceToggle:true` is passed when switching from Advanced to Basic mode despite a loss of information
|
|
155
|
-
const toggleMode = (forceToggle) => {
|
|
156
|
-
setModeWarning(false); // Clear any previous warning
|
|
157
|
-
if (isBasicMode) {
|
|
158
|
-
// Update the logicString to match the operatorsList before switching the mode
|
|
159
|
-
setLogicString(getLogicFromBasicMode(operatorsList));
|
|
160
|
-
setBasicMode(false);
|
|
161
|
-
}
|
|
162
|
-
else if (forceToggle) {
|
|
163
|
-
// If forcibly toggling from Advanced to Basic despite a loss of operators
|
|
164
|
-
// Set up the default `AND` operator between all rows before switching to Basic mode
|
|
165
|
-
setOperatorsList(new Array(conditionRows.length - 1).fill('AND'));
|
|
166
|
-
setBasicMode(true);
|
|
167
|
-
}
|
|
168
|
-
else {
|
|
169
|
-
// If switching from Advanced mode
|
|
170
|
-
const condition = validateLogicString(logicString); // Validate the logicString. If it is an invalid, an appropriate error is shown for the user to act on
|
|
171
|
-
if (condition !== null) {
|
|
172
|
-
// If it's a valid condition, check if it's simple enough to be shown in Basic mode without any loss of information
|
|
173
|
-
if (isBasicModeApplicable(condition)) {
|
|
174
|
-
// If the logic is simple enough, identify the new set of operators before switching to Basic mode
|
|
175
|
-
setOperatorsList(getBasicModeOperators(logicString));
|
|
176
|
-
setBasicMode(true);
|
|
177
|
-
}
|
|
178
|
-
else {
|
|
179
|
-
// If the logic is too complex show a warning for the user to act on
|
|
180
|
-
setModeWarning(true);
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
};
|
|
185
|
-
const getCondition = () => {
|
|
186
|
-
setErrorIndicators(false); // Reset any previous flag
|
|
187
|
-
setLogicError(null); // Clear any previous error
|
|
188
|
-
// If there's a single empty condition, submit with an `undefined` value (useful for clearing a previous condition)
|
|
189
|
-
if (conditionRows.length === 1 && conditionRows[0].lhs.field === '') {
|
|
190
|
-
return [true, undefined];
|
|
191
|
-
}
|
|
192
|
-
// Identify and indicate any invalid values in the condition rows
|
|
193
|
-
const hasInvalidRows = conditionRows.some(row => !isValidCondition(row, fields, true));
|
|
194
|
-
if (hasInvalidRows) {
|
|
195
|
-
setErrorIndicators(true); // Enable inline error indications
|
|
196
|
-
return [false];
|
|
197
|
-
}
|
|
198
|
-
// Validate the Logic string
|
|
199
|
-
const logicToUse = isBasicMode ? getLogicFromBasicMode(operatorsList) : logicString; // If in Basic-mode, construct a different logicString based on the operatorsList
|
|
200
|
-
const condition = validateLogicString(logicToUse); // This also takes care of showing an appropriate error feedback as needed
|
|
201
|
-
if (condition !== null) {
|
|
202
|
-
// Can be null when the logic string has errors
|
|
203
|
-
return [true, condition];
|
|
204
|
-
}
|
|
205
|
-
return [false];
|
|
206
|
-
};
|
|
207
|
-
useImperativeHandle(handle, () => ({
|
|
208
|
-
getCondition
|
|
209
|
-
}), [getCondition]);
|
|
210
|
-
const t = useI18n();
|
|
211
|
-
useEffect(() => {
|
|
212
|
-
if (showModeWarning)
|
|
213
|
-
bannerHandleRef.current?.focus();
|
|
214
|
-
}, [showModeWarning]);
|
|
215
|
-
const { announceAssertive } = useLiveLog();
|
|
216
|
-
useEffect(() => {
|
|
217
|
-
announceAssertive({ message: t('condition_builder_mode_switch') });
|
|
218
|
-
}, [isBasicMode]);
|
|
219
|
-
return (_jsxs(StyledConditionBuilder, { container: { direction: 'column', gap: 4 }, children: [_jsx("div", { children: conditionRows.map(({ id, label, ...leaf }, index) => {
|
|
220
|
-
const deleteAriaLabel = `${t('condition_builder_field_label')} ${leaf.lhs.field
|
|
221
|
-
.split('.')
|
|
222
|
-
.pop()}`;
|
|
223
|
-
let alignItems = 'end';
|
|
224
|
-
if (showErrorIndicators && !isValidCondition(leaf, fields, false)) {
|
|
225
|
-
alignItems = 'center';
|
|
226
|
-
}
|
|
227
|
-
return (_jsxs(Fragment, { children: [isBasicMode && index > 0 && (_jsx(StyledRowWithSeparator, { container: true, shaded: enableShading && operatorsList[index - 1] === 'AND', children: _jsx(MenuButton, { popover: { hideOnTargetHidden: false }, text: operatorsList[index - 1].toLowerCase(), variant: 'simple', menu: {
|
|
228
|
-
items: [
|
|
229
|
-
{
|
|
230
|
-
id: 'AND',
|
|
231
|
-
primary: 'and',
|
|
232
|
-
onClick: () => updateOperator(index - 1, 'AND')
|
|
233
|
-
},
|
|
234
|
-
{
|
|
235
|
-
id: 'OR',
|
|
236
|
-
primary: 'or',
|
|
237
|
-
onClick: () => updateOperator(index - 1, 'OR')
|
|
238
|
-
}
|
|
239
|
-
]
|
|
240
|
-
}, ref: index === focusRowIndex ? newOperatorButtonRef : null }) })), _jsxs(StyledRow, { container: { gap: 0.5, alignItems }, shaded: isBasicMode && enableShading, ref: index === focusRowIndex ? focusRowRef : null, children: [!isBasicMode && _jsx(StyledLabel, { children: label }), _jsx(Flex, { item: { grow: 1 }, children: _jsx(AtomicCondition, { condition: leaf, fields: fields, indicateErrors: showErrorIndicators, validComparators: validComparators, dateFunctions: dateFunctions, onChange: (newCondition) => {
|
|
241
|
-
const newRow = { id, label, ...newCondition };
|
|
242
|
-
dispatch({ type: UPDATE_ROW, payload: { updateAt: index, newRow } });
|
|
243
|
-
}, validRhsTypes: validRhsTypes }) }), _jsx(Button, { icon: true, variant: 'simple', onClick: () => insertRow(index + 1), label: t('add_condition'), children: _jsx(Icon, { name: 'plus' }) }), _jsx(Button, { icon: true, variant: 'simple', onClick: () => removeRow(index), label: t('delete'), "aria-label": deleteAriaLabel ? t('delete_noun', [deleteAriaLabel]) : undefined, children: _jsx(Icon, { name: 'trash' }) })] })] }, id));
|
|
244
|
-
}) }), !isBasicMode && (_jsx("div", { children: _jsxs(Flex, { container: { gap: 0.25, direction: 'column' }, children: [_jsxs(Flex, { container: { alignItems: 'center' }, children: [_jsxs(Label, { htmlFor: labelId, children: [t('condition_builder_advanced_condition_label'), "\u00A0"] }), _jsx(AdditionalInfo, { heading: t('condition_builder_advanced_condition_label'), children: t('condition_builder_advanced_condition_tooltip', [
|
|
245
|
-
disallowNOT ? 'AND/OR' : 'AND/OR/NOT',
|
|
246
|
-
disallowNOT ? '(1 OR 2) AND (3 OR 4 OR 5)' : '(1 AND 2) OR (3 AND NOT 4)'
|
|
247
|
-
]) })] }), _jsx(Input, { id: labelId, type: 'text', placeholder: t('condition_builder_advanced_condition_placeholder'), value: logicString, onChange: (e) => setLogicString(e.target.value), onBlur: () => validateLogicString(logicString), status: logicError !== null ? 'error' : undefined, info: logicError })] }) })), _jsxs(Flex, { container: { direction: 'column', gap: 2 }, children: [showModeWarning && (_jsx(Banner, { variant: 'warning', messages: [
|
|
248
|
-
{
|
|
249
|
-
label: t('condition_builder_switch_banner_text'),
|
|
250
|
-
action: {
|
|
251
|
-
text: t('condition_builder_confirm_button_label'),
|
|
252
|
-
onClick: () => {
|
|
253
|
-
toggleMode(true);
|
|
254
|
-
buttonRef.current?.focus();
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
], onDismiss: () => {
|
|
259
|
-
setModeWarning(false);
|
|
260
|
-
buttonRef.current?.focus();
|
|
261
|
-
}, handle: bannerHandleRef })), _jsx(Flex, { container: { justify: 'between' }, children: _jsx(Button, { name: 'mode-switch', variant: 'simple', onClick: () => {
|
|
262
|
-
toggleMode(false);
|
|
263
|
-
}, "aria-label": isBasicMode
|
|
264
|
-
? t('condition_builder_advanced_mode_button_aria_label')
|
|
265
|
-
: t('condition_builder_basic_mode_button_aria_label'), ref: buttonRef, children: isBasicMode
|
|
266
|
-
? t('condition_builder_advanced_mode_button_label')
|
|
267
|
-
: t('condition_builder_basic_mode_button_label') }) })] })] }));
|
|
268
|
-
};
|
|
269
|
-
ConditionBuilder.defaultProps = defaultProps;
|
|
270
|
-
export default ConditionBuilder;
|
|
271
|
-
//# sourceMappingURL=ConditionBuilder.js.map
|