15
15
*/
16
16
17
17
import React from 'react'
18
- import { Select , SelectOption } from '@patternfly/react-core'
18
+ import { Divider , Select , SelectGroup , SelectOption } from '@patternfly/react-core'
19
19
20
- import { pexecInCurrentTab } from '@kui-shell/core'
20
+ import { flatten , pexecInCurrentTab } from '@kui-shell/core'
21
21
22
- import Props , { SelectOptions } from '../model'
22
+ import Props , { SelectOptions , isGrouped , isDivider } from '../model'
23
23
24
24
interface State {
25
25
isOpen : boolean
@@ -60,10 +60,53 @@ export default class PatternFlySelect extends React.PureComponent<Props, State>
60
60
}
61
61
}
62
62
63
- private readonly _onClicks = this . props . options . map ( option => this . onClick . bind ( this , option ) )
63
+ private readonly _onClicks = isGrouped ( this . props )
64
+ ? flatten (
65
+ this . props . groups . map ( group =>
66
+ isDivider ( group ) ? [ ] : group . options . map ( option => this . onClick . bind ( this , option ) )
67
+ )
68
+ )
69
+ : this . props . options . map ( option => this . onClick . bind ( this , option ) )
70
+
64
71
private readonly _onSelect = this . onSelect . bind ( this )
65
72
private readonly _onToggle = this . onToggle . bind ( this )
66
73
74
+ /** @return UI for the given option */
75
+ private option ( option : SelectOptions , index : number ) {
76
+ return (
77
+ < SelectOption
78
+ className = "kui--select-option"
79
+ data-value = { option . label }
80
+ key = { index }
81
+ value = { option . label }
82
+ isSelected = { option . isSelected }
83
+ description = { option . description }
84
+ onClick = { this . _onClicks [ index ] }
85
+ isDisabled = { option . isDisabled }
86
+ />
87
+ )
88
+ }
89
+
90
+ /** @return UI for all of the options */
91
+ private options ( ) {
92
+ if ( isGrouped ( this . props ) ) {
93
+ let runningIdx = 0
94
+ const groups = this . props . groups . map ( ( group , idx1 ) =>
95
+ isDivider ( group ) ? (
96
+ < Divider key = { `divider-${ idx1 } ` } />
97
+ ) : (
98
+ < SelectGroup label = { group . label } key = { `group-${ idx1 } ` } >
99
+ { group . options . map ( option => this . option ( option , runningIdx ++ ) ) }
100
+ </ SelectGroup >
101
+ )
102
+ )
103
+
104
+ return groups
105
+ } else {
106
+ return this . props . options . map ( ( option , idx ) => this . option ( option , idx ) )
107
+ }
108
+ }
109
+
67
110
public render ( ) {
68
111
return (
69
112
< Select
@@ -72,23 +115,13 @@ export default class PatternFlySelect extends React.PureComponent<Props, State>
72
115
variant = { this . props . variant }
73
116
typeAheadAriaLabel = "Select from the Options"
74
117
selections = { this . state . selected }
118
+ isGrouped = { isGrouped ( this . props ) }
75
119
maxHeight = { this . props . maxHeight }
76
120
onToggle = { this . _onToggle }
77
121
onSelect = { this . _onSelect }
78
122
isDisabled = { this . props . isDisabled }
79
123
>
80
- { this . props . options . map ( ( option , index ) => (
81
- < SelectOption
82
- className = "kui--select-option"
83
- data-value = { option . label }
84
- key = { index }
85
- value = { option . label }
86
- isSelected = { option . isSelected }
87
- description = { option . description }
88
- onClick = { this . _onClicks [ index ] }
89
- isDisabled = { option . isDisabled }
90
- />
91
- ) ) }
124
+ { this . options ( ) }
92
125
</ Select >
93
126
)
94
127
}
0 commit comments