diff --git a/src/components/Checkbox.js b/src/components/Checkbox.js index 5124150a..cd24803b 100644 --- a/src/components/Checkbox.js +++ b/src/components/Checkbox.js @@ -116,7 +116,7 @@ Checkbox.propTypes = { disabled: PropTypes.bool, optional: PropTypes.bool, validate: PropTypes.oneOfType([PropTypes.func, PropTypes.bool]), - validateData: PropTypes.object, + validateData: PropTypes.any, children: PropTypes.node.isRequired, testId: PropTypes.string, __internal__keyboardFocus: PropTypes.bool diff --git a/src/components/DatePicker.js b/src/components/DatePicker.js index e5a85052..4254b4b3 100644 --- a/src/components/DatePicker.js +++ b/src/components/DatePicker.js @@ -153,6 +153,7 @@ function DatePicker(props) { { - const { name } = event.target; - const parentName = getParentFieldName(name); + const { target } = event; + const parentName = getParentFieldName(target); lastFocusedFieldName.current = parentName; - setState(state => - setPath( - state, - "shouldValidateOnChange", - Array.isArray(state.errors[parentName]) - ) - ); + setState(state => { + const errors = getPath(state.errors, parentName); + const hasErrors = Array.isArray(errors) && errors.length > 0; + + return setPath(state, "shouldValidateOnChange", hasErrors); + }); }; const onBlur = event => { const { target } = event; - const { name } = target; - const parentName = getParentFieldName(name); + const parentName = getParentFieldName(target); lastFocusedFieldName.current = null; @@ -73,8 +71,7 @@ function Form(_props) { if ( parentName !== lastFocusedFieldName.current && (lastMouseDownInputElement.current === null || - parentName !== - getParentFieldName(lastMouseDownInputElement.current.name)) + parentName !== getParentFieldName(lastMouseDownInputElement.current)) ) { setState(state => setPath(state, "namesToValidate", [parentName])); } @@ -98,7 +95,7 @@ function Form(_props) { */ if (state.shouldValidateOnChange || isCheckbox) { newState = setPath(newState, "namesToValidate", [ - getParentFieldName(name) + getParentFieldName(target) ]); } @@ -127,8 +124,8 @@ function Form(_props) { registerField, unregisterField }; - const validateField = useCallback((state, name) => { - const value = state.values[name]; + const validateField = useCallback((values, name) => { + const value = getPath(values, name); const field = fields.current[name]; if ( @@ -156,27 +153,22 @@ function Form(_props) { }, []); const getNewErrors = useCallback( state => { - const names = state.namesToValidate; - const newErrors = { ...state.errors }; - - names.forEach(name => { - const errors = validateField(state, name); + const { namesToValidate, values } = state; - if (errors === null) { - delete newErrors[name]; - } else { - newErrors[name] = errors; - } - }); + return namesToValidate.reduce((acc, name) => { + const errors = validateField(values, name); - return newErrors; + return errors === null + ? deletePath(acc, name) + : setPath(acc, name, errors); + }, state.errors); }, [validateField] ); const submitForm = useCallback(() => { setState(state => ({ ...state, - namesToValidate: Object.keys(state.values), + namesToValidate: Object.keys(fields.current), submitStatus: "VALIDATE_THEN_SUBMIT" })); }, []); diff --git a/src/components/Frequency.js b/src/components/Frequency.js index 2217e806..818480a1 100644 --- a/src/components/Frequency.js +++ b/src/components/Frequency.js @@ -174,6 +174,7 @@ function Frequency(props) { const inputComponent = ( 0 && (