@@ -7,78 +7,79 @@ import Placeholder from './Placeholder';
77import type { SharedContentProps } from '.' ;
88import SelectContext from '../../SelectContext' ;
99
10- export default React . forwardRef < HTMLInputElement , SharedContentProps > ( function SingleContent (
11- { inputProps } ,
12- ref ,
13- ) {
14- const { prefixCls, searchValue, activeValue, displayValues, maxLength, mode } =
15- useSelectInputContext ( ) ;
16- const { triggerOpen } = useBaseProps ( ) ;
17- const selectContext = React . useContext ( SelectContext ) ;
10+ const SingleContent = React . forwardRef < HTMLInputElement , SharedContentProps > (
11+ ( { inputProps } , ref ) => {
12+ const { prefixCls, searchValue, activeValue, displayValues, maxLength, mode } =
13+ useSelectInputContext ( ) ;
14+ const { triggerOpen } = useBaseProps ( ) ;
15+ const selectContext = React . useContext ( SelectContext ) ;
1816
19- const [ inputChanged , setInputChanged ] = React . useState ( false ) ;
17+ const [ inputChanged , setInputChanged ] = React . useState ( false ) ;
2018
21- const combobox = mode === 'combobox' ;
22- const displayValue = displayValues [ 0 ] ;
19+ const combobox = mode === 'combobox' ;
20+ const displayValue = displayValues [ 0 ] ;
2321
24- // Implement the same logic as the old SingleSelector
25- const mergedSearchValue = React . useMemo ( ( ) => {
26- if ( combobox && activeValue && ! inputChanged && triggerOpen ) {
27- return activeValue ;
28- }
22+ // Implement the same logic as the old SingleSelector
23+ const mergedSearchValue = React . useMemo ( ( ) => {
24+ if ( combobox && activeValue && ! inputChanged && triggerOpen ) {
25+ return activeValue ;
26+ }
2927
30- return searchValue || '' ;
31- } , [ combobox , activeValue , inputChanged , triggerOpen , searchValue ] ) ;
28+ return searchValue || '' ;
29+ } , [ combobox , activeValue , inputChanged , triggerOpen , searchValue ] ) ;
3230
33- // Extract option props, excluding label and value, and handle className/style merging
34- const optionProps = React . useMemo ( ( ) => {
35- let restProps : React . HTMLAttributes < HTMLDivElement > = {
36- className : `${ prefixCls } -content-value` ,
37- style : {
38- visibility : mergedSearchValue ? 'hidden' : 'visible' ,
39- } ,
40- } ;
31+ // Extract option props, excluding label and value, and handle className/style merging
32+ const optionProps = React . useMemo ( ( ) => {
33+ let restProps : React . HTMLAttributes < HTMLDivElement > = {
34+ className : `${ prefixCls } -content-value` ,
35+ style : {
36+ visibility : mergedSearchValue ? 'hidden' : 'visible' ,
37+ } ,
38+ } ;
4139
42- if ( displayValue && selectContext ?. flattenOptions ) {
43- const option = selectContext . flattenOptions . find ( ( opt ) => opt . value === displayValue . value ) ;
44- if ( option ?. data ) {
45- const { label, value, className, style, ...rest } = option . data ;
40+ if ( displayValue && selectContext ?. flattenOptions ) {
41+ const option = selectContext . flattenOptions . find ( ( opt ) => opt . value === displayValue . value ) ;
42+ if ( option ?. data ) {
43+ const { label, value, className, style, key , ...rest } = option . data ;
4644
47- restProps = {
48- ...restProps ,
49- ...rest ,
50- className : clsx ( restProps . className , className ) ,
51- style : { ...restProps . style , ...style } ,
52- } ;
45+ restProps = {
46+ ...restProps ,
47+ ...rest ,
48+ className : clsx ( restProps . className , className ) ,
49+ style : { ...restProps . style , ...style } ,
50+ } ;
51+ }
5352 }
54- }
5553
56- return restProps ;
57- } , [ displayValue , selectContext ?. flattenOptions , prefixCls , mergedSearchValue ] ) ;
54+ return restProps ;
55+ } , [ displayValue , selectContext ?. flattenOptions , prefixCls , mergedSearchValue ] ) ;
56+
57+ React . useEffect ( ( ) => {
58+ if ( combobox ) {
59+ setInputChanged ( false ) ;
60+ }
61+ } , [ combobox , activeValue ] ) ;
5862
59- React . useEffect ( ( ) => {
60- if ( combobox ) {
61- setInputChanged ( false ) ;
62- }
63- } , [ combobox , activeValue ] ) ;
63+ return (
64+ < div className = { `${ prefixCls } -content` } >
65+ { displayValue ? (
66+ < div { ...optionProps } > { displayValue . label } </ div >
67+ ) : (
68+ < Placeholder hasSearchValue = { ! ! mergedSearchValue } />
69+ ) }
70+ < Input
71+ ref = { ref }
72+ { ...inputProps }
73+ value = { mergedSearchValue }
74+ maxLength = { mode === 'combobox' ? maxLength : undefined }
75+ onChange = { ( e ) => {
76+ setInputChanged ( true ) ;
77+ inputProps . onChange ?.( e ) ;
78+ } }
79+ />
80+ </ div >
81+ ) ;
82+ } ,
83+ ) ;
6484
65- return (
66- < div className = { `${ prefixCls } -content` } >
67- { displayValue ? (
68- < div { ...optionProps } > { displayValue . label } </ div >
69- ) : (
70- < Placeholder hasSearchValue = { ! ! mergedSearchValue } />
71- ) }
72- < Input
73- ref = { ref }
74- { ...inputProps }
75- value = { mergedSearchValue }
76- maxLength = { mode === 'combobox' ? maxLength : undefined }
77- onChange = { ( e ) => {
78- setInputChanged ( true ) ;
79- inputProps . onChange ?.( e ) ;
80- } }
81- />
82- </ div >
83- ) ;
84- } ) ;
85+ export default SingleContent ;
0 commit comments