Skip to content

fix(taro-components): textarea autoHeight 回显不生效#19271

Merged
Single-Dancer merged 1 commit into
mainfrom
fix/textarea
May 26, 2026
Merged

fix(taro-components): textarea autoHeight 回显不生效#19271
Single-Dancer merged 1 commit into
mainfrom
fix/textarea

Conversation

@Single-Dancer

@Single-Dancer Single-Dancer commented May 25, 2026

Copy link
Copy Markdown
Collaborator

这个 PR 做了什么? (简要描述所做更改)
修复 textarea h5 props.value 有值的情况下 autoHeight 不生效,对齐微信小程序表现

这个 PR 是什么类型? (至少选择一个)

  • 错误修复 (Bugfix) issue: fix #
  • 新功能 (Feature)
  • 代码重构 (Refactor)
  • TypeScript 类型定义修改 (Types)
  • 文档修改 (Docs)
  • 代码风格更新 (Code style update)
  • 构建优化 (Chore)
  • 其他,请描述 (Other, please describe):

这个 PR 涉及以下平台:

  • 所有平台
  • Web 端(H5)
  • 移动端(React-Native)
  • 鸿蒙(Harmony)
  • 鸿蒙容器(Harmony Hybrid)
  • ASCF 元服务
  • 快应用(QuickApp)
  • 所有小程序
  • 微信小程序
  • 企业微信小程序
  • 京东小程序
  • 百度小程序
  • 支付宝小程序
  • 支付宝 IOT 小程序
  • 钉钉小程序
  • QQ 小程序
  • 飞书小程序
  • 快手小程序
  • 头条小程序

Summary by CodeRabbit

  • Bug Fixes
    • 修复受控文本区域在 value 更新后未触发行数/高度变化事件的问题,现会在同步写入后触发 linechange 上报。
    • 优化组件首次加载时的行数检测,组件初始化后会立即计算并上报当前行数变化,提升初始显示准确性。

Review Change Stack

@Single-Dancer Single-Dancer requested a review from Muyouz May 25, 2026 06:07
@Single-Dancer Single-Dancer added this to the 4.2.1 milestone May 25, 2026
@Single-Dancer Single-Dancer added the A-components Area - H5 组件库相关 label May 25, 2026
@coderabbitai

coderabbitai Bot commented May 25, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 7a6b418d-2775-45b2-b556-d502b97f1cd4

📥 Commits

Reviewing files that changed from the base of the PR and between 04ff944 and 938e8b7.

📒 Files selected for processing (1)
  • packages/taro-components/src/components/textarea/textarea.tsx

Walkthrough

在受控 value 更新后和组件首次加载时新增对 handleLineChange() 的调用,确保由值变化或初始内容引起的行数/高度变化能触发并上报 linechange 事件。

改动内容

Textarea 行变化事件同步

Layer / File(s) Summary
行变化事件触发点
packages/taro-components/src/components/textarea/textarea.tsx
watchValue 中值同步至 textareaRef.value 后调用 handleLineChange(),并在 componentDidLoad 中新增对 handleLineChange() 的调用以初始化并可能发出 linechange 事件。

预期代码审查工作量

🎯 1 (简单) | ⏱️ ~5 分钟

可能相关的 PRs

  • NervJS/taro#18731: 也修正了受控输入/组合输入相关的值/行变化处理逻辑,涉及类似的事件/值同步改动。

建议审查人

  • tutuxxx

🐰 文本框数行行,
加载更新都记心,
handleLineChange 来报到,
事件不再漏一分,
换行高度自适应。

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed PR 标题清晰准确地反映了主要改动:修复 textarea 的 autoHeight 在有 value 时不生效的问题,与代码变更(在 watchValue 和 componentDidLoad 中调用 handleLineChange)完全对应。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/textarea

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install failed: dependency version conflict. Check your lock file or package.json.


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@Single-Dancer Single-Dancer requested a review from ZakaryCode May 25, 2026 06:07
@coderabbitai coderabbitai Bot requested a review from tutuxxx May 25, 2026 06:08

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/taro-components/src/components/textarea/textarea.tsx`:
- Line 101: Add defensive null checks around usage of the textarea DOM ref:
ensure this.textareaRef is present before calling this.handleLineChange() in
componentDidLoad or—preferably—add an early return guard at the start of
handleLineChange() and getNumberOfLines() that checks if this.textareaRef (and
any accessed properties like value/scrollHeight) is null/undefined; this
prevents runtime errors when textareaRef is not yet set and keeps callers (e.g.,
componentDidLoad) simple.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e7db2fd1-5bd3-4db5-9608-2cb9496d841b

📥 Commits

Reviewing files that changed from the base of the PR and between 9c6c511 and 04ff944.

📒 Files selected for processing (1)
  • packages/taro-components/src/components/textarea/textarea.tsx

componentDidLoad () {
this.textareaRef?.addEventListener('compositionstart', this.handleComposition)
this.textareaRef?.addEventListener('compositionend', this.handleComposition)
this.handleLineChange()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

建议添加防御性的空值检查。

虽然在正常情况下 componentDidLoad 执行时 textareaRef 应该已经设置,但 handleLineChange()getNumberOfLines() 方法都直接访问 this.textareaRef 的属性而没有进行空值检查(参见第 177、272 行)。建议添加防御性检查以避免潜在的运行时错误。

🛡️ 建议的修复方案

方案一:在调用点检查

 componentDidLoad () {
   this.textareaRef?.addEventListener('compositionstart', this.handleComposition)
   this.textareaRef?.addEventListener('compositionend', this.handleComposition)
-  this.handleLineChange()
+  if (this.textareaRef) {
+    this.handleLineChange()
+  }
 }

方案二:在 handleLineChange 方法内部检查(更全面)

 handleLineChange = () => {
+  if (!this.textareaRef) return
   const line = this.getNumberOfLines()
   if (line !== this.line) {
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/taro-components/src/components/textarea/textarea.tsx` at line 101,
Add defensive null checks around usage of the textarea DOM ref: ensure
this.textareaRef is present before calling this.handleLineChange() in
componentDidLoad or—preferably—add an early return guard at the start of
handleLineChange() and getNumberOfLines() that checks if this.textareaRef (and
any accessed properties like value/scrollHeight) is null/undefined; this
prevents runtime errors when textareaRef is not yet set and keeps callers (e.g.,
componentDidLoad) simple.

@codecov

codecov Bot commented May 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 56.07%. Comparing base (bd51e97) to head (938e8b7).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main   #19271   +/-   ##
=======================================
  Coverage   56.07%   56.07%           
=======================================
  Files         447      447           
  Lines       23454    23454           
  Branches     5831     5813   -18     
=======================================
  Hits        13151    13151           
  Misses       8583     8583           
  Partials     1720     1720           
Flag Coverage Δ
taro-cli 72.85% <ø> (ø)
taro-runtime 58.33% <ø> (ø)
taro-web 53.12% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Single-Dancer Single-Dancer merged commit fac9a2c into main May 26, 2026
41 checks passed
@Single-Dancer Single-Dancer deleted the fix/textarea branch May 26, 2026 04:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-components Area - H5 组件库相关

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants