1
1
import * as React from 'react'
2
- import * as ReactDOM from 'react-dom'
3
2
import * as PropTypes from 'prop-types'
4
3
import * as _ from 'lodash'
5
4
@@ -11,11 +10,11 @@ import {
11
10
UIComponentProps ,
12
11
ChildrenComponentProps ,
13
12
commonPropTypes ,
14
- rtlTextContainer ,
15
13
} from '../../lib'
16
- import Label from '../Label/Label '
14
+ import Box from '../Box/Box '
17
15
import { ComponentEventHandler , ReactProps , ShorthandValue } from '../../types'
18
16
import Icon from '../Icon/Icon'
17
+ import Ref from '../Ref/Ref'
19
18
import { Accessibility } from '../../lib/accessibility/types'
20
19
import { radioGroupItemBehavior } from '../../lib/accessibility'
21
20
@@ -37,7 +36,7 @@ export interface RadioGroupItemProps extends UIComponentProps, ChildrenComponent
37
36
checkedChanged ?: ComponentEventHandler < RadioGroupItemProps >
38
37
39
38
/** The label of the radio item. */
40
- label ?: React . ReactNode
39
+ label ?: ShorthandValue
41
40
42
41
/** Initial checked value. */
43
42
defaultChecked ?: boolean
@@ -96,7 +95,7 @@ class RadioGroupItem extends AutoControlledComponent<
96
95
ReactProps < RadioGroupItemProps > ,
97
96
RadioGroupItemState
98
97
> {
99
- private elementRef : HTMLElement
98
+ private elementRef = React . createRef < HTMLElement > ( )
100
99
101
100
static create : Function
102
101
@@ -112,7 +111,7 @@ class RadioGroupItem extends AutoControlledComponent<
112
111
defaultChecked : PropTypes . bool ,
113
112
disabled : PropTypes . bool ,
114
113
icon : customPropTypes . itemShorthand ,
115
- label : customPropTypes . nodeContent ,
114
+ label : customPropTypes . itemShorthand ,
116
115
name : PropTypes . string ,
117
116
onBlur : PropTypes . func ,
118
117
onClick : PropTypes . func ,
@@ -133,29 +132,39 @@ class RadioGroupItem extends AutoControlledComponent<
133
132
componentDidUpdate ( prevProps , prevState ) {
134
133
const checked = this . state . checked
135
134
if ( checked !== prevState . checked ) {
136
- checked && this . props . shouldFocus && this . elementRef . focus ( )
135
+ checked && this . props . shouldFocus && this . elementRef . current . focus ( )
137
136
_ . invoke ( this . props , 'checkedChanged' , undefined , { ...this . props , checked } )
138
137
}
139
138
}
140
139
141
- componentDidMount ( ) {
142
- this . elementRef = ReactDOM . findDOMNode ( this ) as HTMLElement
140
+ handleFocus = ( e : React . SyntheticEvent ) => {
141
+ this . setState ( { isFromKeyboard : isFromKeyboard ( ) } )
142
+ _ . invoke ( this . props , 'onFocus' , e , this . props )
143
+ }
144
+
145
+ handleBlur = ( e : React . SyntheticEvent ) => {
146
+ this . setState ( { isFromKeyboard : false } )
147
+ _ . invoke ( this . props , 'onBlur' , e , this . props )
148
+ }
149
+
150
+ handleClick = e => {
151
+ _ . invoke ( this . props , 'onClick' , e , this . props )
143
152
}
144
153
145
154
renderComponent ( { ElementType, classes, unhandledProps, styles, accessibility } ) {
146
155
const { label, icon } = this . props
147
156
148
157
return (
149
- < ElementType
150
- { ... accessibility . attributes . root }
151
- { ...accessibility . keyHandlers . root }
152
- { ...unhandledProps }
153
- onFocus = { this . handleFocus }
154
- onBlur = { this . handleBlur }
155
- onClick = { this . handleClick }
156
- className = { classes . root }
157
- >
158
- < Label styles = { styles . label } >
158
+ < Ref innerRef = { this . elementRef } >
159
+ < ElementType
160
+ { ...accessibility . attributes . root }
161
+ { ...accessibility . keyHandlers . root }
162
+ { ... unhandledProps }
163
+ onFocus = { this . handleFocus }
164
+ onBlur = { this . handleBlur }
165
+ onClick = { this . handleClick }
166
+ className = { classes . root }
167
+ >
159
168
{ Icon . create ( icon || '' , {
160
169
defaultProps : {
161
170
circular : true ,
@@ -164,25 +173,15 @@ class RadioGroupItem extends AutoControlledComponent<
164
173
styles : styles . icon ,
165
174
} ,
166
175
} ) }
167
- { rtlTextContainer . createFor ( { element : label } ) }
168
- </ Label >
169
- </ ElementType >
176
+ { Box . create ( label , {
177
+ defaultProps : {
178
+ as : 'span' ,
179
+ } ,
180
+ } ) }
181
+ </ ElementType >
182
+ </ Ref >
170
183
)
171
184
}
172
-
173
- private handleFocus = ( e : React . SyntheticEvent ) => {
174
- this . setState ( { isFromKeyboard : isFromKeyboard ( ) } )
175
- _ . invoke ( this . props , 'onFocus' , e , this . props )
176
- }
177
-
178
- private handleBlur = ( e : React . SyntheticEvent ) => {
179
- this . setState ( { isFromKeyboard : false } )
180
- _ . invoke ( this . props , 'onBlur' , e , this . props )
181
- }
182
-
183
- private handleClick = e => {
184
- _ . invoke ( this . props , 'onClick' , e , this . props )
185
- }
186
185
}
187
186
188
187
RadioGroupItem . create = createShorthandFactory ( { Component : RadioGroupItem , mappedProp : 'label' } )
0 commit comments