-
Notifications
You must be signed in to change notification settings - Fork 292
fix(input): 兼容h5和小程序获取原生input标签 #3341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(input): 兼容h5和小程序获取原生input标签 #3341
Conversation
Walkthrough在 Taro 版 Input 中新增 getNativeInput(),用于在 WEB 环境定位包裹内的原生 input;非 WEB 环境返回现有 ref。将原先直接对 inputRef 调用 focus/blur 的代码改为对 getNativeInput() 返回的原生元素调用。无对外 API 变更。 Changes
Sequence Diagram(s)sequenceDiagram
participant U as 用户
participant Input as Input(Taro)
participant G as getNativeInput()
participant N as 原生 input 元素
U->>Input: 触发 focus() / blur()
Input->>G: 调用 getNativeInput()
alt WEB
Note right of G: 在容器内查询实际 input(DOM)
G-->>Input: 返回内部原生 input 元素
else 非 WEB
Note right of G: 返回 ref.current(已有引用)
G-->>Input: 返回原生 input 引用
end
Input->>N: 对返回元素调用 focus() 或 blur()(可选链)
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 分钟 Possibly related PRs
Suggested reviewers
Poem
Tip 🔌 Remote MCP (Model Context Protocol) integration is now available!Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats. 📜 Recent review detailsConfiguration used: CodeRabbit UI 💡 Knowledge Base configuration:
You can enable these sources in your CodeRabbit configuration. 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## feat_v3.x #3341 +/- ##
=============================================
- Coverage 88.13% 88.12% -0.01%
=============================================
Files 291 291
Lines 19140 19140
Branches 2971 2970 -1
=============================================
- Hits 16869 16868 -1
- Misses 2266 2267 +1
Partials 5 5 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (1)
src/packages/input/input.taro.tsx(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: build
- GitHub Check: test
🔇 Additional comments (1)
src/packages/input/input.taro.tsx (1)
96-102: 用原生 input 执行 focus/blur 的方向正确在 H5 下通过定位到 wrapper 内部的原生 input 再调用
.focus()/.blur(),可以有效规避taro-input-core包裹导致的聚焦失效问题。与非 WEB 环境保持兼容,不改变对外 API,取舍合理。请在以下环境做一次冒烟验证以确保没有回归:
- H5(WEB):ref.current.focus() 能正确聚焦到实际的原生输入框。
- 小程序(WEAPP):ref.current.focus() 仍按旧行为可用(或不报错)。
| // 兼容H5和小程序获取原生input标签 | ||
| const getNativeInput = () => { | ||
| if (Taro.getEnv() === 'WEB') { | ||
| const taroInputCoreEl = inputRef.current as HTMLElement | ||
| const inputEl = taroInputCoreEl?.querySelector?.('input') | ||
| return inputEl | ||
| } | ||
| return inputRef.current | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
明确返回类型并增强选择器,避免 TS 报错与 H5 兼容隐患
当前 getNativeInput() 在分支中返回 querySelector 的结果,TS 会推断为 Element | null。在行 96/101 对其调用 .focus()/.blur() 时,Element 类型并不包含这些方法,严格 TS 设置下会报错。此外,H5 下 Taro 自定义元素可能启用 Shadow DOM,直接 querySelector('input') 在未来版本可能失效。
建议:
- 显式将返回类型标注为
HTMLElement | null,并将querySelector结果断言为HTMLInputElement | null。 - 同时兼容
shadowRoot下的查询,提升健壮性。 - 与本文件后文一致,统一使用
getEnv() === ENV_TYPE.WEB,避免硬编码字符串。
可在本段内直接改为:
- // 兼容H5和小程序获取原生input标签
- const getNativeInput = () => {
- if (Taro.getEnv() === 'WEB') {
- const taroInputCoreEl = inputRef.current as HTMLElement
- const inputEl = taroInputCoreEl?.querySelector?.('input')
- return inputEl
- }
- return inputRef.current
- }
+ // 兼容 H5 和小程序获取原生 input 标签
+ const getNativeInput = (): HTMLElement | null => {
+ if (getEnv() === ENV_TYPE.WEB) {
+ const coreEl = inputRef.current as HTMLElement | null
+ const inputEl =
+ coreEl?.querySelector?.('input') ??
+ coreEl?.shadowRoot?.querySelector?.('input')
+ return (inputEl as HTMLInputElement | null) ?? null
+ }
+ // 非 WEB:保持历史行为,直接返回 ref(其实例通常也具备 focus/blur)
+ return (inputRef.current as unknown as HTMLElement) ?? null
+ }额外(变更范围外)的类型一致性建议:将 inputRef 从 useRef<HTMLInputElement>(null) 调整为更符合实际的 useRef<HTMLElement | null>(null),减少后续断言。
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| // 兼容H5和小程序获取原生input标签 | |
| const getNativeInput = () => { | |
| if (Taro.getEnv() === 'WEB') { | |
| const taroInputCoreEl = inputRef.current as HTMLElement | |
| const inputEl = taroInputCoreEl?.querySelector?.('input') | |
| return inputEl | |
| } | |
| return inputRef.current | |
| } | |
| // 兼容 H5 和小程序获取原生 input 标签 | |
| const getNativeInput = (): HTMLElement | null => { | |
| if (getEnv() === ENV_TYPE.WEB) { | |
| const coreEl = inputRef.current as HTMLElement | null | |
| const inputEl = | |
| coreEl?.querySelector?.('input') ?? | |
| coreEl?.shadowRoot?.querySelector?.('input') | |
| return (inputEl as HTMLInputElement | null) ?? null | |
| } | |
| // 非 WEB:保持历史行为,直接返回 ref(其实例通常也具备 focus/blur) | |
| return (inputRef.current as unknown as HTMLElement) ?? null | |
| } |
🤖 Prompt for AI Agents
In src/packages/input/input.taro.tsx around lines 80 to 88, getNativeInput
currently returns a loosely-typed Element | null from querySelector and uses a
hard-coded 'WEB' env check; update it to explicitly return HTMLElement | null
(more specifically HTMLInputElement | null when possible), replace Taro.getEnv()
=== 'WEB' with Taro.getEnv() === ENV_TYPE.WEB, cast the querySelector result to
HTMLInputElement | null, and also check for and query inside
taroInputCoreEl.shadowRoot if present so Shadow DOM inputs are found; optionally
update the inputRef type to use useRef<HTMLElement | null>(null) (or HTMLElement
| HTMLInputElement | null) to reduce later assertions where .focus()/.blur() are
called (lines ~96/101).
* chore(release): v3.0.18 * fix(swiper): duration属性透传 (#3337) * feat: 修复属性透传 * feat: 直接删除duration * fix(range): taro环境异步渲染useReady不会触发 (#3297) * fix(range): taro环境异步渲染useReady不会触发 * refactor: 将组件中使用的useReady替换为useLayoutEffect * chore: 升级版本号 * feat(popup): 增加上下滑动修改高度 * fix(input): 兼容h5和小程序获取原生input标签 (#3341) * feat: inputRef获取真实input-dom * feat: 兼容小程序和h5获取input标签 * feat: 取消?问好,出错直接抛出 * feat(Popup): 新增弹层可上下滑动 (#3340) * feat: 支持popup 高度可以伸缩 * feat: 适配小程序 * feat: 修改文档 * fix: 默认值不需要,走样式 * feat: 增加使用的限制条件 * docs: 增加文档 * fix: 适配鸿蒙,初始值重置修改 * test: 添加单测 * fix: 增加h5 的初始值 * fix: 增加高度下限约束 * test: fix 单测 * feat(popup): 添加自定义顶部 * fix(input): taro 下只读可以点击 --------- Co-authored-by: Alex.hxy <1872591453@qq.com> Co-authored-by: YONGQI <holyfata@qq.com> Co-authored-by: RyanCW <75795462+Ryan-CW-Code@users.noreply.github.com>
🤔 这个变动的性质是?
🔗 相关 Issue
@nutui/nutui-react-taro在h5点应用场景下,如果直接获取Taro组件库的Input元素调用focus方法会失效,应为Taro在外层保留一个taro-input-core

💡 需求背景和解决方案
解决h5环境下无法正确获取input原生标签
☑️ 请求合并前的自查清单
Summary by CodeRabbit