1
1
import * as React from 'react' ;
2
- import { FormGroup } from '@patternfly/react-core' ;
3
- import { Dropdown , DropdownItem , DropdownToggle } from '@patternfly/react-core/deprecated' ;
2
+ import { Dropdown , DropdownItem , FormGroup , MenuToggle } from '@patternfly/react-core' ;
4
3
import { useTranslation } from '../../../common/hooks/use-translation-wrapper' ;
5
4
import {
6
5
architectureData ,
@@ -12,30 +11,18 @@ import {
12
11
import { useField } from 'formik' ;
13
12
import { useFormikHelpers } from '../../../common/hooks/useFormikHelpers' ;
14
13
15
- const allDropdownItems = {
16
- [ CpuArchitecture . x86 ] : (
17
- < DropdownItem key = { 'x86_64' } id = "x86_64" description = { architectureData [ 'x86_64' ] . description } >
18
- { architectureData [ 'x86_64' ] . label }
19
- </ DropdownItem >
20
- ) ,
21
- [ CpuArchitecture . ARM ] : (
22
- < DropdownItem key = { 'arm64' } id = "arm64" description = { architectureData [ 'arm64' ] . description } >
23
- { architectureData [ 'arm64' ] . label }
24
- </ DropdownItem >
25
- ) ,
26
- [ CpuArchitecture . s390x ] : (
27
- < DropdownItem key = { 's390x' } id = "s390x" description = { architectureData [ 's390x' ] . description } >
28
- { architectureData [ 's390x' ] . label }
29
- </ DropdownItem >
30
- ) ,
31
- } ;
14
+ const cimCpuArchitectures = [
15
+ CpuArchitecture . x86 ,
16
+ CpuArchitecture . ARM ,
17
+ CpuArchitecture . s390x ,
18
+ ] as SupportedCpuArchitecture [ ] ;
32
19
33
20
const CpuArchitectureDropdown = ( {
34
21
isDisabled = false ,
35
22
cpuArchitectures,
36
23
} : {
37
24
isDisabled ?: boolean ;
38
- cpuArchitectures ?: string [ ] ;
25
+ cpuArchitectures ?: SupportedCpuArchitecture [ ] ;
39
26
} ) => {
40
27
const { t } = useTranslation ( ) ;
41
28
const [ { name, value } , , { setValue } ] = useField < SupportedCpuArchitecture | '' > (
@@ -45,44 +32,68 @@ const CpuArchitectureDropdown = ({
45
32
const fieldId = getFieldId ( name , 'input' ) ;
46
33
const { setValue : setUserManagedNetworking } = useFormikHelpers < boolean > ( 'userManagedNetworking' ) ;
47
34
48
- const onCpuArchSelect = ( e ?: React . SyntheticEvent < HTMLDivElement > ) => {
35
+ const onCpuArchSelect = ( e ?: React . MouseEvent < Element , MouseEvent > ) => {
49
36
const val = e ?. currentTarget . id as SupportedCpuArchitecture ;
50
37
setValue ( val ) ;
51
38
setUserManagedNetworking ( val === CpuArchitecture . s390x ) ;
52
39
53
40
setCpuArchOpen ( false ) ;
54
41
} ;
55
42
56
- const dropdownItems = React . useMemo ( ( ) => {
57
- if ( ! cpuArchitectures ) {
58
- return Object . values ( allDropdownItems ) ;
59
- } else {
60
- return Object . entries ( allDropdownItems )
61
- . filter ( ( [ key , _ ] ) => cpuArchitectures . includes ( key ) )
62
- . map ( ( [ _ , val ] ) => val ) ;
63
- }
64
- } , [ cpuArchitectures ] ) ;
43
+ const dropdownItems = React . useMemo (
44
+ ( ) =>
45
+ cimCpuArchitectures . map ( ( arch ) => {
46
+ const isItemEnabled = ! cpuArchitectures || cpuArchitectures . includes ( arch ) ;
47
+ const disabledReason = t (
48
+ 'ai:This option is not available with the selected OpenShift version' ,
49
+ ) ;
50
+ return (
51
+ < DropdownItem
52
+ key = { arch }
53
+ id = { arch }
54
+ isAriaDisabled = { ! isItemEnabled }
55
+ selected = { arch === value }
56
+ description = { architectureData [ arch ] . description }
57
+ tooltipProps = { { content : disabledReason , position : 'top' } }
58
+ onClick = { ( e : React . MouseEvent ) => e . preventDefault ( ) }
59
+ >
60
+ { architectureData [ arch ] . label }
61
+ </ DropdownItem >
62
+ ) ;
63
+ } ) ,
64
+ [ cpuArchitectures , t , value ] ,
65
+ ) ;
65
66
66
67
React . useEffect ( ( ) => {
67
68
if ( ! isDisabled && value !== '' && cpuArchitectures && ! cpuArchitectures ?. includes ( value ) ) {
68
- setValue ( '' ) ;
69
+ const defaultVal = cpuArchitectures ?. [ 0 ] || CpuArchitecture . x86 ;
70
+ setValue ( defaultVal ) ;
69
71
}
70
72
} , [ cpuArchitectures , isDisabled , setValue , value ] ) ;
71
73
72
74
return ! isDisabled ? (
73
- < FormGroup isInline fieldId = { fieldId } label = { t ( 'ai:CPU architecture' ) } isRequired >
75
+ < FormGroup
76
+ isInline
77
+ fieldId = { fieldId }
78
+ label = { t ( 'ai:CPU architecture' ) }
79
+ isRequired
80
+ name = { 'cpuArchiteture' }
81
+ >
74
82
< Dropdown
75
- toggle = {
76
- < DropdownToggle onToggle = { ( ) => setCpuArchOpen ( ! cpuArchOpen ) } className = "pf-u-w-100" >
83
+ toggle = { ( toggleRef ) => (
84
+ < MenuToggle
85
+ ref = { toggleRef }
86
+ onClick = { ( ) => setCpuArchOpen ( ! cpuArchOpen ) }
87
+ className = "pf-u-w-100"
88
+ >
77
89
{ value ? architectureData [ value ] . label : t ( 'ai:CPU architecture' ) }
78
- </ DropdownToggle >
79
- }
80
- name = "cpuArchitecture"
90
+ </ MenuToggle >
91
+ ) }
81
92
isOpen = { cpuArchOpen }
82
93
onSelect = { onCpuArchSelect }
83
- dropdownItems = { dropdownItems }
84
- className = "pf-u-w-100"
85
- / >
94
+ >
95
+ { dropdownItems }
96
+ </ Dropdown >
86
97
</ FormGroup >
87
98
) : (
88
99
< StaticField name = { 'cpuArchitecture' } label = { t ( 'ai:CPU architecture' ) } isRequired >
0 commit comments