Has 7 sections:
{conjunctions, operators, widgets, types, funcs, settings, fields}
Each section is described below.
Usually it’s enough to just reuse basic config, provide your own fields and maybe change some settings.
Optionally you can override some options in basic config or add your own types/widgets/operators (or even conjunctions like XOR or NOR).
There are functions for building query string: formatConj
, formatValue
, formatOp
, formatField
, formatFunc
which are used for QbUtils.queryString()
.
They have common param isForDisplay
- false by default, true will be used for QbUtils.queryString(immutableTree, config, true)
(see 3rd param true).
Also there are similar mongoConj
, mongoFormatOp
, mongoFormatValue
, mongoFunc
for building MongoDb query with QbUtils.mongodbFormat()
.
And sqlFormatConj
, sqlFormatOp
, sqlFormatValue
, sqlFormatReverse
, sqlFunc
for building SQL where query with QbUtils.sqlFormat()
.
💡
|
Example: demo config
|
import {BasicConfig} from 'react-awesome-query-builder';
const {
conjunctions: {
AND,
OR
},
operators: {
equal,
not_equal,
less,
less_or_equal,
greater,
greater_or_equal,
like,
not_like,
between,
not_between,
range_between, // like `between`, but for slider
range_not_between,
is_empty,
is_not_empty,
select_equals, // like `equal`, but for select
select_not_equals,
select_any_in,
select_not_any_in,
multiselect_equals, // like `equal`, but for multiselect
multiselect_not_equals,
proximity, // complex operator with options
},
widgets: {
text,
number,
slider,
rangeslider,
select,
multiselect,
date,
time,
datetime,
boolean,
field, // to compare field with another field of same type
func, // to compare field with result of function
},
types: {
text,
number,
date,
time,
datetime,
select,
multiselect,
boolean,
},
settings
} = BasicConfig;
const myConfig = {
...BasicConfig, // reuse basic config
fields: {
stock: {
label: 'In stock',
type: 'boolean',
},
// ... my other fields
}
};
{
// simple
qty: {
type: 'number',
label: 'Quantity',
},
// complex
user: {
type: '!struct', // special keyword for comlex fields
label: 'User',
subfields: {
// subfields of complex field
name: {
type: 'text',
label: 'Name',
label2: 'User name', //optional, see below
},
},
},
...
}
key | required | default | meaning |
---|---|---|---|
|
+ |
One of types described in config.types or |
|
|
+ for |
Config for subfields of complex field (multiple nesting is supported) |
|
|
+ |
Label to be displayed in field list |
|
|
Can be optionally specified for nested fields. |
||
|
Optional tooltip to be displayed in field list by hovering on item |
||
|
+ for |
List of values for Select widget. |
|
|
- for |
false |
If true, user can provide own options in multiselect, otherwise they will be limited to |
|
Default value |
||
|
Settings for widgets, will be passed as props. Example: |
||
|
See usecase at |
||
|
You can override config of corresponding type (see below at section config.types) |
||
|
Shorthand for |
||
|
Can exclude some operators, eg. |
||
|
If comparing with funcs is enabled for this field ( |
||
|
false |
If true, field will appear only at right side (when you compare field with another field) |
|
|
false |
If true, field will appear only at left side |
import en_US from 'antd/lib/locale-provider/en_US';
import {Widgets} from 'react-awesome-query-builder';
const { FieldCascader, FieldDropdown } = Widgets;
{
valueSourcesInfo: {
value: {
label: "Value"
},
field: {
label: "Field",
widget: "field",
},
func: {
label: "Function",
widget: "func",
}
},
locale: {
short: 'ru',
full: 'ru-RU',
antd: ru_RU,
},
renderField: (props) => <FieldCascader {...props} />,
renderOperator: (props) => <FieldDropdown {...props} />,
renderFunc: (props) => <FieldDropdown {...props} />,
canReorder: true,
canRegroup: true,
hideConjForOne: true,
maxNesting: 10,
showLabels: false,
showNot: true,
setOpOnChangeField: ['keep', 'default'],
customFieldSelectProps: {
showSearch: true
},
...
}
Behaviour settings:
key | default | meaning |
---|---|---|
|
|
By default fields can be compared with values. |
|
true |
Activate reordering support for rules and groups of rules? |
|
true |
Allow move rules (or groups) in/out groups during reorder? |
|
true |
Show |
|
Max nesting for rule groups. |
|
|
false |
Not allow to add/delete rules or groups, but allow change |
|
|
Strategies for selecting operator for new field (used by order until success): |
|
false |
Clear value on field change? false - if prev & next fields have same type (widget), keep |
|
false |
Clear value on operator change? |
|
false |
Leave empty group after deletion or add 1 clean rule immediately? |
|
For |
Render settings:
key | default | meaning |
---|---|---|
|
|
Size of AntDesign components - |
|
|
Render fields list |
|
|
Render operators list |
|
|
Render functions list |
|
false |
Show labels above all fields? |
|
true |
Don’t show conjunctions switcher for only 1 rule? |
|
100 |
To shorten long labels of fields/values (by length, i.e. number of chars) |
|
|
Placement of antdesign’s dropdown pop-up menu |
|
false |
How to render conjunctions switcher? true - use |
|
|
You can pass props to |
|
|
You can change the position of the group actions to the following: |
Other settings:
key | default | meaning |
---|---|---|
|
en |
Locale used for AntDesign widgets |
|
Function for formatting query string, used to format rule with reverse operator which haven’t |
|
|
Function for formatting query string, used to format field |
|
|
|
Separaor for struct fields. Also used for formatting |
|
|
Separaor for struct fields to show at field’s select tooltip. |
Localization:
key | default |
---|---|
|
Value |
|
Value |
|
Field |
|
Operator |
|
Function |
|
Select field |
|
Select function |
|
Select operator |
|
|
|
|
|
Add group |
|
Add rule |
|
Not |
|
Select value source |
|
If you want to ask confirmation of removing non-empty rule/group, add these options. |
|
Are you sure delete this rule? |
|
Yes |
|
|
|
Are you sure delete this group? |
|
Yes |
|
|
{
AND: {
label: 'And',
formatConj: (children, _conj, not) => ( (not ? 'NOT ' : '') + '(' + children.join(' || ') + ')' ),
reversedConj: 'OR',
mongoConj: '$and',
},
OR: {...},
}
where AND
and OR
- available conjuctions (logical operators). You can add NOR
if you want.
key | required | meaning |
---|---|---|
|
+ |
Label to be displayed in conjunctions swicther |
|
+ |
Function for formatting query, used to join rules into group with conjunction. |
|
+ for MongoDB format |
Name of logical operator for MongoDb |
|
+ for SQL format |
See |
|
Opposite logical operator. |
{
equal: {
label: 'equals',
reversedOp: 'not_equal',
labelForFormat: '==',
cardinality: 1,
isUnary: false,
formatOp: (field, _op, value, _valueSrc, _valueType, opDef) => `${field} ${opDef.labelForFormat} ${value}`,
mongoFormatOp: (field, op, value) => ({ [field]: { '$eq': value } }),
},
..
}
key | required | default | meaning |
---|---|---|---|
|
+ |
Label to be displayed in operators select component |
|
|
+ |
Opposite operator. |
|
|
false |
true for |
|
|
1 |
Number of right operands (1 for binary, 2 for |
|
|
+ |
Function for formatting query string, used to join operands into rule. |
|
|
If |
||
|
+ for MongoDB format |
Function for formatting MongoDb expression, used to join operands into rule. |
|
|
+ for SQL format |
Operator name in SQL |
|
|
- for SQL format |
Function for advanced formatting SQL WHERE query if just |
|
|
+ for |
Labels to be displayed on top of 2 values widgets if |
|
|
+ for |
Labels to be displayed before each 2 values widgets |
|
|
Special for |
||
|
false |
Special for |
ℹ️
|
There is also special import {Operators: {ProximityOperator}} from 'react-awesome-query-builder'; See |
import {Widgets} from 'react-awesome-query-builder';
const {
TextWidget,
NumberWidget,
SelectWidget,
MultiSelectWidget,
DateWidget,
BooleanWidget,
TimeWidget,
DateTimeWidget,
ValueFieldWidget,
FuncWidget
} = Widgets;
{
text: {
type: 'text',
valueSrc: 'value',
factory: (props) => <TextWidget {...props} />,
formatValue: (val, _fieldDef, _wgtDef, isForDisplay) => (isForDisplay ? '"' + val + '"' : JSON.stringify(val)),
mongoFormatValue: (val, _fieldDef, _wgtDef) => (val),
validateValue: (val, _fieldDef) => (val.length < 5),
// Options:
valueLabel: "Text",
valuePlaceholder: "Enter text",
// Custom props (https://ant.design/components/input/):
customProps: {
maxLength: 3
},
},
..
},
key | required | default | meaning |
---|---|---|---|
|
+ |
One of types described in config.types |
|
|
+ |
React function component |
|
|
+ |
Function for formatting widget’s value in query string. |
|
|
- for MongoDB format |
v ⇒ v |
Function for formatting widget’s value in MongoDb query. |
|
- for SQL format |
|
Function for formatting widget’s value in SQL WHERE query. |
|
Function to validate entered value. |
||
|
|
Common option, text to be placed on top of widget if |
|
|
|
Common option, placeholder text to be shown in widget for empty value |
|
|
|
Option for |
|
|
|
Option for |
|
|
|
Option for |
|
|
Option for |
||
|
Option for |
||
|
You can pass any props directly to widget with |
ℹ️
|
There is special field widget, rendered by <ValueFieldWidget> .It can be used to compare field with another field of same type. To enable this feature set valueSources of type to ['value', 'field''] (see below in config.types).
|
ℹ️
|
There is special func widget, rendered by <FuncWidget> .It can be used to compare field with result of function (see config.funcs). To enable this feature set valueSources of type to ['value', 'func''] (see below in config.types).
|
{
time: {
valueSources: ['value', 'field', 'func'],
defaultOperator: 'equal',
widgets: {
time: {
operators: ['equal', 'between'],
widgetProps: {
valuePlaceholder: "Time",
timeFormat: 'h:mm:ss A',
use12Hours: true,
},
opProps: {
between: {
valueLabels: ['Time from', 'Time to'],
},
},
},
},
},
..
}
key | required | default | meaning |
---|---|---|---|
|
keys of |
Array with values |
|
|
If specified, it will be auto selected when user selects field |
||
|
+ |
Available widgets for current type and their config. |
|
|
List of operators for widget, described in config.operators |
||
|
Can be used to override config of corresponding widget specified in config.widgets. Example: |
||
|
Can be used to override config of operator for widget. Example: |
{
lower: {
label: 'Lowercase',
sqlFunc: 'LOWER',
mongoFunc: '$toLower',
returnType: 'text',
args: {
str: {
type: 'text',
valueSources: ['value', 'field'],
}
}
},
..
}
key | required | default | meaning |
---|---|---|---|
|
+ |
One of types described in config.types |
|
|
same as func key |
Label to be displayed in functions list |
|
|
Example: for |
Function for formatting func expression in query rule. |
|
|
- for SQL format |
same as func key |
Func name in SQL |
|
- for SQL format |
Can be used instead of |
|
|
- for MongoDB format |
same as func key |
Func name in Mongo |
|
false |
||
|
- for MongoDB format |
Can be used instead of |
|
|
Arguments of function. Config is almost same as for simple fields |
||
|
arg’s key |
Label to be displayed in arg’s label or placeholder (if |
|
|
+ |
One of types described in config.types |
|
|
keys of |
Array with values |
|
|
Default value |
||
|
+ for |
List of values for Select widget. |
|
|
Settings for widgets, will be passed as props. Example: |
||
|
false |
Last args can be optional |
|
|
|
Can render custom function brackets in UI (or not render). |
|
|
|
Can render custom arguments separators in UI (other than |