1
- import React from 'react' ;
1
+ import React , { useCallback } from 'react' ;
2
2
3
+ import { useToggle } from '../../../../utilities/use-toggle' ;
3
4
import { classNames } from '../../../../utilities/css' ;
4
5
import { IconProps } from '../../../../types' ;
5
6
import { ThumbnailProps } from '../../../Thumbnail' ;
@@ -24,106 +25,92 @@ export interface OptionProps {
24
25
onClick ( section : number , option : number ) : void ;
25
26
}
26
27
27
- interface State {
28
- focused : boolean ;
29
- }
30
-
31
- export class Option extends React . Component < OptionProps , State > {
32
- state : State = {
33
- focused : false ,
34
- } ;
35
-
36
- render ( ) {
37
- const {
38
- label,
39
- value,
40
- id,
41
- select,
42
- active,
43
- allowMultiple,
44
- disabled,
45
- role,
46
- media,
47
- } = this . props ;
48
- const { focused} = this . state ;
49
-
50
- const mediaMarkup = media ? (
51
- < div className = { styles . Media } > { media } </ div >
52
- ) : null ;
53
-
54
- const singleSelectClassName = classNames (
55
- styles . SingleSelectOption ,
56
- focused && styles . focused ,
57
- disabled && styles . disabled ,
58
- select && styles . select ,
59
- active && styles . active ,
60
- ) ;
61
-
62
- const multiSelectClassName = classNames (
63
- styles . Label ,
64
- active && styles . active ,
65
- ) ;
66
-
67
- const checkBoxRole = role === 'option' ? 'presentation' : undefined ;
68
-
69
- const optionMarkup = allowMultiple ? (
70
- < label htmlFor = { id } className = { multiSelectClassName } >
71
- < div className = { styles . Checkbox } >
72
- < Checkbox
73
- id = { id }
74
- value = { value }
75
- checked = { select }
76
- active = { active }
77
- disabled = { disabled }
78
- onChange = { this . handleClick }
79
- role = { checkBoxRole }
80
- />
81
- </ div >
82
- { mediaMarkup }
83
- { label }
84
- </ label >
85
- ) : (
86
- < button
87
- id = { id }
88
- type = "button"
89
- className = { singleSelectClassName }
90
- onClick = { this . handleClick }
91
- disabled = { disabled }
92
- onFocus = { this . toggleFocus }
93
- onBlur = { this . toggleFocus }
94
- >
95
- { mediaMarkup }
96
- { label }
97
- </ button >
98
- ) ;
99
-
100
- const scrollMarkup = active ? < Scrollable . ScrollTo /> : null ;
101
-
102
- return (
103
- < li
104
- key = { id }
105
- className = { styles . Option }
106
- tabIndex = { - 1 }
107
- aria-selected = { active }
108
- role = { role }
109
- >
110
- { scrollMarkup }
111
- { optionMarkup }
112
- </ li >
113
- ) ;
114
- }
115
-
116
- private handleClick = ( ) => {
117
- const { onClick, section, index, disabled} = this . props ;
118
-
28
+ export function Option ( {
29
+ label,
30
+ value,
31
+ id,
32
+ select,
33
+ active,
34
+ allowMultiple,
35
+ disabled,
36
+ role,
37
+ media,
38
+ onClick,
39
+ section,
40
+ index,
41
+ } : OptionProps ) {
42
+ const { value : focused , toggle : toggleFocused } = useToggle ( false ) ;
43
+
44
+ const handleClick = useCallback ( ( ) => {
119
45
if ( disabled ) {
120
46
return ;
121
47
}
122
48
123
49
onClick ( section , index ) ;
124
- } ;
125
-
126
- private toggleFocus = ( ) => {
127
- this . setState ( ( prevState ) => ( { focused : ! prevState . focused } ) ) ;
128
- } ;
50
+ } , [ disabled , index , onClick , section ] ) ;
51
+
52
+ const mediaMarkup = media ? (
53
+ < div className = { styles . Media } > { media } </ div >
54
+ ) : null ;
55
+
56
+ const singleSelectClassName = classNames (
57
+ styles . SingleSelectOption ,
58
+ focused && styles . focused ,
59
+ disabled && styles . disabled ,
60
+ select && styles . select ,
61
+ active && styles . active ,
62
+ ) ;
63
+
64
+ const multiSelectClassName = classNames (
65
+ styles . Label ,
66
+ active && styles . active ,
67
+ ) ;
68
+
69
+ const checkBoxRole = role === 'option' ? 'presentation' : undefined ;
70
+
71
+ const optionMarkup = allowMultiple ? (
72
+ < label htmlFor = { id } className = { multiSelectClassName } >
73
+ < div className = { styles . Checkbox } >
74
+ < Checkbox
75
+ id = { id }
76
+ value = { value }
77
+ checked = { select }
78
+ active = { active }
79
+ disabled = { disabled }
80
+ onChange = { handleClick }
81
+ role = { checkBoxRole }
82
+ />
83
+ </ div >
84
+ { mediaMarkup }
85
+ { label }
86
+ </ label >
87
+ ) : (
88
+ < button
89
+ id = { id }
90
+ type = "button"
91
+ className = { singleSelectClassName }
92
+ onClick = { handleClick }
93
+ disabled = { disabled }
94
+ onFocus = { toggleFocused }
95
+ onBlur = { toggleFocused }
96
+ >
97
+ { mediaMarkup }
98
+ { label }
99
+ </ button >
100
+ ) ;
101
+
102
+ const scrollMarkup = active ? < Scrollable . ScrollTo /> : null ;
103
+
104
+ return (
105
+ < li
106
+ key = { id }
107
+ className = { styles . Option }
108
+ tabIndex = { - 1 }
109
+ aria-selected = { active }
110
+ role = { role }
111
+ >
112
+ { scrollMarkup }
113
+ { optionMarkup }
114
+ </ li >
115
+ ) ;
129
116
}
0 commit comments