@@ -8,21 +8,33 @@ import { flat, noop } from '@utils';
88import ContextProvider from '@contexts/context-provider' ;
99import SingleSelect from './views/single-select' ;
1010import MultiSelect from './views/multi-select' ;
11+ import GroupSelect from './views/group-select' ;
1112import Selected from './views/selected' ;
1213import Option from './views/option' ;
1314import { selector } from './views/common' ;
1415
15- import { formatOptionSource } from './utils' ;
16+ import { formatOptionSource , isGroupSelectPicker } from './utils' ;
1617
1718import './index.less' ;
1819
19- const getSelected = ( data , children ) => {
20+ const getSelected = ( data , children , dataSource ) => {
2021 const options = Array . isArray ( data ) ? data : [ data ] ;
2122 if ( ! options . length ) return [ ] ;
22- const selected = Children . map ( children , ( child ) => {
23- const { children : label , value } = child . props ;
24- return options . includes ( value ) ? { label, value } : null ;
25- } ) ;
23+ let selected = [ ] ;
24+ if ( isGroupSelectPicker ( dataSource ) ) {
25+ const groupOptionData = dataSource . map ( ( x ) => x . options ) . flat ( ) ;
26+ groupOptionData . forEach ( ( x ) => {
27+ const { label, value } = x ;
28+ if ( options . includes ( value ) ) {
29+ selected . push ( { label, value } ) ;
30+ }
31+ } ) ;
32+ } else {
33+ selected = Children . map ( children , ( child ) => {
34+ const { children : label , value } = child . props ;
35+ return options . includes ( value ) ? { label, value } : null ;
36+ } ) ;
37+ }
2638 return selected ;
2739} ;
2840
@@ -112,7 +124,7 @@ class Select extends Component {
112124 props . supportLightText ,
113125 props . lightTextColor ,
114126 ) ;
115- const selected = getSelected ( displayValue , source ) ;
127+ const selected = getSelected ( displayValue , source , dataSource ) ;
116128 const emptyValue = multiple ? [ ] : '' ;
117129 const currentValue = displayValue !== null ? displayValue : emptyValue ;
118130 return {
@@ -220,6 +232,9 @@ class Select extends Component {
220232
221233 if ( childs . length ) return childs ;
222234
235+ if ( isGroupSelectPicker ( dataSource ) ) {
236+ return this . getGroupOptions ( ) ;
237+ }
223238 return getOptions (
224239 dataSource ,
225240 labelKey ,
@@ -245,6 +260,26 @@ class Select extends Component {
245260 return ele . getBoundingClientRect ( ) ;
246261 }
247262
263+ getGroupOptions = ( ) => {
264+ const {
265+ dataSource, labelKey, valueKey, isSupportTitle,
266+ } = this . props ;
267+ return dataSource . map ( ( group ) => {
268+ const groupItem = getOptions (
269+ group . options || [ ] ,
270+ labelKey ,
271+ valueKey ,
272+ isSupportTitle ,
273+ ) ;
274+ return (
275+ < div >
276+ < p className = "groupName" > { group . label } </ p >
277+ { groupItem }
278+ </ div >
279+ ) ;
280+ } ) ;
281+ } ;
282+
248283 positionPop = ( ) => {
249284 const {
250285 props : { isAppendToBody, position } ,
@@ -431,7 +466,7 @@ class Select extends Component {
431466 } ;
432467
433468 renderOptions ( ) {
434- const { multiple, confirmTemplate } = this . props ;
469+ const { multiple, confirmTemplate, dataSource } = this . props ;
435470 const { value } = this . state ;
436471
437472 if ( multiple ) {
@@ -449,6 +484,17 @@ class Select extends Component {
449484 ) ;
450485 }
451486
487+ if ( isGroupSelectPicker ( dataSource ) ) {
488+ return (
489+ < GroupSelect
490+ { ...this . props }
491+ value = { value }
492+ dataSource = { this . children }
493+ onChange = { this . onSimpleOptionChange }
494+ />
495+ ) ;
496+ }
497+
452498 return (
453499 < SingleSelect
454500 { ...this . props }
0 commit comments