react-querybuilder

Credits
This component was inspired by prior work from:
Getting Started

npm install react-querybuilder --save
Demo
To run a demo of the react-querybuilder being used, go through the following steps.
npm install
Install npm packagesnpm start
Run a local serverhttp://localhost:8080/
Visit your localhost in your browser
OR
See live Demo.
Usage
import QueryBuilder from 'react-querybuilder';
const fields = [
{ name: 'firstName', label: 'First Name' },
{ name: 'lastName', label: 'Last Name' },
{ name: 'age', label: 'Age' },
{ name: 'address', label: 'Address' },
{ name: 'phone', label: 'Phone' },
{ name: 'email', label: 'Email' },
{ name: 'twitter', label: 'Twitter' },
{ name: 'isDev', label: 'Is a Developer?', value: false }
];
const dom = <QueryBuilder fields={fields} onQueryChange={logQuery} />;
function logQuery(query) {
console.log(query);
}
API
This library exposes a React component, <QueryBuilder />
, and a utility function, formatQuery
. <QueryBuilder />
is the default export, and formatQuery
is exposed as a named export.
QueryBuilder
<QueryBuilder />
supports the following properties:
fields (Required)
[ {name:String, label:String, id:ID} ]
The array of fields that should be used. Each field should be an object with:
{name:String, label:String, id:ID}
|
The id
is optional, if you do not provide an id for a field then the name will be used.
operators (Optional)
[ {name:String, label:String} ]
The array of operators that should be used. The default operators include:
[
{ name: 'null', label: 'Is Null' },
{ name: 'notNull', label: 'Is Not Null' },
{ name: 'in', label: 'In' },
{ name: 'notIn', label: 'Not In' },
{ name: '=', label: '=' },
{ name: '!=', label: '!=' },
{ name: '<', label: '<' },
{ name: '>', label: '>' },
{ name: '<=', label: '<=' },
{ name: '>=', label: '>=' }
];
combinators (Optional)
[ {name:String, label:String} ]
The array of combinators that should be used for RuleGroups. The default set includes:
[{ name: 'and', label: 'AND' }, { name: 'or', label: 'OR' }];
controlElements (Optional)
React.PropTypes.shape({
addGroupAction: React.PropTypes.func,
removeGroupAction: React.PropTypes.func,
addRuleAction: React.PropTypes.func,
removeRuleAction: React.PropTypes.func,
combinatorSelector: React.PropTypes.func,
fieldSelector: React.PropTypes.func,
operatorSelector: React.PropTypes.func,
valueEditor: React.PropTypes.func
notToggle: React.PropTypes.func
});
This is a custom controls object that allows you to override the control elements used. The following control overrides are supported:
addGroupAction
: By default a <button />
is used. The following props are passed:
{
label: React.PropTypes.string,
className: React.PropTypes.string,
handleOnClick: React.PropTypes.func,
rules: React.PropTypes.array,
level: React.PropTypes.number
}
removeGroupAction
: By default a <button />
is used. The following props are passed:
{
label: React.PropTypes.string,
className: React.PropTypes.string,
handleOnClick: React.PropTypes.func,
rules: React.PropTypes.array,
level: React.PropTypes.number
}
addRuleAction
: By default a <button />
is used. The following props are passed:
{
label: React.PropTypes.string,
className: React.PropTypes.string,
handleOnClick: React.PropTypes.func,
rules: React.PropTypes.array,
level: React.PropTypes.number
}
removeRuleAction
: By default a <button />
is used. The following props are passed:
{
label: React.PropTypes.string,
className: React.PropTypes.string,
handleOnClick: React.PropTypes.func,
level: React.PropTypes.number
}
combinatorSelector
: By default a <select />
is used. The following props are passed:
{
options: React.PropTypes.array.isRequired,
value: React.PropTypes.string,
className: React.PropTypes.string,
handleOnChange: React.PropTypes.func,
rules: React.PropTypes.array,
level: React.PropTypes.number
}
fieldSelector
: By default a <select />
is used. The following props are passed:
{
options: React.PropTypes.array.isRequired,
value: React.PropTypes.string,
className: React.PropTypes.string,
handleOnChange: React.PropTypes.func,
level: React.PropTypes.number
}
operatorSelector
: By default a <select />
is used. The following props are passed:
{
field: React.PropTypes.string,
options: React.PropTypes.array.isRequired,
value: React.PropTypes.string,
className: React.PropTypes.string,
handleOnChange: React.PropTypes.func,
level: React.PropTypes.number
}
valueEditor
: By default an <input type="text" />
is used. The following props are passed:
{
field: React.PropTypes.string,
operator: React.PropTypes.string,
value: React.PropTypes.string,
handleOnChange: React.PropTypes.func,
type: React.PropTypes.oneOf(['text', 'select', 'checkbox', 'radio']),
inputType: React.PropTypes.string,
values: React.PropTypes.arrayOf(React.PropTypes.object),
level: React.PropTypes.number,
className: React.PropTypes.string,
}
notToggle
: By default, <label><input type="checkbox" />Not</label>
is used. The following props are passed:
{
checked: React.PropTypes.bool,
handleOnChange: React.PropTypes.func,
title: React.PropTypes.string,
level: React.PropTypes.number,
className: React.PropTypes.string,
}
getOperators (Optional)
function(field):[]
This is a callback function invoked to get the list of allowed operators for the given field.
getValueEditorType (Optional)
function(field, operator):string
This is a callback function invoked to get the type of ValueEditor
for the given field and operator. Allowed values are "text"
(the default), "select"
, "checkbox"
, and "radio"
.
getInputType (Optional)
function(field, operator):string
This is a callback function invoked to get the type
of <input />
for the given field and operator (only applicable when getValueEditorType
returns "text"
or a falsy value). If no function is provided, "text"
is used as the default.
getValues (Optional)
function(field, operator):[]
This is a callback function invoked to get the list of allowed values for the given field and operator (only applicable when getValueEditorType
returns "select"
or "radio"
). If no function is provided, an empty array is used as the default.
onQueryChange (Optional)
function(queryJSON):void
This is a notification that is invoked anytime the query configuration changes. The query is provided as a JSON structure, as shown below:
{
"combinator": "and",
"not": false,
"rules": [
{
"field": "firstName",
"operator": "null",
"value": ""
},
{
"field": "lastName",
"operator": "null",
"value": ""
},
{
"combinator": "and",
"rules": [
{
"field": "age",
"operator": ">",
"value": "30"
}
]
}
]
}
controlClassnames (Optional)
This can be used to assign specific CSS
classes to various controls that are created by the <QueryBuilder />
. This is an object with the following properties:
{
queryBuilder:String,
ruleGroup:String,
combinators:String,
addRule:String,
addGroup:String,
removeGroup:String,
notToggle:String,
rule:String,
fields:String,
operators:String,
value:String,
removeRule:String
}
translations (Optional)
This can be used to override translatable texts applied to various controls that are created by the <QueryBuilder />
. This is an object with the following properties:
{
fields: {
title: "Fields",
},
operators: {
title: "Operators",
},
value: {
title: "Value",
},
removeRule: {
label: "x",
title: "Remove rule",
},
removeGroup: {
label: "x",
title: "Remove group",
},
addRule: {
label: "+Rule",
title: "Add rule",
},
addGroup: {
label: "+Group",
title: "Add group",
},
combinators: {
title: "Combinators",
},
notToggle: {
title: "Invert this group",
}
}
showCombinatorsBetweenRules (Optional)
boolean
Pass true
to show the combinators (and/or) between rules and rule groups instead of at the top of rule groups. This can make some queries easier to understand as it encourages a more natural style of reading.
showNotToggle (Optional)
boolean
Pass true
to show the "Not" toggle switch for each rule group.
formatQuery
formatQuery
formats a given query in either JSON or SQL format. Example:
import { formatQuery } from 'react-querybuilder';
const query = {
id: 'g-b6SQ6WCcup8e37xhydwHE',
rules: [
{
id: 'r-zITQOjVEWlsU1fncraSNn',
field: 'firstName',
value: 'Steve',
operator: '='
},
{
id: 'r-zVx7ARNak3TCZNFHkwMG2',
field: 'lastName',
value: 'Vai',
operator: '='
}
],
combinator: 'and',
not: false
};
console.log(formatQuery(query, 'sql'));
An optional third argument can be passed into formatQuery
if you need to control the way the value portion of the output is processed. (This is only applicable when the format is "sql"
.)
const query = {
id: 'g-J5GsbcFmZ6xOJCLPPKIfE',
rules: [
{
id: 'r-KneYcwIPPHDGSogtKhG4g',
field: 'instrument',
value: ['Guitar', 'Vocals'],
operator: 'in'
},
{
id: 'r-wz6AkZbzSyDYbPk1AxgvO',
field: 'lastName',
value: 'Vai',
operator: '='
}
],
combinator: 'and',
not: false
};
const valueProcessor = (field, operator, value) => {
if (operator === 'in') {
return `(${value.map((v) => `"${v.trim()}"`).join(',')})`;
} else {
return `"${value}"`;
}
};
console.log(formatQuery(query, 'sql', valueProcessor));
Development
Changelog Generation
We are using github-changes to generate the changelog.
To use it:
- tag your commit using semantic versioning
- run
npm run generate-changelog
- enter your github credentials at the prompt
- commit
- push your commit and tags