Skip to content

Commit 424cbc5

Browse files
authored
Prevent focus reset on date/time input clicks in Safari (#7189)
1 parent 70f5ffe commit 424cbc5

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

.changeset/eleven-apes-prove.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/react': patch
3+
---
4+
5+
Prevent focus reset on date/time input clicks in Safari.

packages/react/src/TextInput/TextInput.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const Playground = (args: FormControlArgs<TextInputProps>) => {
3232
<form className={classes.Container}>
3333
<FormControl {...parentArgs}>
3434
<FormControl.Label {...labelArgs} />
35-
<TextInput value={value} onChange={handleChange} {...args} />
35+
<TextInput value={value} onChange={handleChange} onInput={handleChange} {...args} />
3636
{captionArgs.children && <FormControl.Caption {...captionArgs} />}
3737
{validationArgs.children && validationArgs.variant && (
3838
<FormControl.Validation {...validationArgs} variant={validationArgs.variant} />

packages/react/src/TextInput/TextInput.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,15 @@ const TextInput = React.forwardRef<HTMLInputElement, TextInputProps>(
9797
loading && (loaderPosition === 'leading' || Boolean(LeadingVisual && loaderPosition !== 'trailing'))
9898
const showTrailingLoadingIndicator =
9999
loading && (loaderPosition === 'trailing' || Boolean(loaderPosition === 'auto' && !LeadingVisual))
100-
const focusInput: MouseEventHandler = () => {
101-
inputRef.current?.focus()
100+
101+
// Date/time input types that have segment-based focus
102+
const isSegmentedInputType = type === 'date' || type === 'time' || type === 'datetime-local'
103+
104+
const focusInput: MouseEventHandler = e => {
105+
// Don't call focus() if the input itself was clicked on date/time inputs.
106+
if (e.target !== inputRef.current || !isSegmentedInputType) {
107+
inputRef.current?.focus()
108+
}
102109
}
103110
const leadingVisualId = useId()
104111
const trailingVisualId = useId()

0 commit comments

Comments
 (0)