Skip to content

Commit

Permalink
Merge pull request #24027 from rayane-djouah/Migrate-SignInPageForm/i…
Browse files Browse the repository at this point in the history
…ndex.js-to-function-component

Migrate SignInPageForm/index.js to function component
  • Loading branch information
deetergp authored Aug 31, 2023
2 parents b57d3a3 + ec137d9 commit 5f9bb2e
Showing 1 changed file with 34 additions and 38 deletions.
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;

0 comments on commit 5f9bb2e

Please sign in to comment.