Skip to content

Commit e73e91a

Browse files
authored
RadioButton support for rendering content on right (#889)
* Add "add" icon to our demo app * Update TabController example screen * RadioButton - add contentOnRight prop to control content position
1 parent c75938f commit e73e91a

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

demo/src/screens/componentScreens/RadioButtonScreen.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ export default class RadioButtonScreen extends Component {
2020
};
2121
}
2222

23-
renderRadioButton(value, text) {
23+
renderRadioButton(value, text, props) {
2424
return (
2525
<View row centerV marginB-5>
26-
<RadioButton value={value} label={text}/>
26+
<RadioButton value={value} label={text} {...props}/>
2727
</View>
2828
);
2929
}
@@ -71,10 +71,12 @@ export default class RadioButtonScreen extends Component {
7171

7272
<RadioGroup marginT-30 initialValue={this.state.textSide} onValueChange={value => this.setState({textSide: value})}>
7373
<Text marginB-20 text60 dark10>
74-
Select text side
74+
Alignments
7575
</Text>
76-
{this.renderRadioButtonWithImageAndText('right', 'Text on right')}
77-
{this.renderRadioButtonWithImageAndText('left', 'Text on left', true)}
76+
{this.renderRadioButtonWithImageAndText('right-icon', 'Text on right')}
77+
{this.renderRadioButtonWithImageAndText('left-icon', 'Text on left', true)}
78+
{this.renderRadioButton('left-content', 'Content on left', true)}
79+
{this.renderRadioButton('right-content', 'Content on right', {contentOnRight: true})}
7880
<Text marginT-10>You chose: {this.state.textSide}</Text>
7981
</RadioGroup>
8082

src/components/radioButton/RadioButton.tsx

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ export type RadioButtonPropTypes = RadioGroupContextPropTypes & ViewProps & {
7272
* Should the icon be on the right side of the label
7373
*/
7474
iconOnRight?: boolean;
75-
}
75+
/**
76+
* Should the content be rendered right to the button
77+
*/
78+
contentOnRight?: boolean;
79+
};
7680

7781
interface RadioButtonState {
7882
opacityAnimationValue: Animated.Value;
@@ -206,10 +210,10 @@ class RadioButton extends PureComponent<Props, RadioButtonState> {
206210
}
207211

208212
renderLabel() {
209-
const {label, labelStyle} = this.props;
213+
const {label, labelStyle, contentOnRight} = this.props;
210214
return (
211215
label && (
212-
<Text marginL-10 style={labelStyle}>
216+
<Text marginL-10={!contentOnRight} marginR-10={contentOnRight} style={labelStyle}>
213217
{label}
214218
</Text>
215219
)
@@ -222,9 +226,24 @@ class RadioButton extends PureComponent<Props, RadioButtonState> {
222226
return iconSource && <Image style={style} source={iconSource}/>;
223227
}
224228

225-
render() {
226-
const {onPress, onValueChange, ...others} = this.props;
229+
renderButton() {
227230
const {opacityAnimationValue, scaleAnimationValue} = this.state;
231+
232+
return (
233+
<View style={this.getRadioButtonOutlineStyle()}>
234+
<Animated.View
235+
style={[
236+
this.getRadioButtonInnerStyle(),
237+
{opacity: opacityAnimationValue},
238+
{transform: [{scale: scaleAnimationValue}]}
239+
]}
240+
/>
241+
</View>
242+
);
243+
}
244+
245+
render() {
246+
const {onPress, onValueChange, contentOnRight, ...others} = this.props;
228247
const Container = onPress || onValueChange ? TouchableOpacity : View;
229248

230249
return (
@@ -234,21 +253,13 @@ class RadioButton extends PureComponent<Props, RadioButtonState> {
234253
centerV
235254
activeOpacity={1}
236255
{...others}
237-
style={undefined}
238256
onPress={this.onPress}
239257
{...this.getAccessibilityProps()}
240258
>
241-
<View style={this.getRadioButtonOutlineStyle()}>
242-
<Animated.View
243-
style={[
244-
this.getRadioButtonInnerStyle(),
245-
{opacity: opacityAnimationValue},
246-
{transform: [{scale: scaleAnimationValue}]}
247-
]}
248-
/>
249-
</View>
259+
{!contentOnRight && this.renderButton()}
250260
{this.props.iconOnRight ? this.renderLabel() : this.renderIcon()}
251261
{this.props.iconOnRight ? this.renderIcon() : this.renderLabel()}
262+
{contentOnRight && this.renderButton()}
252263
</Container>
253264
);
254265
}

0 commit comments

Comments
 (0)