Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
cpr-query-builder
Advanced tools
A React component for building queries/filters.
The QueryBuilder
component provides a simple and user-friendly interface to build a query (or set of filters) for a data set, based on custom filters and operators.
It is intended to produce Json-Query-Language, but operators can be defined and handled however you want.
npm install cpr-query-builder
Note: not yet published
filterDefs
: (required) an array of objects that define all possible filters that can be applied to the queryhandleQueryChange
: (required) a function that gets called every time the query changes, to be called with one argument: the new queryinitialFilters
: (optional) filters with which to initializeimport QueryBuilder from 'react-query-builder';
let myFilterDefs = [
{
field: 'name',
label: 'Name',
operators: [
{
label: 'Is',
operator: 'eq',
inputType: 'text',
}
],
}
];
function handleQueryChange(query) {
// Do stuff with the query
}
<QueryBuilder filterDefs={myFilterDefs} handleQueryChange={handleQueryChange} />
filterDefs
is an array of objects that define all possible filters that can be applied to the query. Each object in the array has the following fields:
field
: (required) the field on which to filter, will be passed into the query (i.e. 'first_name')label
: (required) the display label for the filter (i.e. 'First Name')operators
: (required) an array of operator objects that are supported on this filter (see operators
below)shouldDisableFilter
: (optional) a function to determine whether the filter should be disabled. When disabled, a filter will not appear in the list of available filters, and any currently applied filters of that type will be "grayed out" in the list and not included in the query. This is useful for indicating that a filter is "incompatible" with something else.Example:
let myFilterDefs = [
{
field: 'first_name',
label: 'First Name',
operators: [/* see section on operators below */],
shouldDisableFilter: () => true,
},
];
operators
, within each filterDef object, is an array of operator objects that describes which operators are supported by the filter and what the input type should look like for that operator.
For example, a first_name
filter might support an operator called "startswith" and use a text input, while a created_on
filter might support an operator called "between" and use a datepicker range input.
Each operator object has (or can have) the following fields:
operator
: (required) the operator value to be passed to the query (i.e. 'in')label
: (required) the display label for the operator, displayed as a radio button option within a filter (i.e. 'is any of')inputType
: (required) the input type to display for that operator. Supported values are 'text' and 'none' (use a defaultValue
, see below)defaultValue
: (optional) the default value for a filter. This is especially useful if you want your operator to have an inputType
of 'none' to have a "hard-coded" filter.shouldApplyFilter
: (optional) a function to determine whether to apply the filter to the query, to be called with one argument: the current user-entered value. This is useful if you don't want to apply, say, filters with empty strings.valueTransformer
: (optional) a function to transform your value before applying to the query, to be called with one argument: the current user-entered value.Example:
let myOperators = [
{
label: 'Starts With',
operator: 'startswith',
inputType: 'text',
shouldApplyFilter: value => value && value.length > 0,
valueTransformer: value => 'foo' + value,
},
{
operator: 'eq',
label: 'Is Empty',
inputType: 'none',
defaultValue: null,
},
];
The query passed to your handleQueryChange
function will be an array of objects, each with three fields: field
, operator
, and value
.
Optionally, you can pass an array of filters to initialize the component. Each filter should have the following fields:
field
: the filtered fieldoperatorIndex
: the index (in operators
of the filtered field's filterDef) of the applied operator. (This has to be an index because a filter can have multiple operators with the same operator
string.)value
: the valueExample:
let myInitialFilters = {
field: 'first_name',
operatorIndex: 0,
value: 'jim',
}
cd react-query-builder
npm install
npm run watch
demo/index.html
FAQs
A React component for building queries/filters.
We found that cpr-query-builder demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.