Skip to content

Commit b6d59e3

Browse files
authored
Merge pull request #5 from react-querybuilder/option-groups
Add docs for option group support
2 parents 7036078 + 3a6e0f6 commit b6d59e3

21 files changed

+1559
-1711
lines changed

.prettierrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"tabWidth": 2,
3+
"semi": true,
4+
"printWidth": 100,
5+
"singleQuote": true,
6+
"trailingComma": "es5",
7+
"bracketSpacing": true,
8+
"bracketSameLine": true,
9+
"arrowParens": "avoid"
10+
}

docs/api/export.mdx

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Export
33
description: Convert query builder objects to SQL, etc.
44
---
55

6-
import TypeScriptAdmonition from "./_ts_admonition.md";
6+
import TypeScriptAdmonition from './_ts_admonition.md';
77

88
<TypeScriptAdmonition />
99

@@ -33,21 +33,21 @@ For the next few sections, assume the `query` variable has been defined as:
3333

3434
```ts
3535
const query: RuleGroupType = {
36-
id: "root",
37-
combinator: "and",
36+
id: 'root',
37+
combinator: 'and',
3838
not: false,
3939
rules: [
4040
{
41-
id: "rule1",
42-
field: "firstName",
43-
value: "Steve",
44-
operator: "=",
41+
id: 'rule1',
42+
field: 'firstName',
43+
value: 'Steve',
44+
operator: '=',
4545
},
4646
{
47-
id: "rule2",
48-
field: "lastName",
49-
value: "Vai",
50-
operator: "=",
47+
id: 'rule2',
48+
field: 'lastName',
49+
value: 'Vai',
50+
operator: '=',
5151
},
5252
],
5353
};
@@ -62,7 +62,7 @@ To export the internal query representation like what `react-querybuilder` passe
6262
```ts
6363
formatQuery(query);
6464
// or
65-
formatQuery(query, "json");
65+
formatQuery(query, 'json');
6666
```
6767

6868
The output will be a multi-line string representation of the query using 2 spaces for indentation.
@@ -94,7 +94,7 @@ The output will be a multi-line string representation of the query using 2 space
9494
To export the internal query representation without formatting (single-line, no indentation) and without the `id` attribute on each object, use the "json_without_ids" format. This is useful if you need to serialize the query for storage.
9595

9696
```ts
97-
formatQuery(query, "json_without_ids");
97+
formatQuery(query, 'json_without_ids');
9898
```
9999

100100
Output:
@@ -108,7 +108,7 @@ Output:
108108
`formatQuery` can export SQL compatible with most RDBMS engines. To export a SQL `WHERE` clause, use the "sql" format.
109109

110110
```ts
111-
formatQuery(query, "sql");
111+
formatQuery(query, 'sql');
112112
```
113113

114114
Output:
@@ -122,7 +122,7 @@ Output:
122122
To export a SQL `WHERE` clause with bind variables instead of explicit values, use the "parameterized" format. The output is an object with `sql` and `params` attributes.
123123

124124
```ts
125-
formatQuery(query, "parameterized");
125+
formatQuery(query, 'parameterized');
126126
```
127127

128128
Output:
@@ -139,7 +139,7 @@ Output:
139139
If anonymous parameters (aka bind variables) are not acceptable, `formatQuery` can output named parameters based on the field names. Use the "parameterized_named" format. The output object is similar to the "parameterized" format, but the `params` attribute is an object instead of an array.
140140

141141
```ts
142-
formatQuery(query, "parameterized_named");
142+
formatQuery(query, 'parameterized_named');
143143
```
144144

145145
Output:
@@ -159,7 +159,7 @@ Output:
159159
For MongoDB-compatible output, use the "mongodb" format.
160160

161161
```ts
162-
formatQuery(query, "mongodb");
162+
formatQuery(query, 'mongodb');
163163
```
164164

165165
Output:
@@ -178,31 +178,31 @@ If you need to control the way the value portion of the output is processed, you
178178

179179
```ts
180180
const query: RuleGroupType = {
181-
combinator: "and",
181+
combinator: 'and',
182182
not: false,
183183
rules: [
184184
{
185-
field: "instrument",
186-
value: ["Guitar", "Vocals"],
187-
operator: "in",
185+
field: 'instrument',
186+
value: ['Guitar', 'Vocals'],
187+
operator: 'in',
188188
},
189189
{
190-
field: "lastName",
191-
value: "Vai",
192-
operator: "=",
190+
field: 'lastName',
191+
value: 'Vai',
192+
operator: '=',
193193
},
194194
],
195195
};
196196

197197
const valueProcessor = (field, operator, value) => {
198-
if (operator === "in") {
198+
if (operator === 'in') {
199199
// Assuming `value` is an array, such as from a multi-select
200-
return `(${value.map((v) => `"${v.trim()}"`).join(",")})`;
200+
return `(${value.map(v => `"${v.trim()}"`).join(',')})`;
201201
}
202202
return defaultValueProcessor(field, operator, value);
203203
};
204204

205-
formatQuery(query, { format: "sql", valueProcessor });
205+
formatQuery(query, { format: 'sql', valueProcessor });
206206
// Output: "(instrument in ('Guitar','Vocals') and lastName = 'Vai')"
207207
```
208208

@@ -211,7 +211,7 @@ formatQuery(query, { format: "sql", valueProcessor });
211211
Some database engines wrap field names in backticks. This can be configured with the `quoteFieldNamesWith` option.
212212

213213
```ts
214-
formatQuery(query, { format: "sql", quoteFieldNamesWith: "`" });
214+
formatQuery(query, { format: 'sql', quoteFieldNamesWith: '`' });
215215
// Output: "(`firstName` = 'Steve' and `lastName` = 'Vai')"
216216
```
217217

@@ -221,8 +221,8 @@ If the "parameterized_named" format is used, configure the parameter prefix used
221221

222222
```ts
223223
const p = formatQuery(query, {
224-
format: "parameterized_named",
225-
paramPrefix: "$",
224+
format: 'parameterized_named',
225+
paramPrefix: '$',
226226
});
227227
// p.sql === "(firstName = $firstName_1 and lastName = $lastName_1)"
228228
```
@@ -241,43 +241,43 @@ Example:
241241

242242
```ts
243243
const query: RuleGroupType = {
244-
id: "root",
244+
id: 'root',
245245
rules: [
246246
{
247-
id: "r1",
248-
field: "firstName",
249-
value: "",
250-
operator: "=",
247+
id: 'r1',
248+
field: 'firstName',
249+
value: '',
250+
operator: '=',
251251
},
252252
{
253-
id: "r2",
254-
field: "lastName",
255-
value: "Vai",
256-
operator: "=",
253+
id: 'r2',
254+
field: 'lastName',
255+
value: 'Vai',
256+
operator: '=',
257257
},
258258
],
259-
combinator: "and",
259+
combinator: 'and',
260260
not: false,
261261
};
262262

263263
// Query is invalid based on the validator function
264264
formatQuery(query, {
265-
format: "sql",
265+
format: 'sql',
266266
validator: () => false,
267267
});
268268
// Output: "(1 = 1)" <-- see `fallbackExpression` option
269269

270270
// Rule "r1" is invalid based on the validation map
271271
formatQuery(query, {
272-
format: "sql",
272+
format: 'sql',
273273
validator: () => ({ r1: false }),
274274
});
275275
// Output: "(lastName = 'Vai')" <-- skipped `firstName` rule with `id === 'r1'`
276276

277277
// Rule "r1" is invalid based on the field validator for `firstName`
278278
formatQuery(query, {
279-
format: "sql",
280-
fields: [{ name: "firstName", validator: () => false }],
279+
format: 'sql',
280+
fields: [{ name: 'firstName', validator: () => false }],
281281
});
282282
// Output: "(lastName = 'Vai')" <-- skipped `firstName` rule because field validator returned `false`
283283
```

docs/api/import.mdx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Import
33
description: Convert SQL to query builder objects
44
---
55

6-
import TypeScriptAdmonition from "./_ts_admonition.md";
6+
import TypeScriptAdmonition from './_ts_admonition.md';
77

88
<TypeScriptAdmonition />
99

@@ -25,16 +25,16 @@ Running any of the following statements will produce the same result (see below)
2525
parseSQL(`SELECT * FROM t WHERE firstName = 'Steve' AND lastName = 'Vai'`);
2626

2727
parseSQL(`SELECT * FROM t WHERE firstName = ? AND lastName = ?`, {
28-
params: ["Steve", "Vai"],
28+
params: ['Steve', 'Vai'],
2929
});
3030

3131
parseSQL(`SELECT * FROM t WHERE firstName = :p1 AND lastName = :p2`, {
32-
params: { p1: "Steve", p2: "Vai" },
32+
params: { p1: 'Steve', p2: 'Vai' },
3333
});
3434

3535
parseSQL(`SELECT * FROM t WHERE firstName = $p1 AND lastName = $p2`, {
36-
params: { p1: "Steve", p2: "Vai" },
37-
paramPrefix: "$",
36+
params: { p1: 'Steve', p2: 'Vai' },
37+
paramPrefix: '$',
3838
});
3939
```
4040

@@ -100,15 +100,15 @@ Output (`RuleGroupTypeIC`):
100100
{
101101
rules: [
102102
{
103-
field: "firstName",
104-
operator: "=",
105-
value: "Steve",
103+
field: 'firstName',
104+
operator: '=',
105+
value: 'Steve',
106106
},
107-
"and",
107+
'and',
108108
{
109-
field: "lastName",
110-
operator: "=",
111-
value: "Vai",
109+
field: 'lastName',
110+
operator: '=',
111+
value: 'Vai',
112112
},
113113
];
114114
}

docs/api/misc.mdx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ hide_table_of_contents: true
44
description: Assorted utilities and other exports
55
---
66

7-
import TypeScriptAdmonition from "./_ts_admonition.md";
7+
import TypeScriptAdmonition from './_ts_admonition.md';
88

99
<TypeScriptAdmonition />
1010

@@ -25,10 +25,7 @@ You can see an example of the default validator in action in the [demo](https://
2525
### `findPath`
2626

2727
```ts
28-
function findPath(
29-
path: number[],
30-
query: RuleGroupType
31-
): RuleType | RuleGroupType;
28+
function findPath(path: number[], query: RuleGroupType): RuleType | RuleGroupType;
3229
```
3330

3431
`findPath` is a utility function for finding the rule or group within the query hierarchy that has a given `path`. Useful in custom [`onAddRule`](./querybuilder#onaddrule) and [`onAddGroup`](./querybuilder#onaddgroup) functions.

0 commit comments

Comments
 (0)