@carbon/ibm-products 2.0.0-rc.10 → 2.0.0-rc.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. package/css/index-full-carbon.css +102 -1
  2. package/css/index-full-carbon.css.map +1 -1
  3. package/css/index-full-carbon.min.css +2 -2
  4. package/css/index-full-carbon.min.css.map +1 -1
  5. package/css/index-without-carbon.css +102 -1
  6. package/css/index-without-carbon.css.map +1 -1
  7. package/css/index-without-carbon.min.css +2 -2
  8. package/css/index-without-carbon.min.css.map +1 -1
  9. package/css/index.css +93 -1
  10. package/css/index.css.map +1 -1
  11. package/css/index.min.css +1 -1
  12. package/css/index.min.css.map +1 -1
  13. package/es/components/DataSpreadsheet/DataSpreadsheet.js +1 -4
  14. package/es/components/DataSpreadsheet/utils/handleCellDeletion.js +6 -4
  15. package/es/components/Datagrid/Datagrid/Datagrid.js +7 -3
  16. package/es/components/Datagrid/Datagrid/DatagridContent.js +8 -3
  17. package/es/components/Datagrid/Datagrid/DatagridEmptyBody.js +17 -4
  18. package/es/components/Datagrid/Datagrid/DatagridRow.js +2 -10
  19. package/es/components/Datagrid/Datagrid/DatagridToolbar.js +22 -4
  20. package/es/components/Datagrid/Datagrid/DatagridVirtualBody.js +29 -8
  21. package/es/components/Datagrid/Datagrid/addons/Filtering/FilterFlyout.js +424 -0
  22. package/es/components/Datagrid/Datagrid/addons/Filtering/FilterProvider.js +83 -0
  23. package/es/components/Datagrid/Datagrid/addons/Filtering/constants.js +20 -0
  24. package/es/components/Datagrid/Datagrid/addons/Filtering/hooks/useInitialStateFromFilters.js +22 -0
  25. package/es/components/Datagrid/Datagrid/addons/Filtering/index.js +7 -0
  26. package/es/components/Datagrid/Datagrid/addons/Filtering/utils.js +38 -0
  27. package/es/components/Datagrid/index.js +2 -1
  28. package/es/components/Datagrid/useFiltering.js +90 -0
  29. package/es/components/Datagrid/useParentDimensions.js +3 -1
  30. package/es/components/Datagrid/utils/DatagridActions.js +13 -26
  31. package/es/components/Datagrid/utils/getArgTypes.js +5 -0
  32. package/es/components/Datagrid/utils/getInlineEditColumns.js +6 -1
  33. package/es/components/Datagrid/utils/makeData.js +24 -16
  34. package/es/components/FilterSummary/FilterSummary.js +54 -0
  35. package/es/components/FilterSummary/index.js +7 -0
  36. package/lib/components/DataSpreadsheet/DataSpreadsheet.js +1 -4
  37. package/lib/components/DataSpreadsheet/utils/handleCellDeletion.js +6 -4
  38. package/lib/components/Datagrid/Datagrid/Datagrid.js +8 -3
  39. package/lib/components/Datagrid/Datagrid/DatagridContent.js +8 -3
  40. package/lib/components/Datagrid/Datagrid/DatagridEmptyBody.js +17 -4
  41. package/lib/components/Datagrid/Datagrid/DatagridRow.js +2 -19
  42. package/lib/components/Datagrid/Datagrid/DatagridToolbar.js +24 -3
  43. package/lib/components/Datagrid/Datagrid/DatagridVirtualBody.js +31 -8
  44. package/lib/components/Datagrid/Datagrid/addons/Filtering/FilterFlyout.js +443 -0
  45. package/lib/components/Datagrid/Datagrid/addons/Filtering/FilterProvider.js +104 -0
  46. package/lib/components/Datagrid/Datagrid/addons/Filtering/constants.js +35 -0
  47. package/lib/components/Datagrid/Datagrid/addons/Filtering/hooks/useInitialStateFromFilters.js +33 -0
  48. package/lib/components/Datagrid/Datagrid/addons/Filtering/index.js +15 -0
  49. package/lib/components/Datagrid/Datagrid/addons/Filtering/utils.js +47 -0
  50. package/lib/components/Datagrid/index.js +9 -1
  51. package/lib/components/Datagrid/useFiltering.js +94 -0
  52. package/lib/components/Datagrid/useParentDimensions.js +3 -1
  53. package/lib/components/Datagrid/utils/DatagridActions.js +13 -25
  54. package/lib/components/Datagrid/utils/getArgTypes.js +5 -0
  55. package/lib/components/Datagrid/utils/getInlineEditColumns.js +6 -1
  56. package/lib/components/Datagrid/utils/makeData.js +26 -18
  57. package/lib/components/FilterSummary/FilterSummary.js +72 -0
  58. package/lib/components/FilterSummary/index.js +15 -0
  59. package/package.json +2 -2
  60. package/scss/components/Datagrid/_datagrid.scss +0 -1
  61. package/scss/components/Datagrid/_storybook-styles.scss +12 -0
  62. package/scss/components/Datagrid/styles/_datagrid.scss +18 -0
  63. package/scss/components/Datagrid/styles/_index.scss +1 -0
  64. package/scss/components/Datagrid/styles/addons/_FilterFlyout.scss +87 -0
  65. package/scss/components/FilterSummary/_filter-summary.scss +20 -0
  66. package/scss/components/FilterSummary/_index.scss +10 -0
  67. package/scss/components/FilterSummary/_storybook-styles.scss +14 -0
  68. package/scss/components/_index.scss +1 -0
@@ -0,0 +1,424 @@
1
+ import _extends from "@babel/runtime/helpers/extends";
2
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
3
+ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
4
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
5
+
6
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
7
+
8
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
9
+
10
+ // @flow
11
+
12
+ /*
13
+ * Licensed Materials - Property of IBM
14
+ * 5724-Q36
15
+ * (c) Copyright IBM Corp. 2022
16
+ * US Government Users Restricted Rights - Use, duplication or disclosure
17
+ * restricted by GSA ADP Schedule Contract with IBM Corp.
18
+ */
19
+ import { Filter } from '@carbon/react/icons';
20
+ import { Button, Checkbox, DatePicker, DatePickerInput, Dropdown, FormGroup, NumberInput, RadioButton, RadioButtonGroup } from '@carbon/react';
21
+ import cx from 'classnames';
22
+ import PropTypes from 'prop-types';
23
+ import React, { useCallback, useRef, useState } from 'react';
24
+ import { useEffect } from 'react';
25
+ import { useContext } from 'react';
26
+ import { useClickOutside } from '../../../../../global/js/hooks';
27
+ import { pkg } from '../../../../../settings';
28
+ import { ActionSet } from '../../../../ActionSet';
29
+ import { BATCH, CHECKBOX, CLEAR_FILTERS, DATE, DROPDOWN, INSTANT, NUMBER, RADIO } from './constants';
30
+ import { FilterContext } from './FilterProvider';
31
+ import useInitialStateFromFilters from './hooks/useInitialStateFromFilters';
32
+ import { getInitialStateFromFilters } from './utils';
33
+ var blockClass = "".concat(pkg.prefix, "--datagrid");
34
+ var componentClass = "".concat(blockClass, "-filter-flyout");
35
+
36
+ var FilterFlyout = function FilterFlyout(_ref) {
37
+ var _cx2;
38
+
39
+ var _ref$updateMethod = _ref.updateMethod,
40
+ updateMethod = _ref$updateMethod === void 0 ? BATCH : _ref$updateMethod,
41
+ _ref$flyoutIconDescri = _ref.flyoutIconDescription,
42
+ flyoutIconDescription = _ref$flyoutIconDescri === void 0 ? 'Open filters' : _ref$flyoutIconDescri,
43
+ _ref$filters = _ref.filters,
44
+ filters = _ref$filters === void 0 ? [] : _ref$filters,
45
+ _ref$title = _ref.title,
46
+ title = _ref$title === void 0 ? 'Filter' : _ref$title,
47
+ _ref$primaryActionLab = _ref.primaryActionLabel,
48
+ primaryActionLabel = _ref$primaryActionLab === void 0 ? 'Apply' : _ref$primaryActionLab,
49
+ _ref$onFlyoutOpen = _ref.onFlyoutOpen,
50
+ onFlyoutOpen = _ref$onFlyoutOpen === void 0 ? function () {} : _ref$onFlyoutOpen,
51
+ _ref$onFlyoutClose = _ref.onFlyoutClose,
52
+ onFlyoutClose = _ref$onFlyoutClose === void 0 ? function () {} : _ref$onFlyoutClose,
53
+ _ref$onApply = _ref.onApply,
54
+ onApply = _ref$onApply === void 0 ? function () {} : _ref$onApply,
55
+ _ref$onCancel = _ref.onCancel,
56
+ onCancel = _ref$onCancel === void 0 ? function () {} : _ref$onCancel,
57
+ _ref$secondaryActionL = _ref.secondaryActionLabel,
58
+ secondaryActionLabel = _ref$secondaryActionL === void 0 ? 'Cancel' : _ref$secondaryActionL,
59
+ setAllFilters = _ref.setAllFilters;
60
+
61
+ /** Context state and methods */
62
+ var _useContext = useContext(FilterContext),
63
+ EventEmitter = _useContext.EventEmitter;
64
+ /** State */
65
+
66
+
67
+ var _useState = useState(false),
68
+ _useState2 = _slicedToArray(_useState, 2),
69
+ open = _useState2[0],
70
+ setOpen = _useState2[1];
71
+
72
+ var _useInitialStateFromF = useInitialStateFromFilters(filters),
73
+ _useInitialStateFromF2 = _slicedToArray(_useInitialStateFromF, 2),
74
+ filtersState = _useInitialStateFromF2[0],
75
+ setFiltersState = _useInitialStateFromF2[1];
76
+
77
+ var _useState3 = useState([]),
78
+ _useState4 = _slicedToArray(_useState3, 2),
79
+ filtersObjectArray = _useState4[0],
80
+ setFiltersObjectArray = _useState4[1];
81
+ /** Refs */
82
+
83
+
84
+ var filterFlyoutRef = useRef(null); // When using batch actions we have to store the filters to then apply them later
85
+
86
+ var prevFiltersRef = useRef(JSON.stringify(filtersState));
87
+ var prevFiltersObjectArrayRef = useRef(JSON.stringify(filtersObjectArray));
88
+ /** Memos */
89
+
90
+ var showActionSet = updateMethod === BATCH;
91
+ /** Methods */
92
+
93
+ var openFlyout = function openFlyout() {
94
+ setOpen(true);
95
+ onFlyoutOpen();
96
+ };
97
+
98
+ var closeFlyout = function closeFlyout() {
99
+ setOpen(false);
100
+ onFlyoutClose();
101
+ };
102
+
103
+ var apply = function apply() {
104
+ setAllFilters(filtersObjectArray);
105
+ closeFlyout();
106
+ onApply(); // updates the ref so next time the flyout opens we have records of the previous filters
107
+
108
+ prevFiltersRef.current = JSON.stringify(filtersState);
109
+ prevFiltersObjectArrayRef.current = JSON.stringify(filtersObjectArray);
110
+ };
111
+
112
+ var cancel = function cancel() {
113
+ // Reverting to previous filters only applies when using batch actions
114
+ if (updateMethod === BATCH) {
115
+ revertToPreviousFilters();
116
+ onCancel();
117
+ }
118
+
119
+ closeFlyout();
120
+ };
121
+
122
+ var reset = function reset() {
123
+ // Get the initial values for the filters
124
+ var initialFiltersState = getInitialStateFromFilters(filters);
125
+ var initialFiltersObjectArray = []; // Set the state to the initial values
126
+
127
+ setFiltersState(initialFiltersState);
128
+ setFiltersObjectArray(initialFiltersObjectArray);
129
+ setAllFilters([]); // Update their respective refs so everything is in sync
130
+
131
+ prevFiltersRef.current = JSON.stringify(initialFiltersState);
132
+ prevFiltersObjectArrayRef.current = JSON.stringify(initialFiltersObjectArray);
133
+ }; // If the user decides to cancel or click outside the flyout, it reverts back to the filters that were
134
+ // there when they opened the flyout
135
+
136
+
137
+ var revertToPreviousFilters = function revertToPreviousFilters() {
138
+ setFiltersState(JSON.parse(prevFiltersRef.current));
139
+ setFiltersObjectArray(JSON.parse(prevFiltersObjectArrayRef.current));
140
+ };
141
+
142
+ var applyFilters = useCallback(function (_ref2) {
143
+ var column = _ref2.column,
144
+ value = _ref2.value,
145
+ type = _ref2.type;
146
+
147
+ // If no end date is selected return because we need the end date to do computations
148
+ if (type === DATE && !value[1]) {
149
+ return;
150
+ }
151
+
152
+ var filtersObjectArrayCopy = _toConsumableArray(filtersObjectArray); // check if the filter already exists in the array
153
+
154
+
155
+ var filter = filtersObjectArrayCopy.find(function (item) {
156
+ return item.id === column;
157
+ }); // if filter exists in array then update the filter's new value
158
+
159
+ if (filter) {
160
+ filter.value = value;
161
+ } else {
162
+ filtersObjectArrayCopy.push({
163
+ id: column,
164
+ value: value,
165
+ type: type
166
+ });
167
+ }
168
+
169
+ setFiltersObjectArray(filtersObjectArrayCopy); // Automatically apply the filters if the updateMethod is instant
170
+
171
+ if (updateMethod === INSTANT) {
172
+ setAllFilters(filtersObjectArrayCopy);
173
+ }
174
+ }, [setAllFilters, updateMethod, filtersObjectArray]);
175
+ /** Effects */
176
+
177
+ useClickOutside(filterFlyoutRef, function (target) {
178
+ var hasClickedOnDatePicker = target.closest('.flatpickr-calendar');
179
+
180
+ if (!open || hasClickedOnDatePicker) {
181
+ return;
182
+ }
183
+
184
+ cancel();
185
+ });
186
+ useEffect(function subscribeToEmitter() {
187
+ // This event is emitted from the DatagridToolbar component when clearFilters is clicked in FilterSummary
188
+ EventEmitter.subscribe(CLEAR_FILTERS, reset);
189
+ });
190
+ /** Render the individual filter component */
191
+
192
+ var renderFilter = useCallback(function (_ref3) {
193
+ var type = _ref3.type,
194
+ column = _ref3.column,
195
+ components = _ref3.props;
196
+ var key = "".concat(type, "-").concat(column, "-").concat(Math.random());
197
+
198
+ if (type === DATE) {
199
+ return /*#__PURE__*/React.createElement(DatePicker, _extends({
200
+ key: key
201
+ }, components.DatePicker, {
202
+ onChange: function onChange(value) {
203
+ var _components$DatePicke, _components$DatePicke2;
204
+
205
+ setFiltersState(_objectSpread(_objectSpread({}, filtersState), {}, _defineProperty({}, column, value)));
206
+ applyFilters({
207
+ column: column,
208
+ value: value,
209
+ type: type
210
+ });
211
+ (_components$DatePicke = (_components$DatePicke2 = components.DatePicker).onChange) === null || _components$DatePicke === void 0 ? void 0 : _components$DatePicke.call(_components$DatePicke2, value);
212
+ },
213
+ value: filtersState[column],
214
+ datePickerType: "range"
215
+ }), /*#__PURE__*/React.createElement(DatePickerInput, _extends({
216
+ placeholder: "mm/dd/yyyy",
217
+ labelText: "Start date"
218
+ }, components.DatePickerInput.start)), /*#__PURE__*/React.createElement(DatePickerInput, _extends({
219
+ placeholder: "mm/dd/yyyy",
220
+ labelText: "End date"
221
+ }, components.DatePickerInput.end)));
222
+ } else if (type === NUMBER) {
223
+ return /*#__PURE__*/React.createElement(NumberInput, _extends({
224
+ key: key,
225
+ step: 1,
226
+ allowEmpty: true,
227
+ hideSteppers: true
228
+ }, components.NumberInput, {
229
+ onChange: function onChange(event) {
230
+ var _components$NumberInp, _components$NumberInp2;
231
+
232
+ setFiltersState(_objectSpread(_objectSpread({}, filtersState), {}, _defineProperty({}, column, event.target.value)));
233
+ applyFilters({
234
+ column: column,
235
+ value: event.target.value,
236
+ type: type
237
+ });
238
+ (_components$NumberInp = (_components$NumberInp2 = components.NumberInput).onChange) === null || _components$NumberInp === void 0 ? void 0 : _components$NumberInp.call(_components$NumberInp2, event);
239
+ },
240
+ value: filtersState[column]
241
+ }));
242
+ } else if (type === CHECKBOX) {
243
+ return /*#__PURE__*/React.createElement(FormGroup, _extends({
244
+ key: key
245
+ }, components.FormGroup), filtersState[column].map(function (option) {
246
+ return /*#__PURE__*/React.createElement(Checkbox, _extends({
247
+ key: option.labelText
248
+ }, option, {
249
+ onChange: function onChange(_, _ref4) {
250
+ var _option$onChange;
251
+
252
+ var checked = _ref4.checked;
253
+ var checkboxCopy = filtersState[column];
254
+ var foundCheckbox = checkboxCopy.find(function (checkbox) {
255
+ return checkbox.value === option.value;
256
+ });
257
+ foundCheckbox.selected = checked;
258
+ setFiltersState(_objectSpread(_objectSpread({}, filtersState), {}, _defineProperty({}, column, checkboxCopy)));
259
+ applyFilters({
260
+ column: column,
261
+ value: _toConsumableArray(filtersState[column]),
262
+ type: type
263
+ });
264
+ (_option$onChange = option.onChange) === null || _option$onChange === void 0 ? void 0 : _option$onChange.call(option, checked);
265
+ },
266
+ checked: option.selected
267
+ }));
268
+ }));
269
+ } else if (type === RADIO) {
270
+ return /*#__PURE__*/React.createElement(FormGroup, _extends({
271
+ key: key
272
+ }, components.FormGroup), /*#__PURE__*/React.createElement(RadioButtonGroup, _extends({}, components.RadioButtonGroup, {
273
+ valueSelected: filtersState[column],
274
+ onChange: function onChange(selected) {
275
+ var _components$RadioButt, _components$RadioButt2;
276
+
277
+ setFiltersState(_objectSpread(_objectSpread({}, filtersState), {}, _defineProperty({}, column, selected)));
278
+ applyFilters({
279
+ column: column,
280
+ value: selected,
281
+ type: type
282
+ });
283
+ (_components$RadioButt = (_components$RadioButt2 = components.RadioButtonGroup).onChange) === null || _components$RadioButt === void 0 ? void 0 : _components$RadioButt.call(_components$RadioButt2, selected);
284
+ }
285
+ }), components.RadioButton.map(function (radio) {
286
+ var _ref5, _radio$id;
287
+
288
+ return /*#__PURE__*/React.createElement(RadioButton, _extends({
289
+ key: (_ref5 = (_radio$id = radio.id) !== null && _radio$id !== void 0 ? _radio$id : radio.labelText) !== null && _ref5 !== void 0 ? _ref5 : radio.value
290
+ }, radio));
291
+ })));
292
+ } else if (type === DROPDOWN) {
293
+ return /*#__PURE__*/React.createElement(Dropdown, _extends({
294
+ key: key
295
+ }, components.Dropdown, {
296
+ selectedItem: filtersState[column],
297
+ onChange: function onChange(_ref6) {
298
+ var _components$Dropdown$, _components$Dropdown;
299
+
300
+ var selectedItem = _ref6.selectedItem;
301
+ setFiltersState(_objectSpread(_objectSpread({}, filtersState), {}, _defineProperty({}, column, selectedItem)));
302
+ applyFilters({
303
+ column: column,
304
+ value: selectedItem,
305
+ type: type
306
+ });
307
+ (_components$Dropdown$ = (_components$Dropdown = components.Dropdown).onChange) === null || _components$Dropdown$ === void 0 ? void 0 : _components$Dropdown$.call(_components$Dropdown, selectedItem);
308
+ }
309
+ }));
310
+ }
311
+ }, [filtersState, applyFilters, setFiltersState]);
312
+ /** Renders all filters */
313
+
314
+ var renderFilters = function renderFilters() {
315
+ return filters.map(renderFilter);
316
+ };
317
+
318
+ var renderActionSet = function renderActionSet() {
319
+ return showActionSet && /*#__PURE__*/React.createElement(ActionSet, {
320
+ actions: [{
321
+ key: 1,
322
+ kind: 'primary',
323
+ label: primaryActionLabel,
324
+ onClick: apply,
325
+ isExpressive: false
326
+ }, {
327
+ key: 3,
328
+ kind: 'secondary',
329
+ label: secondaryActionLabel,
330
+ onClick: cancel,
331
+ isExpressive: false
332
+ }],
333
+ size: "md",
334
+ buttonSize: "md"
335
+ });
336
+ };
337
+
338
+ return /*#__PURE__*/React.createElement("div", {
339
+ className: "".concat(componentClass, "__container")
340
+ }, /*#__PURE__*/React.createElement(Button, {
341
+ kind: "ghost",
342
+ hasIconOnly: true,
343
+ tooltipPosition: "bottom",
344
+ renderIcon: function renderIcon() {
345
+ return /*#__PURE__*/React.createElement(Filter, {
346
+ size: 16
347
+ });
348
+ },
349
+ iconDescription: flyoutIconDescription,
350
+ onClick: open ? closeFlyout : openFlyout,
351
+ className: cx("".concat(componentClass, "__trigger"), _defineProperty({}, "".concat(componentClass, "__trigger--open"), open))
352
+ }), /*#__PURE__*/React.createElement("div", {
353
+ ref: filterFlyoutRef,
354
+ className: cx(componentClass, (_cx2 = {}, _defineProperty(_cx2, "".concat(componentClass, "--open"), open), _defineProperty(_cx2, "".concat(componentClass, "--batch"), showActionSet), _defineProperty(_cx2, "".concat(componentClass, "--instant"), !showActionSet), _cx2))
355
+ }, /*#__PURE__*/React.createElement("div", {
356
+ className: "".concat(componentClass, "__inner-container")
357
+ }, /*#__PURE__*/React.createElement("span", {
358
+ className: "".concat(componentClass, "__title")
359
+ }, title), /*#__PURE__*/React.createElement("div", {
360
+ className: "".concat(componentClass, "__filters")
361
+ }, renderFilters())), renderActionSet()));
362
+ };
363
+
364
+ FilterFlyout.propTypes = {
365
+ /**
366
+ * Array of filters to render
367
+ */
368
+ filters: PropTypes.arrayOf(PropTypes.shape({
369
+ type: PropTypes.string.isRequired,
370
+ column: PropTypes.string.isRequired,
371
+ props: PropTypes.object.isRequired
372
+ })).isRequired,
373
+
374
+ /**
375
+ * Icon description for the filter flyout button
376
+ */
377
+ flyoutIconDescription: PropTypes.string,
378
+
379
+ /**
380
+ * Callback when the apply button is clicked
381
+ */
382
+ onApply: PropTypes.func,
383
+
384
+ /**
385
+ * Callback when the cancel button is clicked
386
+ */
387
+ onCancel: PropTypes.func,
388
+
389
+ /**
390
+ * Callback when the flyout closes
391
+ */
392
+ onFlyoutClose: PropTypes.func,
393
+
394
+ /**
395
+ * Callback when the flyout opens
396
+ */
397
+ onFlyoutOpen: PropTypes.func,
398
+
399
+ /**
400
+ * Label text of the primary action in the flyout
401
+ */
402
+ primaryActionLabel: PropTypes.string,
403
+
404
+ /**
405
+ * Label text of the secondary action in the flyout
406
+ */
407
+ secondaryActionLabel: PropTypes.string,
408
+
409
+ /**
410
+ * Function that sets all the filters, this comes from the datagridState
411
+ */
412
+ setAllFilters: PropTypes.func.isRequired,
413
+
414
+ /**
415
+ * Title of the filter flyout
416
+ */
417
+ title: PropTypes.string,
418
+
419
+ /**
420
+ * The update method used to apply the filters
421
+ */
422
+ updateMethod: PropTypes.oneOf([BATCH, INSTANT]).isRequired
423
+ };
424
+ export default FilterFlyout;
@@ -0,0 +1,83 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
2
+
3
+ /**
4
+ * Copyright IBM Corp. 2022, 2022
5
+ *
6
+ * This source code is licensed under the Apache-2.0 license found in the
7
+ * LICENSE file in the root directory of this source tree.
8
+ */
9
+ import React, { createContext } from 'react';
10
+ import PropTypes from 'prop-types';
11
+ import { DATE, DROPDOWN, NUMBER, RADIO, CHECKBOX } from './constants';
12
+ export var FilterContext = /*#__PURE__*/createContext();
13
+ var EventEmitter = {
14
+ events: {},
15
+ dispatch: function dispatch(event, data) {
16
+ if (!this.events[event]) {
17
+ return;
18
+ }
19
+
20
+ this.events[event].forEach(function (callback) {
21
+ return callback(data);
22
+ });
23
+ },
24
+ subscribe: function subscribe(event, callback) {
25
+ if (!this.events[event]) {
26
+ this.events[event] = [];
27
+ } else {
28
+ this.events[event].push(callback);
29
+ }
30
+ }
31
+ };
32
+
33
+ var prepareFiltersForTags = function prepareFiltersForTags(filters) {
34
+ var tags = [];
35
+ filters.forEach(function (_ref) {
36
+ var id = _ref.id,
37
+ type = _ref.type,
38
+ value = _ref.value;
39
+
40
+ if (type === DROPDOWN || type === RADIO || type === NUMBER) {
41
+ tags.push({
42
+ key: id,
43
+ value: value
44
+ });
45
+ } else if (type === DATE) {
46
+ var _value = _slicedToArray(value, 2),
47
+ startDate = _value[0],
48
+ endDate = _value[1];
49
+
50
+ tags.push({
51
+ key: id,
52
+ value: "".concat(startDate.toLocaleDateString(), " - ").concat(endDate.toLocaleDateString())
53
+ });
54
+ } else if (type === CHECKBOX) {
55
+ value.forEach(function (checkbox) {
56
+ if (checkbox.selected) {
57
+ tags.push({
58
+ key: id,
59
+ value: checkbox.value
60
+ });
61
+ }
62
+ });
63
+ }
64
+ });
65
+ return tags;
66
+ };
67
+
68
+ export var FilterProvider = function FilterProvider(_ref2) {
69
+ var children = _ref2.children,
70
+ filters = _ref2.filters;
71
+ var filterTags = prepareFiltersForTags(filters);
72
+ var value = {
73
+ filterTags: filterTags,
74
+ EventEmitter: EventEmitter
75
+ };
76
+ return /*#__PURE__*/React.createElement(FilterContext.Provider, {
77
+ value: value
78
+ }, children);
79
+ };
80
+ FilterProvider.propTypes = {
81
+ children: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.node), PropTypes.node]).isRequired,
82
+ filters: PropTypes.arrayOf(PropTypes.object).isRequired
83
+ };
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Copyright IBM Corp. 2022, 2022
3
+ *
4
+ * This source code is licensed under the Apache-2.0 license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ /** Constants for update methods can either be batch or instant */
9
+ export var BATCH = 'batch';
10
+ export var INSTANT = 'instant';
11
+ /** Constants for filter type */
12
+
13
+ export var DATE = 'date';
14
+ export var NUMBER = 'number';
15
+ export var CHECKBOX = 'checkbox';
16
+ export var RADIO = 'radio';
17
+ export var DROPDOWN = 'dropdown';
18
+ /** Constants for event emitters */
19
+
20
+ export var CLEAR_FILTERS = 'clearFilters';
@@ -0,0 +1,22 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
2
+
3
+ /*
4
+ * Licensed Materials - Property of IBM
5
+ * 5724-Q36
6
+ * (c) Copyright IBM Corp. 2022
7
+ * US Government Users Restricted Rights - Use, duplication or disclosure
8
+ * restricted by GSA ADP Schedule Contract with IBM Corp.
9
+ */
10
+ import { useState } from 'react';
11
+ import { getInitialStateFromFilters } from '../utils';
12
+
13
+ var useInitialStateFromFilters = function useInitialStateFromFilters(filters) {
14
+ var _useState = useState(getInitialStateFromFilters(filters)),
15
+ _useState2 = _slicedToArray(_useState, 2),
16
+ state = _useState2[0],
17
+ setState = _useState2[1];
18
+
19
+ return [state, setState];
20
+ };
21
+
22
+ export default useInitialStateFromFilters;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Copyright IBM Corp. 2022, 2022
3
+ *
4
+ * This source code is licensed under the Apache-2.0 license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ export { default as FilterFlyout } from './FilterFlyout';
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Copyright IBM Corp. 2022, 2022
3
+ *
4
+ * This source code is licensed under the Apache-2.0 license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ // This functions takes the filters passed in and makes an object to track it's state
8
+ export var getInitialStateFromFilters = function getInitialStateFromFilters(filters) {
9
+ var initialFilterState = {};
10
+ filters.forEach(function (_ref) {
11
+ var type = _ref.type,
12
+ column = _ref.column,
13
+ props = _ref.props;
14
+
15
+ if (type === 'checkbox') {
16
+ initialFilterState[column] = props.Checkbox.map(function (_ref2) {
17
+ var id = _ref2.id,
18
+ labelText = _ref2.labelText,
19
+ value = _ref2.value;
20
+ return {
21
+ id: id,
22
+ labelText: labelText,
23
+ value: value,
24
+ selected: false
25
+ };
26
+ });
27
+ } else if (type === 'date') {
28
+ initialFilterState[column] = [undefined, undefined];
29
+ } else if (type === 'number') {
30
+ initialFilterState[column] = '';
31
+ } else if (type === 'radio') {
32
+ initialFilterState[column] = '';
33
+ } else if (type === 'dropdown') {
34
+ initialFilterState[column] = '';
35
+ }
36
+ });
37
+ return initialFilterState;
38
+ };
@@ -22,4 +22,5 @@ export { default as useCustomizeColumns } from './useCustomizeColumns';
22
22
  export { default as useSelectAllWithToggle } from './useSelectAllToggle';
23
23
  export { default as useColumnCenterAlign } from './useColumnCenterAlign';
24
24
  export { default as useColumnOrder } from './useColumnOrder';
25
- export { default as useInlineEdit } from './useInlineEdit';
25
+ export { default as useInlineEdit } from './useInlineEdit';
26
+ export { default as useFiltering } from './useFiltering';