♻️ enhan: auto focus input email

:%s
This commit is contained in:
lytrankieio123
2021-09-29 17:24:45 +07:00
parent 48ab2f2242
commit 1383d66832
2 changed files with 16 additions and 4 deletions

View File

@@ -1,10 +1,13 @@
import classNames from 'classnames' import classNames from 'classnames'
import { Field } from 'formik' import { Field } from 'formik'
import React, { useMemo, useRef } from 'react' import React, { forwardRef, useImperativeHandle, useMemo, useRef } from 'react'
import { IconCheck, IconError } from 'src/components/icons' import { IconCheck, IconError } from 'src/components/icons'
import { KEY } from 'src/utils/constanst.utils' import { KEY } from 'src/utils/constanst.utils'
import s from './InputFiledInForm.module.scss' import s from './InputFiledInForm.module.scss'
type Ref = {
focus: () => void
} | null
interface Props { interface Props {
placeholder?: string placeholder?: string
type?: 'text' | 'number' | 'email' | 'password' type?: 'text' | 'number' | 'email' | 'password'
@@ -21,7 +24,7 @@ interface Props {
onEnter?: (value: string | number) => void onEnter?: (value: string | number) => void
} }
const InputFiledInForm = ({ const InputFiledInForm = forwardRef<Ref, Props>(({
name, name,
placeholder, placeholder,
type, type,
@@ -32,9 +35,15 @@ const InputFiledInForm = ({
isShowIconSuccess, isShowIconSuccess,
error, error,
onEnter, onEnter,
}: Props) => { }: Props, ref) => {
const inputElementRef = useRef<HTMLInputElement>(null) const inputElementRef = useRef<HTMLInputElement>(null)
useImperativeHandle(ref, () => ({
focus: () => {
inputElementRef.current?.focus()
},
}))
const iconElement = useMemo(() => { const iconElement = useMemo(() => {
if (error) { if (error) {
return ( return (
@@ -83,12 +92,14 @@ const InputFiledInForm = ({
placeholder={placeholder} placeholder={placeholder}
onKeyDown={handleKeyDown} onKeyDown={handleKeyDown}
type={type} type={type}
innerRef={inputElementRef}
/> />
</div> </div>
{error && <div className={s.errorMessage}>{error}</div>} {error && <div className={s.errorMessage}>{error}</div>}
</div> </div>
) )
} })
InputFiledInForm.displayName = 'InputFiledInForm' InputFiledInForm.displayName = 'InputFiledInForm'
export default InputFiledInForm export default InputFiledInForm

View File

@@ -83,6 +83,7 @@ const FormRegister = ({ onSwitch, isHide }: Props) => {
<InputFiledInForm <InputFiledInForm
name="email" name="email"
placeholder="Email Address" placeholder="Email Address"
ref = {emailRef}
error={ error={
touched.email && errors.email touched.email && errors.email
? errors.email.toString() ? errors.email.toString()