Skip to content

Commit e92e80a

Browse files
authored
Merge pull request #14 from react-querybuilder/between-fields
Docs for 4.5.2
2 parents 260cc33 + 126c18c commit e92e80a

File tree

3 files changed

+96
-5
lines changed

3 files changed

+96
-5
lines changed

docs/api/classnames.mdx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ import { standardClassnames as sc } from 'react-querybuilder';
4444
label: `.${sc.valueSource}`,
4545
valueSources: ['value', 'field'],
4646
},
47+
{
48+
name: 'fb1',
49+
label: `Value list`,
50+
operators: [{ name: 'between', label: 'between' }],
51+
valueSources: ['field', 'value'],
52+
comparator: f => f.name === 'fb2',
53+
},
54+
{
55+
name: 'fb2',
56+
label: `.${sc.valueListItem}`,
57+
},
4758
]}
4859
combinators={[{ name: 'and', label: `.${sc.combinators}` }]}
4960
getOperators={() => [{ name: '=', label: `.${sc.operators}` }]}
@@ -138,6 +149,7 @@ import { standardClassnames as sc } from 'react-querybuilder';
138149
],
139150
},
140151
{ field: 'f3', operator: '=', value: 'f1' },
152+
{ field: 'fb1', operator: 'between', value: 'fb2,fb2', valueSource: 'field' },
141153
],
142154
}}
143155
/>

docs/api/export.mdx

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,49 @@ const query: RuleGroupType = {
5050
};
5151
```
5252

53+
:::tip
54+
55+
_<abbr title="Too long; didn't read">TL;DR</abbr>: For best results, use the [default combinators and operators](./misc#defaults) or map custom combinators/operators to the defaults with [`transformQuery`](./misc#transformquery)._
56+
57+
While `formatQuery` technically accepts query objects of type `RuleGroupTypeAny` (i.e. `RuleGroupType` or `RuleGroupTypeIC`), it is not guaranteed to process a query correctly unless the query also conforms to the type `DefaultRuleGroupTypeAny` (i.e. `DefaultRuleGroupType` or `DefaultRuleGroupTypeIC`).
58+
59+
In practice, this means that all `combinator` and `operator` properties in the query must match the `name` of an element in [`defaultCombinators` or `defaultOperators`](./misc#defaults), respectively. If you implement custom combinator/operator names, you can use the [`transformQuery` function](./misc#transformquery) to map your query properties to the defaults.
60+
61+
For example, assume your implementation replaces the default "between" operator (`{ name: "between", label: "between" }`) with `{ name: "b/w", label: "b/w" }`. Any rules using this operator would have `operator: "b/w"` instead of `operator: "between"`. So if a query looked like this...
62+
63+
```json
64+
{
65+
"combinator": "and",
66+
"rules": [
67+
{
68+
"field": "someNumber",
69+
"operator": "b/w",
70+
"value": "12,14"
71+
}
72+
]
73+
}
74+
```
75+
76+
...you could run it through `transformQuery` with the `operatorMap` option:
77+
78+
```ts
79+
const newQuery = transformQuery(query, { operatorMap: { 'b/w': 'between' } });
80+
// {
81+
// "combinator": "and",
82+
// "rules": [
83+
// {
84+
// "field": "someNumber",
85+
// "operator": "between",
86+
// "value": "12,14"
87+
// }
88+
// ]
89+
// }
90+
```
91+
92+
The `newQuery` object would be ready for processing by `formatQuery`, including its special handling of the "between" operator.
93+
94+
:::
95+
5396
## Basic usage
5497

5598
### JSON
@@ -260,17 +303,17 @@ To avoid information loss, this option is more strict about what qualifies as "n
260303
The following expressions all evaluate to `true`:
261304

262305
```ts
263-
parseFloat('000111abcdef') === 111;
306+
parseFloat('000111abcdef') === 111; // everything from the 'a' on is ignored by `parseFloat`
264307

265308
formatQuery(
266309
{ rules: [{ field: 'f', operator: '=', value: '000111abcdef' }] },
267310
{ format: 'sql', parseNumbers: true }
268-
) === "(f = '000111abcdef')";
311+
) === "(f = '000111abcdef')"; // `value` contains non-numeric characters, so remains as-is
269312

270313
formatQuery(
271314
{ rules: [{ field: 'f', operator: '=', value: ' 000111 ' }] },
272315
{ format: 'sql', parseNumbers: true }
273-
) === '(f = 111)';
316+
) === '(f = 111)'; // after trimming whitespace, `value` is wholly numeric
274317
```
275318

276319
:::

docs/api/querybuilder.mdx

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ If the `independentCombinators` prop is provided, then the `query` argument will
101101

102102
`RuleGroupTypeAny`
103103

104-
The query is an object of type `RuleGroupType` (or `RuleGroupTypeIC`, if [`independentCombinators`](#independentCombinators) is `true`). If this prop is provided, `<QueryBuilder />` will be a controlled component.
104+
The query is an object of type `RuleGroupType` (or `RuleGroupTypeIC`, if [`independentCombinators`](#independentcombinators) is `true`). If this prop is provided, `<QueryBuilder />` will be a controlled component.
105105

106106
The `query` prop follows the same format as the parameter passed to the [`onQueryChange`](#onquerychange) callback since they are meant to be used together to control the component. See [examples](https://github.com/react-querybuilder/react-querybuilder/blob/main/examples).
107107

@@ -720,11 +720,47 @@ Pass `false` to automatically add an "empty" option with value `"~"` and label `
720720

721721
Pass `true` to automatically add a rule to new groups. If a `query` prop is not passed in, a rule will be added to the root group when the component is mounted. If a `query` prop is passed in with an empty `rules` array, no rule will be added automatically.
722722

723+
### `listsAsArrays`
724+
725+
`boolean` (default `false`) [_Click for demo_](https://react-querybuilder.js.org/react-querybuilder/#listsAsArrays=true)
726+
727+
Pass `true` to update rule values that represent lists with proper arrays instead of comma-separated strings. Applies when `valueEditorType` is "multiselect" and when a rule's `operator` is "between", "notBetween", "in", or "notIn".
728+
729+
For example, the default behavior for the "between" operator might produce this rule:
730+
731+
```json {4}
732+
{
733+
"field": "f1",
734+
"operator": "between",
735+
"value": "f2,f3",
736+
"valueSource": "field"
737+
}
738+
```
739+
740+
When `listsAsArrays` is true, the rule's `value` will be an array:
741+
742+
```json {4}
743+
{
744+
"field": "f1",
745+
"operator": "between",
746+
"value": ["f2", "f3"],
747+
"valueSource": "field"
748+
}
749+
```
750+
723751
### `independentCombinators`
724752

725753
`boolean` (default `false`) [_Click for demo_](https://react-querybuilder.js.org/react-querybuilder/#independentCombinators=true)
726754

727-
Pass `true` to insert an independent combinator selector between each rule/group in a rule group. The combinator selector at the group level will not be available. Visually, this is similar to the [`showCombinatorsBetweenRules`](#showcombinatorsbetweenrules) option, except that each combinator selector is independently controlled. You may find that users take to this configuration more easily, as it can allow them to express queries more like they would in natural language.
755+
Pass `true` to insert an independent combinator selector between each rule/group in a rule group. The combinator selector at the group level will not be rendered.
756+
757+
Visually, this option has a similar effect as the [`showCombinatorsBetweenRules`](#showcombinatorsbetweenrules) option, except that each combinator selector is independently controlled. You may find that users take to this configuration more easily, as it can allow them to express queries more like they would in natural language.
758+
759+
:::caution
760+
761+
When the `independentCombinators` option is enabled, the `query` (or `defaultQuery`) prop _must_ be of type `RuleGroupTypeIC` instead of the default `RuleGroupType`. See [`onQueryChange`](#onquerychange) above, or the [Rules and groups section](../typescript#rules-and-groups) of the TypeScript documentation for more information.
762+
763+
:::
728764

729765
### `enableDragAndDrop`
730766

0 commit comments

Comments
 (0)