Skip to content
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
2 changes: 2 additions & 0 deletions example/src/ExampleList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import TextExample from './Examples/TextExample';
import TextInputExample from './Examples/TextInputExample';
import ToggleButtonExample from './Examples/ToggleButtonExample';
import TouchableRippleExample from './Examples/TouchableRippleExample';
import RadioButtonGroupWithItemsExample from './Examples/RadioButtonGroupWithItemsExample';

type Props = {
theme: Theme;
Expand Down Expand Up @@ -61,6 +62,7 @@ export const examples: { [key: string]: any } = {
progressbar: ProgressBarExample,
radio: RadioButtonExample,
radioGroup: RadioButtonGroupExample,
radioGroupWithItems: RadioButtonGroupWithItemsExample,
searchbar: SearchbarExample,
snackbar: SnackbarExample,
surface: SurfaceExample,
Expand Down
55 changes: 55 additions & 0 deletions example/src/Examples/RadioButtonGroupWithItemsExample.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import * as React from 'react';
import { View, StyleSheet } from 'react-native';
import { Colors, withTheme, RadioButton, Theme } from 'react-native-paper';

type Props = {
theme: Theme;
};

type State = {
value: string;
};

class RadioButtonGroupWithItemsExample extends React.Component<Props, State> {
static title = 'Radio Button Group With Items';

state = {
value: 'first',
};

render() {
const {
theme: {
colors: { background },
},
} = this.props;
return (
<View
style={[
styles.container,
{
backgroundColor: background,
},
]}
>
<RadioButton.Group
value={this.state.value}
onValueChange={(value: string) => this.setState({ value })}
>
<RadioButton.Item label="First item" value="first" />
<RadioButton.Item label="Second item" value="second" />
</RadioButton.Group>
</View>
);
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: Colors.white,
padding: 8,
},
});

export default withTheme(RadioButtonGroupWithItemsExample);
4 changes: 4 additions & 0 deletions src/components/RadioButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import RadioButtonAndroid, {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
RadioButtonAndroid as _RadioButtonAndroid,
} from './RadioButtonAndroid';
import RadioButtonItem from './RadioButtonItem';
import RadioButtonIOS, {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
RadioButtonIOS as _RadioButtonIOS,
Expand Down Expand Up @@ -110,6 +111,9 @@ class RadioButton extends React.Component<Props> {
// @component ./RadioButtonIOS.tsx
static IOS = RadioButtonIOS;

// @component = ./RadioButtonItem.tsx
static Item = RadioButtonItem;

handlePress = (context: RadioButtonContextType) => {
const { onPress } = this.props;
const onValueChange = context ? context.onValueChange : () => {};
Expand Down
104 changes: 104 additions & 0 deletions src/components/RadioButtonItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import * as React from 'react';
import { View, Text, StyleSheet, StyleProp, ViewStyle } from 'react-native';
import TouchableRipple from './TouchableRipple';
import RadioButton from './RadioButton';
import { RadioButtonContext, RadioButtonContextType } from './RadioButtonGroup';

type Props = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to accept style prop and pass it to the View like that: style={[styles.container, style]} so users can change flex-direction or other properties

/**
* Value of the radio button.
*/
value: string;
/**
* Label to be displayed on the item.
*/
label: string;
/**
* Function to execute on press.
*/
onPress?: () => void;
/**
* Status of radio button.
*/
status?: 'checked' | 'unchecked';
/**
* Additional styles for container View
*/
style?: StyleProp<ViewStyle>;
};

/**
* RadioButton.Item allows you to press the whole row (item) instead of only the RadioButton.
*
* ## Usage
* ```js
* import * as React from 'react';
* import { View } from 'react-native';
* import { RadioButton, Text } from 'react-native-paper';
*
* export default class MyComponent extends React.Component {
* state = {
* value: 'first',
* };
*
* render() {
* return(
* <RadioButton.Group
* onValueChange={value => this.setState({ value })}
* value={this.state.value}
* >
* <RadioButton.Item label="First item" value="first" />
* <RadioButton.Item label="Second item" value="second" />
* </RadioButton.Group>
* )
* }
* }
*```
*/
class RadioButtonItem extends React.Component<Props> {
static displayName = 'RadioButton.Item';

isChecked = (context: RadioButtonContextType) =>
context.value === this.props.value ? 'checked' : 'unchecked';

handlePress = (context: RadioButtonContextType) => () => {
const { onPress } = this.props;
const onValueChange = context ? context.onValueChange : () => {};

onPress ? onPress() : onValueChange(this.props.value);
};

render() {
const { value, label, style } = this.props;

return (
<RadioButtonContext.Consumer>
{context => (
<TouchableRipple onPress={this.handlePress(context)}>
<View style={[styles.container, style]} pointerEvents="none">
<Text>{label}</Text>
<RadioButton
value={value}
status={
this.props.status || (context && this.isChecked(context))
}
></RadioButton>
</View>
</TouchableRipple>
)}
</RadioButtonContext.Consumer>
);
}
}

export default RadioButtonItem;

const styles = StyleSheet.create({
container: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
paddingVertical: 8,
paddingHorizontal: 16,
},
});