diff --git a/assets/index.less b/assets/index.less index 08cebed8..becd71c2 100644 --- a/assets/index.less +++ b/assets/index.less @@ -105,6 +105,17 @@ } } + &-clear-icon { + padding: 0; + font-size: 12px; + background: none; + border: none; + + &-hidden { + display: none; + } + } + .handler-disabled() { opacity: 0.3; &:hover { diff --git a/docs/api.md b/docs/api.md index 97406ba4..7114974e 100644 --- a/docs/api.md +++ b/docs/api.md @@ -117,6 +117,18 @@ nav: Number Specifies the defaultValue of an InputNumber + + + allowClear + boolean | { clearIcon?: React.ReactNode; clearValue?: T } + false + If allow to remove input content with clear icon + + + onClear + Function + + Called when value of an InputNumber cleared onChange diff --git a/docs/demo/allow-clear.tsx b/docs/demo/allow-clear.tsx new file mode 100644 index 00000000..970ecc19 --- /dev/null +++ b/docs/demo/allow-clear.tsx @@ -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(100); + + const onChange = (val: number) => { + console.log('onChange:', val, typeof val); + setValue(val); + }; + + return ( +
+ + +
+

with suffix

+ + + +
+

with custom clear value

+ + + +
+

with custom clear icon

+ + clear }} + /> +
+ ); +}; diff --git a/docs/example.md b/docs/example.md index 7b2ec5d6..24b4fb11 100644 --- a/docs/example.md +++ b/docs/example.md @@ -60,3 +60,7 @@ nav: ## spinner + +## allow-clear + + diff --git a/src/InputNumber.tsx b/src/InputNumber.tsx index 7ffc2bdc..7ef5a420 100644 --- a/src/InputNumber.tsx +++ b/src/InputNumber.tsx @@ -96,6 +96,7 @@ export interface InputNumberProps step?: ValueType; tabIndex?: number; controls?: boolean; + allowClear?: boolean | { clearIcon?: React.ReactNode; clearValue?: T }; prefix?: React.ReactNode; suffix?: React.ReactNode; classNames?: Partial>; @@ -119,6 +120,7 @@ export interface InputNumberProps onInput?: (text: string) => void; onChange?: (value: T | null) => void; onPressEnter?: React.KeyboardEventHandler; + onClear?: () => void; onStep?: ( value: T, @@ -155,6 +157,7 @@ const InputNumber = React.forwardRef((props, r prefix, suffix, + allowClear, stringMode, parser, @@ -165,6 +168,7 @@ const InputNumber = React.forwardRef((props, r onChange, onInput, onPressEnter, + onClear, onStep, // Mouse Events @@ -655,6 +659,35 @@ const InputNumber = React.forwardRef((props, r ); + 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 = ( + + ); + } + // >>>>>> Render return (
((props, r {...restProps} /> + {clearButton} + {suffix !== undefined && (
{suffix}