grommet 2.20.0 → 2.20.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.
@@ -101,20 +101,18 @@ var validate = function validate(rule, fieldValue, formValue, format, messages)
101
101
  var validateName = function validateName(validationRules, required) {
102
102
  return function (name, formValue, format, messages) {
103
103
  var fieldValue = getFieldValue(name, formValue);
104
- var validationResult; // ValidateArg is something that gets passed in from a FormField component
105
- // See 'validate' prop in FormField
104
+ var validationResult;
106
105
 
107
106
  if (required && ( // false is for CheckBox
108
107
  fieldValue === undefined || fieldValue === '' || fieldValue === false || Array.isArray(fieldValue) && !fieldValue.length)) {
109
- // There is no value at that name, and one is required
110
108
  validationResult = format({
111
109
  id: 'form.required',
112
110
  messages: messages
113
111
  });
114
112
  } else if (validationRules) {
115
113
  if (Array.isArray(validationRules)) {
116
- validationRules.some(function (validator) {
117
- validationResult = validate(validator, fieldValue, formValue, format, messages);
114
+ validationRules.some(function (rule) {
115
+ validationResult = validate(rule, fieldValue, formValue, format, messages);
118
116
  return !!validationResult;
119
117
  });
120
118
  } else {
@@ -124,8 +122,7 @@ var validateName = function validateName(validationRules, required) {
124
122
 
125
123
  return validationResult;
126
124
  };
127
- }; // validations is an array from Object.entries()
128
- // Validates all keys in formValue
125
+ }; // Validates all keys in formValue
129
126
 
130
127
 
131
128
  var validateForm = function validateForm(validationRules, formValue, format, messages, omitValid) {
@@ -200,28 +197,32 @@ var Form = /*#__PURE__*/forwardRef(function (_ref2, ref) {
200
197
  touched = _useState2[0],
201
198
  setTouched = _useState2[1];
202
199
 
203
- var _useState3 = useState(defaultValidationResults),
200
+ var _useState3 = useState({
201
+ errors: errorsProp,
202
+ infos: infosProp
203
+ }),
204
204
  validationResults = _useState3[0],
205
- setValidationResults = _useState3[1]; // when onBlur input validation is triggered, we need to complete any
206
- // potential click events before running the onBlur validation.
207
- // otherwise, click events like reset, etc. may not be registered.
208
- // for a detailed scenario/discussion,
209
- // see: https://github.com/grommet/grommet/issues/4863
210
- // the value of pendingValidation is the name of the FormField
211
- // awaiting validation.
205
+ setValidationResults = _useState3[1]; // maintain a copy of validationResults in a ref for useEffects
206
+ // which can't depend on validationResults directly without
207
+ // causing infinite renders.
212
208
 
213
209
 
214
- var _useState4 = useState(undefined),
215
- pendingValidation = _useState4[0],
216
- setPendingValidation = _useState4[1];
210
+ var validationResultsRef = useRef({}); // Simulated onMount state. Consider Form to be mounted once it has
211
+ // accounted for values originating from controlled inputs (available
212
+ // at second rendering).
213
+
214
+ var _useState4 = useState('unmounted'),
215
+ mounted = _useState4[0],
216
+ setMounted = _useState4[1];
217
217
 
218
218
  useEffect(function () {
219
- setPendingValidation(undefined);
220
- setValidationResults({
221
- errors: errorsProp,
222
- infos: infosProp
223
- });
224
- }, [errorsProp, infosProp]);
219
+ if (!mounted) setMounted('mounting');else if (mounted === 'mounting') setMounted('mounted');
220
+ }, [mounted]); // `pendingValidation` is the name of the FormField awaiting validation.
221
+
222
+ var _useState5 = useState(undefined),
223
+ pendingValidation = _useState5[0],
224
+ setPendingValidation = _useState5[1];
225
+
225
226
  var validationRulesRef = useRef({});
226
227
  var requiredFields = useRef([]);
227
228
  var buildValid = useCallback(function (nextErrors) {
@@ -244,95 +245,90 @@ var Form = /*#__PURE__*/forwardRef(function (_ref2, ref) {
244
245
  }).forEach(function (n) {
245
246
  return delete nextValidations[n];
246
247
  });
247
- }; // On initial mount, when validateOn is change or blur,
248
- // set validation results for any set fields and calculate whether
249
- // the form is valid overall.
248
+ };
250
249
 
250
+ var applyValidationRules = useCallback(function (validationRules) {
251
+ var _validateForm = validateForm(validationRules, value, format, messages),
252
+ validatedErrors = _validateForm[0],
253
+ validatedInfos = _validateForm[1];
251
254
 
252
- useEffect(function () {
253
- var validationsForSetFields = Object.entries(validationRulesRef.current).filter(function (_ref3) {
254
- var n = _ref3[0];
255
- return getFieldValue(n, value);
256
- });
255
+ setValidationResults(function (prevValidationResults) {
256
+ // Keep any previous errors and infos for untouched keys,
257
+ // these may have come from a Submit.
258
+ var nextErrors = _extends({}, prevValidationResults.errors, validatedErrors);
259
+
260
+ var nextInfos = _extends({}, prevValidationResults.infos, validatedInfos); // Remove previous errors and infos for keys no longer in the
261
+ // form, these may have been fields removed from a dynamic form.
257
262
 
258
- if (validationsForSetFields.length > 0 && validateOn !== 'submit') {
259
- var _validateForm = validateForm(validationsForSetFields, value, format, messages),
260
- errors = _validateForm[0],
261
- infos = _validateForm[1];
262
263
 
263
- filterRemovedFields(errors);
264
- filterRemovedFields(infos);
264
+ filterRemovedFields(nextErrors);
265
+ filterRemovedFields(nextInfos);
265
266
  var nextValidationResults = {
266
- errors: errors,
267
- infos: infos,
268
- valid: buildValid(errors)
267
+ errors: nextErrors,
268
+ infos: nextInfos
269
269
  };
270
- if (onValidate) onValidate(nextValidationResults);
271
- setValidationResults(nextValidationResults);
272
- } // We only want to run this for the value we have on initial mount.
273
- // We don't want subsequent changes to the value to re-run this.
274
- // eslint-disable-next-line react-hooks/exhaustive-deps
270
+ if (onValidate) onValidate(_extends({}, nextValidationResults, {
271
+ valid: buildValid(nextErrors)
272
+ }));
273
+ validationResultsRef.current = nextValidationResults;
274
+ return nextValidationResults;
275
+ });
276
+ }, [buildValid, format, messages, onValidate, value]); // Validate all fields holding values onMount if set to
277
+ // validate when blur or change.
275
278
 
276
- }, []); // Currently, onBlur validation will trigger after a timeout of 120ms.
279
+ useEffect(function () {
280
+ var validationRules = Object.entries(validationRulesRef.current); // Use simulated onMount state to account for values provided by
281
+ // controlled inputs.
282
+
283
+ if (mounted !== 'mounted' && ['blur', 'change'].includes(validateOn) && Object.keys(value).length > 0 && Object.keys(touched).length === 0) {
284
+ applyValidationRules(validationRules.filter(function (_ref3) {
285
+ var n = _ref3[0];
286
+ return value[n];
287
+ }) // Exlude empty arrays which may be initial values in
288
+ // an input such as DateInput.
289
+ .filter(function (_ref4) {
290
+ var n = _ref4[0];
291
+ return !(Array.isArray(value[n]) && value[n].length === 0);
292
+ }));
293
+ }
294
+ }, [applyValidationRules, mounted, touched, validateOn, value]); // Run validation against fields with pendingValidations from onBlur
295
+ // and/or onChange.
277
296
 
278
297
  useEffect(function () {
298
+ var validationRules = Object.entries(validationRulesRef.current);
279
299
  var timer = setTimeout(function () {
280
- if (pendingValidation) {
281
- // run validations on the pending one and any other touched fields
282
- var _validateForm2 = validateForm(Object.entries(validationRulesRef.current).filter(function (_ref4) {
283
- var n = _ref4[0];
300
+ if (pendingValidation && ['blur', 'change'].includes(validateOn)) {
301
+ applyValidationRules(validationRules.filter(function (_ref5) {
302
+ var n = _ref5[0];
284
303
  return touched[n] || pendingValidation.includes(n);
285
- }), value, format, messages),
286
- validatedErrors = _validateForm2[0],
287
- validatedInfos = _validateForm2[1];
288
-
304
+ }));
289
305
  setPendingValidation(undefined);
290
- setValidationResults(function (prevValidationResults) {
291
- // keep any previous errors and infos for untouched keys,
292
- // these may have come from a submit
293
- var nextErrors = _extends({}, prevValidationResults.errors, validatedErrors);
294
-
295
- var nextInfos = _extends({}, prevValidationResults.infos, validatedInfos);
296
-
297
- filterRemovedFields(nextErrors);
298
- filterRemovedFields(nextInfos);
299
- var nextValidationResults = {
300
- errors: nextErrors,
301
- infos: nextInfos,
302
- valid: buildValid(nextErrors)
303
- };
304
- if (onValidate) onValidate(nextValidationResults);
305
- return nextValidationResults;
306
- });
307
- } // a timeout is needed to ensure that a click event (like one on a reset
308
- // button) completes prior to running the validation. without a timeout,
309
- // the blur will always complete and trigger a validation prematurely
310
- // The following values have been empirically tested, but 120 was
311
- // selected because it is the largest value
306
+ } // Complete any potential click events before running onBlur validation.
307
+ // Otherwise, click events like reset, etc. may not be registered. For a
308
+ // detailed scenario/discussion, see: https://github.com/grommet/grommet/issues/4863
309
+ // Values empirically tested; 120 was selected because it is the largest
312
310
  // Chrome: 100, Safari: 120, Firefox: 80
313
311
 
314
312
  }, 120);
315
313
  return function () {
316
314
  return clearTimeout(timer);
317
315
  };
318
- }, [buildValid, format, messages, pendingValidation, onValidate, touched, value, requiredFields]); // clear any errors when value changes
316
+ }, [applyValidationRules, pendingValidation, touched, validateOn]); // Re-run validation rules for all fields with prior errors.
317
+ // if validate=blur this helps re-validate if there are errors
318
+ // as the user fixes them (basically act like validate=change for that)
319
319
 
320
320
  useEffect(function () {
321
- if (validateOn !== 'change') setPendingValidation(undefined);
322
- setValidationResults(function (prevValidationResults) {
323
- var _validateForm3 = validateForm(Object.entries(validationRulesRef.current).filter(function (_ref5) {
324
- var n = _ref5[0];
325
- return prevValidationResults.errors[n] || prevValidationResults.infos[n];
326
- }), value, format, messages),
327
- nextErrors = _validateForm3[0],
328
- nextInfos = _validateForm3[1];
321
+ var _validationResultsRef;
329
322
 
330
- return {
331
- errors: _extends({}, prevValidationResults.errors, nextErrors),
332
- infos: _extends({}, prevValidationResults.infos, nextInfos)
333
- };
334
- });
335
- }, [format, messages, touched, validateOn, value]); // There are three basic patterns of handling form input value state:
323
+ var validationRules = Object.entries(validationRulesRef.current);
324
+
325
+ if ((_validationResultsRef = validationResultsRef.current) != null && _validationResultsRef.errors && Object.keys(validationResultsRef.current.errors).length > 0) {
326
+ applyValidationRules(validationRules.filter(function (_ref6) {
327
+ var n = _ref6[0];
328
+ return touched[n] && validationResultsRef.current.errors[n];
329
+ }));
330
+ }
331
+ }, [applyValidationRules, touched]); // There are three basic patterns of handling form input value state:
336
332
  //
337
333
  // 1 - form controlled
338
334
  //
@@ -366,15 +362,15 @@ var Form = /*#__PURE__*/forwardRef(function (_ref2, ref) {
366
362
  //
367
363
 
368
364
  var formContextValue = useMemo(function () {
369
- var useFormInput = function useFormInput(_ref6) {
370
- var name = _ref6.name,
371
- componentValue = _ref6.value,
372
- initialValue = _ref6.initialValue,
373
- validateArg = _ref6.validate;
365
+ var useFormInput = function useFormInput(_ref7) {
366
+ var name = _ref7.name,
367
+ componentValue = _ref7.value,
368
+ initialValue = _ref7.initialValue,
369
+ validateArg = _ref7.validate;
374
370
 
375
- var _useState5 = useState(initialValue),
376
- inputValue = _useState5[0],
377
- setInputValue = _useState5[1];
371
+ var _useState6 = useState(initialValue),
372
+ inputValue = _useState6[0],
373
+ setInputValue = _useState6[1];
378
374
 
379
375
  var formValue = name ? getFieldValue(name, value) : undefined; // for dynamic forms, we need to track when an input has been added to
380
376
  // the form value. if the input is unmounted, we will delete its
@@ -417,7 +413,8 @@ var Form = /*#__PURE__*/forwardRef(function (_ref2, ref) {
417
413
  };
418
414
  }, // eslint-disable-next-line react-hooks/exhaustive-deps
419
415
  [] // only run onmount and unmount
420
- );
416
+ ); // Create validation rules for fields
417
+
421
418
  useEffect(function () {
422
419
  if (validateArg) {
423
420
  if (!validationRulesRef.current[name]) {
@@ -465,15 +462,16 @@ var Form = /*#__PURE__*/forwardRef(function (_ref2, ref) {
465
462
  }];
466
463
  };
467
464
 
468
- var useFormField = function useFormField(_ref7) {
469
- var errorArg = _ref7.error,
470
- infoArg = _ref7.info,
471
- name = _ref7.name,
472
- required = _ref7.required,
473
- disabled = _ref7.disabled,
474
- validateArg = _ref7.validate;
465
+ var useFormField = function useFormField(_ref8) {
466
+ var errorArg = _ref8.error,
467
+ infoArg = _ref8.info,
468
+ name = _ref8.name,
469
+ required = _ref8.required,
470
+ disabled = _ref8.disabled,
471
+ validateArg = _ref8.validate;
475
472
  var error = disabled ? undefined : errorArg || validationResults.errors[name];
476
- var info = infoArg || validationResults.infos[name];
473
+ var info = infoArg || validationResults.infos[name]; // Create validation rules for field
474
+
477
475
  useEffect(function () {
478
476
  var index = requiredFields.current.indexOf(name);
479
477
 
@@ -544,9 +542,9 @@ var Form = /*#__PURE__*/forwardRef(function (_ref2, ref) {
544
542
  event.preventDefault();
545
543
  setPendingValidation(undefined);
546
544
 
547
- var _validateForm4 = validateForm(Object.entries(validationRulesRef.current), value, format, messages, true),
548
- nextErrors = _validateForm4[0],
549
- nextInfos = _validateForm4[1];
545
+ var _validateForm2 = validateForm(Object.entries(validationRulesRef.current), value, format, messages, true),
546
+ nextErrors = _validateForm2[0],
547
+ nextInfos = _validateForm2[1];
550
548
 
551
549
  setValidationResults(function () {
552
550
  var nextValidationResults = {
@@ -556,6 +554,7 @@ var Form = /*#__PURE__*/forwardRef(function (_ref2, ref) {
556
554
  valid: buildValid(nextErrors)
557
555
  };
558
556
  if (onValidate) onValidate(nextValidationResults);
557
+ validationResultsRef.current = nextValidationResults;
559
558
  return nextValidationResults;
560
559
  });
561
560
 
@@ -134,6 +134,7 @@ export var FieldBorderPosition = function FieldBorderPosition() {
134
134
  gap: "medium"
135
135
  }, borderPositions && borderPositions.map(function (example, index) {
136
136
  return /*#__PURE__*/React.createElement(FormExample, {
137
+ key: example.name,
137
138
  borderPosition: example.name,
138
139
  theme: example.theme,
139
140
  index: index
@@ -9,7 +9,7 @@ function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) r
9
9
  import React, { Children, cloneElement, forwardRef, useContext, useState } from 'react';
10
10
  import styled, { ThemeContext } from 'styled-components';
11
11
  import { defaultProps } from '../../default-props';
12
- import { shouldKeepFocus } from '../../utils/DOM';
12
+ import { containsFocus, shouldKeepFocus } from '../../utils/DOM';
13
13
  import { focusStyle } from '../../utils/styles';
14
14
  import { parseMetricToNum } from '../../utils/mixins';
15
15
  import { useForwardedRef } from '../../utils/refs';
@@ -245,6 +245,10 @@ var FormField = /*#__PURE__*/forwardRef(function (_ref3, ref) {
245
245
  if (child && child.type && 'FileInput'.indexOf(child.type.displayName) !== -1) isFileInputComponent = true;
246
246
  })) ;
247
247
 
248
+ if (component && component.displayName === 'FileInput' && !isFileInputComponent) {
249
+ isFileInputComponent = true;
250
+ }
251
+
248
252
  if (!themeBorder) {
249
253
  contents = /*#__PURE__*/React.createElement(Box, _extends({}, themeContentProps, contentProps), contents);
250
254
  }
@@ -353,7 +357,7 @@ var FormField = /*#__PURE__*/forwardRef(function (_ref3, ref) {
353
357
  }, outerProps, {
354
358
  style: outerStyle,
355
359
  onFocus: function onFocus(event) {
356
- setFocus(shouldKeepFocus());
360
+ setFocus(containsFocus(formFieldRef.current) && shouldKeepFocus());
357
361
  if (_onFocus) _onFocus(event);
358
362
  },
359
363
  onBlur: function onBlur(event) {
@@ -304,7 +304,7 @@ var TextInput = /*#__PURE__*/forwardRef(function (_ref, ref) {
304
304
  ref: itemRef
305
305
  }, /*#__PURE__*/React.createElement(Button, {
306
306
  active: activeSuggestionIndex === index,
307
- fill: true,
307
+ fill: "horizontal",
308
308
  plain: !child ? undefined : true,
309
309
  align: "start",
310
310
  kind: !child ? 'option' : undefined,