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.
- package/components/Button/StyledButtonKind.js +5 -3
- package/components/Calendar/utils.js +1 -1
- package/components/DateInput/utils.js +9 -2
- package/components/Form/Form.js +111 -112
- package/components/Form/stories/FieldBorderPosition.js +1 -0
- package/components/FormField/FormField.js +5 -1
- package/components/TextInput/TextInput.js +1 -1
- package/es6/components/Button/StyledButtonKind.js +6 -3
- package/es6/components/Calendar/utils.js +1 -1
- package/es6/components/DateInput/utils.js +9 -2
- package/es6/components/Form/Form.js +111 -112
- package/es6/components/Form/stories/FieldBorderPosition.js +1 -0
- package/es6/components/FormField/FormField.js +6 -2
- package/es6/components/TextInput/TextInput.js +1 -1
- package/grommet.min.js +1 -1
- package/grommet.min.js.map +1 -1
- package/package.json +47 -47
|
@@ -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;
|
|
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 (
|
|
117
|
-
validationResult = validate(
|
|
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
|
-
}; //
|
|
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(
|
|
200
|
+
var _useState3 = useState({
|
|
201
|
+
errors: errorsProp,
|
|
202
|
+
infos: infosProp
|
|
203
|
+
}),
|
|
204
204
|
validationResults = _useState3[0],
|
|
205
|
-
setValidationResults = _useState3[1]; //
|
|
206
|
-
//
|
|
207
|
-
//
|
|
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
|
|
215
|
-
|
|
216
|
-
|
|
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
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
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
|
-
};
|
|
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
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
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(
|
|
264
|
-
filterRemovedFields(
|
|
264
|
+
filterRemovedFields(nextErrors);
|
|
265
|
+
filterRemovedFields(nextInfos);
|
|
265
266
|
var nextValidationResults = {
|
|
266
|
-
errors:
|
|
267
|
-
infos:
|
|
268
|
-
valid: buildValid(errors)
|
|
267
|
+
errors: nextErrors,
|
|
268
|
+
infos: nextInfos
|
|
269
269
|
};
|
|
270
|
-
if (onValidate) onValidate(nextValidationResults
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
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
|
-
|
|
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
|
-
|
|
282
|
-
|
|
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
|
-
})
|
|
286
|
-
validatedErrors = _validateForm2[0],
|
|
287
|
-
validatedInfos = _validateForm2[1];
|
|
288
|
-
|
|
304
|
+
}));
|
|
289
305
|
setPendingValidation(undefined);
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
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
|
-
}, [
|
|
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
|
-
|
|
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
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
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(
|
|
370
|
-
var name =
|
|
371
|
-
componentValue =
|
|
372
|
-
initialValue =
|
|
373
|
-
validateArg =
|
|
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
|
|
376
|
-
inputValue =
|
|
377
|
-
setInputValue =
|
|
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(
|
|
469
|
-
var errorArg =
|
|
470
|
-
infoArg =
|
|
471
|
-
name =
|
|
472
|
-
required =
|
|
473
|
-
disabled =
|
|
474
|
-
validateArg =
|
|
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
|
|
548
|
-
nextErrors =
|
|
549
|
-
nextInfos =
|
|
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:
|
|
307
|
+
fill: "horizontal",
|
|
308
308
|
plain: !child ? undefined : true,
|
|
309
309
|
align: "start",
|
|
310
310
|
kind: !child ? 'option' : undefined,
|