Git Product home page Git Product logo

mui-querybuilder's Introduction

MUI-QueryBuilder

Query builder for React applications based on Material-UI.

npm package npm downloads

Installation

MUI-QueryBuilder is available as an npm package.

yarn add --exact mui-querybuilder \
    @date-io/[email protected] \
    @date-io/[email protected] \
    @material-ui/core \
    @material-ui/icons \
    @material-ui/lab \
    @material-ui/pickers \
    date-fns \
    prop-types

Usage

Here is a quick example to get you started:

import MuiQueryBuilder from "mui-querybuilder";

import React, { useState } from "react";
import ReactDOM from "react-dom";

const filters = [
    {
        label: "Article",
        options: [
            {
                label: "Title",
                value: "title",
                type: "text",
            },
            {
                label: "URL",
                value: "url",
                type: "text",
            },
        ],
    },
];

function App() {
    const [query, setQuery] = useState({
        combinator: "and",
        rules: [
            {
                field: "title",
                operator: "contains",
                value: "France",
            },
        ],
    });
    return (
        <MuiQueryBuilder
            filters={filters}
            query={query}
            onChange={(query, valid) => {
                setQuery(query);
            }}
        />
    );
}

ReactDOM.render(<App />, document.querySelector('#app'));

Examples

Check out some examples in here!

API

Types

  • date: renders a date picker, date only.
  • integer: renders a text field that only accepts numbers.
  • number: renders a text field that only accepts fractional numbers.
  • multiselect: renders an autocomplete input for multiple items.
  • radio: renders radio buttons with true and false values.
  • select: renders an autocomplete input for a single item.
  • switch: renders an on/off switch input.
  • text: renders a text field.

Filters

The filters object is a list of grouped objects with label (string) and options (list) properties. Each option is an object with label (string), value (string), and type (string). The label can be anything, but the value must be an unique key, used by each field in a ruleset. In case an option's type is select or multiselect, it will require a nested options (list) property with label & value items.

Operators

In order to relate operators to their corresponding types, we rely on this data structure. Each operator must have a label (string), unique value (string), and the types (list) it supports.

Query

The query object is a recursive data structure composed of combinator (string) and rules (list) properties. Each rule is an object with field (string), operator (string), and value (anything, depending on the field's type). In case the rule contains a combinator property, it's considered a nested group.

Properties

Property Type Default Description
customOperators object {} Additional operators to be used by the query builder.
debug bool false Dev mode helper that renders the generated query directly in the screen.
filters array [] The filters the query builder framework will rely to create rules.
maxLevels number 1 The max nesting level, with 0 meaning no nesting at all.
operators array [...operators] The operators to be used, in case you want to change or translate the default ones.
onChange func null Function (query: object, valid: bool) => void with the current query and its validity (true if all rules have values).
query object { combinator: "and", rules: [{ field: null, operator: null, value: null }] } The initial query to be rendered.
sortFilters bool true Option to disable sorting filters within their groups.

Utility functions

import MuiQueryBuilder from "mui-querybuilder";

// Formats a query by deleting IDs from all nodes.
const formattedQuery = MuiQueryBuilder.formatQuery(query);

// Verifies if a query is valid, i.e. all rules and nested groups are filled.
const valid = MuiQueryBuilder.isQueryValid(query);

License

This project is licensed under the terms of the MIT license.

mui-querybuilder's People

Contributors

tiagofernandez avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.