QueryBuilder
Refer to the TypeScript reference page for information about the types and interfaces referenced below.
The primary export of react-querybuilder
is the <QueryBuilder />
React component.
QueryBuilder
calls the useQueryBuilderSetup
Hook to merge props and context values with defaults, generate update methods, etc. It then renders a context provider over an internal component that calls the useQueryBuilderSchema
Hook to consume the context, prepare the query, and finalize the schema.
Subcomponents
QueryBuilder
renders a RuleGroup
representing the root of the query.
That root RuleGroup
is nested within a <div>
that has the standard queryBuilder
class, any classes added by controlClassnames.queryBuilder
, and data-
properties with "enabled"/"disabled" values indicating whether drag-and-drop or inline combinators (either showCombinatorsBetweenRules
is true
or the query is using independent combinators) are enabled.
Finally, everything is wrapped in <QueryBuilderStateProvider>
and <QueryBuilderContext.Provider>
. The latter inherits any values from ancestor context providers and propogates them down to subcomponents. Props supersede context values.
Props
All QueryBuilder
props are optional, but as stated in the getting started guide, the query builder is really only useful when at least the fields
and onQueryChange
props are implemented.
When you see RuleGroupTypeAny
below (e.g. for query, defaultQuery, and onQueryChange), that means the type must either be RuleGroupType
or RuleGroupTypeIC
. For more information about RuleGroupTypeIC
, see the independent combinators section below.
fields
OptionList<Field> | Record<string, Field>
The array of fields that should be used or an array of option groups containing arrays of fields. (Alternatively, fields
can be an object where the keys correspond to each field name
and the values are the field definitions. If fields
is an object, then the options
array passed to the fieldSelector
component will be sorted alphabetically by the label
property.)
Field objects can also contain custom properties. Each field object will be passed in its entirety to the appropriate OperatorSelector
and ValueEditor
components as the fieldData
prop (see the section on controlElements
).
onQueryChange
(query: RuleGroupTypeAny) => void
This function is invoked with the updated query whenever a change is made from within the component.
query
RuleGroupTypeAny
The query is an object of type RuleGroupType
or RuleGroupTypeIC
. If this prop is provided, <QueryBuilder />
will be a controlled component.
The query
prop follows the same format as the parameter passed to the onQueryChange
callback since they are meant to be used in concert to control the component. See examples here.
defaultQuery
RuleGroupTypeAny
The initial query when <QueryBuilder />
is uncontrolled.
Do not provide both query
and defaultQuery
props. To use <QueryBuilder />
as a controlled component, provide and manage the query
prop in combination with the onQueryChange
callback. Use defaultQuery
(or neither query prop) to render an uncontrolled component. The onQueryChange
callback will be invoked for every update regardless of which prop is used.
If both props are defined, an error will be logged to the console during runtime (in "development" mode only). Errors will also be logged to the console if the query
prop is defined during one render and undefined in the next, or vice versa.
context
any
A "bucket" for passing arbitrary props down to custom components. The default components ignore this prop, but it is passed to each and every component so it's accessible anywhere in the QueryBuilder
component tree.
operators
OptionList<Operator>
The array of operators that should be used. Custom operators must define a label
property and either a name
or value
(value
takes precedence). An arity
property, which can be "unary", "binary", or a number, may also be defined for each operator. If arity
is either "unary" or a number less than 2, the value editor component will not be rendered when that operator is selected.
To build the operator list dynamically depending on a rule's field
property, use getOperators
. The result of getOperators
, if not null
, will supersede the operators
prop.
The default operator list is below.
export const defaultOperators = [
{ name: '=', value: '=', label: '=' } as const,
{ name: '!=', value: '!=', label: '!=' } as const,
{ name: '<', value: '<', label: '<' } as const,
{ name: '>', value: '>', label: '>' } as const,
{ name: '<=', value: '<=', label: '<=' } as const,
{ name: '>=', value: '>=', label: '>=' } as const,
{ name: 'contains', value: 'contains', label: 'contains' } as const,
{ name: 'beginsWith', value: 'beginsWith', label: 'begins with' } as const,
{ name: 'endsWith', value: 'endsWith', label: 'ends with' } as const,
{ name: 'doesNotContain', value: 'doesNotContain', label: 'does not contain' } as const,
{ name: 'doesNotBeginWith', value: 'doesNotBeginWith', label: 'does not begin with' } as const,
{ name: 'doesNotEndWith', value: 'doesNotEndWith', label: 'does not end with' } as const,
{ name: 'null', value: 'null', label: 'is null' } as const,
{ name: 'notNull', value: 'notNull', label: 'is not null' } as const,
{ name: 'in', value: 'in', label: 'in' } as const,
{ name: 'notIn', value: 'notIn', label: 'not in' } as const,
{ name: 'between', value: 'between', label: 'between' } as const,
{ name: 'notBetween', value: 'notBetween', label: 'not between' } as const,
] satisfies DefaultOperator[];
Source: /packages/react-querybuilder/src/defaults.ts#L134-L153
combinators
OptionList
The array of combinators that should be used for RuleGroups. The default combinator list is below.
export const defaultCombinators = [
{ name: 'and', value: 'and', label: 'AND' } as const,
{ name: 'or', value: 'or', label: 'OR' } as const,
] satisfies DefaultCombinator[];
Source: /packages/react-querybuilder/src/defaults.ts#L184-L187
controlClassnames
Partial<Classnames>
This prop can be used to assign custom CSS classes to the various controls rendered by the <QueryBuilder />
component. Each attribute is a Classname
which can be a string
, string[]
, or Record<string, any>
(see documentation for clsx
):
Usage example
In the example below, any "+ Rule" buttons in the query builder will have the "bold" class which might have the associated CSS rule .bold { font-weight: bold; }
.
function App() {
return (
<QueryBuilder controlClassnames={{ addRule: 'bold' }}>
)
}
Property | Classes are applied to... |
---|---|
queryBuilder | ...the outermost <div> element |
ruleGroup | ...each <div> wrapping a group |
header | ...each <div> wrapping a group's header controls |
body | ...each <div> wrapping a group's body elements (child rules/groups) |
combinators | ...each <select> control for combinators |
addRule | ...each <button> that adds a rule |
addGroup | ...each <button> that adds a group |
cloneRule | ...each <button> that clones a rule |
cloneGroup | ...each <button> that clones a group |
removeGroup | ...each <button> that removes a group |
lockRule | ...each <button> that locks/disables a rule |
lockGroup | ...each <button> that locks/disables a group |
notToggle | ...each <label> on a "not" (aka "inversion") toggle |
rule | ...each <div> containing a rule |
fields | ...each <select> control for selecting a field |
operators | ...each <select> control for selecting an operator |
value | ...each <input> for entering a value |
removeRule | ...each <button> that removes a rule |
dragHandle | ...each <span> acting as a drag handle |
valueSource | ...each <select> control for selecting a value source |
controlElements
Partial<Controls>
This object allows you to override the default components.
Usage example
function App() {
return (
<QueryBuilder controlElements={{ valueEditor: CustomValueEditor }}>
)
}
The following control overrides are supported per the Controls
interface:
Property | Type |
---|---|
actionElement | React.ComponentType<ActionProps> |
addGroupAction | React.ComponentType<ActionWithRulesAndAddersProps> |
addRuleAction | React.ComponentType<ActionWithRulesAndAddersProps> |
cloneGroupAction | React.ComponentType<ActionWithRulesProps> |
cloneRuleAction | React.ComponentType<ActionProps> |
combinatorSelector | React.ComponentType<CombinatorSelectorProps> |
dragHandle | React.ForwardRefExoticComponent<DragHandleProps & React.RefAttributes<HTMLSpanElement>> |
fieldSelector | React.ComponentType<FieldSelectorProps> |
inlineCombinator | React.ComponentType<InlineCombinatorProps> |
lockGroupAction | React.ComponentType<ActionWithRulesProps> |
lockRuleAction | React.ComponentType<ActionProps> |
notToggle | React.ComponentType<NotToggleProps> |
operatorSelector | React.ComponentType<OperatorSelectorProps> |
removeGroupAction | React.ComponentType<ActionWithRulesProps> |
removeRuleAction | React.ComponentType<ActionProps> |
rule | React.ComponentType<RuleProps> |
ruleGroup | React.ComponentType<RuleGroupProps> |
shiftActions | React.ComponentType<ShiftActionsProps> |
valueEditor | React.ComponentType<ValueEditorProps> |
valueSelector | React.ComponentType<ValueSelectorProps> |
valueSourceSelector | React.ComponentType<ValueSourceSelectorProps> |
actionElement
The component for all button-type controls. Default is ActionElement
. Receives props per the ActionProps
, ActionWithRulesProps
, or ActionWithRulesAndAddersProps
interface depending on the control, which can be any of the following:
addGroupAction
addRuleAction
cloneGroupAction
cloneRuleAction
lockGroupAction
lockRuleAction
removeGroupAction
removeRuleAction
For example, this:
<QueryBuilder controlElements={{ actionElement: MyAwesomeButton }} />
...is equivalent to this:
<QueryBuilder
controlElements={{
addGroupAction: MyAwesomeButton
addRuleAction: MyAwesomeButton
cloneGroupAction: MyAwesomeButton
cloneRuleAction: MyAwesomeButton
lockGroupAction: MyAwesomeButton
lockRuleAction: MyAwesomeButton
removeGroupAction: MyAwesomeButton
removeRuleAction: MyAwesomeButton
}}
/>
addGroupAction
Adds a sub-group to the current group. Default is ActionElement
. Receives the following props per the ActionWithRulesAndAddersProps
interface:
Prop | Type | Description |
---|---|---|
label | ReactNode | translations.addGroup.label , e.g. "+ Group" |
title | string | translations.addGroup.title , e.g. "Add group" |
className | string | CSS classNames to be applied |
handleOnClick | (e: React.MouseEvent, context?: any) => void | Adds a new sub-group to this group |
rules | RuleOrGroupArray | The rules array for this group |
ruleOrGroup | RuleGroupTypeAny | This group |
level | number | The level of this group |
context | any | Container for custom props that are passed to all components |
validation | boolean | ValidationResult | Validation result of this group |
disabled | boolean | Whether this group is disabled/locked |
path | Path | Path of this group |
schema | Schema | Query schema |
addRuleAction
Adds a rule to the current group. Default is ActionElement
. Receives the following props per the ActionWithRulesAndAddersProps
interface:
Prop | Type | Description |
---|---|---|
label | ReactNode | translations.addRule.label , e.g. "+ Rule" |
title | string | translations.addRule.title , e.g. "Add rule" |
className | string | CSS classNames to be applied |
handleOnClick | (e: React.MouseEvent, context?: any) => void | Adds a new rule to this rule |
rules | RuleOrGroupArray | The rules array for this rule |
ruleOrGroup | RuleGroupTypeAny | This rule |
level | number | The level of this rule |
context | any | Container for custom props that are passed to all components |
validation | boolean | ValidationResult | Validation result of this rule |
disabled | boolean | Whether this rule is disabled/locked |
path | Path | Path of this rule |
schema | Schema | Query schema |
cloneGroupAction
Clones the current group. Default is ActionElement
. Receives the following props per the ActionWithRulesProps
interface:
Prop | Type | Description |
---|---|---|
label | ReactNode | translations.cloneRuleGroup.label , e.g. "⧉" |
title | string | translations.cloneRuleGroup.title , e.g. "Clone group" |
className | string | CSS classNames to be applied |
handleOnClick | (e: React.MouseEvent) => void | Clones this group |
rules | RuleOrGroupArray | The rules array for this group |
ruleOrGroup | RuleGroupTypeAny | This group |
level | number | The level of this group |
context | any | Container for custom props that are passed to all components |
validation | boolean | ValidationResult | Validation result of this group |
disabled | boolean | Whether this group is disabled/locked |
path | Path | Path of this group |
schema | Schema | Query schema |
cloneRuleAction
Clones the current rule. Default is ActionElement
. Receives the following props per the ActionProps
interface:
Prop | Type | Description |
---|---|---|
label | string | translations.cloneRule.label , e.g. "⧉" |
title | string | translations.cloneRule.title , e.g. "Clone rule" |
className | string | CSS classNames to be applied |
handleOnClick | (e: React.MouseEvent) => void | Clones the rule |
ruleOrGroup | RuleType | This rule |
level | number | The level of this rule |
context | any | Container for custom props that are passed to all components |
validation | boolean | ValidationResult | Validation result of this rule |
disabled | boolean | Whether this rule is disabled/locked |
path | Path | Path of this rule |
schema | Schema | Query schema |
combinatorSelector
Selects the combinator
property for the current group, or the current independent combinator value. Default is ValueSelector
. Receives the following props per the CombinatorSelectorProps
interface:
Prop | Type | Description |
---|---|---|
options | OptionList | Same as combinators prop passed into QueryBuilder |
value | string | Selected combinator from the existing query representation, if any |
className | string | CSS classNames to be applied |
handleOnChange | (value: any) => void | Updates the group's combinator |
rules | RuleOrGroupArray | The rules array for this group |
title | string | translations.combinators.title , e.g. "Combinators" |
level | number | The level of this group |
context | any | Container for custom props that are passed to all components |
validation | boolean | ValidationResult | Validation result of this group |
disabled | boolean | Whether this group is disabled/locked |
path | Path | Path of this group |
schema | Schema | Query schema |