Skip to content

Commit 3a6e0f6

Browse files
committed
Add some notes about RuleGroupTypeIC
1 parent ab7cb6b commit 3a6e0f6

File tree

2 files changed

+52
-12
lines changed

2 files changed

+52
-12
lines changed

docs/api/querybuilder.mdx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ import TypeScriptAdmonition from './_ts_admonition.md';
99

1010
The default export of `react-querybuilder` is the `<QueryBuilder />` React component.
1111

12-
## Props
12+
All props are optional, but as stated in the [getting started guide](../intro), the query builder is really only useful when, at a minimum, the `fields` prop is defined.
1313

1414
:::note
1515

16-
All props are optional, but as stated in the [getting started guide](../intro), the query builder is really only useful when, at a minimum, the `fields` prop is defined.
16+
When you see `RuleGroupTypeAny` below (e.g. for [query](#query), [defaultQuery](#defaultquery), and [onQueryChange](#onquerychange)), that means the type must either be `RuleGroupType` or `RuleGroupTypeIC`. However, if the type is `RuleGroupTypeIC`, then the [`independentCombinators` prop](#independentcombinators) must be set to `true`. Likewise, if the type is `RuleGroupType` then `independentCombinators` must be `false` or `undefined`.
1717

1818
:::
1919

20+
## Props
21+
2022
### `fields`
2123

2224
`Field[] | OptionGroup<Field>[]`

docs/typescript.mdx

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
2-
title: TypeScript
2+
title: TypeScript reference
33
---
44

5-
These are some of the [TypeScript](https://www.typescriptlang.org/) types and interfaces you'll see throughout the documentation. Even if you are not using TypeScript (you really should! 😊), you can use the information below to understand the required shape of the props and function parameters. To see the full type definitions for the `react-querybuilder` library, [click here](https://github.com/react-querybuilder/react-querybuilder/blob/master/src/types.ts).
5+
These are some of the [TypeScript](https://www.typescriptlang.org/) types and interfaces you'll see throughout the documentation. Even if you are not using TypeScript (you really should! 😊), you can use the information below to understand the required shape of the props and function parameters. To see the full type definitions for the `react-querybuilder` library, [click here](https://github.com/react-querybuilder/react-querybuilder/tree/master/packages/react-querybuilder/src/types).
66

77
## Fields
88

@@ -25,34 +25,72 @@ interface Field {
2525
## Rules and groups
2626

2727
```ts
28-
interface RuleType {
28+
type RuleType = {
2929
path?: number[];
3030
id?: string;
3131
field: string;
3232
operator: string;
3333
value: any;
34-
}
34+
};
3535

36-
interface RuleGroupType {
36+
type RuleGroupType = {
3737
path?: number[];
3838
id?: string;
3939
combinator: string;
4040
rules: (RuleType | RuleGroupType)[];
4141
not?: boolean;
42-
}
42+
};
4343

44-
interface RuleGroupTypeIC {
44+
type RuleGroupTypeIC = {
4545
path?: number[];
4646
id?: string;
47-
rules: (RuleType | RuleGroupTypeIC | string)[];
47+
rules: (RuleType | RuleGroupTypeIC | string)[]; // see note below
4848
not?: boolean;
49-
}
49+
};
5050

5151
type RuleGroupTypeAny = RuleGroupType | RuleGroupTypeIC;
5252

53-
type RuleOrGroupArray = (RuleType | RuleGroupType)[] | (RuleType | RuleGroupTypeIC | string)[];
53+
type RuleOrGroupArray = RuleGroupType['rules'] | RuleGroupTypeIC['rules'];
54+
```
55+
56+
:::note
57+
58+
`RuleGroupTypeIC` is greatly simplified here for brevity. In reality:
59+
60+
- Only even-numbered indexes in the `rules` array can be `RuleType | RuleGroupTypeIC`
61+
- Only odd-numbered indexes can be `string`
62+
- The `rules` array length must be odd or zero
63+
64+
For example, the following would be invalid because the first element (the `0`th index, which should be `RuleType | RuleGroupTypeIC`) is a `string` and the second element (the `1`st index, which should be a `string`) is a `RuleType`. Also the length (2) is even.
65+
66+
```ts
67+
const ruleGroup: RuleGroupTypeIC = {
68+
rules: ['and', { field: 'firstName', operator: '=', value: 'Steve' }],
69+
};
5470
```
5571

72+
Either removing the first element or inserting another rule before it will resolve the issue:
73+
74+
```ts
75+
const ruleGroupFixed1: RuleGroupTypeIC = {
76+
rules: [{ field: 'firstName', operator: '=', value: 'Steve' }],
77+
};
78+
```
79+
80+
or
81+
82+
```ts
83+
const ruleGroupFixed2: RuleGroupTypeIC = {
84+
rules: [
85+
{ field: 'lastName', operator: '=', value: 'Vai' },
86+
'and',
87+
{ field: 'firstName', operator: '=', value: 'Steve' },
88+
],
89+
};
90+
```
91+
92+
:::
93+
5694
## Export
5795

5896
```ts

0 commit comments

Comments
 (0)