Skip to content

Commit 01b5b81

Browse files
committed
Use transformQuery in recursion tip
1 parent 33d9e5e commit 01b5b81

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

docs/api/misc.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function convertQuery(query: RuleGroupTypeIC): RuleGroupType;
4242

4343
`convertQuery` toggles a query between the conventional `RuleGroupType` structure (with combinators at the group level) and the "independent combinators" structure (`RuleGroupTypeIC`, used with the [`independentCombinators` prop](./querybuilder#independentcombinators)).
4444

45-
`convertToIC` and `convertFromIC` do the same thing as `convertQuery`, but only in one direction.
45+
`convertToIC` and `convertFromIC` do the same thing as `convertQuery`, but only in the directions indicated by their respective names.
4646

4747
### `transformQuery`
4848

docs/tips/adding-removing-query-properties.mdx

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,31 @@ console.log(formatQuery(query, 'json_without_ids'));
3535

3636
## Adding properties
3737

38-
To produce a query object with additional properties, you can loop through the `rules` array recursively. In the example below (from [issue #226](https://github.com/react-querybuilder/react-querybuilder/issues/226)), the `inputType` from the `fields` array is added to each rule.
38+
To produce a query object with additional properties, you can loop through the `rules` array recursively. The [`transformQuery`](../api/misc#transformquery) function is provided for use cases such as this.
39+
40+
In the example below (inspired by [issue #226](https://github.com/react-querybuilder/react-querybuilder/issues/226)), the `inputType` from the `fields` array is added to each rule.
41+
42+
```ts
43+
import type { Field, RuleGroupType, RuleType } from 'react-querybuilder';
44+
import { transformQuery } from 'react-querybuilder';
45+
46+
const fields: Field[] = [
47+
{ name: 'description', label: 'Description', inputType: 'string' },
48+
{ name: 'price', label: 'Price', inputType: 'number' },
49+
];
50+
51+
const ruleProcessor = (r: RuleType): RuleType & { inputType?: string } => ({
52+
...r,
53+
inputType: fields.find(f => f.name === r.field)?.inputType,
54+
});
55+
56+
const result = transformQuery(query, { ruleProcessor });
57+
```
58+
59+
<details>
60+
<summary>Manual recursion</summary>
61+
62+
This example (_directly_ from [issue #226](https://github.com/react-querybuilder/react-querybuilder/issues/226)) demonstrates manual recursion to achieve exactly the same result as the `transformQuery` example above.
3963

4064
```ts
4165
import type { Field, RuleGroupType, RuleType } from 'react-querybuilder';
@@ -62,3 +86,5 @@ const processGroup = (rg: RuleGroupType): RuleGroupType => ({
6286

6387
const result = processGroup(query);
6488
```
89+
90+
</details>

0 commit comments

Comments
 (0)