@@ -41,6 +41,9 @@ import {MultiUser, multiUserValidate} from './MultiUser'
41
41
import { NonCancelableEventHandler } from '@cloudscape-design/components/internal/events'
42
42
import TitleDescriptionHelpPanel from '../../components/help-panel/TitleDescriptionHelpPanel'
43
43
import { useHelpPanel } from '../../components/help-panel/HelpPanel'
44
+ import { useCallback , useMemo } from 'react'
45
+ import { OptionDefinition } from '@cloudscape-design/components/internal/components/option/interfaces'
46
+ import { SelectProps } from '@cloudscape-design/components/select/interfaces'
44
47
45
48
// Constants
46
49
const errorsPath = [ 'app' , 'wizard' , 'errors' , 'cluster' ]
@@ -89,12 +92,20 @@ function clusterValidate() {
89
92
return valid
90
93
}
91
94
92
- function itemToOption ( item : string | null ) {
93
- if ( ! item ) return
94
- return {
95
- label : item ,
96
- value : item ,
95
+ function itemToOption (
96
+ item : string | [ string , string ] | null ,
97
+ ) : SelectProps . Option | null {
98
+ if ( ! item ) return null
99
+ let label , value
100
+
101
+ if ( typeof item == 'string' ) {
102
+ label = item
103
+ value = item
104
+ } else {
105
+ ; [ value , label ] = item
97
106
}
107
+
108
+ return { label, value}
98
109
}
99
110
100
111
function RegionSelect ( ) {
@@ -148,29 +159,24 @@ function RegionSelect() {
148
159
149
160
return (
150
161
< >
151
- < Header
152
- // @ts -expect-error TS(2322) FIXME: Type '"h4"' is not assignable to type 'Variant | u... Remove this comment to see the full error message
153
- variant = "h4"
162
+ < FormField
163
+ label = { t ( 'wizard.cluster.region.label' ) }
154
164
description = { t ( 'wizard.cluster.region.description' ) }
155
- actions = {
156
- < Select
157
- disabled = { editing }
158
- // @ts -expect-error TS(2322) FIXME: Type '{ label: Element; value: string; } | undefin... Remove this comment to see the full error message
159
- selectedOption = { itemToOption (
160
- // @ts -expect-error TS(2345) FIXME: Argument of type 'string[] | undefined' is not ass... Remove this comment to see the full error message
161
- findFirst ( supportedRegions , ( r : string ) => {
162
- return r === region
163
- } ) ,
164
- ) }
165
- onChange = { handleChange }
166
- // @ts -expect-error TS(2322) FIXME: Type '({ label: Element; value: string; } | undefi... Remove this comment to see the full error message
167
- options = { supportedRegions . map ( itemToOption ) }
168
- selectedAriaLabel = { t ( 'wizard.cluster.region.selectedAriaLabel' ) }
169
- />
170
- }
171
165
>
172
- < Trans i18nKey = "wizard.cluster.region.label" />
173
- </ Header >
166
+ < Select
167
+ disabled = { editing }
168
+ selectedOption = { itemToOption (
169
+ // @ts -expect-error TS(2345) FIXME: Argument of type 'string[] | undefined' is not ass... Remove this comment to see the full error message
170
+ findFirst ( supportedRegions , ( r : string ) => {
171
+ return r === region
172
+ } ) ,
173
+ ) }
174
+ onChange = { handleChange }
175
+ // @ts -expect-error TS(2322) FIXME: Type '({ label: Element; value: string; } | undefi... Remove this comment to see the full error message
176
+ options = { supportedRegions . map ( itemToOption ) }
177
+ selectedAriaLabel = { t ( 'wizard.cluster.region.selectedAriaLabel' ) }
178
+ />
179
+ </ FormField >
174
180
</ >
175
181
)
176
182
}
@@ -186,33 +192,33 @@ function OsSelect() {
186
192
const osPath = [ 'app' , 'wizard' , 'config' , 'Image' , 'Os' ]
187
193
const os = useState ( osPath ) || 'alinux2'
188
194
const editing = useState ( [ 'app' , 'wizard' , 'editing' ] )
195
+
196
+ const osesOptions = useMemo ( ( ) => oses . map ( itemToOption ) , [ oses ] )
197
+
198
+ const selectedOs : OptionDefinition | null = useMemo ( ( ) => {
199
+ const selectedOsTuple = findFirst ( oses , ( x : any ) => x [ 0 ] === os ) || null
200
+ return itemToOption ( selectedOsTuple )
201
+ } , [ os , oses ] )
202
+
203
+ const handleChange = useCallback ( ( { detail} : any ) => {
204
+ setState ( osPath , detail . selectedOption . value )
205
+ } , [ ] )
206
+
189
207
return (
190
208
< >
191
- < Header
192
- // @ts -expect-error TS(2322) FIXME: Type '"h4"' is not assignable to type 'Variant | u... Remove this comment to see the full error message
193
- variant = "h4"
209
+ < FormField
210
+ label = { t ( 'wizard.cluster.os.label' ) }
194
211
description = { t ( 'wizard.cluster.os.description' ) }
195
- actions = {
196
- < Select
197
- disabled = { editing }
198
- // @ts -expect-error TS(2322) FIXME: Type '{ label: Element; value: string; } | undefin... Remove this comment to see the full error message
199
- selectedOption = { itemToOption (
200
- // @ts -expect-error TS(2345) FIXME: Argument of type '[string, string] | undefined' is... Remove this comment to see the full error message
201
- findFirst ( oses , ( x : any ) => {
202
- return x [ 0 ] === os
203
- } ) ,
204
- ) }
205
- onChange = { ( { detail} ) =>
206
- setState ( osPath , detail . selectedOption . value )
207
- }
208
- // @ts -expect-error TS(2322) FIXME: Type '({ label: Element; value: string; } | undefi... Remove this comment to see the full error message
209
- options = { oses . map ( itemToOption ) }
210
- selectedAriaLabel = { t ( 'wizard.cluster.os.selectedAriaLabel' ) }
211
- />
212
- }
213
212
>
214
- < Trans i18nKey = "wizard.cluster.os.label" />
215
- </ Header >
213
+ < Select
214
+ disabled = { editing }
215
+ selectedOption = { selectedOs }
216
+ onChange = { handleChange }
217
+ // @ts -expect-error TS(2322) FIXME: Type '({ label: Element; value: string; } | undefi... Remove this comment to see the full error message
218
+ options = { osesOptions }
219
+ selectedAriaLabel = { t ( 'wizard.cluster.os.selectedAriaLabel' ) }
220
+ />
221
+ </ FormField >
216
222
</ >
217
223
)
218
224
}
@@ -311,29 +317,24 @@ function VpcSelect() {
311
317
}
312
318
313
319
return (
314
- < Header
315
- // @ts -expect-error TS(2322) FIXME: Type '"h4"' is not assignable to type 'Variant | u... Remove this comment to see the full error message
316
- variant = "h4"
320
+ < FormField
321
+ errorText = { error }
317
322
description = { t ( 'wizard.cluster.vpc.description' ) }
318
- actions = {
319
- < FormField errorText = { error } >
320
- < Select
321
- disabled = { editing }
322
- // @ts -expect-error TS(2322) FIXME: Type '{ label: JSX.Element; value: any; }' is not ... Remove this comment to see the full error message
323
- selectedOption = { vpcToDisplayOption (
324
- findFirst ( vpcs , x => x . VpcId === vpc ) ,
325
- ) }
326
- onChange = { ( { detail} ) => {
327
- setVpc ( detail . selectedOption . value )
328
- } }
329
- options = { vpcs . map ( vpcToOption ) }
330
- selectedAriaLabel = { t ( 'wizard.cluster.vpc.selectedAriaLabel' ) }
331
- />
332
- </ FormField >
333
- }
323
+ label = { t ( 'wizard.cluster.vpc.label' ) }
334
324
>
335
- < Trans i18nKey = "wizard.cluster.vpc.label" />
336
- </ Header >
325
+ < Select
326
+ disabled = { editing }
327
+ // @ts -expect-error TS(2322) FIXME: Type '{ label: JSX.Element; value: any; }' is not ... Remove this comment to see the full error message
328
+ selectedOption = { vpcToDisplayOption (
329
+ findFirst ( vpcs , x => x . VpcId === vpc ) ,
330
+ ) }
331
+ onChange = { ( { detail} ) => {
332
+ setVpc ( detail . selectedOption . value )
333
+ } }
334
+ options = { vpcs . map ( vpcToOption ) }
335
+ selectedAriaLabel = { t ( 'wizard.cluster.vpc.selectedAriaLabel' ) }
336
+ />
337
+ </ FormField >
337
338
)
338
339
}
339
340
@@ -483,4 +484,4 @@ const ClusterPropertiesHelpPanel = () => {
483
484
)
484
485
}
485
486
486
- export { Cluster , clusterValidate , ClusterPropertiesHelpPanel }
487
+ export { Cluster , clusterValidate , ClusterPropertiesHelpPanel , itemToOption }
0 commit comments