Skip to content

RadioButton support for rendering content on right #889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions demo/src/screens/componentScreens/RadioButtonScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ export default class RadioButtonScreen extends Component {
};
}

renderRadioButton(value, text) {
renderRadioButton(value, text, props) {
return (
<View row centerV marginB-5>
<RadioButton value={value} label={text}/>
<RadioButton value={value} label={text} {...props}/>
</View>
);
}
Expand Down Expand Up @@ -71,10 +71,12 @@ export default class RadioButtonScreen extends Component {

<RadioGroup marginT-30 initialValue={this.state.textSide} onValueChange={value => this.setState({textSide: value})}>
<Text marginB-20 text60 dark10>
Select text side
Alignments
</Text>
{this.renderRadioButtonWithImageAndText('right', 'Text on right')}
{this.renderRadioButtonWithImageAndText('left', 'Text on left', true)}
{this.renderRadioButtonWithImageAndText('right-icon', 'Text on right')}
{this.renderRadioButtonWithImageAndText('left-icon', 'Text on left', true)}
{this.renderRadioButton('left-content', 'Content on left', true)}
{this.renderRadioButton('right-content', 'Content on right', {contentOnRight: true})}
<Text marginT-10>You chose: {this.state.textSide}</Text>
</RadioGroup>

Expand Down
41 changes: 26 additions & 15 deletions src/components/radioButton/RadioButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ export type RadioButtonPropTypes = RadioGroupContextPropTypes & ViewProps & {
* Should the icon be on the right side of the label
*/
iconOnRight?: boolean;
}
/**
* Should the content be rendered right to the button
*/
contentOnRight?: boolean;
};

interface RadioButtonState {
opacityAnimationValue: Animated.Value;
Expand Down Expand Up @@ -206,10 +210,10 @@ class RadioButton extends PureComponent<Props, RadioButtonState> {
}

renderLabel() {
const {label, labelStyle} = this.props;
const {label, labelStyle, contentOnRight} = this.props;
return (
label && (
<Text marginL-10 style={labelStyle}>
<Text marginL-10={!contentOnRight} marginR-10={contentOnRight} style={labelStyle}>
{label}
</Text>
)
Expand All @@ -222,9 +226,24 @@ class RadioButton extends PureComponent<Props, RadioButtonState> {
return iconSource && <Image style={style} source={iconSource}/>;
}

render() {
const {onPress, onValueChange, ...others} = this.props;
renderButton() {
const {opacityAnimationValue, scaleAnimationValue} = this.state;

return (
<View style={this.getRadioButtonOutlineStyle()}>
<Animated.View
style={[
this.getRadioButtonInnerStyle(),
{opacity: opacityAnimationValue},
{transform: [{scale: scaleAnimationValue}]}
]}
/>
</View>
);
}

render() {
const {onPress, onValueChange, contentOnRight, ...others} = this.props;
const Container = onPress || onValueChange ? TouchableOpacity : View;

return (
Expand All @@ -234,21 +253,13 @@ class RadioButton extends PureComponent<Props, RadioButtonState> {
centerV
activeOpacity={1}
{...others}
style={undefined}
onPress={this.onPress}
{...this.getAccessibilityProps()}
>
<View style={this.getRadioButtonOutlineStyle()}>
<Animated.View
style={[
this.getRadioButtonInnerStyle(),
{opacity: opacityAnimationValue},
{transform: [{scale: scaleAnimationValue}]}
]}
/>
</View>
{!contentOnRight && this.renderButton()}
{this.props.iconOnRight ? this.renderLabel() : this.renderIcon()}
{this.props.iconOnRight ? this.renderIcon() : this.renderLabel()}
{contentOnRight && this.renderButton()}
</Container>
);
}
Expand Down