@@ -6,7 +6,7 @@ import { isEqual } from 'lodash-es';
66import useConfig from '../../hooks/useConfig' ;
77import { useLocaleReceiver } from '../../locale/LocalReceiver' ;
88import usePanelVirtualScroll from '../hooks/usePanelVirtualScroll' ;
9- import { getSelectValueArr } from '../util/helper' ;
9+ import { getKeyMapping , getSelectValueArr } from '../util/helper' ;
1010import Option , { type SelectOptionProps } from './Option' ;
1111import OptionGroup from './OptionGroup' ;
1212
@@ -83,12 +83,12 @@ const PopupContent = React.forwardRef<HTMLDivElement, SelectPopupProps>((props,
8383 scroll : propsScroll ,
8484 hoverIndex,
8585 } = props ;
86+ const { classPrefix } = useConfig ( ) ;
8687
87- // 国际化文本初始化
8888 const [ local , t ] = useLocaleReceiver ( 'select' ) ;
8989 const emptyText = t ( local . empty ) ;
90- const popupContentRef = useRef < HTMLDivElement > ( null ) ;
9190
91+ const popupContentRef = useRef < HTMLDivElement > ( null ) ;
9292 popupContentRef . current = getPopupInstance ( ) ;
9393
9494 const { visibleData, handleRowMounted, isVirtual, panelStyle, cursorStyle } = usePanelVirtualScroll ( {
@@ -98,6 +98,9 @@ const PopupContent = React.forwardRef<HTMLDivElement, SelectPopupProps>((props,
9898 size,
9999 } ) ;
100100
101+ const isObjectType = useMemo ( ( ) => valueType === 'object' , [ valueType ] ) ;
102+ const { valueKey, labelKey } = useMemo ( ( ) => getKeyMapping ( keys ) , [ keys ] ) ;
103+
101104 const optionsExcludedCheckAll = useMemo ( ( ) => {
102105 const uniqueOptions = { } ;
103106 propsOptions ?. forEach ( ( option : SelectOption ) => {
@@ -114,36 +117,18 @@ const PopupContent = React.forwardRef<HTMLDivElement, SelectPopupProps>((props,
114117 return Object . values ( uniqueOptions ) ;
115118 } , [ propsOptions ] ) ;
116119
117- const { classPrefix } = useConfig ( ) ;
118- if ( ! children && ! propsOptions ) {
119- return null ;
120- }
121-
122120 const onSelect : SelectOptionProps [ 'onSelect' ] = ( selectedValue , { label, selected, event, restData } ) => {
123- const isValObj = valueType === 'object' ;
124- let objVal = { } ;
125- if ( isValObj ) {
126- objVal = { ...restData } ;
127- if ( ! keys ?. label ) {
128- Object . assign ( objVal , { label } ) ;
129- }
130- if ( ! keys ?. value ) {
131- Object . assign ( objVal , { value : selectedValue } ) ;
132- }
133- }
134-
135- if ( ! Object . keys ( objVal ) . length ) {
136- Object . assign ( objVal , { [ keys ?. label || 'label' ] : label , [ keys ?. value || 'value' ] : selectedValue } ) ;
137- }
121+ const objVal = {
122+ ...( isObjectType ? { ...restData } : { } ) ,
123+ [ labelKey ] : label ,
124+ [ valueKey ] : selectedValue ,
125+ } ;
138126
139127 if ( multiple ) {
140- // calc multiple select values
141128 const values = getSelectValueArr ( value , selectedValue , selected , valueType , keys , objVal ) ;
142129 onChange ( values , { label, value : selectedValue , e : event , trigger : selected ? 'uncheck' : 'check' } ) ;
143130 } else {
144- // calc single select value
145131 const selectVal = valueType === 'object' ? objVal : selectedValue ;
146-
147132 if ( ! isEqual ( value , selectVal ) ) {
148133 onChange ( selectVal , { label, value : selectVal , e : event , trigger : 'check' } ) ;
149134 }
@@ -159,6 +144,9 @@ const PopupContent = React.forwardRef<HTMLDivElement, SelectPopupProps>((props,
159144 return child ;
160145 } ) ;
161146
147+ const isEmpty =
148+ ( Array . isArray ( childrenWithProps ) && ! childrenWithProps . length ) || ( propsOptions && propsOptions . length === 0 ) ;
149+
162150 // 渲染 options
163151 const renderOptions = ( options : SelectOption [ ] , startIndex = 0 ) => {
164152 if ( options ) {
@@ -225,9 +213,6 @@ const PopupContent = React.forwardRef<HTMLDivElement, SelectPopupProps>((props,
225213 return < ul className = { `${ classPrefix } -select__list` } > { childrenWithProps } </ ul > ;
226214 } ;
227215
228- const isEmpty =
229- ( Array . isArray ( childrenWithProps ) && ! childrenWithProps . length ) || ( propsOptions && propsOptions . length === 0 ) ;
230-
231216 const renderPanel = ( renderedOptions : SelectOption [ ] , extraStyle ?: React . CSSProperties ) => (
232217 < div
233218 ref = { ref }
@@ -245,6 +230,7 @@ const PopupContent = React.forwardRef<HTMLDivElement, SelectPopupProps>((props,
245230 { ! loading && ! isEmpty && renderOptions ( renderedOptions ) }
246231 </ div >
247232 ) ;
233+
248234 if ( isVirtual ) {
249235 return (
250236 < >
@@ -258,6 +244,8 @@ const PopupContent = React.forwardRef<HTMLDivElement, SelectPopupProps>((props,
258244 ) ;
259245 }
260246
247+ if ( ! children && ! propsOptions ) return null ;
248+
261249 return (
262250 < >
263251 { panelTopContent }
0 commit comments