User-friendly query builder for React
User-friendly query builder for React
Inspired by jQuery QueryBuilder. Using awesome UI frameworks for widgets: Ant Design, Material-UI, Bootstrap. Now Fluent UI is also supported!
See live demo
React-Awesome-Query-Builder Bot will help you understand this repository better. You can ask for code examples, installation guide, debugging help and much more.
From v6 library is divided into packages:
@react-awesome-query-builder/core - has core functionality to import/export/store query, provides utils@react-awesome-query-builder/ui - has core React components like <Query> <Builder> and CSS, provides config with basic (vanilla) widgets@react-awesome-query-builder/antd - provides config with Ant Design widgets@react-awesome-query-builder/mui - provides config with MUI widgets@react-awesome-query-builder/material - provides config with Material-UI v4 widgets (deprecated)@react-awesome-query-builder/bootstrap - provides config with Bootstrap widgets@react-awesome-query-builder/fluent - provides config with Fluent UI widgetsgraph LR;
core-->ui;
core-->sql((sql));
ui-->antd;
ui-->mui;
ui-->material;
ui-->bootstrap;
ui-->fluent;ui re-exports from core, other packages re-export from ui.
For using this library on frontend you need to install and use only ui (for basic widgets) or one of framework-specific packages (antd / mui / bootstrap / fluent).
For using this library on server-side (Node.js) you need only core.
This is useful if you want to pass query value from frontend to backend in JSON format and perform export eg. to SQL on server-side for security reasons.
Example of installation if you use MUI:
npm i @react-awesome-query-builder/mui --saveNote: We use pnpm. If you want to clone this project and run scripts, please install pnpm:
npm install -g pnpmSee basic usage for minimum code example.
See API and config for documentation.
pnpm start - demo app with hot reload of demo code and local library code, uses TS, uses complex config to demonstrate anvanced usage, uses all supported UI frameworks.pnpm sandbox-ts - simple demo app, built with Vite, uses TS, uses MUI widgets.pnpm sandbox-js - simplest demo app, built with Vite, not uses TS, uses vanilla widgets.pnpm sandbox-next - advanced demo app with server side, built with Next.js, uses TS, uses MUI widgets, has API to save/load query value and query config from storage.……<Query />Props:
{...config} - destructured CONFIGvalue - query value in internal Immutable formatonChange - callback called when query value changes. Params: value (in Immutable format), config, actionMeta (details about action which led to the change, see ActionMeta in index.d.ts), actions (you can use to run actions programmatically, see Actions in index.d.ts).onInit - callback called before initial render, has same arguments as onChange (but actionMeta is undefined)renderBuilder - function to render query builder itself. Takes 1 param props you need to pass into <Builder {...props} />.Notes:
useCallback for onChange and renderBuilder for performance reason<Dialog /> or <Popover />, please:disableEnforceFocus={true} for dialog or popver.MuiPopover-root, .MuiDialog-root { z-index: 900 !important; } (or 1000 for AntDesign v3)<Panel />, please:.ms-Layer.ms-Layer--fixed.root-119 { z-index: 900 !important; }props arg in renderBuilder have actions and dispatch you can use to run actions programmaticallyActions interface in index.d.ts. See runActions() in examples as a demonstration of calling actions programmatically.<Builder />Render this component only inside Query.renderBuilder() like in example above:
renderBuilder = (props) => (
</div>
)Wrapping <Builder /> in div.query-builder is necessary for drag-n-drop support.
Optionally you can add class .qb-lite to it for showing action buttons (like delete rule/group, add, etc.) only on hover, which will look cleaner.
Wrapping in div.query-builder-container is necessary for correct drag-n-drop support if you put query builder inside scrollable block.
UtilsgetTree Utils.getTree (immutableValue, light = true, children1AsArray = true) -> Object
Convert query value from internal Immutable format to JS object.
You can use it to save value on backend in onChange callback of <Query>.
Tip: Use light = false in case if you want to store query value in your state in JS format and pass it as value of <Query> after applying loadTree() (which is not recommended because of double conversion). See issue #190
loadTree Utils.loadTree (jsValue) -> Immutable
Convert query value from JS format to internal Immutable format.
You can use it to load saved value from backend and pass as value prop to <Query>.
isValidTree Utils.isValidTree (immutableValue, config) -> Boolean
If showErrorMessage in config.settings is true, use this method to check if query has validation errors (presented in UI with red text color under the rule).
Note that incomplete rules or empty groups are not counted as validation errors for this function.
If showErrorMessage is false, this function will always return true.
validateTree Utils.validateTree (immutableValue, config, options?) -> Array
Validates immutable query value to check it corresponds to the config and has no parts that are invalid or incomplete.
Returns array of errors grouped by item in tree.
Each array element is { itemStr, itemPositionStr, errors, path } (see type ValidationItemErrors).
To present item for user you can use itemStr (string representation of rule eg. Number > 55) and itemPositionStr (eg. Rule #4 (index path: 1, 2)).
Also you can use path to get raw item data with Utils.TreeUtils.getItemByPath(tree, path) (advanced).
errors is an array of objects { str, key, args, side, delta } (see type ValidationError).
str is an error message translated with i18next.t(key, args) (namespace is raqbvalidation).
side can be one of rhs or lhs.
delta can be 0 or 1 for between operator.
You can override/extend translations with:
Utils.i18n.addResources("en", "raqbvalidation", { ...yourTranslations })
See default validation translations.
See i18n for validation.
sanitizeTree Utils.sanitizeTree (immutableValue, config, options?) -> { fixedTree, fixedErrors, nonFixedErrors }
Validates and modifies immutable query value to ensure it corresponds to the config and has no parts that are invalid or incomplete.
Invalid rules (eg. if field is not found in config) will always be deleted.
Invalid values (eg. value > max or < min, value not passing validateValue() in field config) will be either:
showErrorMessage in config.settings is falseNo open issues yet, or sync has not completed.