-
-
Notifications
You must be signed in to change notification settings - Fork 185
feat: support allowClear #728
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
Open
BadMooncc
wants to merge
1
commit into
react-component:master
Choose a base branch
from
BadMooncc:develop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+246
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /* eslint no-console:0 */ | ||
| import InputNumber from '@rc-component/input-number'; | ||
| import React from 'react'; | ||
| import '../../assets/index.less'; | ||
|
|
||
| export default () => { | ||
| const [value, setValue] = React.useState<string | number>(100); | ||
|
|
||
| const onChange = (val: number) => { | ||
| setValue(val); | ||
| }; | ||
|
|
||
| return ( | ||
| <div style={{ margin: 10 }}> | ||
| <InputNumber | ||
| allowClear={{ clearValue: 1 }} | ||
| style={{ width: 200 }} | ||
| value={value} | ||
| onChange={onChange} | ||
| prefix="¥" | ||
| suffix="RMB" | ||
| /> | ||
| </div> | ||
| ); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| import * as React from 'react'; | ||
| import { render, fireEvent } from '@testing-library/react'; | ||
| import InputNumber from '../src'; | ||
|
|
||
| describe('InputNumber.AllowClear', () => { | ||
| it('should render clear icon when allowClear is true and value is not empty', () => { | ||
| const { container } = render( | ||
| <InputNumber allowClear value={123} /> | ||
| ); | ||
|
|
||
| const clearIcon = container.querySelector('.rc-input-number-clear-icon'); | ||
| expect(clearIcon).toBeTruthy(); | ||
| expect(clearIcon).not.toHaveClass('rc-input-number-clear-icon-hidden'); | ||
| }); | ||
|
|
||
| it('should not render clear icon when value is empty', () => { | ||
| const { container } = render( | ||
| <InputNumber allowClear value={null} /> | ||
| ); | ||
|
|
||
| const clearIcon = container.querySelector('.rc-input-number-clear-icon'); | ||
| expect(clearIcon).toHaveClass('rc-input-number-clear-icon-hidden'); | ||
| }); | ||
|
|
||
| it('should render clear icon when value is 0', () => { | ||
| const { container } = render( | ||
| <InputNumber allowClear value={0} /> | ||
| ); | ||
| const clearIcon = container.querySelector('.rc-input-number-clear-icon'); | ||
| expect(clearIcon).not.toHaveClass('rc-input-number-clear-icon-hidden'); | ||
| }); | ||
|
|
||
| it('should not render clear icon when disabled', () => { | ||
| const { container } = render( | ||
| <InputNumber allowClear value={123} disabled /> | ||
| ); | ||
|
|
||
| const clearIcon = container.querySelector('.rc-input-number-clear-icon'); | ||
| expect(clearIcon).toHaveClass('rc-input-number-clear-icon-hidden'); | ||
| }); | ||
|
|
||
| it('should not render clear icon when readOnly', () => { | ||
| const { container } = render( | ||
| <InputNumber allowClear value={123} readOnly /> | ||
| ); | ||
|
|
||
| const clearIcon = container.querySelector('.rc-input-number-clear-icon'); | ||
| expect(clearIcon).toHaveClass('rc-input-number-clear-icon-hidden'); | ||
| }); | ||
|
|
||
| it('should clear value to null when allowClear is true (boolean type)', () => { | ||
| const onChange = jest.fn(); | ||
| const { container } = render( | ||
| <InputNumber allowClear value={123} onChange={onChange} /> | ||
| ); | ||
|
|
||
| const clearIcon = container.querySelector('.rc-input-number-clear-icon'); | ||
| fireEvent.click(clearIcon); | ||
|
|
||
| expect(onChange).toHaveBeenCalledTimes(1); | ||
| expect(onChange).toHaveBeenCalledWith(null); | ||
| }); | ||
|
|
||
| it('should clear value to custom value when allowClear is object with clearValue', () => { | ||
| const onChange = jest.fn(); | ||
| const { container } = render( | ||
| <InputNumber | ||
| allowClear={{ clearValue: 0 }} | ||
| value={123} | ||
| onChange={onChange} | ||
| /> | ||
| ); | ||
|
|
||
| const clearIcon = container.querySelector('.rc-input-number-clear-icon'); | ||
| fireEvent.click(clearIcon); | ||
|
|
||
| expect(onChange).toHaveBeenCalledTimes(1); | ||
| expect(onChange).toHaveBeenCalledWith(0); | ||
| }); | ||
|
|
||
| it('should handle string clearValue correctly', () => { | ||
| const onChange = jest.fn(); | ||
| const { container } = render( | ||
| <InputNumber | ||
| allowClear={{ clearValue: 'reset' }} | ||
| value={123} | ||
| onChange={onChange} | ||
| stringMode | ||
| /> | ||
| ); | ||
|
|
||
| const clearIcon = container.querySelector('.rc-input-number-clear-icon'); | ||
| fireEvent.click(clearIcon); | ||
|
|
||
| expect(onChange).toHaveBeenCalledTimes(1); | ||
| expect(onChange).toHaveBeenCalledWith(null); | ||
| }); | ||
|
|
||
| it('should support all clearValue types', () => { | ||
| const onChange1 = jest.fn(); | ||
| const onChange2 = jest.fn(); | ||
| const onChange3 = jest.fn(); | ||
|
|
||
| // Test number clearValue | ||
| const { container: container1, unmount: unmount1 } = render( | ||
| <InputNumber allowClear={{ clearValue: 42 }} value={123} onChange={onChange1} /> | ||
| ); | ||
| fireEvent.click(container1.querySelector('.rc-input-number-clear-icon')); | ||
| expect(onChange1).toHaveBeenCalledWith(42); | ||
| unmount1(); | ||
|
|
||
| // Test zero clearValue | ||
| const { container: container2, unmount: unmount2 } = render( | ||
| <InputNumber allowClear={{ clearValue: 0 }} value={123} onChange={onChange2} /> | ||
| ); | ||
| fireEvent.click(container2.querySelector('.rc-input-number-clear-icon')); | ||
| expect(onChange2).toHaveBeenCalledWith(0); | ||
| unmount2(); | ||
|
|
||
| // Test undefined clearValue | ||
| const { container: container3, unmount: unmount3 } = render( | ||
| <InputNumber allowClear={{ clearValue: undefined }} value={123} onChange={onChange3} /> | ||
| ); | ||
| fireEvent.click(container3.querySelector('.rc-input-number-clear-icon')); | ||
| expect(onChange3).toHaveBeenCalledWith(null); | ||
| unmount3(); | ||
| }); | ||
|
|
||
| it('should trigger onClear callback when clear icon is clicked', () => { | ||
| const onClear = jest.fn(); | ||
| const onChange = jest.fn(); | ||
| const { container } = render( | ||
| <InputNumber | ||
| allowClear | ||
| value={123} | ||
| onClear={onClear} | ||
| onChange={onChange} | ||
| /> | ||
| ); | ||
|
|
||
| const clearIcon = container.querySelector('.rc-input-number-clear-icon'); | ||
| fireEvent.click(clearIcon); | ||
|
|
||
| expect(onClear).toHaveBeenCalledTimes(1); | ||
| expect(onChange).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('should have correct className when suffix is provided', () => { | ||
| const { container } = render( | ||
| <InputNumber allowClear value={123} suffix="$" /> | ||
| ); | ||
|
|
||
| const clearIcon = container.querySelector('.rc-input-number-clear-icon'); | ||
| expect(clearIcon).toHaveClass('rc-input-number-clear-icon-has-suffix'); | ||
| }); | ||
|
|
||
| it('should work with defaultValue', () => { | ||
| const onChange = jest.fn(); | ||
| const { container } = render( | ||
| <InputNumber | ||
| allowClear | ||
| defaultValue={100} | ||
| onChange={onChange} | ||
| /> | ||
| ); | ||
|
|
||
| const clearIcon = container.querySelector('.rc-input-number-clear-icon'); | ||
| expect(clearIcon).not.toHaveClass('rc-input-number-clear-icon-hidden'); | ||
| fireEvent.click(clearIcon); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| expect(onChange).toHaveBeenCalledTimes(1); | ||
| expect(onChange).toHaveBeenCalledWith(null); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
建议在此处增加一个测试用例,用于验证当
value为0时清除图标的可见性。这是一个重要的边界情况,因为0是一个假值(falsy value),可能会导致可见性判断出错。你可以添加如下测试: