@bigbinary/neeto-filters-frontend 2.13.1 → 2.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +156 -83
- package/dist/index.cjs.js +252 -99
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.js +215 -62
- package/dist/index.js.map +1 -1
- package/package.json +3 -1
- package/types.d.ts +1 -1
package/README.md
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
|
|
2
1
|
# @bigbinary/neeto-filters-frontend
|
|
3
2
|
|
|
4
3
|
neetoFilters is the library that manages filters across neeto products.
|
|
@@ -12,49 +11,65 @@ yarn add @bigbinary/neeto-filters-frontend
|
|
|
12
11
|
**neetoFilters** has a few peer dependencies that are required for the proper
|
|
13
12
|
functioning of the package. Install all the peer dependencies using the below
|
|
14
13
|
command:
|
|
14
|
+
|
|
15
15
|
```bash
|
|
16
16
|
yarn add @bigbinary/neetoui @bigbinary/neeto-icons axios@0.27.2 classnames@2.3.1 dayjs@1.11.1 formik@2.2.0 ramda@0.28.0 react-router-dom@5.3.0 react-router-nav-prompt@0.4.1 react-toastify@9.0.1 yup@0.32.11
|
|
17
17
|
```
|
|
18
|
+
|
|
18
19
|
## Usage
|
|
19
20
|
|
|
20
|
-
neeto-filters-frontend exports 2 components (`Filters` & `Bar`) and 2 functions
|
|
21
|
+
neeto-filters-frontend exports 2 components (`Filters` & `Bar`) and 2 functions
|
|
22
|
+
(`buildFiltersFromURL()` & `buildSelectorsFromURL()`).
|
|
23
|
+
|
|
21
24
|
### 1. `Filters` component
|
|
22
25
|
|
|
23
26
|
#### Props
|
|
24
27
|
|
|
25
28
|
1. **isOpen**: To specify whether the Pane component is open or not.
|
|
26
29
|
2. **setIsOpen**: To trigger the isOpen state.
|
|
27
|
-
3. **columns**: The list of columns/fields that needs to be represented in the
|
|
28
|
-
|
|
29
|
-
|
|
30
|
+
3. **columns**: The list of columns/fields that needs to be represented in the
|
|
31
|
+
filters pane.
|
|
32
|
+
4. **onChange**: To specify the callback function that is triggered whenever a
|
|
33
|
+
filter is applied or cleared. This function will get the updated filters as
|
|
34
|
+
argument.
|
|
35
|
+
5. **onSelectorsChange**: To specify the callback function that is triggered
|
|
36
|
+
whenever a selector value is changed. This function will get the updated
|
|
37
|
+
selectors as argument.
|
|
30
38
|
6. **buttonProps**: To specify the props to be passed to the button component.
|
|
31
39
|
7. **paneProps**: To specify the props to be passed to the pane component.
|
|
32
|
-
8. **isSearchable**: To specify whether you need a search bar in filters pane
|
|
40
|
+
8. **isSearchable**: To specify whether you need a search bar in filters pane
|
|
41
|
+
to search through filter fields.
|
|
33
42
|
9. **className**: To specify classes to the Filters component.
|
|
34
43
|
10. **keyword**: To specify the props related to searching.
|
|
35
|
-
11. **childrenKeys** The list of keys which has to be cleared from url during
|
|
36
|
-
|
|
37
|
-
|
|
44
|
+
11. **childrenKeys** The list of keys which has to be cleared from url during
|
|
45
|
+
clear filters operation.
|
|
46
|
+
12. **children**: To specify the custom components specific to the host
|
|
47
|
+
application.
|
|
48
|
+
13. **size**: To specify the width of the filters pane. It accepts "xsmall"
|
|
49
|
+
(default), "small", and "large" as value.
|
|
38
50
|
|
|
39
51
|
Here is an example of `keyword` prop:
|
|
52
|
+
|
|
40
53
|
```js
|
|
41
54
|
const [searchTerm, setSearchTerm] = useState("");
|
|
42
55
|
const searchKeywordProps = {
|
|
43
56
|
key: "keyword",
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
57
|
+
node: "name",
|
|
58
|
+
value: searchTerm.trim(),
|
|
59
|
+
setValue: setSearchTerm,
|
|
60
|
+
};
|
|
48
61
|
```
|
|
62
|
+
|
|
49
63
|
This `searchKeywordProps` can be passed as the value to `keyword` prop.
|
|
50
64
|
|
|
51
|
-
> :memo: **Note:**
|
|
52
|
-
|
|
65
|
+
> :memo: **Note:** If you wish to conduct a search for the keyword among
|
|
66
|
+
> multiple columns spanning different tables in the DB, then you should pass the
|
|
67
|
+
> name of the models corresponding to the tables as value of `models` prop.
|
|
53
68
|
>
|
|
54
69
|
> Example:
|
|
55
70
|
>
|
|
56
|
-
> If you are querying the user records and searching the keyword among users'
|
|
57
|
-
node will be like:
|
|
71
|
+
> If you are querying the user records and searching the keyword among users'
|
|
72
|
+
> name and users' organization name, then node will be like:
|
|
58
73
|
>
|
|
59
74
|
> `node = "first_name,last_name,organization.name"`
|
|
60
75
|
>
|
|
@@ -63,6 +78,7 @@ node will be like:
|
|
|
63
78
|
> `model = "User,Organization"`
|
|
64
79
|
|
|
65
80
|
Here is an example for the `columns` prop:
|
|
81
|
+
|
|
66
82
|
```js
|
|
67
83
|
export const SAMPLE_FILTER_FIELDS = [
|
|
68
84
|
{
|
|
@@ -88,17 +104,25 @@ export const SAMPLE_FILTER_FIELDS = [
|
|
|
88
104
|
},
|
|
89
105
|
];
|
|
90
106
|
```
|
|
107
|
+
|
|
91
108
|
- `key` : A unique identifier provided to each columns/field.
|
|
92
109
|
- `group`: The label of the group, if the field is associated with a group.
|
|
93
110
|
- `node`: The column in DB table corresponding to the field.
|
|
94
111
|
- `label` The label to be shown just above the field in Pane.
|
|
95
|
-
- `suffixComponent` The component to be shown at the end of the respective Block
|
|
96
|
-
|
|
112
|
+
- `suffixComponent` The component to be shown at the end of the respective Block
|
|
113
|
+
in filters Bar.
|
|
114
|
+
- `preprocessor` The function to be used to preprocess the value before applying
|
|
115
|
+
the filter.
|
|
97
116
|
- `type`: The nature/type of the field needed in pane.
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
117
|
+
- The supported types are `text`, `number`, `decimal`, `date`, `single_date`,
|
|
118
|
+
`time`, `single_time`, `single_option`, `multi_option` and `boolean`
|
|
119
|
+
- `model`: If the node provided is **not** directly refering to the column
|
|
120
|
+
within the table being queried for the filtered results, then we need to
|
|
121
|
+
provide the **model** corresponding to the target table. - For example: We are
|
|
122
|
+
adding a filter field for querying on `users` table to list all users with
|
|
123
|
+
address containing the a certain word. Say the addresses are stored in
|
|
124
|
+
**another table `addresses`**. Then the node will be `address.line_1,line_2`.
|
|
125
|
+
In such cases we need to pass this extra prop like `model: "Address"`.
|
|
102
126
|
- Then whole object for the field might look like this
|
|
103
127
|
```js
|
|
104
128
|
{
|
|
@@ -109,123 +133,172 @@ Then the node will be `address.line_1,line_2`. In such cases we need to pass thi
|
|
|
109
133
|
type: "text"
|
|
110
134
|
}
|
|
111
135
|
```
|
|
112
|
-
- `values`: The list of label-value pair to be shown in dropdown, if the `type`
|
|
113
|
-
|
|
114
|
-
- `
|
|
115
|
-
|
|
136
|
+
- `values`: The list of label-value pair to be shown in dropdown, if the `type`
|
|
137
|
+
provided is `single_option` or `multi_option`.
|
|
138
|
+
- `showRuleSelector` : Boolean value, which determines whether to show a rule
|
|
139
|
+
selector above the `text` , `number` and `decimal` type fields. `false` by
|
|
140
|
+
default.
|
|
141
|
+
- `hidden`: Boolean value, which determines to show or hide field from filter
|
|
142
|
+
pane. `false ` by default.
|
|
143
|
+
- `defaultValue`: The default value that should appear as chosen one in the
|
|
144
|
+
filter fields.
|
|
116
145
|
- `defaultType`: The type of the input that should be used in the Filters pane.
|
|
117
|
-
- `isClearable`: Boolean value which specifies whether the field can be cleared
|
|
146
|
+
- `isClearable`: Boolean value which specifies whether the field can be cleared
|
|
147
|
+
or not.
|
|
148
|
+
|
|
149
|
+
- By `selectors` we mean the filter fields which doesn't have any relation
|
|
150
|
+
(`node`) to a DB >table column.
|
|
151
|
+
- For example, consider a field in filter pane called `Group by`. We cannot
|
|
152
|
+
provide a `node` to associate it with a column in DB table. Still it can
|
|
153
|
+
provide an important value which can be used for the computation as if by
|
|
154
|
+
telling a means by which the records can be grouped after filtering.
|
|
155
|
+
- Fields without a `node`, among the ones passed as value to the `column` prop
|
|
156
|
+
of `Filters` are >considered as selectors.
|
|
118
157
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
value: "location"
|
|
135
|
-
}
|
|
136
|
-
],
|
|
158
|
+
```js
|
|
159
|
+
{
|
|
160
|
+
key: "groupBy",
|
|
161
|
+
label: "Group by",
|
|
162
|
+
type: "single_option",
|
|
163
|
+
values: [
|
|
164
|
+
{
|
|
165
|
+
label: "Team",
|
|
166
|
+
value: "team"
|
|
167
|
+
},
|
|
168
|
+
{
|
|
169
|
+
label: "Location",
|
|
170
|
+
value: "location"
|
|
171
|
+
}
|
|
172
|
+
],
|
|
137
173
|
}
|
|
138
|
-
|
|
139
|
-
When user selects a value from this dropdown we get that value in the `selectors` state. This can be used for further computations outside the neeto-filters.
|
|
174
|
+
```
|
|
140
175
|
|
|
176
|
+
When user selects a value from this dropdown we get that value in the
|
|
177
|
+
`selectors` state. This can be used for further computations outside the
|
|
178
|
+
neeto-filters.
|
|
141
179
|
|
|
142
180
|
### 2. `Bar` component
|
|
143
181
|
|
|
144
182
|
#### Props
|
|
145
183
|
|
|
146
|
-
1. **columns**: The list of columns/fields that needs to be represented in the
|
|
147
|
-
|
|
184
|
+
1. **columns**: The list of columns/fields that needs to be represented in the
|
|
185
|
+
filters pane.
|
|
186
|
+
2. **onChange**: To specify the callback function that is triggered whenever a
|
|
187
|
+
filter is applied or cleared. This function will get the updated filters as
|
|
188
|
+
argument.
|
|
148
189
|
3. **setIsPaneOpen**: To trigger the isOpen state.
|
|
149
190
|
4. **className**: To specify classes to the Bar component.
|
|
150
|
-
5.
|
|
151
|
-
6.
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
The
|
|
191
|
+
5. **keyword**: To specify the props related to searching.
|
|
192
|
+
6. **onSelectorsChange**: To specify the callback function that is triggered
|
|
193
|
+
whenever a selector value is changed. This function will get the updated
|
|
194
|
+
selectors as argument.
|
|
195
|
+
7. **childrenKeys** The list of keys which has to be cleared from url during
|
|
196
|
+
clear filters operation.
|
|
197
|
+
8. **entity**: The entity associated with the filters.
|
|
198
|
+
|
|
199
|
+
All these props must be same as that passed to the `Filters` component. The
|
|
200
|
+
option to save filters as segments will be enabled only when the entity prop is
|
|
201
|
+
passed to the component.
|
|
157
202
|
|
|
158
203
|
### 3. `Segments` component
|
|
159
204
|
|
|
160
205
|
#### Props
|
|
161
206
|
|
|
162
|
-
1. **columns**: The list of columns/fields that needs to be represented in the
|
|
207
|
+
1. **columns**: The list of columns/fields that needs to be represented in the
|
|
208
|
+
filters pane.
|
|
163
209
|
2. **entity**: The entity associated with the segments.
|
|
164
210
|
3. **setIsPaneOpen**: To trigger the isOpen state of the filters pane.
|
|
165
211
|
|
|
166
|
-
The "columns" prop must be same as that passed to the `Filters` component.
|
|
167
|
-
|
|
212
|
+
The "columns" prop must be same as that passed to the `Filters` component. To
|
|
213
|
+
set up the neeto-filters-engine with support for Segments, please follow the
|
|
214
|
+
instructions provided in the installation
|
|
215
|
+
[documentation](https://github.com/bigbinary/neeto-filters-engine#installation).
|
|
168
216
|
|
|
169
|
-
|
|
217
|
+
### 3. `buildFiltersFromURL` function
|
|
170
218
|
|
|
171
219
|
- This function takes a list as argument.
|
|
172
|
-
- This list should contains all the filter columns/fields and the value passed
|
|
173
|
-
|
|
220
|
+
- This list should contains all the filter columns/fields and the value passed
|
|
221
|
+
to `keyword` prop of `Filters` and `Bar`.
|
|
222
|
+
- It builds the filter payload from the URL search params. This function should
|
|
223
|
+
be called for initializing the `filters` state.
|
|
224
|
+
|
|
174
225
|
```js
|
|
175
|
-
const [filters, setFilters] = useState(() =>
|
|
226
|
+
const [filters, setFilters] = useState(() =>
|
|
227
|
+
buildFiltersFromURL([...SAMPLE_FILTER_COLUMNS, searchKeywordProps])
|
|
228
|
+
);
|
|
176
229
|
```
|
|
177
230
|
|
|
178
231
|
### 4. `buildSelectorsFromURL` function
|
|
179
232
|
|
|
180
|
-
- By `selectors` we mean the filter fields which doesn't have any relation
|
|
181
|
-
|
|
182
|
-
-
|
|
233
|
+
- By `selectors` we mean the filter fields which doesn't have any relation
|
|
234
|
+
(`node`) to a DB table column.
|
|
235
|
+
- For example, consider a field in filter pane called `Group by`. We cannot
|
|
236
|
+
provide a `node` to associate it with a column in DB table. Still it can
|
|
237
|
+
provide an important value which can be used for the computation as if by
|
|
238
|
+
telling a means by which the records can be grouped after filtering.
|
|
239
|
+
- Fields without a `node`, among the ones passed as value to the `column` prop
|
|
240
|
+
of `Filters` are considered as selectors.
|
|
183
241
|
- This function takes a list as argument.
|
|
184
242
|
- This list should contains all the filter columns/fields.
|
|
185
|
-
- It builds the selectors' value from the URL search params. This function
|
|
186
|
-
|
|
243
|
+
- It builds the selectors' value from the URL search params. This function
|
|
244
|
+
should be called for initializing the `selectors` state.
|
|
245
|
+
- `selectors` will be an object with keys same as provided in filter fields and
|
|
246
|
+
selected value as its value.
|
|
247
|
+
|
|
187
248
|
```js
|
|
188
|
-
const [selectors, setSelectors] = useState(() =>
|
|
249
|
+
const [selectors, setSelectors] = useState(() =>
|
|
250
|
+
buildSelectorsFromURL(FILTER_COLUMNS)
|
|
251
|
+
);
|
|
189
252
|
```
|
|
190
253
|
|
|
191
254
|
### 5. `buildFilterColumnsFromFields` function
|
|
192
255
|
|
|
193
|
-
The `buildFilterColumnsFromFields` function is used to generate an array of
|
|
256
|
+
The `buildFilterColumnsFromFields` function is used to generate an array of
|
|
257
|
+
filter columns based on the provided fields (often referred as custom fields)
|
|
258
|
+
and selected field names.
|
|
194
259
|
|
|
195
260
|
#### Parameters
|
|
196
261
|
|
|
197
262
|
- `fields` (array): An array of field objects representing the available fields.
|
|
198
|
-
- `selectedFields` (array, optional): An array of strings representing the
|
|
263
|
+
- `selectedFields` (array, optional): An array of strings representing the
|
|
264
|
+
selected field names. Defaults to an empty array.
|
|
265
|
+
- `showRuleSelector` (boolean, optional): A boolean value which specify whether
|
|
266
|
+
to show the rule selector for each filter or not. It is `false` by default.
|
|
199
267
|
|
|
200
268
|
```js
|
|
201
269
|
const fields = [
|
|
202
270
|
{
|
|
203
|
-
id:
|
|
204
|
-
name:
|
|
205
|
-
kind:
|
|
271
|
+
id: "1",
|
|
272
|
+
name: "name",
|
|
273
|
+
kind: "text",
|
|
206
274
|
},
|
|
207
275
|
{
|
|
208
|
-
id:
|
|
209
|
-
name:
|
|
210
|
-
kind:
|
|
276
|
+
id: "2",
|
|
277
|
+
name: "age",
|
|
278
|
+
kind: "number",
|
|
211
279
|
},
|
|
212
280
|
// Add more field objects as needed
|
|
213
281
|
];
|
|
214
282
|
|
|
215
|
-
const selectedFields = [
|
|
283
|
+
const selectedFields = ["name", "age"];
|
|
216
284
|
|
|
217
|
-
const filterColumns = buildFilterColumnsFromFields(fields, selectedFields);
|
|
285
|
+
const filterColumns = buildFilterColumnsFromFields({ fields, selectedFields });
|
|
218
286
|
```
|
|
219
287
|
|
|
220
288
|
## Development
|
|
221
289
|
|
|
222
290
|
Install all the dependencies by executing the following command
|
|
291
|
+
|
|
223
292
|
```bash
|
|
224
293
|
yarn install
|
|
225
294
|
```
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
295
|
+
|
|
296
|
+
Start the development server using the `yarn start` command. In order to run the
|
|
297
|
+
application you need to run the Rails server of the
|
|
298
|
+
[neeto-filters-engine](https://github.com/bigbinary/neeto-filters-engine)'s
|
|
299
|
+
dummy application as well. This is necessary to make sure that all the APIs and
|
|
300
|
+
React Query hooks are working as per the requirement.
|
|
301
|
+
|
|
229
302
|
```bash
|
|
230
303
|
git clone https://github.com/bigbinary/neeto-filters-engine.git
|
|
231
304
|
./test/dummy/bin/setup
|