clonardo / react-query-filter

[WIP] A query filter builder component in React.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

react-query-filters

Now with Ant Design components

Heads up: This is still a work in progress. A lot of breaking change might happen and a lot of features are still missing. PR's are very welcome but please open an issue first describing what you think could be better.

Set of utilities to implement a Query Builder for filters.

This library ships a useQueryFilters hook that you can use to implement an UI on top of it.

The useQueryFilters will track state changes and enable you do build your query filter builder using the styles you want.

Features

  • Fully headless: Bring your own UI
  • Conditional operations based on field type
    • Strings
    • Numbers
    • Boolean
    • Finish migrating to AntD
    • Add Formik
    • Dates
    • Single Select
    • Multiple Select
  • Conditional value based on operation type
    • Value is always undefined if operation type is is-empty or is-not-empty
  • AND & OR logic gates supported
  • Support for controlled state
  • Support for nested conditions

Signature

type PropertyDescription =
  | StringPropertyDescription
  | NumberPropertyDescription
  | BooleanPropertyDescription;

interface Filter {
  field?: string;
  operation?: string;
  value?: string;
  binding?: 'and' | 'or';
  type?: SupportedFieldType;
}

interface FilterRowProps {
  properties: PropertyDescription[];
  filter: Filter;
  isFirst: boolean;
  onRemove: () => void;
  onChangeBinding: (event: React.ChangeEvent<HTMLSelectElement>) => void;
  onChangeField: (event: React.ChangeEvent<HTMLSelectElement>) => void;
  onChangeOperation: (event: React.ChangeEvent<HTMLSelectElement>) => void;
  onChangeValue: (event: React.ChangeEvent<HTMLInputElement>) => void;
}

const useQueryFilters: (properties: PropertyDescription[]) => {
  filters: Filter[];
  onAddFilter: () => void;
  createFilterRowProps: (index: number) => FilterRowProps;
}

Usage

Code speaks for itself, so here is an example implementing a FilterSelection and FilterRow components using useQueryFilters and chakra-ui.

First, make sure you have the following dependencies in place:

# install chakra and dependencies
yarn add @chakra-ui/react @emotion/react @emotion/styled framer-motion

# install react-query-filters
yarn add react-query-filters
/** FilterSelection.tsx */
import React, { FC } from 'react';
import {
  Button,
  HStack,
  SimpleGrid,
  SlideFade,
  Code,
} from '@chakra-ui/react';
import { useQueryFilters, Filter, PropertyDescription } from 'react-query-filters';
import { FilterRow } from './FilterRow';

interface Props {
  value?: Filter[];
  onChange?: (filters: Filter[]) => void;
  properties: PropertyDescription[];
}

export const FilterSelection: FC<Props> = ({ properties }) => {
  const { filters, onAddFilter, createFilterRowProps } = useQueryFilters(
    properties
  );

  return (
    <SimpleGrid columns={1} spacingY={4}>
      <SimpleGrid columns={1} spacingY={3}>
        {filters.map((_filter, index) => (
          <SlideFade in={true} key={index}>
            <FilterRow {...createFilterRowProps(index)} />
          </SlideFade>
        ))}
      </SimpleGrid>

      <HStack>
        <Button size="sm" onClick={onAddFilter}>
          Add filter
        </Button>
      </HStack>

      <Code padding={4}>
        <pre>{JSON.stringify(filters, null, 2)}</pre>
      </Code>
    </SimpleGrid>
  );
};
/** FilterRow.tsx */
import React, { FC } from 'react';
import {
  CloseButton,
  Text,
  HStack,
  Input,
  Select,
  Tooltip,
} from '@chakra-ui/react';
import { FilterRowProps, useRowUtilities } from 'react-query-filters';

export const FilterRow: FC<FilterRowProps> = ({
  properties,
  filter,
  isFirst,
  onRemove,
  onChangeBinding,
  onChangeField,
  onChangeOperation,
  onChangeValue,
}) => {
  const {
    getFilterOperationsForType,
    shouldRenderValueInputForOperation,
  } = useRowUtilities();

  return (
    <HStack>
      <Tooltip shouldWrapChildren label="Remove Filter" placement="left">
        <CloseButton onClick={onRemove} />
      </Tooltip>

      {isFirst ? (
        <Text fontSize="sm">Where&nbsp;</Text>
      ) : (
        <Select
          size="sm"
          maxWidth="6rem"
          value={filter.binding}
          onChange={onChangeBinding}
        >
          <option value="and">And</option>
          <option value="or">Or</option>
        </Select>
      )}

      <Select
        size="sm"
        value={filter.field}
        onChange={onChangeField}
        placeholder="Field"
      >
        {properties.map((prop, index) => (
          <option value={prop.key} key={index}>
            {prop.label}
          </option>
        ))}
      </Select>

      <Select
        size="sm"
        value={filter.operation}
        onChange={onChangeOperation}
        placeholder="Operation"
      >
        {getFilterOperationsForType(filter.type).map((operation, index) => (
          <option value={operation.value} key={index}>
            {operation.label}
          </option>
        ))}
      </Select>

      {shouldRenderValueInputForOperation(filter.operation) && (
        <Input
          size="sm"
          placeholder="Value"
          value={filter.value ?? ''}
          onChange={onChangeValue}
        />
      )}
    </HStack>
  );
};

This component can the be used like this:

const properties: PropertyDescription[] = [
  {
    label: 'Name',
    key: 'name',
    type: 'string',
    suggestions: ['Artemis', 'Apollo', 'Donna', 'Dhio'],
  },
  {
    label: 'Age',
    key: 'age',
    type: 'number',
    suggestions: [1, 2, 3],
  },
  {
    label: 'Has Owner',
    key: 'has_owner',
    type: 'boolean',
  },
];

<FilterSelection properties={properties} />

License

MIT © Armando Magalhaes

About

[WIP] A query filter builder component in React.

License:MIT License


Languages

Language:TypeScript 92.2%Language:JavaScript 5.8%Language:HTML 2.0%