`autofocus` prop has no effect on initial render in React components (e.g. `<TextField autofocus />`)
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 68/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- react, typescript
- Lĩnh vực
- frontend
Hướng nghiên cứu
Start by reading DelegateFocusMixin and the @lit/react createComponent path, then compare the ordering used by createComponentWithOrderedProps. Reproduce the React example and verify that TextField receives focus on initial render while preserving the plain-HTML behavior; existing relevant tests, if present, should cover both property-setting orders.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Description
When a field is rendered from React with the autofocus prop, it does not receive focus on initial render. Focus stays on <body>.
The prop is set on the element (element.autofocus === true afterwards), but it has no effect. DelegateFocusMixin reads autofocus only once, in ready(). That runs when the element is connected. The React wrapper (@lit/react createComponent) assigns element properties in a useLayoutEffect, after React has inserted the element into the DOM. So ready() has already run with autofocus === false.
Expected outcome
<TextField autofocus /> focuses the field's input on initial render, the same as in plain HTML, where autofocus is set before the element is connected.
Minimal reproducible example
import { TextField } from '@vaadin/react-components/TextField.js';
export default function View() {
return <TextField label="Email" autofocus />;
}
Open the view, then check document.activeElement → BODY.
Without React, the result depends on when autofocus is set:
const a = document.createElement('vaadin-text-field');
a.autofocus = true; // set BEFORE connecting
document.body.append(a); // → gets focus ✅
const b = document.createElement('vaadin-text-field');
document.body.append(b);
b.autofocus = true; // set AFTER connecting (what the React wrapper does)
// → no focus ❌
Workaround
Call focus() from a ref after mount, deferred by one animation frame:
const ref = useRef<TextFieldElement>(null);
useEffect(() => {
const f = requestAnimationFrame(() => ref.current?.focus());
return () => cancelAnimationFrame(f);
}, []);
Possible fixes
DelegateFocusMixin: also react toautofocuschanging totruebefore the element has been focused or interacted with (e.g. an observer, or reading it infirstUpdated), or@vaadin/react-components: applyautofocusbefore the element is connected (likecreateComponentWithOrderedPropsdoes for other order-sensitive props).
Environment
- Vaadin 25.0.9 (
@vaadin/react-components25.0.9,@vaadin/text-field25.0.9,@vaadin/a11y-base25.0.9) @lit/react1.0.8, React 19.2.4- Hilla file router, Spring Boot 4.0.6, Java 25
Browsers
Chrome 153 (Windows 11). Also reproduced in headless Chrome.
- Ngôn ngữ chính
- TypeScript
- Star
- 18
- Fork
- 4
- Merge trung bình
- 44 phút
- Pull request đã merge (30 ngày)
- 9
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của vaadin/react-components
-
bug Impact: Low Severity: Major
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 48/100
vaadin/react-components#398 · 1 bình luận ·
-
Add Vertical mode in TabSheetĐang mởenhancement
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 45/100
vaadin/react-components#338 · 1 reaction ·
-
hilla react
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 45/100
vaadin/react-components#337 · 8 bình luận ·
-
bug Impact: Low Severity: Major
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 50/100
vaadin/react-components#323 ·
-
bug Impact: Low Severity: Minor
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 45/100
vaadin/react-components#322 ·
Tất cả issue của vaadin/react-components
Issue tương tự
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 82/100
siyuan-note/siyuan#20313 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 92/100
alunduil/projects-v2-sync#14 ·
-
Service process inherits the caller's cwd at first use, holding that folder open on Windows (EBUSY)Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
-
DevTools page styles leak into the host app in developmentCó thể đã có người làm @onmax đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
nuxt-modules/better-auth#567 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày