Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit abe2d6c

Browse files
authored
fix(RadioGroup): use regular components instead of Label (#1070)
* fix(RadioGroup): use regular components instead of Label * update CHANGELOG entry * update entry and type
1 parent 27ef22f commit abe2d6c

File tree

4 files changed

+49
-47
lines changed

4 files changed

+49
-47
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1717

1818
## [Unreleased]
1919

20+
### BREAKING CHANGES
21+
- Use regular components instead of `Label` in `RadioGroupItem` @layershifter ([#1070](https://github.com/stardust-ui/react/pull/1070))
22+
2023
<!--------------------------------[ v0.23.1 ]------------------------------- -->
2124
## [v0.23.1](https://github.com/stardust-ui/react/tree/v0.23.1) (2019-03-13)
2225
[Compare changes](https://github.com/stardust-ui/react/compare/v0.23.0...v0.23.1)

packages/react/src/components/RadioGroup/RadioGroupItem.tsx

+35-36
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as React from 'react'
2-
import * as ReactDOM from 'react-dom'
32
import * as PropTypes from 'prop-types'
43
import * as _ from 'lodash'
54

@@ -11,11 +10,11 @@ import {
1110
UIComponentProps,
1211
ChildrenComponentProps,
1312
commonPropTypes,
14-
rtlTextContainer,
1513
} from '../../lib'
16-
import Label from '../Label/Label'
14+
import Box from '../Box/Box'
1715
import { ComponentEventHandler, ReactProps, ShorthandValue } from '../../types'
1816
import Icon from '../Icon/Icon'
17+
import Ref from '../Ref/Ref'
1918
import { Accessibility } from '../../lib/accessibility/types'
2019
import { radioGroupItemBehavior } from '../../lib/accessibility'
2120

@@ -37,7 +36,7 @@ export interface RadioGroupItemProps extends UIComponentProps, ChildrenComponent
3736
checkedChanged?: ComponentEventHandler<RadioGroupItemProps>
3837

3938
/** The label of the radio item. */
40-
label?: React.ReactNode
39+
label?: ShorthandValue
4140

4241
/** Initial checked value. */
4342
defaultChecked?: boolean
@@ -96,7 +95,7 @@ class RadioGroupItem extends AutoControlledComponent<
9695
ReactProps<RadioGroupItemProps>,
9796
RadioGroupItemState
9897
> {
99-
private elementRef: HTMLElement
98+
private elementRef = React.createRef<HTMLElement>()
10099

101100
static create: Function
102101

@@ -112,7 +111,7 @@ class RadioGroupItem extends AutoControlledComponent<
112111
defaultChecked: PropTypes.bool,
113112
disabled: PropTypes.bool,
114113
icon: customPropTypes.itemShorthand,
115-
label: customPropTypes.nodeContent,
114+
label: customPropTypes.itemShorthand,
116115
name: PropTypes.string,
117116
onBlur: PropTypes.func,
118117
onClick: PropTypes.func,
@@ -133,29 +132,39 @@ class RadioGroupItem extends AutoControlledComponent<
133132
componentDidUpdate(prevProps, prevState) {
134133
const checked = this.state.checked
135134
if (checked !== prevState.checked) {
136-
checked && this.props.shouldFocus && this.elementRef.focus()
135+
checked && this.props.shouldFocus && this.elementRef.current.focus()
137136
_.invoke(this.props, 'checkedChanged', undefined, { ...this.props, checked })
138137
}
139138
}
140139

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)
143152
}
144153

145154
renderComponent({ ElementType, classes, unhandledProps, styles, accessibility }) {
146155
const { label, icon } = this.props
147156

148157
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+
>
159168
{Icon.create(icon || '', {
160169
defaultProps: {
161170
circular: true,
@@ -164,25 +173,15 @@ class RadioGroupItem extends AutoControlledComponent<
164173
styles: styles.icon,
165174
},
166175
})}
167-
{rtlTextContainer.createFor({ element: label })}
168-
</Label>
169-
</ElementType>
176+
{Box.create(label, {
177+
defaultProps: {
178+
as: 'span',
179+
},
180+
})}
181+
</ElementType>
182+
</Ref>
170183
)
171184
}
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-
}
186185
}
187186

188187
RadioGroupItem.create = createShorthandFactory({ Component: RadioGroupItem, mappedProp: 'label' })

packages/react/src/themes/teams/components/RadioGroup/radioGroupItemStyles.ts

+5-11
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,14 @@ const radioStyles: ComponentSlotStylesInput<
1010
RadioGroupItemProps & RadioGroupItemState,
1111
RadioGroupItemVariables
1212
> = {
13-
root: ({ props }): ICSSInJSStyle => ({
14-
outline: 0,
15-
...(!props.vertical && {
16-
display: 'inline-block',
17-
}),
18-
}),
19-
20-
label: ({ props: p, variables: v }): ICSSInJSStyle => ({
21-
cursor: 'pointer',
22-
display: 'inline-flex',
13+
root: ({ props: p, variables: v }): ICSSInJSStyle => ({
2314
alignItems: 'baseline',
15+
cursor: 'pointer',
16+
display: p.vertical ? 'flex' : 'inline-flex',
2417
fontWeight: 400,
2518
minHeight: '2.5rem',
26-
backgroundColor: 'transparent',
19+
outline: 0,
20+
padding: v.padding,
2721
...(p.disabled && {
2822
color: v.colorDisabled,
2923
}),

packages/react/src/themes/teams/components/RadioGroup/radioGroupItemVariables.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { pxToRem } from '../../../../lib'
2+
13
export type RadioGroupItemVariables = {
24
color: string
35
colorChecked: string
@@ -8,6 +10,8 @@ export type RadioGroupItemVariables = {
810

911
colorBackground: string
1012
colorBackgroundChecked: string
13+
14+
padding: string
1115
}
1216

1317
export default (siteVars: any): RadioGroupItemVariables => ({
@@ -20,4 +24,6 @@ export default (siteVars: any): RadioGroupItemVariables => ({
2024

2125
colorBackground: siteVars.colors.white,
2226
colorBackgroundChecked: siteVars.colors.primary[500],
27+
28+
padding: `0 ${pxToRem(4)}`,
2329
})

0 commit comments

Comments
 (0)