Skip to content

Commit 6c22eb3

Browse files
authored
Merge pull request #24011 from tienifr/fix/23552-form-validation-triggers-on-blur
2 parents 7b13f11 + 4897c4d commit 6c22eb3

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

src/components/Form.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import ScrollViewWithContext from './ScrollViewWithContext';
1717
import stylePropTypes from '../styles/stylePropTypes';
1818
import {withNetwork} from './OnyxProvider';
1919
import networkPropTypes from './networkPropTypes';
20+
import Visibility from '../libs/Visibility';
2021

2122
const propTypes = {
2223
/** A unique Onyx key identifying the form */
@@ -304,18 +305,21 @@ function Form(props) {
304305
defaultValue: undefined,
305306
errorText: errors[inputID] || fieldErrorMessage,
306307
onBlur: (event) => {
307-
// We delay the validation in order to prevent Checkbox loss of focus when
308-
// the user are focusing a TextInput and proceeds to toggle a CheckBox in
309-
// web and mobile web platforms.
310-
setTimeout(() => {
311-
setTouchedInput(inputID);
312-
313-
// To prevent server errors from being cleared inadvertently, we only run validation on blur if any form values have changed since the last validation/submit
314-
const shouldValidate = !_.isEqual(inputValues, lastValidatedValues.current);
315-
if (shouldValidate) {
316-
onValidate(inputValues);
317-
}
318-
}, 200);
308+
// Only run validation when user proactively blurs the input.
309+
if (Visibility.isVisible() && Visibility.hasFocus()) {
310+
// We delay the validation in order to prevent Checkbox loss of focus when
311+
// the user are focusing a TextInput and proceeds to toggle a CheckBox in
312+
// web and mobile web platforms.
313+
setTimeout(() => {
314+
setTouchedInput(inputID);
315+
316+
// To prevent server errors from being cleared inadvertently, we only run validation on blur if any form values have changed since the last validation/submit
317+
const shouldValidate = !_.isEqual(inputValues, lastValidatedValues.current);
318+
if (shouldValidate) {
319+
onValidate(inputValues);
320+
}
321+
}, 200);
322+
}
319323

320324
if (_.isFunction(child.props.onBlur)) {
321325
child.props.onBlur(event);

src/components/TextInput/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import BaseTextInput from './BaseTextInput';
66
import * as baseTextInputPropTypes from './baseTextInputPropTypes';
77
import DomUtils from '../../libs/DomUtils';
88
import Visibility from '../../libs/Visibility';
9+
import * as Browser from '../../libs/Browser';
910

1011
function TextInput(props) {
1112
const textInputRef = useRef(null);
@@ -21,7 +22,7 @@ function TextInput(props) {
2122
}
2223

2324
removeVisibilityListenerRef.current = Visibility.onVisibilityChange(() => {
24-
if (!Visibility.isVisible() || !textInputRef.current || DomUtils.getActiveElement() !== textInputRef.current) {
25+
if (!Browser.isMobileChrome() || !Visibility.isVisible() || !textInputRef.current || DomUtils.getActiveElement() !== textInputRef.current) {
2526
return;
2627
}
2728
textInputRef.current.blur();

0 commit comments

Comments
 (0)