mirror of
https://github.com/vercel/commerce.git
synced 2025-07-22 20:26:49 +00:00
♻️ enhan: auto focus input email
:%s
This commit is contained in:
@@ -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
|
||||||
|
@@ -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()
|
||||||
|
Reference in New Issue
Block a user