grommet 2.29.0 → 2.29.1

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.
Files changed (115) hide show
  1. package/components/Button/Button.js +4 -12
  2. package/components/Button/StyledButtonKind.js +1 -3
  3. package/components/Calendar/Calendar.js +66 -65
  4. package/components/Data/Data.js +48 -26
  5. package/components/Data/DataForm.js +132 -42
  6. package/components/Data/filter.js +17 -6
  7. package/components/Data/index.d.ts +12 -7
  8. package/components/Data/index.js +6 -2
  9. package/components/Data/propTypes.js +3 -2
  10. package/components/Data/stories/Infinite.js +52 -56
  11. package/components/Data/stories/SpaceX.js +113 -139
  12. package/components/Data/stories/StarWars.js +133 -0
  13. package/components/Data/stories/Views.js +56 -0
  14. package/components/DataFilter/DataFilter.js +5 -4
  15. package/components/DataFilters/DataFilters.js +45 -12
  16. package/components/DataSearch/DataSearch.js +17 -8
  17. package/components/DataSort/DataSort.js +6 -6
  18. package/components/DataSummary/DataSummary.js +4 -4
  19. package/components/DataSummary/index.d.ts +1 -1
  20. package/components/DataSummary/propTypes.js +2 -2
  21. package/components/DataTable/Cell.js +1 -1
  22. package/components/DataTable/DataTable.js +23 -9
  23. package/components/DataTable/Resizer.js +1 -3
  24. package/components/DataTable/index.d.ts +23 -6
  25. package/components/DataTable/stories/SpaceX.js +72 -76
  26. package/components/DataTable/stories/SpaceXUngrouped.js +33 -35
  27. package/components/DataTableColumns/DataTableColumns.js +225 -0
  28. package/components/DataTableColumns/README.md +2 -0
  29. package/components/DataTableColumns/index.d.ts +10 -0
  30. package/components/DataTableColumns/index.js +6 -0
  31. package/components/DataTableColumns/propTypes.js +18 -0
  32. package/components/DataTableColumns/stories/Simple.js +47 -0
  33. package/components/DataView/DataView.js +58 -0
  34. package/components/DataView/README.md +2 -0
  35. package/components/DataView/index.d.ts +15 -0
  36. package/components/DataView/index.js +6 -0
  37. package/components/DataView/propTypes.js +10 -0
  38. package/components/DataView/stories/Simple.js +48 -0
  39. package/components/DateInput/utils.js +1 -3
  40. package/components/Drop/DropContainer.js +25 -22
  41. package/components/Form/stories/ValidateOnBlur.js +18 -0
  42. package/components/FormField/FormField.js +14 -4
  43. package/components/Layer/LayerContainer.js +1 -2
  44. package/components/Layer/PortalContext.js +1 -1
  45. package/components/Layer/stories/Form.js +54 -28
  46. package/components/List/List.js +1 -8
  47. package/components/Pagination/Pagination.js +19 -8
  48. package/components/Select/utils.js +1 -1
  49. package/components/Skeleton/stories/GridContainer.js +1 -3
  50. package/components/Video/Video.js +4 -8
  51. package/components/WorldMap/WorldMap.js +1 -3
  52. package/components/index.d.ts +2 -0
  53. package/components/index.js +18 -6
  54. package/es6/components/Button/Button.js +4 -12
  55. package/es6/components/Button/StyledButtonKind.js +1 -3
  56. package/es6/components/Calendar/Calendar.js +66 -65
  57. package/es6/components/Data/Data.js +48 -26
  58. package/es6/components/Data/DataForm.js +128 -45
  59. package/es6/components/Data/filter.js +17 -6
  60. package/es6/components/Data/index.d.ts +12 -7
  61. package/es6/components/Data/index.js +1 -1
  62. package/es6/components/Data/propTypes.js +3 -2
  63. package/es6/components/Data/stories/Infinite.js +52 -56
  64. package/es6/components/Data/stories/SpaceX.js +113 -139
  65. package/es6/components/Data/stories/StarWars.js +127 -0
  66. package/es6/components/Data/stories/Views.js +50 -0
  67. package/es6/components/DataFilter/DataFilter.js +5 -4
  68. package/es6/components/DataFilters/DataFilters.js +45 -12
  69. package/es6/components/DataSearch/DataSearch.js +17 -8
  70. package/es6/components/DataSort/DataSort.js +5 -5
  71. package/es6/components/DataSummary/DataSummary.js +4 -4
  72. package/es6/components/DataSummary/index.d.ts +1 -1
  73. package/es6/components/DataSummary/propTypes.js +2 -2
  74. package/es6/components/DataTable/Cell.js +2 -2
  75. package/es6/components/DataTable/DataTable.js +23 -9
  76. package/es6/components/DataTable/Resizer.js +1 -3
  77. package/es6/components/DataTable/index.d.ts +23 -6
  78. package/es6/components/DataTable/stories/SpaceX.js +72 -76
  79. package/es6/components/DataTable/stories/SpaceXUngrouped.js +33 -35
  80. package/es6/components/DataTableColumns/DataTableColumns.js +218 -0
  81. package/es6/components/DataTableColumns/README.md +2 -0
  82. package/es6/components/DataTableColumns/index.d.ts +10 -0
  83. package/es6/components/DataTableColumns/index.js +1 -0
  84. package/es6/components/DataTableColumns/propTypes.js +12 -0
  85. package/es6/components/DataTableColumns/stories/Simple.js +42 -0
  86. package/es6/components/DataView/DataView.js +51 -0
  87. package/es6/components/DataView/README.md +2 -0
  88. package/es6/components/DataView/index.d.ts +15 -0
  89. package/es6/components/DataView/index.js +1 -0
  90. package/es6/components/DataView/propTypes.js +5 -0
  91. package/es6/components/DataView/stories/Simple.js +42 -0
  92. package/es6/components/DateInput/utils.js +1 -3
  93. package/es6/components/Drop/DropContainer.js +25 -22
  94. package/es6/components/Form/stories/ValidateOnBlur.js +19 -1
  95. package/es6/components/FormField/FormField.js +14 -4
  96. package/es6/components/Layer/LayerContainer.js +1 -2
  97. package/es6/components/Layer/PortalContext.js +1 -1
  98. package/es6/components/Layer/stories/Form.js +56 -29
  99. package/es6/components/List/List.js +1 -8
  100. package/es6/components/Pagination/Pagination.js +19 -8
  101. package/es6/components/Select/utils.js +1 -1
  102. package/es6/components/Skeleton/stories/GridContainer.js +1 -3
  103. package/es6/components/Video/Video.js +4 -8
  104. package/es6/components/WorldMap/WorldMap.js +1 -3
  105. package/es6/components/index.d.ts +2 -0
  106. package/es6/components/index.js +3 -1
  107. package/es6/languages/default.json +7 -2
  108. package/es6/utils/DOM.js +17 -0
  109. package/es6/utils/PortalContext.js +1 -1
  110. package/grommet.min.js +1 -1
  111. package/grommet.min.js.map +1 -1
  112. package/languages/default.json +7 -2
  113. package/package.json +31 -31
  114. package/utils/DOM.js +20 -2
  115. package/utils/PortalContext.js +1 -1
@@ -583,7 +583,7 @@ var Calendar = /*#__PURE__*/forwardRef(function (_ref3, ref) {
583
583
  var days;
584
584
  var firstDayInMonth;
585
585
  var blankWeek = false;
586
- while (day.getTime() < displayBounds[1].getTime()) {
586
+ var _loop = function _loop() {
587
587
  if (day.getDay() === firstDayOfWeek) {
588
588
  if (days) {
589
589
  weeks.push( /*#__PURE__*/React.createElement(StyledWeek, {
@@ -627,73 +627,74 @@ var Calendar = /*#__PURE__*/forwardRef(function (_ref3, ref) {
627
627
  fillContainer: fill
628
628
  })));
629
629
  } else {
630
- (function () {
631
- var dateObject = day;
632
- // this.dayRefs[dateObject] = React.createRef();
633
- var selected = false;
634
- var inRange = false;
635
- var selectedState = withinDates(day, range ? normalizeRange(value, activeDate) : value);
636
- if (selectedState === 2) {
637
- selected = true;
638
- } else if (selectedState === 1) {
639
- inRange = true;
640
- }
641
- var dayDisabled = withinDates(day, normalizeInput(disabled)) || bounds && !betweenDates(day, normalizeInput(bounds));
642
- if (!firstDayInMonth && !dayDisabled && day.getMonth() === reference.getMonth()) {
643
- firstDayInMonth = dateObject;
644
- }
645
- if (!children) {
646
- days.push( /*#__PURE__*/React.createElement(CalendarDay, {
647
- key: day.getTime(),
648
- buttonProps: {
649
- a11yTitle: day.toDateString(),
650
- active: active && active.getTime() === day.getTime(),
651
- disabled: dayDisabled && !!dayDisabled,
652
- onClick: function onClick() {
653
- return _onClick(dateObject);
654
- },
655
- onMouseOver: function onMouseOver() {
656
- return setActive(dateObject);
657
- },
658
- onMouseOut: function onMouseOut() {
659
- return setActive(undefined);
660
- }
630
+ var dateObject = day;
631
+ // this.dayRefs[dateObject] = React.createRef();
632
+ var selected = false;
633
+ var inRange = false;
634
+ var selectedState = withinDates(day, range ? normalizeRange(value, activeDate) : value);
635
+ if (selectedState === 2) {
636
+ selected = true;
637
+ } else if (selectedState === 1) {
638
+ inRange = true;
639
+ }
640
+ var dayDisabled = withinDates(day, normalizeInput(disabled)) || bounds && !betweenDates(day, normalizeInput(bounds));
641
+ if (!firstDayInMonth && !dayDisabled && day.getMonth() === reference.getMonth()) {
642
+ firstDayInMonth = dateObject;
643
+ }
644
+ if (!children) {
645
+ days.push( /*#__PURE__*/React.createElement(CalendarDay, {
646
+ key: day.getTime(),
647
+ buttonProps: {
648
+ a11yTitle: day.toDateString(),
649
+ active: active && active.getTime() === day.getTime(),
650
+ disabled: dayDisabled && !!dayDisabled,
651
+ onClick: function onClick() {
652
+ return _onClick(dateObject);
661
653
  },
662
- isInRange: inRange,
663
- isSelected: selected,
664
- otherMonth: day.getMonth() !== reference.getMonth(),
665
- size: size,
666
- fill: fill
667
- }, day.getDate()));
668
- } else {
669
- days.push( /*#__PURE__*/React.createElement(CalendarCustomDay, {
670
- key: day.getTime(),
671
- buttonProps: onSelect ? {
672
- a11yTitle: day.toDateString(),
673
- active: active && active.getTime() === day.getTime(),
674
- disabled: dayDisabled && !!dayDisabled,
675
- onClick: function onClick() {
676
- return _onClick(dateObject);
677
- },
678
- onMouseOver: function onMouseOver() {
679
- return setActive(dateObject);
680
- },
681
- onMouseOut: function onMouseOut() {
682
- return setActive(undefined);
683
- }
684
- } : null,
685
- size: size,
686
- fill: fill
687
- }, children({
688
- date: day,
689
- day: day.getDate(),
690
- isInRange: inRange,
691
- isSelected: selected
692
- })));
693
- }
694
- })();
654
+ onMouseOver: function onMouseOver() {
655
+ return setActive(dateObject);
656
+ },
657
+ onMouseOut: function onMouseOut() {
658
+ return setActive(undefined);
659
+ }
660
+ },
661
+ isInRange: inRange,
662
+ isSelected: selected,
663
+ otherMonth: day.getMonth() !== reference.getMonth(),
664
+ size: size,
665
+ fill: fill
666
+ }, day.getDate()));
667
+ } else {
668
+ days.push( /*#__PURE__*/React.createElement(CalendarCustomDay, {
669
+ key: day.getTime(),
670
+ buttonProps: onSelect ? {
671
+ a11yTitle: day.toDateString(),
672
+ active: active && active.getTime() === day.getTime(),
673
+ disabled: dayDisabled && !!dayDisabled,
674
+ onClick: function onClick() {
675
+ return _onClick(dateObject);
676
+ },
677
+ onMouseOver: function onMouseOver() {
678
+ return setActive(dateObject);
679
+ },
680
+ onMouseOut: function onMouseOut() {
681
+ return setActive(undefined);
682
+ }
683
+ } : null,
684
+ size: size,
685
+ fill: fill
686
+ }, children({
687
+ date: day,
688
+ day: day.getDate(),
689
+ isInRange: inRange,
690
+ isSelected: selected
691
+ })));
692
+ }
695
693
  }
696
694
  day = addDays(day, 1);
695
+ };
696
+ while (day.getTime() < displayBounds[1].getTime()) {
697
+ _loop();
697
698
  }
698
699
  weeks.push( /*#__PURE__*/React.createElement(StyledWeek
699
700
  // if a week contains only blank days, for screen reader accessibility
@@ -1,4 +1,4 @@
1
- var _excluded = ["children", "data", "id", "messages", "onView", "properties", "toolbar", "total", "updateOn", "view"];
1
+ var _excluded = ["children", "data", "defaultView", "filteredTotal", "id", "messages", "onView", "properties", "toolbar", "total", "updateOn", "view", "views"];
2
2
  function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
3
3
  function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
4
4
  import React, { useEffect, useMemo, useState } from 'react';
@@ -6,17 +6,25 @@ import { Box } from '../Box';
6
6
  import { DataFilters } from '../DataFilters';
7
7
  import { DataSearch } from '../DataSearch';
8
8
  import { DataSummary } from '../DataSummary';
9
+ import { DataView } from '../DataView';
9
10
  import { Toolbar } from '../Toolbar';
10
11
  import { DataContext } from '../../contexts/DataContext';
11
12
  import { DataPropTypes } from './propTypes';
12
13
  import { filter } from './filter';
13
- var defaultView = {
14
- search: '',
15
- properties: {}
14
+ var defaultDefaultView = {
15
+ search: ''
16
+ };
17
+ var normalizeView = function normalizeView(viewProp, views) {
18
+ return typeof viewProp === 'string' && (views == null ? void 0 : views.find(function (v) {
19
+ return v.name === viewProp;
20
+ })) || typeof viewProp === 'object' && viewProp;
16
21
  };
17
22
  export var Data = function Data(_ref) {
18
23
  var children = _ref.children,
19
24
  dataProp = _ref.data,
25
+ _ref$defaultView = _ref.defaultView,
26
+ defaultView = _ref$defaultView === void 0 ? defaultDefaultView : _ref$defaultView,
27
+ filteredTotal = _ref.filteredTotal,
20
28
  _ref$id = _ref.id,
21
29
  id = _ref$id === void 0 ? 'data' : _ref$id,
22
30
  messages = _ref.messages,
@@ -27,48 +35,62 @@ export var Data = function Data(_ref) {
27
35
  _ref$updateOn = _ref.updateOn,
28
36
  updateOn = _ref$updateOn === void 0 ? 'submit' : _ref$updateOn,
29
37
  viewProp = _ref.view,
38
+ views = _ref.views,
30
39
  rest = _objectWithoutPropertiesLoose(_ref, _excluded);
31
- var _useState = useState(viewProp),
40
+ var _useState = useState(normalizeView(viewProp || defaultView, views)),
32
41
  view = _useState[0],
33
42
  setView = _useState[1];
34
43
  useEffect(function () {
35
- return setView(viewProp);
36
- }, [viewProp]);
37
- var data = useMemo(function () {
38
- if (onView) return dataProp;
44
+ return setView(normalizeView(viewProp || defaultView, views));
45
+ }, [defaultView, viewProp, views]);
46
+ var _useState2 = useState([]),
47
+ toolbarKeys = _useState2[0],
48
+ setToolbarKeys = _useState2[1];
49
+ var result = useMemo(function () {
50
+ var _ref2;
51
+ if (onView)
52
+ // caller is filtering
53
+ return {
54
+ data: dataProp,
55
+ total: total,
56
+ filteredTotal: (_ref2 = filteredTotal != null ? filteredTotal : dataProp == null ? void 0 : dataProp.length) != null ? _ref2 : 0
57
+ };
39
58
  return filter(dataProp, view, properties);
40
- }, [dataProp, onView, properties, view]);
59
+ }, [dataProp, filteredTotal, onView, properties, total, view]);
41
60
 
42
61
  // what we use for DataContext value
43
62
  var contextValue = useMemo(function () {
44
- var result = {
63
+ var value = _extends({
45
64
  id: id,
46
65
  messages: messages,
47
66
  properties: properties,
48
67
  updateOn: updateOn,
49
- view: view
68
+ view: view,
69
+ views: views
70
+ }, result);
71
+ value.clearFilters = function () {
72
+ var nextView = defaultView;
73
+ setView(nextView);
74
+ if (onView) onView(nextView);
50
75
  };
51
- if (view != null && view.search || view != null && view.sort || view != null && view.properties && Object.keys(view.properties).length) {
52
- result.clearFilters = function () {
53
- var nextView = defaultView;
54
- setView(nextView);
55
- if (onView) onView(nextView);
56
- };
57
- }
58
- result.onView = function (nextView) {
76
+ value.onView = function (nextView) {
59
77
  setView(nextView);
60
78
  if (onView) onView(nextView);
61
79
  };
62
- result.data = data;
63
- result.unfilteredData = dataProp;
64
- result.total = total !== undefined ? total : dataProp.length;
65
- return result;
66
- }, [data, dataProp, id, messages, onView, properties, total, updateOn, view]);
80
+ value.addToolbarKey = function (key) {
81
+ setToolbarKeys(function (prevKeys) {
82
+ if (prevKeys.includes(key)) return prevKeys;
83
+ return [].concat(prevKeys, [key]);
84
+ });
85
+ };
86
+ value.toolbarKeys = toolbarKeys;
87
+ return value;
88
+ }, [defaultView, id, messages, onView, properties, result, toolbarKeys, updateOn, view, views]);
67
89
  var toolbarContent;
68
90
  if (toolbar) {
69
91
  toolbarContent = [/*#__PURE__*/React.createElement(Toolbar, {
70
92
  key: "toolbar"
71
- }, (toolbar === true || toolbar === 'search') && /*#__PURE__*/React.createElement(DataSearch, null), (toolbar === true || toolbar === 'filters') && /*#__PURE__*/React.createElement(DataFilters, {
93
+ }, (toolbar === true || toolbar === 'search') && /*#__PURE__*/React.createElement(DataSearch, null), (toolbar === true || toolbar === 'view') && /*#__PURE__*/React.createElement(DataView, null), (toolbar === true || toolbar === 'filters') && /*#__PURE__*/React.createElement(DataFilters, {
72
94
  drop: true
73
95
  })), /*#__PURE__*/React.createElement(DataSummary, {
74
96
  key: "summary"
@@ -1,7 +1,7 @@
1
- var _excluded = ["children", "footer", "gap", "onDone", "pad"];
1
+ var _excluded = ["children", "footer", "gap", "onDone", "onTouched", "pad", "updateOn"];
2
2
  function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
3
3
  function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
4
- import React, { useContext, useEffect, useState } from 'react';
4
+ import React, { useCallback, useContext, useEffect, useState } from 'react';
5
5
  import styled from 'styled-components';
6
6
  import { Box } from '../Box';
7
7
  import { Button } from '../Button';
@@ -24,9 +24,21 @@ var hideButtonProps = {
24
24
  // We convert the view structure to something more flat to work better
25
25
  // with the Form inputs. These keys are how we flatten the Form value object
26
26
  // from the view object.
27
- var formSearchKey = '_search';
28
- var formSortKey = '_sort';
29
- var formRangeKey = '_range';
27
+ export var formSearchKey = '_search';
28
+ export var formSortKey = '_sort';
29
+ export var formRangeKey = '_range';
30
+ export var formStepKey = '_step';
31
+ export var formPageKey = '_page';
32
+ export var formColumnsKey = '_columns';
33
+ export var formViewNameKey = '_view';
34
+ var viewFormKeyMap = {
35
+ search: formSearchKey,
36
+ sort: formSortKey,
37
+ step: formStepKey,
38
+ page: formPageKey,
39
+ columns: formColumnsKey,
40
+ view: formViewNameKey
41
+ };
30
42
 
31
43
  // converts from the external view format to the internal Form value format
32
44
  var viewToFormValue = function viewToFormValue(view) {
@@ -39,24 +51,35 @@ var viewToFormValue = function viewToFormValue(view) {
39
51
  result[key] = (_result$key3 = {}, _result$key3[formRangeKey] = [result[key].min, result[key].max], _result$key3);
40
52
  }
41
53
  });
42
- result[formSearchKey] = (view == null ? void 0 : view.search) || '';
43
- if (view != null && view.sort) result[formSortKey] = view == null ? void 0 : view.sort;
54
+
55
+ // convert formal view keys to their form '_' prefixed counterparts
56
+ Object.keys(viewFormKeyMap).forEach(function (key) {
57
+ if (view != null && view[key]) result[viewFormKeyMap[key]] = view[key];
58
+ });
59
+ // always have some blank search text
60
+ if (!result[formSearchKey]) result[formSearchKey] = '';
61
+ if (view != null && view.sort) result[formSortKey] = view.sort;
62
+ if (view != null && view.name) result[formViewNameKey] = view.name;
63
+ if (view != null && view.columns) result[formColumnsKey] = view.columns;
44
64
  return result;
45
65
  };
46
66
 
47
67
  // converts from the internal Form value format to the external view format
48
- var formValueToView = function formValueToView(value) {
49
- var properties = _extends({}, value);
50
- var searchText = value[formSearchKey];
51
- delete properties[formSearchKey];
52
- var sort = value[formSortKey];
53
- delete properties[formSortKey];
54
- var result = _extends({
55
- properties: properties,
56
- search: searchText
57
- }, sort ? {
58
- sort: sort
59
- } : {});
68
+ var formValueToView = function formValueToView(value, views) {
69
+ var result = {};
70
+
71
+ // if the user chose a view, use that
72
+ if (value[formViewNameKey]) result = JSON.parse(JSON.stringify(views.find(function (v) {
73
+ return v.name === value[formViewNameKey];
74
+ })));
75
+ var valueCopy = _extends({}, value);
76
+ Object.keys(viewFormKeyMap).forEach(function (key) {
77
+ if (valueCopy[viewFormKeyMap[key]]) {
78
+ result[key] = valueCopy[viewFormKeyMap[key]];
79
+ }
80
+ delete valueCopy[viewFormKeyMap[key]];
81
+ });
82
+ result.properties = _extends({}, result.properties || {}, valueCopy);
60
83
 
61
84
  // convert any ranges
62
85
  Object.keys(result.properties).forEach(function (key) {
@@ -72,27 +95,76 @@ var formValueToView = function formValueToView(value) {
72
95
 
73
96
  // remove any empty arrays of property values by deleting the key for
74
97
  // that property in the view properties
75
- var clearEmpty = function clearEmpty(properties) {
76
- var result = properties;
77
- Object.keys(result).filter(function (k) {
78
- return k !== formSearchKey;
79
- }).forEach(function (k) {
80
- if (Array.isArray(result[k]) && result[k].length === 0) delete result[k];
98
+ var clearEmpty = function clearEmpty(formValue) {
99
+ var value = formValue;
100
+ Object.keys(value).forEach(function (k) {
101
+ if (Array.isArray(value[k]) && value[k].length === 0) delete value[k];
102
+ });
103
+ return value;
104
+ };
105
+
106
+ // if paging, when anything other than the page changes, reset the page to 1
107
+ var resetPage = function resetPage(nextFormValue, prevFormValue) {
108
+ if (prevFormValue[formPageKey] && prevFormValue[formPageKey] > 1)
109
+ // eslint-disable-next-line no-param-reassign
110
+ nextFormValue[formPageKey] = 1;
111
+ };
112
+ var transformTouched = function transformTouched(touched, value) {
113
+ var result = {};
114
+ Object.keys(touched).forEach(function (key) {
115
+ // special case _range fields
116
+ var parts = key.split('.');
117
+ if (parts[1] === formRangeKey) result[key] = value[parts[0]];else result[key] = value[key];
81
118
  });
82
119
  return result;
83
120
  };
121
+
122
+ // function shared by onSubmit and onChange to coordinate view
123
+ // name changes
124
+ var normalizeValue = function normalizeValue(nextValue, prevValue, views) {
125
+ if (nextValue[formViewNameKey] && nextValue[formViewNameKey] !== prevValue[formViewNameKey]) {
126
+ // view name changed, reset view contents from named view
127
+ return viewToFormValue(views.find(function (v) {
128
+ return v.name === nextValue[formViewNameKey];
129
+ }));
130
+ }
131
+
132
+ // something else changed, clear empty propeties
133
+ var result = clearEmpty(nextValue);
134
+ // if we have a view and something related to it changed, clear the view
135
+ if (result[formViewNameKey]) {
136
+ var view = views.find(function (v) {
137
+ return v.name === result[formViewNameKey];
138
+ });
139
+ var viewValue = viewToFormValue(view);
140
+ clearEmpty(viewValue);
141
+ if (Object.keys(viewValue).some(function (k) {
142
+ return (
143
+ // allow mismatch between empty and set strings
144
+ viewValue[k] && result[k] && JSON.stringify(result[k]) !== JSON.stringify(viewValue[k])
145
+ );
146
+ })) {
147
+ delete result[formViewNameKey];
148
+ }
149
+ }
150
+ return result;
151
+ };
84
152
  export var DataForm = function DataForm(_ref) {
85
153
  var children = _ref.children,
86
154
  footer = _ref.footer,
87
155
  gap = _ref.gap,
88
156
  onDone = _ref.onDone,
157
+ onTouched = _ref.onTouched,
89
158
  pad = _ref.pad,
159
+ updateOnProp = _ref.updateOn,
90
160
  rest = _objectWithoutPropertiesLoose(_ref, _excluded);
91
161
  var _useContext = useContext(DataContext),
92
162
  messages = _useContext.messages,
93
163
  onView = _useContext.onView,
94
- updateOn = _useContext.updateOn,
95
- view = _useContext.view;
164
+ updateOnData = _useContext.updateOn,
165
+ view = _useContext.view,
166
+ views = _useContext.views;
167
+ var updateOn = updateOnProp != null ? updateOnProp : updateOnData;
96
168
  var _useContext2 = useContext(MessageContext),
97
169
  format = _useContext2.format;
98
170
  var _useState = useState(viewToFormValue(view)),
@@ -101,25 +173,39 @@ export var DataForm = function DataForm(_ref) {
101
173
  var _useState2 = useState(),
102
174
  changed = _useState2[0],
103
175
  setChanged = _useState2[1];
176
+ var onSubmit = useCallback(function (_ref2) {
177
+ var value = _ref2.value,
178
+ touched = _ref2.touched;
179
+ var nextValue = normalizeValue(value, formValue, views);
180
+ resetPage(nextValue, formValue);
181
+ setFormValue(nextValue);
182
+ setChanged(false);
183
+ if (onTouched) onTouched(transformTouched(touched, nextValue));
184
+ onView(formValueToView(nextValue, views));
185
+ if (onDone) onDone();
186
+ }, [formValue, onDone, onTouched, onView, views]);
187
+ var onChange = useCallback(function (value, _ref3) {
188
+ var touched = _ref3.touched;
189
+ var nextValue = normalizeValue(value, formValue, views);
190
+ resetPage(nextValue, formValue);
191
+ setFormValue(nextValue);
192
+ setChanged(true);
193
+ if (updateOn === 'change') {
194
+ if (onTouched) onTouched(transformTouched(touched, nextValue));
195
+ onView(formValueToView(nextValue, views));
196
+ }
197
+ }, [formValue, onTouched, onView, updateOn, views]);
198
+ var onReset = useCallback(function () {
199
+ setFormValue(viewToFormValue(view));
200
+ setChanged(false);
201
+ }, [view]);
104
202
  useEffect(function () {
105
203
  return setFormValue(viewToFormValue(view));
106
204
  }, [view]);
107
205
  return /*#__PURE__*/React.createElement(Form, _extends({}, rest, {
108
206
  value: formValue,
109
- onSubmit: updateOn === 'submit' ? function (_ref2) {
110
- var nextValue = _ref2.value;
111
- clearEmpty(nextValue);
112
- setFormValue(nextValue);
113
- setChanged(false);
114
- onView(formValueToView(nextValue));
115
- if (onDone) onDone();
116
- } : undefined,
117
- onChange: function onChange(nextValue) {
118
- clearEmpty(nextValue);
119
- setFormValue(nextValue);
120
- setChanged(true);
121
- if (updateOn === 'change') onView(formValueToView(nextValue));
122
- }
207
+ onSubmit: updateOn === 'submit' ? onSubmit : undefined,
208
+ onChange: onChange
123
209
  }), /*#__PURE__*/React.createElement(Box, {
124
210
  flex: false,
125
211
  pad: pad,
@@ -137,9 +223,6 @@ export var DataForm = function DataForm(_ref) {
137
223
  messages: messages == null ? void 0 : messages.dataForm
138
224
  }),
139
225
  type: "reset",
140
- onClick: function onClick() {
141
- setFormValue(viewToFormValue(view));
142
- setChanged(false);
143
- }
226
+ onClick: onReset
144
227
  }, !changed ? hideButtonProps : {})))));
145
228
  };
@@ -7,14 +7,14 @@ var datumValue = function datumValue(datum, property) {
7
7
  return datumValue(datum[parts[0]], parts.slice(1).join('.'));
8
8
  };
9
9
 
10
- // This is where we filter the data internall, when the caller doesn't
10
+ // This is where we filter the data internally, when the caller doesn't
11
11
  // provide an onView.
12
12
  export var filter = function filter(data, view, properties) {
13
13
  var _view$sort, _view$sort2;
14
14
  // from https://stackoverflow.com/a/6300266/8513067
15
15
  var searchExp = view != null && view.search ? new RegExp(view.search.replace(/[#-.]|[[-^]|[?|{}]/g, '\\$&'), 'i') : undefined;
16
16
  var searchProperties;
17
- if (properties) {
17
+ if (searchExp && properties) {
18
18
  // if we know where we want to search, look there
19
19
  searchProperties = Object.keys(properties).filter(function (k) {
20
20
  return properties[k].search;
@@ -22,7 +22,7 @@ export var filter = function filter(data, view, properties) {
22
22
  // if none specified, look in all defined properties
23
23
  if (searchProperties.length === 0) searchProperties = Object.keys(properties);
24
24
  }
25
- var result = data.filter(function (datum) {
25
+ var filteredData = data.filter(function (datum) {
26
26
  var matched = true;
27
27
 
28
28
  // check whether it matches any search
@@ -62,11 +62,11 @@ export var filter = function filter(data, view, properties) {
62
62
  var _view$sort3 = view.sort,
63
63
  property = _view$sort3.property,
64
64
  direction = _view$sort3.direction;
65
- var prop = property || result.length && Object.keys(result[0])[0];
65
+ var prop = property || filteredData.length && Object.keys(filteredData[0])[0];
66
66
  var sortDesc = direction === 'desc'; // default to asc
67
67
  var before = sortDesc ? -1 : 1;
68
68
  var after = sortDesc ? 1 : -1;
69
- result.sort(function (d1, d2) {
69
+ filteredData.sort(function (d1, d2) {
70
70
  var d1Val = datumValue(d1, prop);
71
71
  var d2Val = datumValue(d2, prop);
72
72
  // sort strings via locale case insensitive
@@ -82,5 +82,16 @@ export var filter = function filter(data, view, properties) {
82
82
  return 0;
83
83
  });
84
84
  }
85
- return result;
85
+ var pagedData;
86
+ if (view != null && view.step) {
87
+ var _view$page;
88
+ var start = view.step * (((_view$page = view == null ? void 0 : view.page) != null ? _view$page : 1) - 1);
89
+ pagedData = filteredData.slice(start, start + view.step);
90
+ }
91
+ return {
92
+ unfilteredData: data,
93
+ data: pagedData || filteredData,
94
+ total: data.length,
95
+ filteredTotal: filteredData.length
96
+ };
86
97
  };
@@ -2,10 +2,13 @@ import * as React from 'react';
2
2
  import { BoxProps } from '../Box/index';
3
3
 
4
4
  export interface View {
5
+ name?: string;
6
+ page?: number; // when paging
5
7
  properties?:
6
8
  | string[]
7
9
  | {
8
10
  [property: string]:
11
+ | undefined
9
12
  | boolean
10
13
  | (string | number | boolean)[]
11
14
  | { max: number; min: number };
@@ -15,12 +18,10 @@ export interface View {
15
18
  property: string;
16
19
  direction: 'asc' | 'desc';
17
20
  };
21
+ step?: number; // page size
18
22
 
19
23
  // Future column ordering, requires 'properties' property on Data
20
24
  // columns?: string[];
21
-
22
- // Future name
23
- // name?: string;
24
25
  }
25
26
 
26
27
  export interface DataProps {
@@ -28,8 +29,11 @@ export interface DataProps {
28
29
 
29
30
  data: object[];
30
31
  total?: number;
32
+ // when paging
33
+ filteredTotal?: number;
31
34
 
32
- view?: View;
35
+ defaultView?: View;
36
+ view?: string | View;
33
37
  onView?: (view: View) => void;
34
38
  // when view changes should be delivered
35
39
  updateOn?: 'change' | 'submit';
@@ -82,17 +86,18 @@ export interface DataProps {
82
86
  direction?: string;
83
87
  };
84
88
  dataSummary?: {
85
- filtered?: string;
89
+ filteredTotal?: string;
86
90
  total?: string;
87
91
  };
88
92
  };
89
93
 
94
+ views?: View[];
95
+
90
96
  // Future selection
91
97
  // select: string[];
92
98
  // onSelect: (select: string[]) => void;
93
99
 
94
- // Future named views
95
- // views?: View[];
100
+ // Future manage named views
96
101
  // onCreateView?: (view: View) => void;
97
102
  // onDeleteView?: (string) => void;
98
103
  }
@@ -1,2 +1,2 @@
1
1
  export { Data } from './Data';
2
- export { DataForm } from './DataForm';
2
+ export { DataForm, formColumnsKey, formRangeKey, formSearchKey, formSortKey } from './DataForm';
@@ -1,6 +1,6 @@
1
1
  import PropTypes from 'prop-types';
2
2
  var viewType = PropTypes.shape({
3
- properties: PropTypes.shape({}),
3
+ properties: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.string), PropTypes.shape({})]),
4
4
  search: PropTypes.string,
5
5
  sort: PropTypes.shape({
6
6
  direction: PropTypes.oneOf(['asc', 'desc']),
@@ -11,12 +11,13 @@ var PropType = {};
11
11
  if (process.env.NODE_ENV !== 'production') {
12
12
  PropType = {
13
13
  data: PropTypes.arrayOf(PropTypes.shape({})),
14
+ defaultView: viewType,
14
15
  onView: PropTypes.func,
15
16
  properties: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.string), PropTypes.shape({})]),
16
17
  toolbar: PropTypes.oneOfType([PropTypes.bool, PropTypes.oneOf(['search', 'filters'])]),
17
18
  total: PropTypes.number,
18
19
  updateOn: PropTypes.oneOf(['change', 'submit']),
19
- view: viewType
20
+ view: PropTypes.oneOfType([PropTypes.string, viewType])
20
21
  };
21
22
  }
22
23
  export var DataPropTypes = PropType;