Skip to content

Commit 3a6bb2f

Browse files
authored
Fix/ radioButton alignments (#1999)
* Fix/ radioButton alignments * deprecate contentOnRight * conver isContentOnLeft to getter * remove alignment
1 parent e81128a commit 3a6bb2f

File tree

3 files changed

+49
-18
lines changed

3 files changed

+49
-18
lines changed

demo/src/screens/componentScreens/RadioButtonScreen.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, {Component} from 'react';
2-
import {TouchableOpacity, ScrollView} from 'react-native';
2+
import {TouchableOpacity, ScrollView, StyleSheet} from 'react-native';
33
import {Assets, Colors, View, Text, RadioButton, RadioGroup} from 'react-native-ui-lib'; //eslint-disable-line
44
const starIcon = require('../../assets/icons/star.png');
55

@@ -11,10 +11,10 @@ const COLORS = {
1111

1212
export default class RadioButtonScreen extends Component {
1313
static colors = COLORS;
14-
14+
1515
constructor(props) {
1616
super(props);
17-
17+
1818
this.state = {
1919
color: undefined,
2020
messageType: undefined,
@@ -89,10 +89,10 @@ export default class RadioButtonScreen extends Component {
8989
<Text marginB-20 text60 $textDefault>
9090
Alignments
9191
</Text>
92-
{this.renderRadioButtonWithImageAndText('right-icon', 'Text on right')}
93-
{this.renderRadioButtonWithImageAndText('left-icon', 'Text on left', true)}
94-
{this.renderRadioButton('left-content', 'Content on left', true)}
95-
{this.renderRadioButton('right-content', 'Content on right', {contentOnRight: true})}
92+
{this.renderRadioButtonWithImageAndText('left-icon', 'Text on right')}
93+
{this.renderRadioButtonWithImageAndText('right-icon', 'Text on left', true)}
94+
{this.renderRadioButton('right-content', 'Content on right', true)}
95+
{this.renderRadioButton('left-content', 'Content on left', {contentOnLeft: true})}
9696
<Text marginT-10>You chose: {this.state.textSide}</Text>
9797
</RadioGroup>
9898

@@ -116,6 +116,8 @@ export default class RadioButtonScreen extends Component {
116116
onPress={() => this.setState({individualValue2: !this.state.individualValue2})}
117117
label="Individual Radio Button (with style)"
118118
labelStyle={{fontSize: 16, fontWeight: 'bold'}}
119+
contentOnLeft
120+
containerStyle={styles.contentOnLeft}
119121
/>
120122
</View>
121123
<TouchableOpacity
@@ -124,7 +126,12 @@ export default class RadioButtonScreen extends Component {
124126
accessible={false}
125127
>
126128
<View row centerV>
127-
<RadioButton selected={this.state.individualValue} label="Individual Radio Button (wrapped)"/>
129+
<RadioButton
130+
contentOnLeft
131+
containerStyle={styles.contentOnLeft}
132+
selected={this.state.individualValue}
133+
label="Individual Radio Button (wrapped)"
134+
/>
128135
</View>
129136
</TouchableOpacity>
130137
<View row centerV marginT-10>
@@ -133,6 +140,8 @@ export default class RadioButtonScreen extends Component {
133140
selected={this.state.disabledValue}
134141
onPress={() => this.setState({disabledValue: !this.state.disabledValue})}
135142
label="Disabled Radio Button"
143+
contentOnLeft
144+
containerStyle={styles.contentOnLeft}
136145
/>
137146
</View>
138147
<View row centerV marginT-10>
@@ -141,10 +150,19 @@ export default class RadioButtonScreen extends Component {
141150
selected={this.state.disabledSelectedValue}
142151
onPress={() => this.setState({disabledSelectedValue: !this.state.disabledSelectedValue})}
143152
label="Disabled Selected Radio Button"
153+
contentOnLeft
154+
containerStyle={styles.contentOnLeft}
144155
/>
145156
</View>
146157
</ScrollView>
147158
</View>
148159
);
149160
}
150161
}
162+
163+
const styles = StyleSheet.create({
164+
contentOnLeft: {
165+
flex: 1,
166+
justifyContent: 'space-between'
167+
}
168+
});

src/components/radioButton/index.tsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,13 @@ export type RadioButtonProps = RadioGroupContextProps &
7373
*/
7474
iconOnRight?: boolean;
7575
/**
76-
* Should the content be rendered right to the button
76+
* @deprecated The content is on right by default, for content on left use 'contentOnLeft'
7777
*/
7878
contentOnRight?: boolean;
79+
/**
80+
* Should the content be rendered left to the button
81+
*/
82+
contentOnLeft?: boolean;
7983
/**
8084
* Additional styling for the container
8185
*/
@@ -127,6 +131,11 @@ class RadioButton extends PureComponent<Props, RadioButtonState> {
127131
}
128132
}
129133

134+
get isContentOnLeft() {
135+
const {contentOnLeft, contentOnRight} = this.props;
136+
return contentOnLeft || contentOnRight;
137+
}
138+
130139
animate() {
131140
const {selected} = this.props;
132141
const {opacityAnimationValue, scaleAnimationValue} = this.state;
@@ -217,10 +226,10 @@ class RadioButton extends PureComponent<Props, RadioButtonState> {
217226
}
218227

219228
renderLabel() {
220-
const {label, labelStyle, contentOnRight} = this.props;
229+
const {label, labelStyle} = this.props;
221230
return (
222231
label && (
223-
<Text marginL-10={!contentOnRight} marginR-10={contentOnRight} $textDefault style={labelStyle}>
232+
<Text marginL-10={!this.isContentOnLeft} marginR-10={this.isContentOnLeft} $textDefault style={labelStyle}>
224233
{label}
225234
</Text>
226235
)
@@ -250,7 +259,7 @@ class RadioButton extends PureComponent<Props, RadioButtonState> {
250259
}
251260

252261
render() {
253-
const {onPress, onValueChange, contentOnRight, containerStyle, ...others} = this.props;
262+
const {onPress, onValueChange, containerStyle, ...others} = this.props;
254263
const Container = onPress || onValueChange ? TouchableOpacity : View;
255264

256265
return (
@@ -264,10 +273,10 @@ class RadioButton extends PureComponent<Props, RadioButtonState> {
264273
onPress={this.onPress}
265274
{...this.getAccessibilityProps()}
266275
>
267-
{!contentOnRight && this.renderButton()}
276+
{!this.isContentOnLeft && this.renderButton()}
268277
{this.props.iconOnRight ? this.renderLabel() : this.renderIcon()}
269278
{this.props.iconOnRight ? this.renderIcon() : this.renderLabel()}
270-
{contentOnRight && this.renderButton()}
279+
{this.isContentOnLeft && this.renderButton()}
271280
</Container>
272281
);
273282
}

src/components/radioButton/radioButton.api.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,14 @@
4040
"description": "Should the icon be on the right side of the label",
4141
"default": "false"
4242
},
43-
{"name": "contentOnRight", "type": "boolean", "description": "Should the content be rendered right to the button"},
43+
{
44+
"name": "contentOnRight",
45+
"type": "boolean",
46+
"description": "Deprecated. The content is on right by default, for content on left use 'contentOnLeft'",
47+
"deprecated": true
48+
},
49+
{"name": "contentOnLeft", "type": "boolean", "description": "Should the content be rendered left to the button"},
4450
{"name": "containerStyle", "type": "ViewStyle", "description": "Additional styling for the container"}
4551
],
46-
"snippet": [
47-
"<RadioButton value={null$1} label={'Default'$2}/>"
48-
]
52+
"snippet": ["<RadioButton value={null$1} label={'Default'$2}/>"]
4953
}

0 commit comments

Comments
 (0)