@@ -12,13 +12,62 @@ import SelectableItem from '../../internal/components/selectable-item';
1212import Tooltip from '../../internal/components/tooltip' ;
1313import useHiddenDescription from '../../internal/hooks/use-hidden-description' ;
1414import { MultiselectProps } from '../../multiselect/interfaces' ;
15- import { ItemProps } from './item' ;
15+ import { ItemParentProps , ItemProps } from './item' ;
1616
1717import styles from './styles.css.js' ;
1818interface MultiselectItemProps extends ItemProps < MultiselectProps . MultiselectOptionItemRenderer > {
1919 indeterminate ?: boolean ;
20+ parentProps ?: MultiselectItemParentProps ;
2021}
2122
23+ export interface MultiselectItemParentProps extends ItemParentProps {
24+ indeterminate : boolean ;
25+ highlighted : boolean ;
26+ selected : boolean ;
27+ }
28+ const toMultiselectOptionGroupItem = (
29+ props : MultiselectItemParentProps
30+ ) : MultiselectProps . MultiselectOptionGroupItem => {
31+ return {
32+ type : 'group' ,
33+ index : props . virtualIndex ?? props . index ,
34+ option : props . option . option as OptionGroup ,
35+ indeterminate : props . indeterminate ?? false ,
36+ selected : props . selected ,
37+ highlighted : props . highlighted ,
38+ disabled : props . disabled ,
39+ } ;
40+ } ;
41+ const toMultiselectOptionItem = ( props : {
42+ index : number ;
43+ virtualIndex ?: number ;
44+ option : DropdownOption ;
45+ disabled : boolean ;
46+ selected : boolean ;
47+ highlighted : boolean ;
48+ parentProps ?: MultiselectItemParentProps ;
49+ } ) : MultiselectProps . MultiselectOptionItem => {
50+ return {
51+ type : 'item' ,
52+ index : props . virtualIndex ?? props . index ,
53+ option : props . option . option as OptionDefinition ,
54+ selected : props . selected ,
55+ highlighted : props . highlighted ,
56+ disabled : props . disabled ,
57+ parent : props . parentProps
58+ ? toMultiselectOptionGroupItem ( {
59+ index : props . parentProps ?. index ,
60+ virtualIndex : props . parentProps ?. virtualIndex ,
61+ option : props . parentProps ?. option ,
62+ disabled : props . disabled ,
63+ highlighted : props . parentProps ?. highlighted ?? false ,
64+ indeterminate : props . parentProps ?. indeterminate ?? false ,
65+ selected : props . parentProps ?. selected ?? false ,
66+ } )
67+ : null ,
68+ } ;
69+ } ;
70+
2271const MultiSelectItem = (
2372 {
2473 index,
@@ -40,6 +89,7 @@ const MultiSelectItem = (
4089 withScrollbar,
4190 sticky,
4291 renderOption,
92+ parentProps,
4393 ...restProps
4494 } : MultiselectItemProps ,
4595 ref : React . Ref < HTMLDivElement >
@@ -63,50 +113,45 @@ const MultiSelectItem = (
63113
64114 const [ canShowTooltip , setCanShowTooltip ] = useState ( true ) ;
65115 useEffect ( ( ) => setCanShowTooltip ( true ) , [ highlighted ] ) ;
66- const globalIndex = virtualIndex ?? index ;
116+
117+ const getMultiselectItemProps = ( option : DropdownOption ) : MultiselectProps . MultiselectItem => {
118+ if ( option . type === 'parent' ) {
119+ return toMultiselectOptionGroupItem ( {
120+ option : option ,
121+ index : index ,
122+ virtualIndex : virtualIndex ,
123+ disabled : ! ! disabled ,
124+ highlighted : ! ! highlighted ,
125+ selected : ! ! selected ,
126+ indeterminate : indeterminate ?? false ,
127+ } ) ;
128+ } else if ( option . type === 'select-all' ) {
129+ return {
130+ type : 'select-all' ,
131+ option : option . option as OptionDefinition ,
132+ indeterminate : indeterminate ?? false ,
133+ selected : ! ! selected ,
134+ highlighted : ! ! highlighted ,
135+ } ;
136+ } else {
137+ return toMultiselectOptionItem ( {
138+ option : option ,
139+ index : index ,
140+ virtualIndex : virtualIndex ,
141+ disabled : ! ! disabled ,
142+ highlighted : ! ! highlighted ,
143+ selected : ! ! selected ,
144+ parentProps : parentProps ,
145+ } ) ;
146+ }
147+ } ;
67148
68149 const renderOptionWrapper = ( option : DropdownOption ) => {
69150 if ( ! renderOption ) {
70151 return null ;
71152 }
72153
73- let item : MultiselectProps . MultiselectItem ;
74-
75- switch ( option . type ) {
76- case 'select-all' :
77- item = {
78- type : 'select-all' ,
79- option : option . option as OptionDefinition ,
80- indeterminate : indeterminate ?? false ,
81- selected : ! ! selected ,
82- highlighted : ! ! highlighted ,
83- } ;
84- break ;
85- case 'parent' :
86- item = {
87- index : globalIndex ,
88- type : 'group' ,
89- option : option . option as OptionGroup ,
90- indeterminate : indeterminate ?? false ,
91- selected : ! ! selected ,
92- highlighted : ! ! highlighted ,
93- disabled : ! ! disabled ,
94- } ;
95- break ;
96- case 'child' :
97- default :
98- item = {
99- index : globalIndex ,
100- type : 'item' ,
101- option : option . option as OptionDefinition ,
102- selected : ! ! selected ,
103- highlighted : ! ! highlighted ,
104- disabled : ! ! disabled ,
105- } ;
106- break ;
107- }
108-
109- return renderOption ( { item, filterText : filteringValue } ) ;
154+ return renderOption ( { item : getMultiselectItemProps ( option ) , filterText : filteringValue } ) ;
110155 } ;
111156
112157 const renderResult = renderOptionWrapper ( option ) ;
0 commit comments