npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

reactquerybuilder

v1.0.4

Published

react querybuilder component

Downloads

5

Readme

react-querybuilder

Credits

This component was inspired by prior work from:

Getting Started

Screenshot

npm install reactquerybuilder --save

Demo

Open /node_modules/reactquerybuilder/dist/demo/index.html in your browser.

Usage

import {QueryBuilder} from 'reactquerybuilder';

const fields = [
    {name: 'firstName', label: 'First Name'},
    {name: 'lastName', label: 'Last Name'},
    {name: 'age', label: 'Age'}
];
const defaultValue =  {
    "combinator": "or",
    "not":false,
    "rules": [
        {
            "field": "firstName",
            "operator": "=",
            "value": "2222"
        },
        {
            "combinator": "and",
            "not": true,
            "rules": [
                {
                    "field": "age",
                    "operator": "=",
                    "value": 77
                }
            ]
        }
    ]
};
const dom = <QueryBuilder fields={fields}
                          defaultValue={defaultValue}
                          onQueryChange={logQuery}/>


function logQuery(query) {
    console.log(query);
}

API

<QueryBuilder /> is the only top-level component exposed from this library. It supports the following properties:

fields (Required)

[ {name:String, label:String} ]

The array of fields that should be used. Each field should be an object with {name:String, label:String} |

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: '>='},
]

defaultValue (Optional)

{
    "combinator": "or",
    "not":false,
    "rules": [
        {
            "field": "firstName",
            "operator": "=",
            "value": "2222"
        },
        {
            "combinator": "and",
            "not": true,
            "rules": [
                {
                    "field": "age",
                    "operator": "=",
                    "value": 77
                }
            ]
        }
    ]
};

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, //returns ReactClass
  removeGroupAction: React.PropTypes.func, //returns ReactClass
  addRuleAction: React.PropTypes.func, //returns ReactClass
  removeRuleAction: React.PropTypes.func, //returns ReactClass
  combinatorSelector: React.PropTypes.func, //returns ReactClass
  fieldSelector: React.PropTypes.func, //returns ReactClass
  operatorSelector: React.PropTypes.func, //returns ReactClass
  valueEditor: React.PropTypes.func //returns ReactClass
})

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, //"+Group"
      className: React.PropTypes.string, //css classNames to be applied
      handleOnClick: React.PropTypes.func //callback function to invoke adding a <RuleGroup />
    }
  • removeGroupAction: By default a <button /> is used. The following props are passed:

    {
      label: React.PropTypes.string, //"x"
      className: React.PropTypes.string, //css classNames to be applied
      handleOnClick: React.PropTypes.func //callback function to invoke removing a <RuleGroup />
    }
  • addRuleAction: By default a <button /> is used. The following props are passed:

    {
      label: React.PropTypes.string, //"+Rule"
      className: React.PropTypes.string, //css classNames to be applied
      handleOnClick: React.PropTypes.func //callback function to invoke adding a <Rule />
    }
  • removeRuleAction: By default a <button /> is used. The following props are passed:

    {
      label: React.PropTypes.string, //"x"
      className: React.PropTypes.string, //css classNames to be applied
      handleOnClick: React.PropTypes.func //callback function to invoke removing a <Rule />
    }
  • combinatorSelector: By default a <select /> is used. The following props are passed:

    {
      options: React.PropTypes.array.isRequired, //same as 'combinators' passed into QueryBuilder
      value: React.PropTypes.string, //selected combinator from the existing query representation, if any
      className: React.PropTypes.string, //css classNames to be applied
      handleOnChange: React.PropTypes.func //callback function to update query representation
    }
  • fieldSelector: By default a <select /> is used. The following props are passed:

    {
      options: React.PropTypes.array.isRequired, //same as 'fields' passed into QueryBuilder
      value: React.PropTypes.string, //selected field from the existing query representation, if any
      className: React.PropTypes.string, //css classNames to be applied
      handleOnChange: React.PropTypes.func //callback function to update query representation
    }
  • operatorSelector: By default a <select /> is used. The following props are passed:

    {
      options: React.PropTypes.array.isRequired, //return value of getOperators(field)
      value: React.PropTypes.string, //selected operator from the existing query representation, if any
      className: React.PropTypes.string, //css classNames to be applied
      handleOnChange: React.PropTypes.func //callback function to update query representation
    }
  • valueEditor: By default a <input type="text" /> is used. The following props are passed:

    {
      field: React.PropTypes.string, //field name corresponding to this Rule
      operator: React.PropTypes.string, //operator name corresponding to this Rule
      value: React.PropTypes.string, //value from the existing query representation, if any
      handleOnChange: React.PropTypes.func //callback function to update the query representation
    }

getOperators (Optional)

function(field):[]

This is a callback function invoked to get the list of allowed operators for the given field

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": true,
  "rules": [
    {
      "field": "firstName",
      "operator": "null",
      "value": ""
    },
    {
      "field": "lastName",
      "operator": "null",
      "value": ""
    },
    {
      "combinator": "and",
      "not": false,
      "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, // Root <div> element

    ruleGroup:String, // <div> containing the RuleGroup
    combinators:String, // <select> control for combinators
    addRule:String, // <button> to add a Rule
    addGroup:String, // <button> to add a RuleGroup
    removeGroup:String, // <button> to remove a RuleGroup

    rule:String, // <div> containing the Rule
    fields:String, // <select> control for fields
    operators:String, // <select> control for operators
    value:String, // <input> for the field value
    removeRule:String // <button> to remove a Rule

}

Build

# build demo
npm run demo
# build
npm run build
# dev
npm start