Skip to content

Conversation

@pingtougea
Copy link

@pingtougea pingtougea commented Nov 11, 2025

Related Issue

close #1151

Changes

  • Implemented maxLength input limitation using Ant Design's native TextArea capability
  • Added showCount character counter with design token styling
  • Verified functionality manually in dev server

Environment Note

Local tests failed on Windows due to jsdom compatibility issue (nodejs/node#49173).
✅ Functionality verified manually
✅ CI will validate tests (Linux/Node 18)

Screenshots

屏幕截图 2025-11-11 151413 屏幕截图 2025-11-11 151428 ## Notes for Reviewers As a first-time contributor, I welcome guidance on: - Test case completeness - Design token usage - API consistency

Summary by CodeRabbit

发布说明

  • 新功能

    • Sender 组件新增 maxLength 最大长度与 showCount 字符计数显示,输入长度实时更新。
  • 示例

    • 在演示页面新增带 maxLength 与 showCount 的 Sender 示例。
  • 测试

    • 新增围绕最大长度与计数显示的单元测试用例。
  • 样式

    • 为计数显示添加对应样式调整。

您好,各位维护者~

我在提交这个 PR 时遇到了一些问题,尝试了一些方法但未能解决,想请大家帮忙指导一下:
测试失败问题
所有测试用例都报 TypeError: Cannot read properties of undefined (reading 'get'),错误堆栈指向 jsdom 及其依赖(whatwg-urlwebidl-conversions)。
我已尝试过:
切换 Node.js 版本到 18.x(之前可能使用了其他版本),并重新安装了依赖,但 jsdom 相关的 TypeError: Cannot read properties of undefined (reading 'get') 错误仍然存在。 查了一些资料,这个错误可能和 jsdom 与 Node.js 18+ 的内置 URL 模块冲突有关,或者是项目中某些依赖(如 whatwg-url)的版本与 jsdom 不兼容,但我不太确定具体如何调整。

@github-actions
Copy link
Contributor

github-actions bot commented Nov 11, 2025

Preview is ready

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 11, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

📝 Walkthrough

Walkthrough

向 Sender 组件添加 maxLength 与 showCount 属性:在组件实现中透传 maxLength 至 Input.TextArea、按需渲染字符计数;新增样式、演示和单元测试以覆盖截断与计数行为;同时微调部分导入与可选链调用;未更改除新增 props 之外的导出签名。

Changes

Cohort / File(s) 变更摘要
Sender 组件核心
components/sender/index.tsx
新增 props maxLength?: numbershowCount?: boolean;将 maxLength 传给 Input.TextArea;当 showCount 为 true 且 maxLength 为数字时,条件渲染字符计数(格式:currentLength / maxLength);将对 inputRef 的 focus/blur 调用改为可选链调用;调整少量 import 顺序。
组件样式
components/sender/style/index.ts
新增计数(count)元素样式(布局、字体大小、颜色、行高、间距、对齐、user-select 与 pointer-events 等)。
组件演示
components/sender/demo/basic.tsx
在 Demo 中新增一个 Sender 实例:maxLength={20}showCount 和占位符 "最多20字"
单元测试
components/sender/__tests__/index.test.tsx
调整 import 顺序并引入 user-event;新增 "maxLength and showCount" 测试套件,包含输入截断、计数显示/隐藏、实时更新及受控组件等用例。
构建/工具配置
biome.json
更新忽略规则(新增 scripts/visual-regression/report-template.html)并扩展 lint suspect 规则(新增 noNonNullAssertedOptionalChain)。
视觉回归模版
scripts/visual-regression/report-template.html
重写表格样式与响应式样式,添加点击打开图片的交互处理与若干样式/结构调整(仅模板内 HTML/CSS/JS 变更)。
测试页面
tests/index.html
移除 body 的 !important 强制高度(height: auto 替代);微调 head/body 格式与空行。
项目清单(无实际改动)
package.json
文件末尾换行/无内容性修改(diff 中无配置变更)。

Sequence Diagram(s)

sequenceDiagram
    participant User as 用户
    participant Sender as Sender 组件
    participant Textarea as Input.TextArea
    participant Counter as 计数显示

    User->>Textarea: 输入字符
    Textarea->>Sender: 触发 onChange
    Sender->>Sender: 更新 innerValue(或使用受控 value)
    alt showCount && typeof maxLength === "number"
        Sender->>Counter: 计算并渲染 currentLength / maxLength
        Counter-->>User: 显示计数
    else
        Sender-->>User: 不渲染计数
    end
    Note over Textarea: TextArea 同步遵循传入的 maxLength 做输入截断
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 分钟

  • 需重点复核:
    • components/sender/index.tsx:受控与非受控场景下计数显示与截断的交互逻辑、props 类型声明及可选链替换对焦点行为的影响。
    • components/sender/__tests__/index.test.tsx:user-event 的使用与超长输入/边界断言是否健壮。
    • components/sender/style/index.ts:计数样式与现有布局在不同视口的兼容性。
    • scripts/visual-regression/report-template.html:新增交互(点击打开图片)的兼容性与潜在安全(target handling)检查。

Poem

🐰 我是代码里的小兔子,敲字轻快不费力,
新添字数与限长数,输入有界心自喜,
计数旁边跟着走,测试演示都到位,
跳着庆贺这次变更,愿用户微笑常相随。

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning 检测到多个与主要功能无关的变更:biome.json 配置修改、scripts/visual-regression/report-template.html 大幅重写、tests/index.html CSS 调整。 建议将 biome.json、report-template.html 和 tests/index.html 的变更分离到单独的 PR 中,保持本 PR 专注于 Sender 组件的 maxLength 和 showCount 功能实现。
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed 标题准确地总结了本次变更的主要内容,即为 Sender 组件实现 maxLength 和 showCount 功能支持。
Linked Issues check ✅ Passed 本次 PR 实现了关联问题 #1151 中的所有核心需求:添加 maxLength 属性限制输入长度,添加 showCount 属性显示字符计数。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

@dosubot dosubot bot added enhancement New feature or request javascript Pull requests that update Javascript code labels Nov 11, 2025
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @pingtougea, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces significant enhancements to the Sender component by adding maxLength and showCount props. These additions provide users with better control over input length and real-time feedback on character usage, improving the overall user experience and input validation capabilities within the application.

Highlights

  • New maxLength Prop: The Sender component now supports a maxLength prop, allowing developers to set a maximum character limit for the input field, leveraging Ant Design's native TextArea capabilities.
  • New showCount Prop: A showCount prop has been added to the Sender component, which, when used with maxLength, displays a real-time character counter below the input field, styled with design tokens.
  • Comprehensive Unit Tests: New unit tests have been introduced to validate the functionality of both maxLength and showCount, covering scenarios like input limiting, real-time updates, and controlled component behavior.
  • Demo and Styling Updates: The basic demo for the Sender component has been updated to showcase the new maxLength and showCount features, and corresponding styles have been added for the character counter display.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request successfully implements maxLength and showCount functionality for the Sender component. The implementation is clean and leverages Ant Design's existing capabilities. The new features are accompanied by a new demo and a comprehensive set of tests. My review focuses on improving the newly added tests to ensure they are accurate and robust. I've identified a few issues, including an unused import, incorrect assertions in the character count tests, and a flawed test for the controlled component's behavior. I've also provided a suggestion to make the maxLength test more reliable by using user-event to better simulate user interaction. Overall, this is a great contribution, and with these test fixes, it will be even better.

Comment on lines 287 to 290
expect(container.querySelector('.ant-sender-count')?.textContent).toBe('5 / 20');

fireEvent.change(textarea, { target: { value: 'hello world' } });
expect(container.querySelector('.ant-sender-count')?.textContent).toBe('11 / 20');
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The test expectations for the character count's text content are incorrect. The implementation renders the count as {length}/{maxLength} without spaces, but the assertions expect spaces around the slash (e.g., '5 / 20'). This will cause the test to fail. The expected strings should be updated to match the implementation.

Suggested change
expect(container.querySelector('.ant-sender-count')?.textContent).toBe('5 / 20');
fireEvent.change(textarea, { target: { value: 'hello world' } });
expect(container.querySelector('.ant-sender-count')?.textContent).toBe('11 / 20');
expect(container.querySelector('.ant-sender-count')?.textContent).toBe('5/20');
fireEvent.change(textarea, { target: { value: 'hello world' } });
expect(container.querySelector('.ant-sender-count')?.textContent).toBe('11/20');

Comment on lines 293 to 302
it('should work with controlled component', () => {
const onChange = jest.fn();
const { container, rerender } = render(
<Sender maxLength={10} showCount value="initial" onChange={onChange} />,
);
expect(container.querySelector('.ant-sender-count')?.textContent).toBe('7 / 10');

rerender(<Sender maxLength={10} showCount value="update" onChange={onChange} />);
expect(container.querySelector('.ant-sender-count')?.textContent).toBe('7 / 10');
});
Copy link
Contributor

Choose a reason for hiding this comment

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

high

This test for the controlled component has a couple of issues:

  1. Similar to the other test, it expects spaces in the count string ('7 / 10'), which is inconsistent with the implementation.
  2. More importantly, after re-rendering with value="update" (length 6), the test still asserts the count is '7 / 10', which is based on the old value 'initial'. For a controlled component, the count should update to reflect the new prop value, so it should be '6/10'.

This test is logically incorrect and needs to be fixed.

Suggested change
it('should work with controlled component', () => {
const onChange = jest.fn();
const { container, rerender } = render(
<Sender maxLength={10} showCount value="initial" onChange={onChange} />,
);
expect(container.querySelector('.ant-sender-count')?.textContent).toBe('7 / 10');
rerender(<Sender maxLength={10} showCount value="update" onChange={onChange} />);
expect(container.querySelector('.ant-sender-count')?.textContent).toBe('7 / 10');
});
it('should work with controlled component', () => {
const onChange = jest.fn();
const { container, rerender } = render(
<Sender maxLength={10} showCount value="initial" onChange={onChange} />,
);
expect(container.querySelector('.ant-sender-count')?.textContent).toBe('7/10');
rerender(<Sender maxLength={10} showCount value="update" onChange={onChange} />);
expect(container.querySelector('.ant-sender-count')?.textContent).toBe('6/10');
});

import mountTest from '../../../tests/shared/mountTest';
import rtlTest from '../../../tests/shared/rtlTest';
import Sender from '../index';
import { values } from 'lodash';
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The values function is imported from lodash but it doesn't seem to be used in this file. It's good practice to remove unused imports to keep the code clean.

Comment on lines 266 to 271
it('should limit input length when maxLength is set', () => {
const { container } = render(<Sender maxLength={5} />);
const textarea = container.querySelector('textarea')!;
fireEvent.change(textarea, { target: { value: 'abcdefgh' } });
expect(textarea).toHaveValue('abcde');
});
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This test uses fireEvent.change to verify the maxLength behavior. This can be unreliable because jsdom (the environment for React Testing Library) doesn't fully simulate browser behavior for the maxLength attribute on a <textarea>. Programmatically setting the value with fireEvent.change bypasses this constraint, which may cause this test to fail or behave inconsistently.

To create a more robust and realistic test, I recommend using @testing-library/user-event's type method. It simulates actual user typing and correctly respects the maxLength attribute.

Example:

import userEvent from '@testing-library/user-event';

it('should limit input length when maxLength is set', async () => {
  const { container } = render(<Sender maxLength={5} />);
  const textarea = container.querySelector('textarea')!;
  await userEvent.type(textarea, 'abcdefgh');
  expect(textarea).toHaveValue('abcde');
});

This will make your test more reliable.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5a89718 and 6431837.

📒 Files selected for processing (5)
  • components/sender/__tests__/index.test.tsx (2 hunks)
  • components/sender/demo/basic.tsx (1 hunks)
  • components/sender/index.tsx (2 hunks)
  • components/sender/style/index.ts (1 hunks)
  • package.json (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). (1)
  • GitHub Check: Socket Security: Pull Request Alerts
🔇 Additional comments (5)
components/sender/style/index.ts (1)

90-101: 样式实现正确且符合设计规范

计数器元素的样式定义合理,正确使用了设计 token,布局属性配置得当。

package.json (1)

274-276: 确认 volta 配置的必要性

添加 volta Node 版本固定配置似乎与 maxLength/showCount 功能本身无关。虽然 PR 描述中提到了 Windows 环境的 jsdom 兼容性问题,但请确认:

  1. 此配置是否确实解决了测试问题
  2. 是否应该在单独的 PR 中处理环境配置变更
components/sender/index.tsx (1)

51-52: 新增属性定义正确

maxLengthshowCount 属性的类型定义准确,符合组件需求。

components/sender/demo/basic.tsx (1)

45-45: 示例演示准确且清晰

新增的 Sender 示例正确展示了 maxLengthshowCount 功能,占位符文本也恰当地说明了限制。

components/sender/__tests__/index.test.tsx (1)

265-303: 测试覆盖全面且准确

新增的测试套件很好地覆盖了 maxLengthshowCount 功能的各种场景:

  • 输入长度限制
  • 计数器显示逻辑
  • 实时更新
  • 受控组件行为

测试用例设计合理,能够有效验证功能正确性。

@socket-security
Copy link

socket-security bot commented Nov 11, 2025

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addedfetch-jsonp@​1.3.010010010078100

View full report

- 移除可选链后的非空断言,符合lint规则
- 统一属性访问方式与计数器格式
- 移除未使用导入和冗余volta字段
@kimteayon
Copy link
Collaborator

机器人的回复需要解决下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request javascript Pull requests that update Javascript code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sender 组件支持长度限制 maxLength

2 participants