-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: prevent redirecting to / on form submit #15240
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -2,6 +2,12 @@ import React from 'react'; | |||
import FormElement from '../FormElement'; | ||||
|
||||
class Form extends React.Component { | ||||
constructor(props) { | ||||
super(props); | ||||
|
||||
this.preventFormDefault = this.preventFormDefault.bind(this); | ||||
} | ||||
|
||||
componentDidMount() { | ||||
if (!this.form) { | ||||
return; | ||||
|
@@ -10,6 +16,22 @@ class Form extends React.Component { | |||
// Password Managers need these attributes to be able to identify the form elements properly. | ||||
this.form.setAttribute('method', 'post'); | ||||
this.form.setAttribute('action', '/'); | ||||
|
||||
this.form.addEventListener('submit', this.preventFormDefault); | ||||
} | ||||
|
||||
componentWillUnmount() { | ||||
if (!this.form) { | ||||
return; | ||||
} | ||||
|
||||
this.form.removeEventListener('submit', this.preventFormDefault); | ||||
} | ||||
|
||||
preventFormDefault(event) { | ||||
// When enter is pressed form is submitted to action url (POST /). | ||||
// As we are using controlled component, we need to disable it here. | ||||
event.preventDefault(); | ||||
} | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is intentional right, why prevent that? cc: @thienlnam There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I believe so, if this was using the form default submit action before then how does this work now? Is there a onSubmit handler for the form now? Also if there is, then you can prevent eventDefault via the onSubmit handler instead of adding an event listener like this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. App/src/pages/SetPasswordPage.js Line 103 in 94057ba
Form submit is handled in this component. I believe, there is no There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We do not use the Form here, curious why? @luacmartins or @thienlnam ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||
|
||||
render() { | ||||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -117,6 +117,7 @@ class SetPasswordPage extends Component { | |||
message={error} | ||||
isAlertVisible={!_.isEmpty(error)} | ||||
isDisabled={!this.state.isFormValid} | ||||
disablePressOnEnter | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Enter should submit the form, right? Can you clarify There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. App/src/pages/SetPasswordPage.js Line 103 in 94057ba
Enter is handled in this component. |
||||
/> | ||||
</View> | ||||
</FormSubmit> | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still confused, here we set post action. And we have a
submit
listener whichThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Santhosh-Sellavel this is the default browser behaviour to submit form data to action url, and this makes a full page refresh. As in our case this is a web app and does not require refresh. We need to prevent this default behaviour, otherwise user will be redirected to / when he presses enter on password input field. Form submition is manually handled using ajax request.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
react-native-web
does not acceptonSubmit
prop, thereforeaddEventListener
is used here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm okay here, but double checking @thienlnam Any thoughts?
cc: @luacmartins would you like to add anything?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where did you find this out? I'm pretty sure this is not true as we have it many places and onSubmit is a common prop that seems like it would be supported in react-native-web
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that this should be supported, and this has been raised earlier in this issue, also you can check the docs page here this mentions the supported props.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Took a look, it seems like in that issue they are trying to add a onSubmit handler to the View which isn't even supported in react-native. The on submit has to be applied to the button event
Following the code, it seems that we're setting up our own onSubmit event handler in FormSubmit which wraps is wrapped in all the sign in content
Can you try preventing default in the FormSubmit component?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried, but it does not work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels off and weird, when I debug this code never executes i.e breakpoint never hit. So I remove the block and checked whether this occurs it didn't occur. But when I turned off debugging I was able to reproduce the issue and adding this back solves the issue.
If we are good with this lets go ahead and merge, thanks! @thienlnam