mirror of
https://github.com/rjNemo/devbook_ts
synced 2026-06-12 13:36:43 +00:00
extract useForm hook
This commit is contained in:
parent
e7c1486e78
commit
252674c5bf
2 changed files with 42 additions and 14 deletions
28
src/hooks/index.tsx
Normal file
28
src/hooks/index.tsx
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
import {useState, ChangeEvent} from 'react';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* provide onChange handler and reset function
|
||||||
|
* T is the initFormData object type
|
||||||
|
*
|
||||||
|
* @param initFormData initial state of the form
|
||||||
|
* @returns formData object,
|
||||||
|
* @returns handleChange function to pass to input tag
|
||||||
|
* @returns resetForm function to revert to initFormData
|
||||||
|
* */
|
||||||
|
const useForm = <T,>(initFormData: T) => {
|
||||||
|
const [formData, setFormData] = useState<T>(initFormData);
|
||||||
|
|
||||||
|
/** update each input state value onChange */
|
||||||
|
const handleChange = (e: ChangeEvent<HTMLInputElement>): void =>
|
||||||
|
setFormData({
|
||||||
|
...formData,
|
||||||
|
[e.target.name]: e.target.value,
|
||||||
|
});
|
||||||
|
|
||||||
|
/** clean form after successful submition */
|
||||||
|
const resetForm = () => setFormData(initFormData);
|
||||||
|
|
||||||
|
return {formData, handleChange, resetForm};
|
||||||
|
};
|
||||||
|
|
||||||
|
export default useForm;
|
||||||
|
|
@ -13,6 +13,7 @@ import Header from '../components/Header';
|
||||||
import {FontAwesomeIcon} from '@fortawesome/react-fontawesome';
|
import {FontAwesomeIcon} from '@fortawesome/react-fontawesome';
|
||||||
import {faExclamationTriangle} from '@fortawesome/free-solid-svg-icons';
|
import {faExclamationTriangle} from '@fortawesome/free-solid-svg-icons';
|
||||||
import GoogleButton from 'react-google-button';
|
import GoogleButton from 'react-google-button';
|
||||||
|
import useForm from '../hooks';
|
||||||
|
|
||||||
// extends withFirebaseProps type to ad profile info
|
// extends withFirebaseProps type to ad profile info
|
||||||
interface IProps extends WithFirebaseProps<User> {
|
interface IProps extends WithFirebaseProps<User> {
|
||||||
|
|
@ -20,32 +21,31 @@ interface IProps extends WithFirebaseProps<User> {
|
||||||
isLoaded: boolean;
|
isLoaded: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface InitFormData {
|
||||||
|
name: string;
|
||||||
|
email: string;
|
||||||
|
password: string;
|
||||||
|
password2: string;
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Sign up form recieves firebase from withFirebase HOC
|
* Sign up form recieves firebase from withFirebase HOC
|
||||||
*/
|
*/
|
||||||
const SignUp: FC<IProps> = ({firebase, isEmpty, isLoaded}) => {
|
const SignUp: FC<IProps> = ({firebase, isEmpty, isLoaded}) => {
|
||||||
const history = useHistory();
|
const history = useHistory();
|
||||||
|
const [error, setError] = useState(false);
|
||||||
|
|
||||||
const initFormData = {
|
const initFormData: InitFormData = {
|
||||||
name: '',
|
name: '',
|
||||||
email: '',
|
email: '',
|
||||||
password: '',
|
password: '',
|
||||||
password2: '',
|
password2: '',
|
||||||
error: false,
|
|
||||||
};
|
};
|
||||||
const [formData, setFormData] = useState(initFormData);
|
|
||||||
|
|
||||||
/** update each input state value onChange */
|
const {formData, handleChange, resetForm} = useForm<InitFormData>(
|
||||||
const handleChange = (e: React.ChangeEvent<HTMLInputElement>): void =>
|
initFormData,
|
||||||
setFormData({
|
);
|
||||||
...formData,
|
|
||||||
[e.target.name]: e.target.value,
|
|
||||||
});
|
|
||||||
|
|
||||||
/** clean form after successful submition */
|
const {name, email, password, password2} = formData;
|
||||||
const resetForm = () => setFormData(initFormData);
|
|
||||||
|
|
||||||
const {name, email, password, password2, error} = formData;
|
|
||||||
|
|
||||||
// prevent submitting invalid forms
|
// prevent submitting invalid forms
|
||||||
const isDisabled: boolean = name === '' || email === '' || password === '';
|
const isDisabled: boolean = name === '' || email === '' || password === '';
|
||||||
|
|
@ -54,7 +54,7 @@ const SignUp: FC<IProps> = ({firebase, isEmpty, isLoaded}) => {
|
||||||
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
|
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
if (password !== password2) {
|
if (password !== password2) {
|
||||||
setFormData({...formData, error: true});
|
setError(true);
|
||||||
} else {
|
} else {
|
||||||
// pass the info to store into the second argument
|
// pass the info to store into the second argument
|
||||||
firebase
|
firebase
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue