Skip to content

Commit b395caa

Browse files
authored
Fix nested group (#280)
* tests reduce whitespaces * fix init load of {} as jlogic * nit * tests - reduce whitespace * fix export of nested groups * added test "query nested !group" * lint fix * fix nested group css * 2.1.16 * added test with two separate group rules added test query with !struct inside !group
1 parent 84d59e6 commit b395caa

File tree

11 files changed

+346
-321
lines changed

11 files changed

+346
-321
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Changelog
2+
- 2.1.16
3+
- Fixed issues with export to Mongo and JsonLogic of queries with nested groups (#279, #279)
4+
- 2.1.15
5+
- Fixed issue #276 with TS definitions
26
- 2.1.14
37
- Require Node v10+ (for karma)
48
- Fixed "SyntaxError Unexpected token '<'" in sandboxes
@@ -60,7 +64,7 @@
6064
- Removed coupling with AntDesign. Now it should be possible to use another UI framework.
6165
- Added vanilla widgets. Added switcher between `antd` and `vanilla` in demo.
6266
- 1.3.7
63-
- Fixed [issue](https://github.com/ukrbublik/react-awesome-query-builder/issues/168) with dot in field name
67+
- Fixed issue #168 with dot in field name
6468
- 1.3.6
6569
- Added config options to disable inputs: `immutableFieldsMode`, `immutableOpsMode`, `immutableValuesMode`
6670
- 1.3.5

css/styles.scss

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,6 @@ div.tooltip-inner {
471471
}
472472
.group--header:hover .group--header,
473473
.group--header:not(:hover),
474-
.rule_group:hover .rule_group,
475474
.rule_group:not(:hover) {
476475
#{$what} {
477476
opacity: 0;
@@ -485,7 +484,6 @@ div.tooltip-inner {
485484
}
486485
.group--header:hover .group--header,
487486
.group--header:not(:hover),
488-
.rule_group:hover .rule_group,
489487
.rule_group:not(:hover) {
490488
#{$inactive} {
491489
width: 0;

modules/export/jsonLogic.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ const jsonLogicFormatItem = (item, config, meta, isRoot, parentField = null) =>
146146
return undefined;
147147
}
148148

149-
const groupField = isRuleGroup ? (parentField ? [parentField, field] : [field]).join(fieldSeparator) : parentField;
149+
const groupField = isRuleGroup ? field : parentField;
150150
const list = children
151151
.map((currentChild) => jsonLogicFormatItem(currentChild, config, meta, false, groupField))
152152
.filter((currentChild) => typeof currentChild !== "undefined");
@@ -173,9 +173,13 @@ const jsonLogicFormatItem = (item, config, meta, isRoot, parentField = null) =>
173173
// rule_group (issue #246)
174174
if (isRuleGroup) {
175175
const op = not ? "none" : "some";
176+
let fieldName = field;
177+
if (parentField) {
178+
fieldName = cutBeginOfString(fieldName, parentField + fieldSeparator);
179+
}
176180
resultQuery = {
177181
[op]: [
178-
[{var: field}],
182+
[{var: fieldName}],
179183
resultQuery
180184
]
181185
};
@@ -244,8 +248,8 @@ const jsonLogicFormatItem = (item, config, meta, isRoot, parentField = null) =>
244248
fieldName = cutBeginOfString(fieldName, parentField + fieldSeparator);
245249
}
246250
const formattedField = { "var": fieldName };
247-
if (meta.usedFields.indexOf(fieldName) == -1)
248-
meta.usedFields.push(fieldName);
251+
if (meta.usedFields.indexOf(field) == -1)
252+
meta.usedFields.push(field);
249253

250254
// format logic
251255
let formatteOp = operator;

modules/export/mongoDb.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ const mongodbFormatItem = (parents, item, config, meta, _not = false) => {
121121
const children = item.get("children1");
122122
const {fieldSeparator, canShortMongoQuery} = config.settings;
123123

124+
const hasParentRuleGroup = parents.filter(it => it.get("type") == "rule_group").length > 0;
125+
const parentPath = parents
126+
.filter(it => it.get("type") == "rule_group")
127+
.map(it => it.get("properties").get("field"))
128+
.slice(-1).pop();
129+
124130
if ((type === "group" || type === "rule_group") && children && children.size) {
125131
const not = _not ? !(properties.get("not")) : (properties.get("not"));
126132
const list = children
@@ -181,7 +187,17 @@ const mongodbFormatItem = (parents, item, config, meta, _not = false) => {
181187
resultQuery = { [mongoConj] : rules };
182188
}
183189
if (field) {
184-
resultQuery = { [field]: {"$elemMatch": resultQuery} };
190+
//format field
191+
let fieldName = field;
192+
if (hasParentRuleGroup) {
193+
if (parentPath && fieldName.indexOf(parentPath+".") == 0) {
194+
fieldName = fieldName.slice((parentPath+".").length);
195+
} else {
196+
meta.errors.push(`Can't cut group ${parentPath} from field ${fieldName}`);
197+
}
198+
}
199+
200+
resultQuery = { [fieldName]: {"$elemMatch": resultQuery} };
185201
}
186202
return resultQuery;
187203
} else if (type === "rule") {
@@ -204,21 +220,16 @@ const mongodbFormatItem = (parents, item, config, meta, _not = false) => {
204220
[operatorDefinition, revOperatorDefinition] = [revOperatorDefinition, operatorDefinition];
205221
}
206222

207-
const hasParentRuleGroup = parents.filter(it => it.get("type") == "rule_group").length > 0;
208-
const parentPath = parents
209-
.filter(it => it.get("type") == "rule_group")
210-
.map(it => it.get("properties").get("field"))
211-
.join(fieldSeparator);
212-
213223
//format field
214224
let fieldName = field;
215225
if (hasParentRuleGroup) {
216-
if (parentPath.length > 0 && fieldName.indexOf(parentPath+".") == 0) {
226+
if (parentPath && fieldName.indexOf(parentPath+".") == 0) {
217227
fieldName = fieldName.slice((parentPath+".").length);
218228
} else {
219-
meta.errors.push(`Can't cut group from field ${fieldName}`);
229+
meta.errors.push(`Can't cut group ${parentPath} from field ${fieldName}`);
220230
}
221231
}
232+
222233
// if (fieldDefinition.tableName) {
223234
// let fieldParts = Array.isArray(field) ? [...field] : field.split(fieldSeparator);
224235
// fieldParts[0] = fieldDefinition.tableName;

modules/import/jsonLogic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const loadFromJsonLogic = (logicTree, config) => {
2828
};
2929
const extendedConfig = extendConfig(config);
3030
const conv = buildConv(extendedConfig);
31-
let jsTree = convertFromLogic(logicTree, conv, extendedConfig, "rule", meta);
31+
let jsTree = logicTree ? convertFromLogic(logicTree, conv, extendedConfig, "rule", meta) : undefined;
3232
if (jsTree && jsTree.type != "group") {
3333
jsTree = wrapInDefaultConj(jsTree, extendedConfig);
3434
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-awesome-query-builder",
3-
"version": "2.1.15",
3+
"version": "2.1.16",
44
"description": "User-friendly query builder for React. Demo: https://ukrbublik.github.io/react-awesome-query-builder",
55
"keywords": [
66
"reactjs",

sandbox/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"@types/lodash": "^4.6",
2424
"antd": ">=4.0.0 <4.5.0",
2525
"react": "^16.4.1",
26-
"react-awesome-query-builder": "^2.1.14",
26+
"react-awesome-query-builder": "^2.1.16",
2727
"react-dom": "^16.4.1",
2828
"react-scripts": "3.0.1",
2929
"typescript": "3.4.5",

sandbox_simple/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"homepage": "https://ukrbublik.github.io/react-awesome-query-builder/",
1919
"dependencies": {
2020
"react": "^16.4.1",
21-
"react-awesome-query-builder": "^2.1.14",
21+
"react-awesome-query-builder": "^2.1.16",
2222
"react-dom": "^16.4.1",
2323
"react-scripts": "3.0.1",
2424
"lodash": "^4.6"

0 commit comments

Comments
 (0)