@@ -70,9 +70,10 @@ const getWheelDeltaY = (event: WheelEvent) => {
7070 }
7171} ;
7272
73- type SemanticName = 'root' | 'actions' | 'input' | 'action' | 'prefix' | 'suffix' ;
73+ type SemanticName = 'root' | 'actions' | 'input' | 'action' | 'prefix' | 'suffix' | 'clear' ;
7474export interface InputNumberProps < T extends ValueType = ValueType >
75- extends Omit <
75+ extends
76+ Omit <
7677 React . InputHTMLAttributes < HTMLInputElement > ,
7778 | 'value'
7879 | 'defaultValue'
@@ -118,6 +119,13 @@ export interface InputNumberProps<T extends ValueType = ValueType>
118119 controls ?: boolean ;
119120 prefix ?: React . ReactNode ;
120121 suffix ?: React . ReactNode ;
122+ allowClear ?:
123+ | boolean
124+ | {
125+ clearIcon ?: React . ReactNode ;
126+ disabled ?: boolean ;
127+ label ?: string ;
128+ } ;
121129 classNames ?: Partial < Record < SemanticName , string > > ;
122130 styles ?: Partial < Record < SemanticName , React . CSSProperties > > ;
123131
@@ -138,6 +146,7 @@ export interface InputNumberProps<T extends ValueType = ValueType>
138146
139147 onInput ?: ( text : string ) => void ;
140148 onChange ?: ( value : T | null ) => void ;
149+ onClear ?: ( ) => void ;
141150 onPressEnter ?: React . KeyboardEventHandler < HTMLInputElement > ;
142151
143152 onStep ?: (
@@ -175,6 +184,7 @@ const InputNumber = React.forwardRef<InputNumberRef, InputNumberProps>((props, r
175184
176185 prefix,
177186 suffix,
187+ allowClear,
178188 stringMode,
179189
180190 parser,
@@ -183,6 +193,7 @@ const InputNumber = React.forwardRef<InputNumberRef, InputNumberProps>((props, r
183193 decimalSeparator,
184194
185195 onChange,
196+ onClear,
186197 onInput,
187198 onPressEnter,
188199 onStep,
@@ -286,6 +297,7 @@ const InputNumber = React.forwardRef<InputNumberRef, InputNumberProps>((props, r
286297
287298 // >>> Formatter
288299 const inputValueRef = React . useRef < string | number > ( '' ) ;
300+ const inputValueUpdateRef = React . useRef ( 0 ) ;
289301 const mergedFormatter = React . useCallback (
290302 ( number : string , userTyping : boolean ) => {
291303 if ( formatter ) {
@@ -446,6 +458,8 @@ const InputNumber = React.forwardRef<InputNumberRef, InputNumberProps>((props, r
446458
447459 // >>> Collect input value
448460 const collectInputValue = ( inputStr : string ) => {
461+ const inputValueUpdateId = ( inputValueUpdateRef . current += 1 ) ;
462+
449463 recordCursor ( ) ;
450464
451465 // Update inputValue in case input can not parse as number
@@ -468,6 +482,10 @@ const InputNumber = React.forwardRef<InputNumberRef, InputNumberProps>((props, r
468482 // optimize for chinese input experience
469483 // https://github.com/ant-design/ant-design/issues/8196
470484 onNextPromise ( ( ) => {
485+ if ( inputValueUpdateId !== inputValueUpdateRef . current ) {
486+ return ;
487+ }
488+
471489 let nextInputStr = inputStr ;
472490 if ( ! parser ) {
473491 nextInputStr = inputStr . replace ( / 。 / g, '.' ) ;
@@ -634,7 +652,12 @@ const InputNumber = React.forwardRef<InputNumberRef, InputNumberProps>((props, r
634652 } , [ changeOnWheel , focus , onInternalWheel ] ) ;
635653
636654 // >>> Focus & Blur
637- const onBlur = ( ) => {
655+ const onBlur : React . FocusEventHandler < HTMLDivElement > = ( event ) => {
656+ // Moving focus between internal controls does not blur InputNumber.
657+ if ( event . relatedTarget instanceof Node && rootRef . current ?. contains ( event . relatedTarget ) ) {
658+ return ;
659+ }
660+
638661 if ( changeOnBlur ) {
639662 flushInputValue ( false ) ;
640663 }
@@ -707,6 +730,63 @@ const InputNumber = React.forwardRef<InputNumberRef, InputNumberProps>((props, r
707730 </ StepHandler >
708731 ) ;
709732
733+ // >>> Clear
734+ const clearConfig =
735+ allowClear && typeof allowClear === 'object' ? allowClear : { disabled : allowClear !== true } ;
736+ const showClear =
737+ ! disabled && ! readOnly && clearConfig . disabled !== true && String ( inputValue ) . length > 0 ;
738+ const hasSuffix = isReactRenderable ( suffix ) ;
739+ const clearIconCls = `${ prefixCls } -clear-icon` ;
740+
741+ const onClearKeyDown : React . KeyboardEventHandler < HTMLButtonElement > = ( event ) => {
742+ const isStepKey = [ 'Up' , 'ArrowUp' , 'Down' , 'ArrowDown' ] . includes ( event . key ) ;
743+ if ( event . key === 'Enter' || ( keyboard !== false && isStepKey ) ) {
744+ event . stopPropagation ( ) ;
745+ }
746+ } ;
747+
748+ const onClearClick = ( ) => {
749+ userTypingRef . current = false ;
750+ inputValueRef . current = '' ;
751+ inputValueUpdateRef . current += 1 ;
752+
753+ const emptyValue = getMiniDecimal ( null ) ;
754+
755+ // `triggerValueUpdate` only refreshes the display when the decimal value changes.
756+ // Clear raw input such as `-`, or restore the source value in controlled mode.
757+ if ( value !== undefined ) {
758+ setInputValue ( decimalValue , false ) ;
759+ } else if ( decimalValue . isEmpty ( ) ) {
760+ setInputValue ( emptyValue , false ) ;
761+ }
762+
763+ inputRef . current ?. focus ( ) ;
764+ triggerValueUpdate ( emptyValue , false ) ;
765+ onClear ?.( ) ;
766+ } ;
767+
768+ const clearNode = allowClear && (
769+ < button
770+ type = "button"
771+ aria-label = { clearConfig . label ?? 'Clear' }
772+ disabled = { ! showClear }
773+ className = { clsx (
774+ clearIconCls ,
775+ {
776+ [ `${ clearIconCls } -hidden` ] : ! showClear ,
777+ [ `${ clearIconCls } -has-suffix` ] : hasSuffix ,
778+ } ,
779+ classNames ?. clear ,
780+ ) }
781+ style = { styles ?. clear }
782+ onMouseDown = { ( event ) => event . preventDefault ( ) }
783+ onKeyDown = { onClearKeyDown }
784+ onClick = { onClearClick }
785+ >
786+ { clearConfig . clearIcon ?? '✖' }
787+ </ button >
788+ ) ;
789+
710790 // >>>>>> Render
711791 return (
712792 < div
@@ -761,8 +841,9 @@ const InputNumber = React.forwardRef<InputNumberRef, InputNumberProps>((props, r
761841 { ...restProps }
762842 />
763843
764- { isReactRenderable ( suffix ) && (
844+ { ( allowClear || hasSuffix ) && (
765845 < div className = { clsx ( `${ prefixCls } -suffix` , classNames ?. suffix ) } style = { styles ?. suffix } >
846+ { clearNode }
766847 { suffix }
767848 </ div >
768849 ) }
0 commit comments