grommet 2.31.0 → 2.32.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 (150) hide show
  1. package/components/Accordion/Accordion.js +4 -2
  2. package/components/Accordion/index.d.ts +1 -0
  3. package/components/Accordion/propTypes.js +1 -0
  4. package/components/AccordionPanel/AccordionPanel.js +2 -1
  5. package/components/Box/Box.js +14 -4
  6. package/components/Box/stories/Border.js +15 -1
  7. package/components/Button/BusyAnimation.js +51 -0
  8. package/components/Button/Button.js +64 -4
  9. package/components/Button/StyledButton.js +4 -2
  10. package/components/Button/StyledButtonKind.js +8 -4
  11. package/components/Button/index.d.ts +7 -0
  12. package/components/Button/propTypes.js +6 -0
  13. package/components/Button/stories/Busy.js +45 -0
  14. package/components/Calendar/stories/Dual.js +4 -6
  15. package/components/Chart/Chart.js +113 -57
  16. package/components/Chart/index.d.ts +3 -0
  17. package/components/Chart/propTypes.js +1 -0
  18. package/components/Chart/stories/Layout.js +6 -3
  19. package/components/Chart/stories/Pattern.js +1 -0
  20. package/components/Chart/stories/Vertical.js +41 -0
  21. package/components/Chart/utils.js +57 -10
  22. package/components/Data/Data.js +2 -1
  23. package/components/Data/DataForm.js +6 -2
  24. package/components/Data/filter.js +12 -1
  25. package/components/Data/stories/Complex.js +63 -6
  26. package/components/Data/stories/Inline.js +19 -2
  27. package/components/DataFilter/DataFilter.js +37 -22
  28. package/components/DataFilters/DataFilters.js +13 -10
  29. package/components/DataTable/Body.js +16 -13
  30. package/components/DataTable/DataTable.js +4 -1
  31. package/components/DataTable/GroupedBody.js +2 -1
  32. package/components/DataTable/Header.js +1 -0
  33. package/components/DataTable/buildState.js +13 -11
  34. package/components/DataTable/stories/Controlled.js +2 -1
  35. package/components/DataTable/stories/ControlledGrouped.js +2 -1
  36. package/components/DataTable/stories/CustomThemed/Custom.js +0 -1
  37. package/components/DataTable/stories/CustomThemed/Fill.js +2 -1
  38. package/components/DataTable/stories/GroupedOnSelect.js +2 -1
  39. package/components/DataTable/stories/NoPrimary.js +0 -1
  40. package/components/DataTable/stories/OnUpdate.js +2 -1
  41. package/components/DataTable/stories/Served.js +2 -1
  42. package/components/DataTable/stories/Tunable.js +2 -1
  43. package/components/DataTable/stories/rowDetails.js +2 -1
  44. package/components/DataTableColumns/DataTableColumns.js +5 -0
  45. package/components/DataTableColumns/stories/Simple.js +4 -3
  46. package/components/DataTableGroupBy/DataTableGroupBy.js +67 -0
  47. package/components/DataTableGroupBy/README.md +2 -0
  48. package/components/DataTableGroupBy/index.d.ts +20 -0
  49. package/components/DataTableGroupBy/index.js +6 -0
  50. package/components/DataTableGroupBy/propTypes.js +17 -0
  51. package/components/DataTableGroupBy/stories/Simple.js +50 -0
  52. package/components/DateInput/DateInput.js +1 -1
  53. package/components/Drop/DropContainer.js +5 -4
  54. package/components/DropButton/DropButton.js +2 -2
  55. package/components/FileInput/FileInput.js +82 -53
  56. package/components/Form/Form.js +95 -44
  57. package/components/Form/stories/TriggerValidationUsingFormField.js +109 -0
  58. package/components/FormField/FormField.js +4 -2
  59. package/components/FormField/index.d.ts +1 -0
  60. package/components/FormField/propTypes.js +2 -1
  61. package/components/Grid/stories/CustomThemed/Responsive.js +0 -1
  62. package/components/InfiniteScroll/InfiniteScroll.js +1 -0
  63. package/components/Layer/Layer.js +1 -1
  64. package/components/Layer/LayerContainer.js +2 -2
  65. package/components/List/index.d.ts +2 -2
  66. package/components/NameValueList/NameValueList.js +1 -1
  67. package/components/RadioButtonGroup/RadioButtonGroup.js +19 -4
  68. package/components/RadioButtonGroup/stories/typescript/Simple.tsx +6 -4
  69. package/components/SelectMultiple/SelectMultiple.js +13 -10
  70. package/components/SelectMultiple/SelectMultipleValue.js +1 -1
  71. package/components/Tip/stories/HeaderActions.js +0 -1
  72. package/components/index.d.ts +3 -1
  73. package/components/index.js +6 -0
  74. package/contexts/DataContext.js +2 -1
  75. package/es6/components/Accordion/Accordion.js +4 -2
  76. package/es6/components/Accordion/index.d.ts +1 -0
  77. package/es6/components/Accordion/propTypes.js +1 -0
  78. package/es6/components/AccordionPanel/AccordionPanel.js +2 -1
  79. package/es6/components/Box/Box.js +14 -4
  80. package/es6/components/Box/stories/Border.js +15 -1
  81. package/es6/components/Button/BusyAnimation.js +41 -0
  82. package/es6/components/Button/Button.js +64 -5
  83. package/es6/components/Button/StyledButton.js +4 -2
  84. package/es6/components/Button/StyledButtonKind.js +8 -4
  85. package/es6/components/Button/index.d.ts +7 -0
  86. package/es6/components/Button/propTypes.js +6 -0
  87. package/es6/components/Button/stories/Busy.js +37 -0
  88. package/es6/components/Calendar/stories/Dual.js +4 -6
  89. package/es6/components/Chart/Chart.js +113 -57
  90. package/es6/components/Chart/index.d.ts +3 -0
  91. package/es6/components/Chart/propTypes.js +1 -0
  92. package/es6/components/Chart/stories/Layout.js +6 -3
  93. package/es6/components/Chart/stories/Pattern.js +1 -0
  94. package/es6/components/Chart/stories/Vertical.js +35 -0
  95. package/es6/components/Chart/utils.js +57 -10
  96. package/es6/components/Data/Data.js +2 -1
  97. package/es6/components/Data/DataForm.js +4 -1
  98. package/es6/components/Data/filter.js +12 -1
  99. package/es6/components/Data/stories/Complex.js +64 -7
  100. package/es6/components/Data/stories/Inline.js +20 -3
  101. package/es6/components/DataFilter/DataFilter.js +37 -22
  102. package/es6/components/DataFilters/DataFilters.js +13 -10
  103. package/es6/components/DataTable/Body.js +15 -13
  104. package/es6/components/DataTable/DataTable.js +4 -1
  105. package/es6/components/DataTable/GroupedBody.js +2 -1
  106. package/es6/components/DataTable/buildState.js +13 -11
  107. package/es6/components/DataTableColumns/DataTableColumns.js +5 -0
  108. package/es6/components/DataTableColumns/stories/Simple.js +5 -4
  109. package/es6/components/DataTableGroupBy/DataTableGroupBy.js +60 -0
  110. package/es6/components/DataTableGroupBy/README.md +2 -0
  111. package/es6/components/DataTableGroupBy/index.d.ts +20 -0
  112. package/es6/components/DataTableGroupBy/index.js +1 -0
  113. package/es6/components/DataTableGroupBy/propTypes.js +11 -0
  114. package/es6/components/DataTableGroupBy/stories/Simple.js +45 -0
  115. package/es6/components/DateInput/DateInput.js +1 -1
  116. package/es6/components/Drop/DropContainer.js +5 -4
  117. package/es6/components/DropButton/DropButton.js +2 -2
  118. package/es6/components/FileInput/FileInput.js +83 -53
  119. package/es6/components/Form/Form.js +95 -44
  120. package/es6/components/Form/stories/TriggerValidationUsingFormField.js +102 -0
  121. package/es6/components/FormField/FormField.js +4 -2
  122. package/es6/components/FormField/index.d.ts +1 -0
  123. package/es6/components/FormField/propTypes.js +2 -1
  124. package/es6/components/InfiniteScroll/InfiniteScroll.js +1 -0
  125. package/es6/components/Layer/Layer.js +1 -1
  126. package/es6/components/Layer/LayerContainer.js +2 -2
  127. package/es6/components/List/index.d.ts +2 -2
  128. package/es6/components/NameValueList/NameValueList.js +1 -1
  129. package/es6/components/RadioButtonGroup/RadioButtonGroup.js +19 -4
  130. package/es6/components/RadioButtonGroup/stories/typescript/Simple.tsx +6 -4
  131. package/es6/components/SelectMultiple/SelectMultiple.js +13 -10
  132. package/es6/components/SelectMultiple/SelectMultipleValue.js +1 -1
  133. package/es6/components/index.d.ts +3 -1
  134. package/es6/components/index.js +1 -0
  135. package/es6/contexts/DataContext.js +2 -1
  136. package/es6/index.d.ts +12 -0
  137. package/es6/index.js +2 -0
  138. package/es6/languages/default.json +15 -2
  139. package/es6/themes/base.js +1 -1
  140. package/es6/utils/index.d.ts +1 -1
  141. package/es6/utils/refs.js +1 -1
  142. package/grommet.min.js +1 -1
  143. package/grommet.min.js.map +1 -1
  144. package/index.d.ts +12 -0
  145. package/index.js +1 -0
  146. package/languages/default.json +15 -2
  147. package/package.json +46 -45
  148. package/themes/base.js +1 -1
  149. package/utils/index.d.ts +1 -1
  150. package/utils/refs.js +1 -1
@@ -60,8 +60,8 @@ var DropContainer = /*#__PURE__*/forwardRef(function (_ref, ref) {
60
60
  var onClickDocument = function onClickDocument(event) {
61
61
  // determine which portal id the target is in, if any
62
62
  var clickedPortalId = null;
63
- var node = containerTarget === document.body ? event.target : event == null ? void 0 : event.composedPath()[0];
64
- while (clickedPortalId === null && node !== document) {
63
+ var node = event.composed && event.composedPath()[0] || event.target;
64
+ while (clickedPortalId === null && node && node !== document && !(node instanceof ShadowRoot)) {
65
65
  var attr = node.getAttribute('data-g-portal-id');
66
66
  if (attr !== null) clickedPortalId = parseInt(attr, 10);
67
67
  node = node.parentNode;
@@ -81,8 +81,9 @@ var DropContainer = /*#__PURE__*/forwardRef(function (_ref, ref) {
81
81
  }, [onClickOutside, containerTarget, portalContext]);
82
82
  useEffect(function () {
83
83
  var notifyAlign = function notifyAlign() {
84
- var styleCurrent = dropRef.current.style;
85
- var alignControl = styleCurrent.top !== '' ? 'top' : 'bottom';
84
+ var _dropRef$current;
85
+ var styleCurrent = dropRef == null ? void 0 : (_dropRef$current = dropRef.current) == null ? void 0 : _dropRef$current.style;
86
+ var alignControl = (styleCurrent == null ? void 0 : styleCurrent.top) !== '' ? 'top' : 'bottom';
86
87
  onAlign(alignControl);
87
88
  };
88
89
 
@@ -38,8 +38,8 @@ var DropButton = /*#__PURE__*/forwardRef(function (_ref, ref) {
38
38
  var onDropClose = useCallback(function (event) {
39
39
  // if the user has clicked on our Button, don't do anything here,
40
40
  // handle that in onClickInternal() below.
41
- var node = event.target;
42
- while (node !== document && node !== buttonRef.current) {
41
+ var node = event.composed && event.composedPath()[0] || event.target;
42
+ while (node && node !== document && !(node instanceof ShadowRoot) && node !== buttonRef.current) {
43
43
  node = node.parentNode;
44
44
  }
45
45
  if (node !== buttonRef.current) {
@@ -6,7 +6,7 @@ import styled, { ThemeContext } from 'styled-components';
6
6
  import { CircleAlert } from 'grommet-icons/icons/CircleAlert';
7
7
  import { MessageContext } from '../../contexts/MessageContext';
8
8
  import { defaultProps } from '../../default-props';
9
- import { disabledStyle, parseMetricToNum, useForwardedRef } from '../../utils';
9
+ import { disabledStyle, focusStyle, parseMetricToNum, unfocusStyle, useForwardedRef, useKeyboard } from '../../utils';
10
10
  import { Anchor } from '../Anchor';
11
11
  import { Box } from '../Box';
12
12
  import { Button } from '../Button';
@@ -26,7 +26,7 @@ import { formatBytes } from './utils/formatBytes';
26
26
  var ContentsBox = styled(Box).withConfig({
27
27
  displayName: "FileInput__ContentsBox",
28
28
  componentId: "sc-1jzq7im-0"
29
- })(["cursor:pointer;position:relative;", " ", ";", ";", ";"], function (props) {
29
+ })(["cursor:pointer;position:relative;", " ", ";", ";", ";", ";", ";"], function (props) {
30
30
  return props.disabled && disabledStyle();
31
31
  }, function (props) {
32
32
  return props.theme.fileInput && props.theme.fileInput.extend;
@@ -34,6 +34,10 @@ var ContentsBox = styled(Box).withConfig({
34
34
  return props.hover && props.theme.fileInput && props.theme.fileInput.hover && props.theme.fileInput.hover.extend;
35
35
  }, function (props) {
36
36
  return props.dragOver && props.theme.fileInput && props.theme.fileInput.dragOver && props.theme.fileInput.dragOver.extend;
37
+ }, function (props) {
38
+ return props.focus && focusStyle();
39
+ }, function (props) {
40
+ return !props.focus && unfocusStyle();
37
41
  });
38
42
  var Label = styled(Text).withConfig({
39
43
  displayName: "FileInput__Label",
@@ -79,6 +83,9 @@ var FileInput = /*#__PURE__*/forwardRef(function (_ref, ref) {
79
83
  var _React$useState2 = React.useState(),
80
84
  dragOver = _React$useState2[0],
81
85
  setDragOver = _React$useState2[1];
86
+ var _React$useState3 = React.useState(),
87
+ focus = _React$useState3[0],
88
+ setFocus = _React$useState3[1];
82
89
  var _useState = useState(false),
83
90
  showRemoveConfirmation = _useState[0],
84
91
  setShowRemoveConfirmation = _useState[1];
@@ -92,6 +99,7 @@ var FileInput = /*#__PURE__*/forwardRef(function (_ref, ref) {
92
99
  var removeRef = useRef();
93
100
  var ConfirmRemove = confirmRemove;
94
101
  var RemoveIcon = theme.fileInput.icons.remove;
102
+ var usingKeyboard = useKeyboard();
95
103
  var _formContext$useFormI = formContext.useFormInput({
96
104
  name: name,
97
105
  value: valueProp,
@@ -241,8 +249,54 @@ var FileInput = /*#__PURE__*/forwardRef(function (_ref, ref) {
241
249
  onMouseOut: disabled ? undefined : function () {
242
250
  return setHover(false);
243
251
  },
244
- dragOver: dragOver
245
- }, (!files.length || files.length > 1) && /*#__PURE__*/React.createElement(Box, {
252
+ dragOver: dragOver,
253
+ focus: usingKeyboard && focus
254
+ }, /*#__PURE__*/React.createElement(StyledFileInput, _extends({
255
+ ref: inputRef,
256
+ type: "file",
257
+ id: id,
258
+ name: name,
259
+ maxSize: maxSize,
260
+ multiple: multiple,
261
+ disabled: disabled,
262
+ plain: true,
263
+ rightOffset: rightOffset
264
+ }, rest, {
265
+ onDragOver: function onDragOver() {
266
+ return setDragOver(true);
267
+ },
268
+ onDragLeave: function onDragLeave() {
269
+ return setDragOver(false);
270
+ },
271
+ onChange: function onChange(event) {
272
+ event.persist();
273
+ var fileList = event.target.files;
274
+ var nextFiles = multiple ? [].concat(files) : [];
275
+ var _loop = function _loop(i) {
276
+ // avoid duplicates
277
+ var existing = nextFiles.filter(function (file) {
278
+ return file.name === fileList[i].name && file.size === fileList[i].size;
279
+ }).length > 0;
280
+ if (!existing) {
281
+ nextFiles.push(fileList[i]);
282
+ }
283
+ };
284
+ for (var i = 0; i < fileList.length; i += 1) {
285
+ _loop(i);
286
+ }
287
+ setFiles(nextFiles);
288
+ setDragOver(false);
289
+ if (_onChange) _onChange(event, {
290
+ files: nextFiles
291
+ });
292
+ },
293
+ onBlur: function onBlur() {
294
+ return setFocus(false);
295
+ },
296
+ onFocus: function onFocus() {
297
+ return setFocus(true);
298
+ }
299
+ })), (!files.length || files.length > 1) && /*#__PURE__*/React.createElement(Box, {
246
300
  align: "center",
247
301
  fill: "horizontal",
248
302
  direction: "row",
@@ -255,7 +309,10 @@ var FileInput = /*#__PURE__*/forwardRef(function (_ref, ref) {
255
309
  onEnter: function onEnter(event) {
256
310
  if (controlRef.current === event.target) inputRef.current.click();
257
311
  }
258
- }, theme.fileInput.button ? /*#__PURE__*/React.createElement(Button, {
312
+ }, theme.fileInput.button ? /*#__PURE__*/React.createElement(Button
313
+ // The focus here is redundant for keyboard users
314
+ , {
315
+ tabIndex: -1,
259
316
  ref: controlRef,
260
317
  kind: theme.fileInput.button,
261
318
  label: format({
@@ -266,8 +323,10 @@ var FileInput = /*#__PURE__*/forwardRef(function (_ref, ref) {
266
323
  inputRef.current.click();
267
324
  inputRef.current.focus();
268
325
  }
269
- }) : /*#__PURE__*/React.createElement(Anchor, {
270
- tabIndex: 0,
326
+ }) : /*#__PURE__*/React.createElement(Anchor
327
+ // The focus here is redundant for keyboard users
328
+ , {
329
+ tabIndex: -1,
271
330
  alignSelf: "center",
272
331
  ref: controlRef,
273
332
  margin: "small",
@@ -315,7 +374,10 @@ var FileInput = /*#__PURE__*/forwardRef(function (_ref, ref) {
315
374
  onEnter: function onEnter(event) {
316
375
  if (controlRef.current === event.target) inputRef.current.click();
317
376
  }
318
- }, theme.fileInput.button ? /*#__PURE__*/React.createElement(Button, {
377
+ }, theme.fileInput.button ? /*#__PURE__*/React.createElement(Button
378
+ // The focus here is redundant for keyboard users
379
+ , {
380
+ tabIndex: -1,
319
381
  ref: controlRef,
320
382
  kind: theme.fileInput.button,
321
383
  label: format({
@@ -326,8 +388,10 @@ var FileInput = /*#__PURE__*/forwardRef(function (_ref, ref) {
326
388
  inputRef.current.click();
327
389
  inputRef.current.focus();
328
390
  }
329
- }) : /*#__PURE__*/React.createElement(Anchor, {
330
- tabIndex: 0,
391
+ }) : /*#__PURE__*/React.createElement(Anchor
392
+ // The focus here is redundant for keyboard users
393
+ , {
394
+ tabIndex: -1,
331
395
  alignSelf: "center",
332
396
  ref: controlRef,
333
397
  margin: "small",
@@ -381,7 +445,10 @@ var FileInput = /*#__PURE__*/forwardRef(function (_ref, ref) {
381
445
  onEnter: function onEnter(event) {
382
446
  if (controlRef.current === event.target) inputRef.current.click();
383
447
  }
384
- }, theme.fileInput.button ? /*#__PURE__*/React.createElement(Button, {
448
+ }, theme.fileInput.button ? /*#__PURE__*/React.createElement(Button
449
+ // The focus here is redundant for keyboard users
450
+ , {
451
+ tabIndex: -1,
385
452
  ref: controlRef,
386
453
  kind: theme.fileInput.button,
387
454
  label: format({
@@ -392,8 +459,10 @@ var FileInput = /*#__PURE__*/forwardRef(function (_ref, ref) {
392
459
  inputRef.current.click();
393
460
  inputRef.current.focus();
394
461
  }
395
- }) : /*#__PURE__*/React.createElement(Anchor, {
396
- tabIndex: 0,
462
+ }) : /*#__PURE__*/React.createElement(Anchor
463
+ // The focus here is redundant for keyboard users
464
+ , {
465
+ tabIndex: -1,
397
466
  ref: controlRef,
398
467
  margin: "small",
399
468
  onClick: function onClick() {
@@ -405,46 +474,7 @@ var FileInput = /*#__PURE__*/forwardRef(function (_ref, ref) {
405
474
  messages: messages
406
475
  })
407
476
  }))));
408
- }), /*#__PURE__*/React.createElement(StyledFileInput, _extends({
409
- ref: inputRef,
410
- type: "file",
411
- id: id,
412
- name: name,
413
- maxSize: maxSize,
414
- multiple: multiple,
415
- disabled: disabled,
416
- plain: true,
417
- rightOffset: rightOffset
418
- }, rest, {
419
- onDragOver: function onDragOver() {
420
- return setDragOver(true);
421
- },
422
- onDragLeave: function onDragLeave() {
423
- return setDragOver(false);
424
- },
425
- onChange: function onChange(event) {
426
- event.persist();
427
- var fileList = event.target.files;
428
- var nextFiles = multiple ? [].concat(files) : [];
429
- var _loop = function _loop(i) {
430
- // avoid duplicates
431
- var existing = nextFiles.filter(function (file) {
432
- return file.name === fileList[i].name && file.size === fileList[i].size;
433
- }).length > 0;
434
- if (!existing) {
435
- nextFiles.push(fileList[i]);
436
- }
437
- };
438
- for (var i = 0; i < fileList.length; i += 1) {
439
- _loop(i);
440
- }
441
- setFiles(nextFiles);
442
- setDragOver(false);
443
- if (_onChange) _onChange(event, {
444
- files: nextFiles
445
- });
446
- }
447
- }))), showRemoveConfirmation && /*#__PURE__*/React.createElement(ConfirmRemove, {
477
+ })), showRemoveConfirmation && /*#__PURE__*/React.createElement(ConfirmRemove, {
448
478
  onConfirm: function onConfirm() {
449
479
  removeFile(pendingRemoval.index);
450
480
  setPendingRemoval(defaultPendingRemoval);
@@ -129,11 +129,13 @@ var validateForm = function validateForm(validationRules, formValue, format, mes
129
129
  var name = _ref[0],
130
130
  _ref$ = _ref[1],
131
131
  field = _ref$.field,
132
- input = _ref$.input;
132
+ input = _ref$.input,
133
+ validateOn = _ref$.validateOn;
133
134
  if (!omitValid) {
134
135
  nextErrors[name] = undefined;
135
136
  nextInfos[name] = undefined;
136
137
  }
138
+ if (!validateOn) return;
137
139
  var result;
138
140
  if (input) {
139
141
  // input() a validation function supplied through useFormInput()
@@ -157,6 +159,9 @@ var validateForm = function validateForm(validationRules, formValue, format, mes
157
159
  });
158
160
  return [nextErrors, nextInfos];
159
161
  };
162
+ var isInstantValidate = function isInstantValidate(validateOn) {
163
+ return ['blur', 'change'].includes(validateOn);
164
+ };
160
165
  var Form = /*#__PURE__*/forwardRef(function (_ref2, ref) {
161
166
  var children = _ref2.children,
162
167
  _ref2$errors = _ref2.errors,
@@ -170,7 +175,7 @@ var Form = /*#__PURE__*/forwardRef(function (_ref2, ref) {
170
175
  _onSubmit = _ref2.onSubmit,
171
176
  onValidate = _ref2.onValidate,
172
177
  _ref2$validate = _ref2.validate,
173
- validateOn = _ref2$validate === void 0 ? 'submit' : _ref2$validate,
178
+ validateOnProp = _ref2$validate === void 0 ? 'submit' : _ref2$validate,
174
179
  valueProp = _ref2.value,
175
180
  rest = _objectWithoutPropertiesLoose(_ref2, _excluded);
176
181
  var formRef = useForwardedRef(ref);
@@ -185,12 +190,15 @@ var Form = /*#__PURE__*/forwardRef(function (_ref2, ref) {
185
190
  var _useState2 = useState(defaultTouched),
186
191
  touched = _useState2[0],
187
192
  setTouched = _useState2[1];
188
- var _useState3 = useState({
193
+ var _useState3 = useState(validateOnProp),
194
+ validateOn = _useState3[0],
195
+ setValidateOn = _useState3[1];
196
+ var _useState4 = useState({
189
197
  errors: errorsProp,
190
198
  infos: infosProp
191
199
  }),
192
- validationResults = _useState3[0],
193
- setValidationResults = _useState3[1];
200
+ validationResults = _useState4[0],
201
+ setValidationResults = _useState4[1];
194
202
  // maintain a copy of validationResults in a ref for useEffects
195
203
  // which can't depend on validationResults directly without
196
204
  // causing infinite renders.
@@ -198,16 +206,16 @@ var Form = /*#__PURE__*/forwardRef(function (_ref2, ref) {
198
206
  // Simulated onMount state. Consider Form to be mounted once it has
199
207
  // accounted for values originating from controlled inputs (available
200
208
  // at second rendering).
201
- var _useState4 = useState('unmounted'),
202
- mounted = _useState4[0],
203
- setMounted = _useState4[1];
209
+ var _useState5 = useState('unmounted'),
210
+ mounted = _useState5[0],
211
+ setMounted = _useState5[1];
204
212
  useEffect(function () {
205
213
  if (!mounted) setMounted('mounting');else if (mounted === 'mounting') setMounted('mounted');
206
214
  }, [mounted]);
207
215
  // `pendingValidation` is the name of the FormField awaiting validation.
208
- var _useState5 = useState(undefined),
209
- pendingValidation = _useState5[0],
210
- setPendingValidation = _useState5[1];
216
+ var _useState6 = useState(undefined),
217
+ pendingValidation = _useState6[0],
218
+ setPendingValidation = _useState6[1];
211
219
  var validationRulesRef = useRef({});
212
220
  var requiredFields = useRef([]);
213
221
  var analyticsRef = useRef({
@@ -279,16 +287,20 @@ var Form = /*#__PURE__*/forwardRef(function (_ref2, ref) {
279
287
  var validationRules = Object.entries(validationRulesRef.current);
280
288
  // Use simulated onMount state to account for values provided by
281
289
  // controlled inputs.
282
- if (mounted !== 'mounted' && ['blur', 'change'].includes(validateOn) && Object.keys(value).length > 0 && Object.keys(touched).length === 0) {
283
- applyValidationRules(validationRules.filter(function (_ref3) {
284
- var n = _ref3[0];
285
- return value[n];
290
+ if (mounted !== 'mounted' && (isInstantValidate(validateOn) || validationRules.some(function (_ref3) {
291
+ var v = _ref3[1];
292
+ return isInstantValidate(v.validateOn);
293
+ })) && Object.keys(value).length > 0 && Object.keys(touched).length === 0) {
294
+ applyValidationRules(validationRules.filter(function (_ref4) {
295
+ var n = _ref4[0],
296
+ v = _ref4[1];
297
+ return getFieldValue(n, value) && v.validateOn;
286
298
  })
287
299
  // Exlude empty arrays which may be initial values in
288
300
  // an input such as DateInput.
289
- .filter(function (_ref4) {
290
- var n = _ref4[0];
291
- return !(Array.isArray(value[n]) && value[n].length === 0);
301
+ .filter(function (_ref5) {
302
+ var n = _ref5[0];
303
+ return !(Array.isArray(getFieldValue(n, value)) && getFieldValue(n, value).length === 0);
292
304
  }));
293
305
  }
294
306
  }, [applyValidationRules, mounted, touched, validateOn, value]);
@@ -298,10 +310,14 @@ var Form = /*#__PURE__*/forwardRef(function (_ref2, ref) {
298
310
  useEffect(function () {
299
311
  var validationRules = Object.entries(validationRulesRef.current);
300
312
  var timer = setTimeout(function () {
301
- if (pendingValidation && ['blur', 'change'].includes(validateOn)) {
302
- applyValidationRules(validationRules.filter(function (_ref5) {
303
- var n = _ref5[0];
304
- return touched[n] || pendingValidation.includes(n);
313
+ if (pendingValidation && (isInstantValidate(validateOn) || validationRules.some(function (_ref6) {
314
+ var v = _ref6[1];
315
+ return isInstantValidate(v.validateOn);
316
+ }))) {
317
+ applyValidationRules(validationRules.filter(function (_ref7) {
318
+ var n = _ref7[0],
319
+ v = _ref7[1];
320
+ return (touched[n] || pendingValidation.includes(n)) && v.validateOn;
305
321
  }));
306
322
  setPendingValidation(undefined);
307
323
  }
@@ -323,8 +339,8 @@ var Form = /*#__PURE__*/forwardRef(function (_ref2, ref) {
323
339
  var _validationResultsRef2;
324
340
  var validationRules = Object.entries(validationRulesRef.current);
325
341
  if ((_validationResultsRef2 = validationResultsRef.current) != null && _validationResultsRef2.errors && Object.keys(validationResultsRef.current.errors).length > 0) {
326
- applyValidationRules(validationRules.filter(function (_ref6) {
327
- var n = _ref6[0];
342
+ applyValidationRules(validationRules.filter(function (_ref8) {
343
+ var n = _ref8[0];
328
344
  return touched[n] && validationResultsRef.current.errors[n];
329
345
  }));
330
346
  }
@@ -384,14 +400,14 @@ var Form = /*#__PURE__*/forwardRef(function (_ref2, ref) {
384
400
  // they can have access to it.
385
401
  //
386
402
  var formContextValue = useMemo(function () {
387
- var useFormInput = function useFormInput(_ref7) {
388
- var name = _ref7.name,
389
- componentValue = _ref7.value,
390
- initialValue = _ref7.initialValue,
391
- validateArg = _ref7.validate;
392
- var _useState6 = useState(initialValue),
393
- inputValue = _useState6[0],
394
- setInputValue = _useState6[1];
403
+ var useFormInput = function useFormInput(_ref9) {
404
+ var name = _ref9.name,
405
+ componentValue = _ref9.value,
406
+ initialValue = _ref9.initialValue,
407
+ validateArg = _ref9.validate;
408
+ var _useState7 = useState(initialValue),
409
+ inputValue = _useState7[0],
410
+ setInputValue = _useState7[1];
395
411
  var formValue = name ? getFieldValue(name, value) : undefined;
396
412
  // for dynamic forms, we need to track when an input has been added to
397
413
  // the form value. if the input is unmounted, we will delete its
@@ -483,15 +499,26 @@ var Form = /*#__PURE__*/forwardRef(function (_ref2, ref) {
483
499
  if (initialValue !== undefined) setInputValue(nextComponentValue);
484
500
  }];
485
501
  };
486
- var useFormField = function useFormField(_ref8) {
487
- var errorArg = _ref8.error,
488
- infoArg = _ref8.info,
489
- name = _ref8.name,
490
- required = _ref8.required,
491
- disabled = _ref8.disabled,
492
- validateArg = _ref8.validate;
502
+ var useFormField = function useFormField(_ref10) {
503
+ var errorArg = _ref10.error,
504
+ infoArg = _ref10.info,
505
+ name = _ref10.name,
506
+ required = _ref10.required,
507
+ disabled = _ref10.disabled,
508
+ validateArg = _ref10.validate,
509
+ validateOnArg = _ref10.validateOn;
493
510
  var error = disabled ? undefined : errorArg || validationResults.errors[name];
494
511
  var info = infoArg || validationResults.infos[name];
512
+ useEffect(function () {
513
+ setValidateOn(function (prevValues) {
514
+ var _extends2;
515
+ if (typeof prevValues === 'string') {
516
+ var _ref11;
517
+ return _ref11 = {}, _ref11[name] = validateOnArg || validateOnProp, _ref11;
518
+ }
519
+ return _extends({}, prevValues, (_extends2 = {}, _extends2[name] = validateOnArg || validateOnProp, _extends2));
520
+ });
521
+ }, [validateOnArg, name]);
495
522
 
496
523
  // Create validation rules for field
497
524
  useEffect(function () {
@@ -504,8 +531,21 @@ var Form = /*#__PURE__*/forwardRef(function (_ref2, ref) {
504
531
  validationRulesRef.current[name] = {};
505
532
  }
506
533
  validationRulesRef.current[name].field = validateName(validateArg, required);
534
+
535
+ // priority is given to validateOn prop on formField, if it is
536
+ // undefined, then we will use the validate prop value of Form.
537
+ // The reason we don't want to add validateOn = "submit" here is
538
+ // because we don't want to trigger validation of "submit" field
539
+ // when the user is typing in the instant (blur, change)
540
+ // validation fields.
541
+ if (validateOnArg && validateOnArg !== 'submit') {
542
+ validationRulesRef.current[name].validateOn = validateOnArg;
543
+ } else if (!validateOnArg && validateOnProp !== 'submit') {
544
+ validationRulesRef.current[name].validateOn = validateOnProp;
545
+ }
507
546
  return function () {
508
547
  delete validationRulesRef.current[name].field;
548
+ delete validationRulesRef.current[name].validateOn;
509
549
  var requiredFieldIndex = requiredFields.current.indexOf(name);
510
550
  if (requiredFieldIndex !== -1) {
511
551
  requiredFields.current.splice(requiredFieldIndex, 1);
@@ -513,15 +553,15 @@ var Form = /*#__PURE__*/forwardRef(function (_ref2, ref) {
513
553
  };
514
554
  }
515
555
  return undefined;
516
- }, [error, name, required, validateArg, disabled]);
556
+ }, [error, name, required, validateArg, disabled, validateOnArg]);
517
557
  return {
518
558
  error: error,
519
559
  info: info,
520
560
  inForm: true,
521
- onBlur: validateOn === 'blur' ? function () {
561
+ onBlur: validateOnArg === 'blur' || validateOn[name] === 'blur' ? function () {
522
562
  return setPendingValidation(pendingValidation ? [].concat(pendingValidation, [name]) : [name]);
523
563
  } : undefined,
524
- onChange: validateOn === 'change' ? function () {
564
+ onChange: validateOnArg === 'change' || validateOn[name] === 'change' ? function () {
525
565
  return setPendingValidation(pendingValidation ? [].concat(pendingValidation, [name]) : [name]);
526
566
  } : undefined
527
567
  };
@@ -531,7 +571,7 @@ var Form = /*#__PURE__*/forwardRef(function (_ref2, ref) {
531
571
  useFormInput: useFormInput,
532
572
  kind: kind
533
573
  };
534
- }, [onChange, kind, pendingValidation, touched, validateOn, validationResults.errors, validationResults.infos, value, valueProp]);
574
+ }, [onChange, kind, pendingValidation, touched, validateOn, validationResults.errors, validationResults.infos, value, valueProp, validateOnProp]);
535
575
  return /*#__PURE__*/React.createElement("form", _extends({
536
576
  ref: formRef
537
577
  }, rest, {
@@ -569,7 +609,18 @@ var Form = /*#__PURE__*/forwardRef(function (_ref2, ref) {
569
609
  // otherwise.
570
610
  event.preventDefault();
571
611
  setPendingValidation(undefined);
572
- var _validateForm2 = validateForm(Object.entries(validationRulesRef.current), value, format, messages, true),
612
+ // adding validateOn: "submit" prop to the undefined validateOn fields
613
+ // as we want to trigger "submit" validation once form is submitted
614
+ var newValidationRulesRef = Object.keys(validationRulesRef.current).reduce(function (acc, key) {
615
+ acc[key] = validationRulesRef.current[key];
616
+ if (!acc[key].validateOn) {
617
+ acc[key] = _extends({}, validationRulesRef.current[key], {
618
+ validateOn: 'submit'
619
+ });
620
+ }
621
+ return acc;
622
+ }, {});
623
+ var _validateForm2 = validateForm(Object.entries(newValidationRulesRef), value, format, messages, true),
573
624
  nextErrors = _validateForm2[0],
574
625
  nextInfos = _validateForm2[1];
575
626
  setValidationResults(function () {
@@ -0,0 +1,102 @@
1
+ import React from 'react';
2
+ import { StatusGood } from "grommet-icons/es6/icons/StatusGood";
3
+ import { Box, Button, Form, FormField, Heading } from 'grommet';
4
+ export var TriggerValidationUsingFormField = function TriggerValidationUsingFormField() {
5
+ return /*#__PURE__*/React.createElement(Box, {
6
+ fill: true,
7
+ align: "center",
8
+ justify: "center"
9
+ }, /*#__PURE__*/React.createElement(Box, {
10
+ width: "medium"
11
+ }, /*#__PURE__*/React.createElement(Heading, {
12
+ alignSelf: "center",
13
+ level: "2"
14
+ }, "Validate On"), /*#__PURE__*/React.createElement(Form, {
15
+ onReset: function onReset(event) {
16
+ return console.log(event);
17
+ },
18
+ onSubmit: function onSubmit(_ref) {
19
+ var value = _ref.value;
20
+ return console.log('Submit', value);
21
+ }
22
+ }, /*#__PURE__*/React.createElement(FormField, {
23
+ label: "Blur",
24
+ name: "blur",
25
+ "aria-label": "blur",
26
+ required: true,
27
+ validate: [{
28
+ regexp: /^[a-z]/i
29
+ }, function (name) {
30
+ if (name && name.length === 1) return 'must be >1 character';
31
+ return undefined;
32
+ }, function (name) {
33
+ if (name === 'good') return {
34
+ message: /*#__PURE__*/React.createElement(Box, {
35
+ align: "end"
36
+ }, /*#__PURE__*/React.createElement(StatusGood, null)),
37
+ status: 'info'
38
+ };
39
+ return undefined;
40
+ }],
41
+ validateOn: "blur"
42
+ }), /*#__PURE__*/React.createElement(FormField, {
43
+ label: "Submit",
44
+ name: "submit",
45
+ "aria-label": "submit",
46
+ required: true,
47
+ validate: [{
48
+ regexp: /^[a-z]/i
49
+ }, function (name) {
50
+ if (name && name.length === 1) return 'must be >1 character';
51
+ return undefined;
52
+ }, function (name) {
53
+ if (name === 'good') return {
54
+ message: /*#__PURE__*/React.createElement(Box, {
55
+ align: "end"
56
+ }, /*#__PURE__*/React.createElement(StatusGood, null)),
57
+ status: 'info'
58
+ };
59
+ return undefined;
60
+ }],
61
+ validateOn: "submit"
62
+ }), /*#__PURE__*/React.createElement(FormField, {
63
+ label: "Change",
64
+ name: "change",
65
+ "aria-label": "change",
66
+ required: true,
67
+ validate: [{
68
+ regexp: /^[a-z]/i
69
+ }, function (name) {
70
+ if (name && name.length === 1) return 'must be >1 character';
71
+ return undefined;
72
+ }, function (name) {
73
+ if (name === 'good') return {
74
+ message: /*#__PURE__*/React.createElement(Box, {
75
+ align: "end"
76
+ }, /*#__PURE__*/React.createElement(StatusGood, null)),
77
+ status: 'info'
78
+ };
79
+ return undefined;
80
+ }],
81
+ validateOn: "change"
82
+ }), /*#__PURE__*/React.createElement(Box, {
83
+ direction: "row",
84
+ justify: "between",
85
+ margin: {
86
+ top: 'medium'
87
+ }
88
+ }, /*#__PURE__*/React.createElement(Button, {
89
+ label: "Cancel"
90
+ }), /*#__PURE__*/React.createElement(Button, {
91
+ type: "reset",
92
+ label: "Reset"
93
+ }), /*#__PURE__*/React.createElement(Button, {
94
+ type: "submit",
95
+ label: "Update",
96
+ primary: true
97
+ })))));
98
+ };
99
+ TriggerValidationUsingFormField.storyName = 'Trigger Validation using Form Field';
100
+ export default {
101
+ title: 'Input/Form/Trigger Validation using Form Field'
102
+ };
@@ -1,6 +1,6 @@
1
1
  var _excluded = ["error", "info", "message", "type"],
2
2
  _excluded2 = ["component", "disabled", "invalid", "name", "onChange"],
3
- _excluded3 = ["children", "className", "component", "contentProps", "disabled", "error", "help", "htmlFor", "info", "label", "margin", "name", "onBlur", "onChange", "onFocus", "pad", "required", "style", "validate"];
3
+ _excluded3 = ["children", "className", "component", "contentProps", "disabled", "error", "help", "htmlFor", "info", "label", "margin", "name", "onBlur", "onChange", "onFocus", "pad", "required", "style", "validate", "validateOn"];
4
4
  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); }
5
5
  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; }
6
6
  import React, { Children, cloneElement, forwardRef, useContext, useState, useEffect } from 'react';
@@ -153,6 +153,7 @@ var FormField = /*#__PURE__*/forwardRef(function (_ref3, ref) {
153
153
  required = _ref3.required,
154
154
  style = _ref3.style,
155
155
  validate = _ref3.validate,
156
+ validateOn = _ref3.validateOn,
156
157
  rest = _objectWithoutPropertiesLoose(_ref3, _excluded3);
157
158
  var theme = useContext(ThemeContext) || defaultProps.theme;
158
159
  var formContext = useContext(FormContext);
@@ -162,7 +163,8 @@ var FormField = /*#__PURE__*/forwardRef(function (_ref3, ref) {
162
163
  info: infoProp,
163
164
  name: name,
164
165
  required: required,
165
- validate: validate
166
+ validate: validate,
167
+ validateOn: validateOn
166
168
  }),
167
169
  error = _formContext$useFormF.error,
168
170
  info = _formContext$useFormF.info,
@@ -33,6 +33,7 @@ export interface FormFieldProps {
33
33
  }
34
34
  | ((...args: any[]) => any)
35
35
  )[];
36
+ validateOn?: 'blur' | 'submit' | 'change';
36
37
  }
37
38
 
38
39
  type inputProps = Omit<
@@ -28,7 +28,8 @@ if (process.env.NODE_ENV !== 'production') {
28
28
  // regular expression
29
29
  message: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
30
30
  status: PropTypes.oneOf(['error', 'info'])
31
- }), PropTypes.func]))])
31
+ }), PropTypes.func]))]),
32
+ validateOn: PropTypes.oneOf(['blur', 'submit', 'change'])
32
33
  };
33
34
  }
34
35
  export var FormFieldPropTypes = PropType;