forked from ukrbublik/react-awesome-query-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaseGroup.jsx
More file actions
185 lines (159 loc) · 4.57 KB
/
CaseGroup.jsx
File metadata and controls
185 lines (159 loc) · 4.57 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import React from "react";
import PropTypes from "prop-types";
import GroupContainer from "../containers/GroupContainer";
import Draggable from "../containers/Draggable";
import {BasicGroup} from "./Group";
import {GroupActions} from "./GroupActions";
import {useOnPropsChanged} from "../../utils/reactUtils";
import {Col, dummyFn, WithConfirmFn} from "../utils";
import Widget from "../rule/Widget";
import classNames from "classnames";
class CaseGroup extends BasicGroup {
static propTypes = {
...BasicGroup.propTypes,
parentReordableNodesCnt: PropTypes.number,
value: PropTypes.any,
setValue: PropTypes.func,
};
constructor(props) {
super(props);
useOnPropsChanged(this);
this.onPropsChanged(props);
}
onPropsChanged(nextProps) {
}
isDefaultCase() {
return this.props.children1 == undefined;
}
reordableNodesCnt() {
// `parentReordableNodesCnt` is number of cases to reorder
return this.props.parentReordableNodesCnt;
}
reordableNodesCntForItem(_item) {
// `reordableNodesCnt` is number of nodes is current case
if (this.props.isLocked)
return 0;
return this.props.reordableNodesCnt;
}
totalRulesCntForItem(_item) {
// `totalRulesCnt` is number of nodes is current case
return this.props.totalRulesCnt;
}
showDragIcon() {
// default impl of `showDragIcon()` uses `this.reordableNodesCnt()`
if (this.isDefaultCase())
return false;
return super.showDragIcon();
}
childrenClassName = () => "case_group--children";
renderFooterWrapper = () => null;
renderHeaderWrapper() {
return (
<div key="group-header" className={classNames(
"group--header",
this.isOneChild() ? "one--child" : "",
this.isOneChild() ? "hide--line" : "",
this.isNoChildren() ? "no--children" : "",
this.showDragIcon() ? "with--drag" : "hide--drag",
this.showConjs() && (!this.isOneChild() || this.showNot()) ? "with--conjs" : "hide--conjs"
)}>
{this.renderHeaderLeft()}
{this.renderHeaderCenter()}
{this.renderActions()}
</div>
);
}
renderChildrenWrapper() {
if (this.isDefaultCase())
return null;
// body has 2 columns: condition & value
return (
<div className={"case_group--body"}>
{this.renderCondition()}
{this.renderValue()}
</div>
);
}
renderHeaderLeft() {
if (this.isDefaultCase()) {
const { defaultCaseLabel } = this.props.config.settings;
return defaultCaseLabel || "";
}
// default impl:
return (
<div className={"group--conjunctions"}>
{this.renderConjs()}
{this.renderDrag()}
</div>
);
}
renderCondition() {
if (this.isDefaultCase())
return null;
return super.renderChildrenWrapper();
}
renderHeaderCenter() {
if (this.isDefaultCase())
return this.renderValue();
else
return null;
}
canAddGroup() {
if (this.isDefaultCase())
return false;
return super.canAddGroup();
}
canAddRule() {
if (this.isDefaultCase())
return false;
return super.canAddRule();
}
renderValue() {
const { config, isLocked, value, setValue, id } = this.props;
const { immutableValuesMode } = config.settings;
const widget = <Widget
key="values"
isCaseValue={true}
field={"!case_value"}
operator={null}
value={value}
valueSrc={"value"}
valueError={null}
config={config}
setValue={!immutableValuesMode ? setValue : dummyFn}
setValueSrc={dummyFn}
readonly={immutableValuesMode || isLocked}
id={id}
groupId={null}
/>;
return (
<Col className="case_group--value">
{widget}
</Col>
);
}
renderActions() {
const {config, addGroup, addRule, isLocked, isTrueLocked, id} = this.props;
return <GroupActions
config={config}
addGroup={addGroup}
addRule={addRule}
canAddRule={this.canAddRule()}
canAddGroup={this.canAddGroup()}
canDeleteGroup={this.canDeleteGroup()}
removeSelf={this.removeSelf}
setLock={this.setLock}
isLocked={isLocked}
isTrueLocked={isTrueLocked}
id={id}
/>;
}
isEmptyCurrentGroup() {
// used to confirm self-deletion
const { value } = this.props;
const oneValue = value && value.size ? value.get(0) : null;
const hasValue = oneValue != null && (Array.isArray(oneValue) ? oneValue.length > 0 : true);
return super.isEmptyCurrentGroup() && !hasValue;
}
}
export default GroupContainer(Draggable("group case_group")(WithConfirmFn(CaseGroup)));