fix(taro-components): textarea autoHeight 回显不生效#19271
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
Walkthrough在受控 value 更新后和组件首次加载时新增对 handleLineChange() 的调用,确保由值变化或初始内容引起的行数/高度变化能触发并上报 linechange 事件。 改动内容Textarea 行变化事件同步
预期代码审查工作量🎯 1 (简单) | ⏱️ ~5 分钟 可能相关的 PRs
建议审查人
诗
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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
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. Comment |
There was a problem hiding this comment.
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
📒 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() |
There was a problem hiding this comment.
建议添加防御性的空值检查。
虽然在正常情况下 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.
04ff944 to
938e8b7
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ 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
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
这个 PR 做了什么? (简要描述所做更改)
修复 textarea h5 props.value 有值的情况下 autoHeight 不生效,对齐微信小程序表现
这个 PR 是什么类型? (至少选择一个)
这个 PR 涉及以下平台:
Summary by CodeRabbit