Skip to content

Commit a497a8d

Browse files
committed
feat: add allowClear
1 parent fdde44f commit a497a8d

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

assets/index.less

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@
105105
}
106106
}
107107

108+
&-clear-icon {
109+
padding: 0;
110+
font-size: 12px;
111+
background: none;
112+
border: none;
113+
114+
&-hidden {
115+
display: none;
116+
}
117+
}
118+
108119
.handler-disabled() {
109120
opacity: 0.3;
110121
&:hover {

src/InputNumber.tsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export interface InputNumberProps<T extends ValueType = ValueType>
9696
step?: ValueType;
9797
tabIndex?: number;
9898
controls?: boolean;
99+
allowClear?: boolean | { clearIcon?: React.ReactNode; clearValue: T };
99100
prefix?: React.ReactNode;
100101
suffix?: React.ReactNode;
101102
classNames?: Partial<Record<SemanticName, string>>;
@@ -119,6 +120,7 @@ export interface InputNumberProps<T extends ValueType = ValueType>
119120
onInput?: (text: string) => void;
120121
onChange?: (value: T | null) => void;
121122
onPressEnter?: React.KeyboardEventHandler<HTMLInputElement>;
123+
onClear?: () => void;
122124

123125
onStep?: (
124126
value: T,
@@ -155,6 +157,7 @@ const InputNumber = React.forwardRef<InputNumberRef, InputNumberProps>((props, r
155157

156158
prefix,
157159
suffix,
160+
allowClear,
158161
stringMode,
159162

160163
parser,
@@ -165,6 +168,7 @@ const InputNumber = React.forwardRef<InputNumberRef, InputNumberProps>((props, r
165168
onChange,
166169
onInput,
167170
onPressEnter,
171+
onClear,
168172
onStep,
169173

170174
// Mouse Events
@@ -655,6 +659,38 @@ const InputNumber = React.forwardRef<InputNumberRef, InputNumberProps>((props, r
655659
</StepHandler>
656660
);
657661

662+
let clearButton: React.ReactNode = null;
663+
if (allowClear) {
664+
const needClear = !disabled && !readOnly && !decimalValue.isEmpty();
665+
const clearIconCls = `${prefixCls}-clear-icon`;
666+
const iconNode =
667+
typeof allowClear === 'object' && allowClear.clearIcon ? allowClear.clearIcon : '✖';
668+
669+
const onInternalClear = () => {
670+
const value = getMiniDecimal(typeof allowClear === 'object' && allowClear.clearValue);
671+
672+
triggerValueUpdate(value, false);
673+
};
674+
675+
clearButton = (
676+
<button
677+
type="button"
678+
tabIndex={-1}
679+
onClick={() => {
680+
onInternalClear();
681+
onClear?.();
682+
}}
683+
onMouseDown={(e) => e.preventDefault()}
684+
className={clsx(`${prefixCls}-clear-icon`, {
685+
[`${clearIconCls}-hidden`]: !needClear,
686+
[`${clearIconCls}-has-suffix`]: !!suffix,
687+
})}
688+
>
689+
{iconNode}
690+
</button>
691+
);
692+
}
693+
658694
// >>>>>> Render
659695
return (
660696
<div
@@ -709,6 +745,8 @@ const InputNumber = React.forwardRef<InputNumberRef, InputNumberProps>((props, r
709745
{...restProps}
710746
/>
711747

748+
{clearButton}
749+
712750
{suffix !== undefined && (
713751
<div className={clsx(`${prefixCls}-suffix`, classNames?.suffix)} style={styles?.suffix}>
714752
{suffix}

0 commit comments

Comments
 (0)