@@ -25,38 +25,94 @@ The `useQueryFilters` will track state changes and enable you do build your quer
2525 - [ ] Multiple Select
2626 - [x] Conditional value based on operation type
2727 - Condition value is always ` undefined ` if operation type is ` is-empty ` or ` is-not-empty `
28- - [x] ` AND ` & ` OR ` logic gates supported
28+ - [x] ` AND ` & ` OR ` logic gates supported, implemented as the ` Binding ` enum
2929 - [ ] Support for controlled state
3030 - [ ] Support for nested conditions
3131
32- ## Signature
32+ ## Types
3333
3434``` tsx
35+ interface StringPropertyDescription {
36+ label: string ;
37+ key: string ;
38+ type: ' string' ;
39+ suggestions? : string [];
40+ }
41+
42+ interface NumberPropertyDescription {
43+ label: string ;
44+ key: string ;
45+ type: ' number' ;
46+ suggestions? : number [];
47+ }
48+
49+ interface BooleanPropertyDescription {
50+ label: string ;
51+ key: string ;
52+ type: ' boolean' ;
53+ }
54+
3555type PropertyDescription =
3656 | StringPropertyDescription
3757 | NumberPropertyDescription
3858 | BooleanPropertyDescription ;
59+
60+ export enum Binding {
61+ AND = ' AND' ,
62+ OR = ' OR' ,
63+ }
3964
40- interface Filter {
65+ export enum OperationType {
66+ CONTAINS = ' CONTAINS' ,
67+ DOES_NOT_CONTAIN = ' DOES_NOT_CONTAIN' ,
68+ IS = ' IS' ,
69+ IS_NOT = ' IS_NOT' ,
70+ IS_EMPTY = ' IS_EMPTY' ,
71+ IS_NOT_EMPTY = ' IS_NOT_EMPTY' ,
72+ EQUAL = ' EQUAL' ,
73+ NOT_EQUAL = ' NOT_EQUAL' ,
74+ BIGGER_THAN = ' BIGGER_THAN' ,
75+ LOWER_THAN = ' LOWER_THAN' ,
76+ BIGGER_OR_EQUAL_THAN = ' BIGGER_OR_EQUAL_THAN' ,
77+ LOWER_OR_EQUAL_THAN = ' LOWER_OR_EQUAL_THAN' ,
78+ }
79+
80+ export interface Filter {
4181 field? : string ;
42- operation? : string ;
82+ operation? : OperationType ;
4383 value? : string ;
44- binding? : ' and ' | ' or ' ;
45- type? : SupportedFieldType ;
84+ binding? : Binding ;
85+ type? : PropertyDescription [ ' type ' ] ;
4686}
4787
48- interface FilterRowProps {
49- properties: PropertyDescription [];
88+ export interface SelectOption <T > {
89+ label: string ;
90+ value: T ;
91+ }
92+
93+ export interface FilterRowProps {
5094 filter: Filter ;
51- isFirst: boolean ;
95+ fields: SelectOption <string >[];
96+ bindings: SelectOption <Binding >[];
97+ operations: SelectOption <OperationType >[];
98+ shouldRenderBindingSelect: boolean ;
99+ shouldRenderValueInput: boolean ;
100+ getFieldSelectOption: (field : string ) => SelectOption <string > | undefined ;
52101 onRemove: () => void ;
53102 onChangeBinding: (event : React .ChangeEvent <HTMLSelectElement >) => void ;
54103 onChangeField: (event : React .ChangeEvent <HTMLSelectElement >) => void ;
55104 onChangeOperation: (event : React .ChangeEvent <HTMLSelectElement >) => void ;
56105 onChangeValue: (event : React .ChangeEvent <HTMLInputElement >) => void ;
57106}
58107
59- const useQueryFilters: (properties : PropertyDescription []) => {
108+ interface HookProps {
109+ properties: PropertyDescription [];
110+ operationLabels? : Record <OperationType , string >;
111+ typeOperationsMap? : Record <string , OperationType []>;
112+ noValueOperations? : OperationType [];
113+ }
114+
115+ const useQueryFilters: (props : HookProps ) => {
60116 filters: Filter [];
61117 onAddFilter: () => void ;
62118 createFilterRowProps: (index : number ) => FilterRowProps ;
@@ -73,8 +129,8 @@ First, make sure you have the following dependencies in place:
73129# install chakra and dependencies
74130yarn add @chakra - ui / react @emotion / react @emotion / styled framer - motion
75131
76- # install react - query - filters
77- yarn add react - query - filters
132+ # install react - query - filter
133+ yarn add react - query - filter
78134```
79135
80136``` tsx
@@ -97,9 +153,9 @@ interface Props {
97153}
98154
99155export const FilterSelection: FC <Props > = ({ properties }) => {
100- const { filters, onAddFilter, createFilterRowProps } = useQueryFilters (
156+ const { filters, onAddFilter, createFilterRowProps } = useQueryFilters ({
101157 properties
102- );
158+ } );
103159
104160 return (
105161 <SimpleGrid columns = { 1 } spacingY = { 4 } >
@@ -127,7 +183,7 @@ export const FilterSelection: FC<Props> = ({ properties }) => {
127183
128184``` tsx
129185/** FilterRow.tsx */
130- import React , { FC } from ' react' ;
186+ import React from ' react' ;
131187import {
132188 CloseButton ,
133189 Text ,
@@ -136,83 +192,94 @@ import {
136192 Select ,
137193 Tooltip ,
138194} from ' @chakra-ui/react' ;
139- import { FilterRowProps , useRowUtilities } from ' react-query-filters' ;
195+ import { FilterRowProps } from ' react-query-filters' ;
140196
141- export const FilterRow: FC <FilterRowProps > = ({
142- properties ,
197+ export const FilterRow: React .FC <FilterRowProps > = ({
143198 filter ,
144- isFirst ,
199+ fields ,
200+ bindings ,
201+ operations ,
202+ shouldRenderBindingSelect ,
203+ shouldRenderValueInput ,
145204 onRemove ,
146205 onChangeBinding ,
147206 onChangeField ,
148207 onChangeOperation ,
149208 onChangeValue ,
150- }) => {
151- const {
152- getFilterOperationsForType,
153- shouldRenderValueInputForOperation,
154- } = useRowUtilities ();
155-
156- return (
157- <HStack >
158- <Tooltip shouldWrapChildren label = " Remove Filter" placement = " left" >
159- <CloseButton onClick = { onRemove } />
160- </Tooltip >
161-
162- { isFirst ? (
163- <Text fontSize = " sm" >Where </Text >
164- ) : (
165- <Select
166- size = " sm"
167- maxWidth = " 6rem"
168- value = { filter .binding }
169- onChange = { onChangeBinding }
170- >
171- <option value = " and" >And</option >
172- <option value = " or" >Or</option >
173- </Select >
174- )}
209+ }) => (
210+ <HStack >
211+ <Tooltip shouldWrapChildren label = " Remove Filter" placement = " left" >
212+ <CloseButton onClick = { onRemove } />
213+ </Tooltip >
175214
215+ { shouldRenderBindingSelect ? (
176216 <Select
177217 size = " sm"
178- value = { filter . field }
179- onChange = { onChangeField }
180- placeholder = " Field "
218+ maxWidth = " 6rem "
219+ value = { filter . binding }
220+ onChange = { onChangeBinding }
181221 >
182- { properties .map ((prop , index ) => (
183- <option value = { prop .key } key = { index } >
184- { prop .label }
222+ { bindings .map (binding => (
223+ <option
224+ value = { binding .value }
225+ key = { binding .value }
226+ selected = { filter .binding === binding .value }
227+ >
228+ { binding .label }
185229 </option >
186230 ))}
187231 </Select >
232+ ) : (
233+ <Text fontSize = " sm" >Where </Text >
234+ )}
235+
236+ <Select
237+ size = " sm"
238+ value = { filter .field }
239+ onChange = { onChangeField }
240+ placeholder = " Field"
241+ >
242+ { fields .map (field => (
243+ <option
244+ value = { field .value }
245+ key = { field .value }
246+ selected = { filter .field === field .value }
247+ >
248+ { field .label }
249+ </option >
250+ ))}
251+ </Select >
252+
253+ <Select
254+ size = " sm"
255+ value = { filter .operation }
256+ onChange = { onChangeOperation }
257+ placeholder = " Operation"
258+ >
259+ { operations .map (operation => (
260+ <option
261+ value = { operation .value }
262+ key = { operation .value }
263+ selected = { filter .operation === operation .value }
264+ >
265+ { operation .label }
266+ </option >
267+ ))}
268+ </Select >
188269
189- <Select
270+ { shouldRenderValueInput && (
271+ <Input
190272 size = " sm"
191- value = { filter .operation }
192- onChange = { onChangeOperation }
193- placeholder = " Operation"
194- >
195- { getFilterOperationsForType (filter .type ).map ((operation , index ) => (
196- <option value = { operation .value } key = { index } >
197- { operation .label }
198- </option >
199- ))}
200- </Select >
201-
202- { shouldRenderValueInputForOperation (filter .operation ) && (
203- <Input
204- size = " sm"
205- placeholder = " Value"
206- value = { filter .value ?? ' ' }
207- onChange = { onChangeValue }
208- />
209- )}
210- </HStack >
211- );
212- };
273+ placeholder = " Value"
274+ value = { filter .value ?? ' ' }
275+ onChange = { onChangeValue }
276+ />
277+ )}
278+ </HStack >
279+ );
213280```
214281
215- This component can the be used like this:
282+ This component can then be used like this:
216283
217284``` tsx
218285const properties: PropertyDescription [] = [
0 commit comments