Skip to content

Commit ca05e7c

Browse files
authored
Fix clicking <Label /> opens <input type="file"> (#3707)
This PR fixes an issue where the `<Label>` component didn't open the `<input type="file">` when clicking it. For native elements, the `Label` component already renders a native `<label>` behind the scenes. Some native elements like `<input type="checkbox">` immediately change the state of the element whereas some other elements don't such as `<select></select>` you just get the focus. However, `<input type="file">` should also immediately open the file picker when clicking the label and this was not the case. This PR fixes that. Since we are already using a native `<label>` _and_ linking the `<label>` with its `<input type="file">` performing a `.click()` is allowed. Fixes: #3680 ## Test plan You can play with it here: https://headlessui-react-git-fix-issue-3680-tailwindlabs.vercel.app/combinations/form This video shows how it behaves now: https://github.com/user-attachments/assets/26467f83-d91d-4a79-98f9-dd91214ea037
1 parent 1461b65 commit ca05e7c

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

packages/@headlessui-react/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Add a quick trigger action to the `Menu`, `Listbox` and `Combobox` components ([#3700](https://github.com/tailwindlabs/headlessui/pull/3700))
1313

14+
### Fixed
15+
16+
- Fix clicking `Label` component should open `<Input type="file">` ([#3707](https://github.com/tailwindlabs/headlessui/pull/3707))
17+
1418
## [2.2.2] - 2025-04-17
1519

1620
### Fixed

packages/@headlessui-react/src/components/label/label.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ function LabelFn<TTag extends ElementType = typeof DEFAULT_LABEL_TAG>(
166166
// unchecked).
167167
if (
168168
(target instanceof HTMLInputElement &&
169-
(target.type === 'radio' || target.type === 'checkbox')) ||
169+
(target.type === 'file' || target.type === 'radio' || target.type === 'checkbox')) ||
170170
target.role === 'radio' ||
171171
target.role === 'checkbox' ||
172172
target.role === 'switch'

playgrounds/react/pages/combinations/form.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Combobox, Listbox, RadioGroup, Switch } from '@headlessui/react'
1+
import { Combobox, Field, Input, Label, Listbox, RadioGroup, Switch } from '@headlessui/react'
22
import { useState } from 'react'
33
import { Button } from '../../components/button'
44
import { classNames } from '../../utils/class-names'
@@ -332,6 +332,24 @@ export default function App() {
332332
</Combobox>
333333
</div>
334334
</Section>
335+
<Section title="Default form controls">
336+
<Field className="flex flex-col p-1">
337+
<Label>Label for {'<Input type="text">'}</Label>
338+
<Input type="text" />
339+
</Field>
340+
<Field className="flex flex-col p-1">
341+
<Label>Label for {'<Input type="checkbox">'}</Label>
342+
<Input type="checkbox" />
343+
</Field>
344+
<Field className="flex flex-col p-1">
345+
<Label>Label for {'<Input type="radio">'}</Label>
346+
<Input type="radio" />
347+
</Field>
348+
<Field className="flex flex-col p-1">
349+
<Label>Label for {'<Input type="file">'}</Label>
350+
<Input type="file" />
351+
</Field>
352+
</Section>
335353
</div>
336354

337355
<div className="space-x-4">

0 commit comments

Comments
 (0)