-
-
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
a497a8d
5a8fb15
65a6681
3c7572d
b9afe24
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -117,6 +117,18 @@ nav: | |
| <td>Number</td> | ||
| <td></td> | ||
| <td>Specifies the defaultValue of an InputNumber</td> | ||
| </tr> | ||
| <tr> | ||
| <td>allowClear</td> | ||
| <td>boolean | { clearIcon?: React.ReactNode; clearValue: T }</td> | ||
| <td>false</td> | ||
| <td>If allow to remove input content with clear icon</td> | ||
| </tr> | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| <tr> | ||
| <td>onClear</td> | ||
| <td>Function</td> | ||
|
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. |
||
| <td></td> | ||
| <td>Called when value of an InputNumber cleared</td> | ||
| </tr> | ||
| <tr> | ||
| <td>onChange</td> | ||
|
|
||
| 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> | ||
| ); | ||
| }; |
| 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,38 @@ 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 iconNode = | ||
| typeof allowClear === 'object' && allowClear.clearIcon ? allowClear.clearIcon : '✖'; | ||
|
|
||
| const onInternalClear = () => { | ||
| const value = getMiniDecimal(typeof allowClear === 'object' && allowClear.clearValue); | ||
|
|
||
| triggerValueUpdate(value, false); | ||
| }; | ||
|
|
||
| clearButton = ( | ||
| <button | ||
| type="button" | ||
| tabIndex={-1} | ||
| onClick={() => { | ||
| onInternalClear(); | ||
| onClear?.(); | ||
| }} | ||
| 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 |
||
| > | ||
| {iconNode} | ||
| </button> | ||
| ); | ||
|
||
| } | ||
|
|
||
| // >>>>>> Render | ||
| return ( | ||
| <div | ||
|
|
@@ -709,6 +745,8 @@ const InputNumber = React.forwardRef<InputNumberRef, InputNumberProps>((props, r | |
| {...restProps} | ||
| /> | ||
|
|
||
| {clearButton} | ||
|
|
||
| {suffix !== undefined && ( | ||
| <div className={clsx(`${prefixCls}-suffix`, classNames?.suffix)} style={styles?.suffix}> | ||
| {suffix} | ||
|
|
||
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
allowClearis documented as having a requiredclearValueproperty when it's an object. However, in the implementation (src/InputNumber.tsx) and the demo (docs/demo/allow-clear.tsx),clearValueis optional. Please update the documentation to reflect thatclearValueis optional to avoid confusion for users of the component.It should be
clearValue?: T.