-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathRuleBuilderHeader.jsx
96 lines (88 loc) · 2.64 KB
/
RuleBuilderHeader.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import * as React from 'react';
import { Add, TextNewLine } from '@carbon/react/icons';
import PropTypes from 'prop-types';
import Button from '../Button';
import { settings } from '../../constants/Settings';
import deprecate from '../../internal/deprecate';
import GroupLogic from './GroupLogic';
import { GroupLogicPropType } from './RuleBuilderPropTypes';
const { iotPrefix } = settings;
const defaultProps = {
i18n: {
addRule: 'Add rule',
addGroup: 'Add group',
},
// the default logic for the primary rule builder group. Can be ALL or ANY
groupLogic: 'ALL',
testId: 'rule-builder-header',
};
const propTypes = {
id: PropTypes.string.isRequired,
groupLogic: GroupLogicPropType,
i18n: PropTypes.shape({
addRule: PropTypes.string,
addGroup: PropTypes.string,
}),
onAddRule: PropTypes.func.isRequired,
onChange: PropTypes.func.isRequired,
// TODO: remove the deprecated 'testID' in v3.
// eslint-disable-next-line react/require-default-props
testID: deprecate(
PropTypes.string,
`The 'testID' prop has been deprecated. Please use 'testId' instead.`
),
testId: PropTypes.string,
};
const RuleBuilderHeader = ({ id, onAddRule, onChange, i18n, groupLogic, testID, testId }) => {
const mergedI18n = React.useMemo(
() => ({
...defaultProps.i18n,
...(i18n ?? {}),
}),
[i18n]
);
const handleChangeGroupLogic = React.useCallback(
({ selectedItem }) => {
onChange(
{
id,
groupLogic: selectedItem.id,
},
true
);
},
[id, onChange]
);
return (
<div
// TODO: remove the deprecated 'testID' in v3.
data-testid={testID || testId}
className={`${iotPrefix}--rule-builder-header`}
>
<GroupLogic id={id} selected={groupLogic} onChange={handleChangeGroupLogic} />
<div className={`${iotPrefix}--rule-builder-header__buttons`}>
<Button
// TODO: remove the deprecated 'testID' in v3.
testId={`${testID || testId}-add-rule-button`}
kind="ghost"
renderIcon={(props) => <Add size={32} {...props} />}
onClick={onAddRule()}
>
{mergedI18n.addRule}
</Button>
<Button
// TODO: remove the deprecated 'testID' in v3.
testId={`${testID || testId}-add-group-button`}
kind="ghost"
renderIcon={(props) => <TextNewLine size={32} {...props} />}
onClick={onAddRule(undefined, true)}
>
{mergedI18n.addGroup}
</Button>
</div>
</div>
);
};
RuleBuilderHeader.defaultProps = defaultProps;
RuleBuilderHeader.propTypes = propTypes;
export default RuleBuilderHeader;