-
-
Notifications
You must be signed in to change notification settings - Fork 185
feat: support allowClear #729
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
hawwei913
wants to merge
5
commits into
react-component:master
Choose a base branch
from
hawwei913:allow-clear
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.
+117
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a497a8d
feat: add allowClear
hawwei913 5a8fb15
fix: allowClear type definition
hawwei913 65a6681
docs: add allowClear documentation & examples
hawwei913 3c7572d
docs: update allowClear type definition
hawwei913 b9afe24
fix: improve clear button logic
hawwei913 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,55 @@ | ||
| /* 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) => { | ||
| console.log('onChange:', val, typeof val); | ||
| setValue(val); | ||
| }; | ||
|
|
||
| return ( | ||
| <div style={{ margin: 10 }}> | ||
| <InputNumber style={{ width: 200 }} value={value} onChange={onChange} allowClear={true} /> | ||
|
|
||
| <hr /> | ||
| <h3>with suffix</h3> | ||
|
|
||
| <InputNumber | ||
| style={{ width: 200 }} | ||
| value={value} | ||
| onChange={onChange} | ||
| prefix="¥" | ||
| suffix="RMB" | ||
| allowClear={true} | ||
| /> | ||
|
|
||
| <hr /> | ||
| <h3>with custom clear value</h3> | ||
|
|
||
| <InputNumber | ||
| style={{ width: 200 }} | ||
| value={value} | ||
| onChange={onChange} | ||
| prefix="¥" | ||
| suffix="RMB" | ||
| allowClear={{ clearValue: 1 }} | ||
| /> | ||
|
|
||
| <hr /> | ||
| <h3>with custom clear icon</h3> | ||
|
|
||
| <InputNumber | ||
| style={{ width: 200 }} | ||
| value={value} | ||
| onChange={onChange} | ||
| prefix="¥" | ||
| suffix="RMB" | ||
| allowClear={{ clearIcon: <span style={{ color: 'red' }}>clear</span> }} | ||
| /> | ||
| </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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -96,6 +96,7 @@ export interface InputNumberProps<T extends ValueType = ValueType> | |
| step?: ValueType; | ||
| tabIndex?: number; | ||
| controls?: boolean; | ||
| allowClear?: boolean | { clearIcon?: React.ReactNode; clearValue?: T }; | ||
| prefix?: React.ReactNode; | ||
| suffix?: React.ReactNode; | ||
| classNames?: Partial<Record<SemanticName, string>>; | ||
|
|
@@ -119,6 +120,7 @@ export interface InputNumberProps<T extends ValueType = ValueType> | |
| onInput?: (text: string) => void; | ||
| onChange?: (value: T | null) => void; | ||
| onPressEnter?: React.KeyboardEventHandler<HTMLInputElement>; | ||
| onClear?: () => void; | ||
|
|
||
| onStep?: ( | ||
| value: T, | ||
|
|
@@ -155,6 +157,7 @@ const InputNumber = React.forwardRef<InputNumberRef, InputNumberProps>((props, r | |
|
|
||
| prefix, | ||
| suffix, | ||
| allowClear, | ||
| stringMode, | ||
|
|
||
| parser, | ||
|
|
@@ -165,6 +168,7 @@ const InputNumber = React.forwardRef<InputNumberRef, InputNumberProps>((props, r | |
| onChange, | ||
| onInput, | ||
| onPressEnter, | ||
| onClear, | ||
| onStep, | ||
|
|
||
| // Mouse Events | ||
|
|
@@ -655,6 +659,35 @@ const InputNumber = React.forwardRef<InputNumberRef, InputNumberProps>((props, r | |
| </StepHandler> | ||
| ); | ||
|
|
||
| let clearButton: React.ReactNode = null; | ||
| if (allowClear) { | ||
| const needClear = !disabled && !readOnly && !decimalValue.isEmpty(); | ||
| const clearIconCls = `${prefixCls}-clear-icon`; | ||
| const clearOptions = typeof allowClear === 'object' ? allowClear : {}; | ||
| const { clearIcon = '✖', clearValue } = clearOptions; | ||
|
|
||
| const onInternalClear = () => { | ||
| const value = getMiniDecimal(clearValue); | ||
| triggerValueUpdate(value, false); | ||
| onClear?.(); | ||
| }; | ||
|
|
||
| clearButton = ( | ||
| <button | ||
| type="button" | ||
| tabIndex={-1} | ||
| onClick={onInternalClear} | ||
| onMouseDown={(e) => e.preventDefault()} | ||
| className={clsx(`${prefixCls}-clear-icon`, { | ||
| [`${clearIconCls}-hidden`]: !needClear, | ||
| [`${clearIconCls}-has-suffix`]: !!suffix, | ||
| })} | ||
|
Comment on lines
+681
to
+684
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. CSS 类名可能未定义。 第 686 行引用了 CSS 类 如果该类名用于在存在 suffix 时调整清除按钮的位置,请在 🤖 Prompt for AI Agents |
||
| > | ||
| {clearIcon} | ||
| </button> | ||
| ); | ||
| } | ||
|
|
||
| // >>>>>> Render | ||
| return ( | ||
| <div | ||
|
|
@@ -709,6 +742,8 @@ const InputNumber = React.forwardRef<InputNumberRef, InputNumberProps>((props, r | |
| {...restProps} | ||
| /> | ||
|
|
||
| {clearButton} | ||
|
|
||
| {suffix !== undefined && ( | ||
| <div className={clsx(`${prefixCls}-suffix`, classNames?.suffix)} style={styles?.suffix}> | ||
| {suffix} | ||
|
|
||
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.
The type for
onClearis documented asFunction, which is a bit vague. It would be more helpful to provide the actual function signature, which is() => void.