Skip to content
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

Migrate SignInPageForm/index.js to function component #24027

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 34 additions & 38 deletions src/components/SignInPageForm/index.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,47 @@
import React from 'react';
import React, {useEffect, useRef} from 'react';
import FormElement from '../FormElement';

class Form extends React.Component {
constructor(props) {
super(props);
const 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();
};

this.preventFormDefault = this.preventFormDefault.bind(this);
}
function Form(props) {
const form = useRef(null);

componentDidMount() {
if (!this.form) {
useEffect(() => {
const formCurrent = form.current;

if (!formCurrent) {
return;
}

// Prevent the browser from applying its own validation, which affects the email input
this.form.setAttribute('novalidate', '');
formCurrent.setAttribute('novalidate', '');

// 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();
}

render() {
return (
<FormElement
ref={(el) => (this.form = el)}
// eslint-disable-next-line react/jsx-props-no-spreading
{...this.props}
/>
);
}
formCurrent.setAttribute('method', 'post');
formCurrent.setAttribute('action', '/');
formCurrent.addEventListener('submit', preventFormDefault);

return () => {
if (!formCurrent) {
return;
}
formCurrent.removeEventListener('submit', preventFormDefault);
};
}, []);

return (
<FormElement
ref={form}
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
/>
);
}

Form.displayName = 'Form';

export default Form;
Loading