Skip to content

Commit b6e0c9c

Browse files
committed
fix: forward ref directly and use the new autoFocus field
1 parent 93dad7f commit b6e0c9c

File tree

3 files changed

+13
-41
lines changed

3 files changed

+13
-41
lines changed

packages/components/src/DateField/DateField.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111
import React, { useState, useRef, useEffect } from 'react'
1212
import styled from 'styled-components'
13-
import { ITextInputProps, TextInput, IRef } from '../TextInput/TextInput'
13+
import { ITextInputProps, TextInput } from '../TextInput/TextInput'
1414
import { InputLabel } from '../InputField/InputLabel'
1515

1616
const DateWrapper = styled.div`
@@ -55,9 +55,9 @@ export const DateField = ({
5555
...props
5656
}: IDateFieldProps) => {
5757
const [date, setDate] = useState<IState>({ yyyy: '', mm: '', dd: '' })
58-
const ddRef = useRef<IRef>(null)
59-
const mmRef = useRef<IRef>(null)
60-
const yyyyRef = useRef<IRef>(null)
58+
const ddRef = useRef<HTMLInputElement>(null)
59+
const mmRef = useRef<HTMLInputElement>(null)
60+
const yyyyRef = useRef<HTMLInputElement>(null)
6161
const { dd, mm, yyyy } = date
6262

6363
useEffect(() => {
@@ -82,15 +82,15 @@ export const DateField = ({
8282
return
8383
}
8484
if (val.length > 1 && mmRef.current) {
85-
mmRef.current.focusField()
85+
mmRef.current.focus()
8686
}
8787
break
8888
case 'mm':
8989
if (val.length > 2 || Number(val) > MAX_MONTH_NUMBER) {
9090
return
9191
}
9292
if (val.length > 1 && yyyyRef.current) {
93-
yyyyRef.current.focusField()
93+
yyyyRef.current.focus()
9494
}
9595
break
9696
case 'yyyy':

packages/components/src/TextInput/TextInput.tsx

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,7 @@ const StyledInput = styled.input<ICustomProps>`
112112
}
113113
`
114114

115-
export interface IRef {
116-
focusField: () => void
117-
}
118-
119-
export const TextInput = React.forwardRef<IRef, ITextInputProps>(
115+
export const TextInput = React.forwardRef<HTMLInputElement, ITextInputProps>(
120116
(
121117
{
122118
focusInput,
@@ -130,31 +126,6 @@ export const TextInput = React.forwardRef<IRef, ITextInputProps>(
130126
},
131127
ref
132128
) => {
133-
const $element = React.useRef<HTMLInputElement>(null)
134-
135-
function focusField(): void {
136-
/*
137-
* Needs to be run on the next tick
138-
* so that 'value' prop has enough time to flow back here
139-
* if the focusInput prop is called right after keydown
140-
*/
141-
setTimeout(() => {
142-
if ($element.current) {
143-
$element.current.focus()
144-
}
145-
})
146-
}
147-
148-
React.useImperativeHandle(ref, () => ({
149-
focusField
150-
}))
151-
152-
React.useEffect(() => {
153-
if (focusInput) {
154-
focusField()
155-
}
156-
}, [focusInput])
157-
158129
return (
159130
<StyledInputContainer
160131
touched={otherProps.touched}
@@ -163,7 +134,8 @@ export const TextInput = React.forwardRef<IRef, ITextInputProps>(
163134
>
164135
{prefix && <StyledPrefix>{prefix}</StyledPrefix>}
165136
<StyledInput
166-
ref={$element}
137+
ref={ref}
138+
autoFocus={focusInput}
167139
name={otherProps.id}
168140
{...otherProps}
169141
autoComplete={

packages/components/src/TimeField/TimeField.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111
import * as React from 'react'
1212
import styled from 'styled-components'
13-
import { ITextInputProps, IRef, TextInput } from '../TextInput/TextInput'
13+
import { ITextInputProps, TextInput } from '../TextInput/TextInput'
1414

1515
export interface IProps {
1616
id: string
@@ -103,15 +103,15 @@ export function TimeField(props: ITimeFieldProps) {
103103
}
104104
}, [props.value])
105105

106-
const hh = React.useRef<IRef>(null)
107-
const mm = React.useRef<IRef>(null)
106+
const hh = React.useRef<HTMLInputElement>(null)
107+
const mm = React.useRef<HTMLInputElement>(null)
108108

109109
function change(event: React.ChangeEvent<HTMLInputElement>) {
110110
const val = event.target.value
111111
if (event.target.id.includes('hh')) {
112112
if (Number(val) < 0 || Number(val) > 23) return
113113
if (val.length === 2 && mm?.current !== null) {
114-
mm.current.focusField()
114+
mm.current.focus()
115115
}
116116
setState((state) => ({ ...state, hh: val }))
117117
} else if (event.target.id.includes('mm')) {

0 commit comments

Comments
 (0)