11import { clsx } from 'clsx' ;
2- import useControlledState from '@rc-component/util/lib/hooks/useControlledState' ;
32import omit from '@rc-component/util/lib/omit' ;
43import React , {
54 forwardRef ,
@@ -11,6 +10,9 @@ import React, {
1110import type { HolderRef } from './BaseInput' ;
1211import BaseInput from './BaseInput' ;
1312import useCount from './hooks/useCount' ;
13+ import useCountDisplay from './hooks/useCountDisplay' ;
14+ import useCountExceed from './hooks/useCountExceed' ;
15+ import useMergedValue from './hooks/useMergedValue' ;
1416import type { ChangeEventInfo , InputProps , InputRef } from './interface' ;
1517import { resolveOnChange } from './utils/commonUtils' ;
1618import {
@@ -58,21 +60,21 @@ const Input = forwardRef<InputRef, InputProps>((props, ref) => {
5860 } ;
5961
6062 // ====================== Value =======================
61- const [ value , setValue ] = useControlledState ( props . defaultValue , props . value ) ;
62- const formatValue =
63- value === undefined || value === null ? '' : String ( value ) ;
64-
65- // =================== Select Range ===================
66- const [ selection , setSelection ] = useState <
67- [ start : number , end : number ] | null
68- > ( null ) ;
63+ const { setValue, formatValue } = useMergedValue (
64+ props . defaultValue ,
65+ props . value ,
66+ ) ;
6967
70- // ====================== Count =======================
7168 const countConfig = useCount ( count , showCount ) ;
72- const mergedMax = countConfig . max || maxLength ;
73- const valueLength = countConfig . strategy ( formatValue ) ;
74-
75- const isOutOfRange = ! ! mergedMax && valueLength > mergedMax ;
69+ const { isOutOfRange, dataCount } = useCountDisplay ( {
70+ countConfig,
71+ value : formatValue ,
72+ maxLength,
73+ } ) ;
74+ const getExceedValue = useCountExceed ( {
75+ countConfig,
76+ getTarget : ( ) => inputRef . current ,
77+ } ) ;
7678
7779 // ======================= Ref ========================
7880 useImperativeHandle ( ref , ( ) => ( {
@@ -108,25 +110,9 @@ const Input = forwardRef<InputRef, InputProps>((props, ref) => {
108110 currentValue : string ,
109111 info : ChangeEventInfo ,
110112 ) => {
111- let cutValue = currentValue ;
112-
113- if (
114- ! compositionRef . current &&
115- countConfig . exceedFormatter &&
116- countConfig . max &&
117- countConfig . strategy ( currentValue ) > countConfig . max
118- ) {
119- cutValue = countConfig . exceedFormatter ( currentValue , {
120- max : countConfig . max ,
121- } ) ;
113+ const cutValue = getExceedValue ( currentValue , compositionRef . current ) ;
122114
123- if ( currentValue !== cutValue ) {
124- setSelection ( [
125- inputRef . current ?. selectionStart || 0 ,
126- inputRef . current ?. selectionEnd || 0 ,
127- ] ) ;
128- }
129- } else if ( info . source === 'compositionEnd' ) {
115+ if ( info . source === 'compositionEnd' && currentValue === cutValue ) {
130116 // Avoid triggering twice
131117 // https://github.com/ant-design/ant-design/issues/46587
132118 return ;
@@ -138,12 +124,6 @@ const Input = forwardRef<InputRef, InputProps>((props, ref) => {
138124 }
139125 } ;
140126
141- useEffect ( ( ) => {
142- if ( selection ) {
143- inputRef . current ?. setSelectionRange ( ...selection ) ;
144- }
145- } , [ selection ] ) ;
146-
147127 const onInternalChange : React . ChangeEventHandler < HTMLInputElement > = ( e ) => {
148128 triggerChange ( e , e . target . value , {
149129 source : 'change' ,
@@ -260,17 +240,7 @@ const Input = forwardRef<InputRef, InputProps>((props, ref) => {
260240
261241 const getSuffix = ( ) => {
262242 // Max length value
263- const hasMaxLength = Number ( mergedMax ) > 0 ;
264-
265243 if ( suffix || countConfig . show ) {
266- const dataCount = countConfig . showFormatter
267- ? countConfig . showFormatter ( {
268- value : formatValue ,
269- count : valueLength ,
270- maxLength : mergedMax ,
271- } )
272- : `${ valueLength } ${ hasMaxLength ? ` / ${ mergedMax } ` : '' } ` ;
273-
274244 return (
275245 < >
276246 { countConfig . show && (
0 commit comments