-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36401 from shubham1206agra/fix-form-default
Fix form default behavior
- Loading branch information
Showing
7 changed files
with
54 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import type {ForwardedRef} from 'react'; | ||
import React, {forwardRef, useEffect, useRef} from 'react'; | ||
import type {ViewProps} from 'react-native'; | ||
import {View} from 'react-native'; | ||
import * as ComponentUtils from '@libs/ComponentUtils'; | ||
import mergeRefs from '@libs/mergeRefs'; | ||
|
||
const preventFormDefault = (event: SubmitEvent) => { | ||
// When Enter is pressed, the form is submitted to the action URL (POST /). | ||
// As we are using a controlled component, we need to disable this behavior here. | ||
event.preventDefault(); | ||
}; | ||
|
||
function FormElement(props: ViewProps, outerRef: ForwardedRef<View>) { | ||
const formRef = useRef<HTMLFormElement & View>(null); | ||
const mergedRef = mergeRefs(formRef, outerRef); | ||
|
||
useEffect(() => { | ||
const formCurrent = formRef.current; | ||
|
||
if (!formCurrent) { | ||
return; | ||
} | ||
|
||
// Prevent the browser from applying its own validation, which affects the email input | ||
formCurrent.setAttribute('novalidate', ''); | ||
|
||
// Password Managers need these attributes to be able to identify the form elements properly. | ||
formCurrent.setAttribute('method', 'post'); | ||
formCurrent.setAttribute('action', '/'); | ||
formCurrent.addEventListener('submit', preventFormDefault); | ||
|
||
return () => { | ||
formCurrent.removeEventListener('submit', preventFormDefault); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<View | ||
role={ComponentUtils.ACCESSIBILITY_ROLE_FORM} | ||
ref={mergedRef} | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...props} | ||
/> | ||
); | ||
} | ||
|
||
FormElement.displayName = 'FormElement'; | ||
|
||
export default forwardRef(FormElement); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters