forked from callstack/react-native-paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRadioButtonItem.tsx
135 lines (128 loc) · 3.07 KB
/
RadioButtonItem.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import * as React from 'react';
import {
View,
StyleSheet,
StyleProp,
ViewStyle,
TextStyle,
} from 'react-native';
import { Theme } from '../../types';
import { withTheme } from '../../core/theming';
import { RadioButtonContext, RadioButtonContextType } from './RadioButtonGroup';
import { handlePress } from './utils';
import TouchableRipple from '../TouchableRipple';
import RadioButton from './RadioButton';
import Text from '../Typography/Text';
export type Props = {
/**
* 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>;
/**
* Style that is passed to Label element.
*/
labelStyle?: StyleProp<TextStyle>;
/**
* @optional
*/
theme: Theme;
};
/**
* 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';
render() {
const {
value,
label,
style,
labelStyle,
onPress,
status,
theme: { colors },
} = this.props;
return (
<RadioButtonContext.Consumer>
{(context?: RadioButtonContextType) => {
return (
<TouchableRipple
onPress={() =>
handlePress({
onPress: onPress,
onValueChange: context?.onValueChange,
value,
})
}
>
<View style={[styles.container, style]} pointerEvents="none">
<Text
style={[styles.label, labelStyle, { color: colors.primary }]}
>
{label}
</Text>
<RadioButton value={value} status={status}></RadioButton>
</View>
</TouchableRipple>
);
}}
</RadioButtonContext.Consumer>
);
}
}
export default withTheme(RadioButtonItem);
// @component-docs ignore-next-line
export { RadioButtonItem };
const styles = StyleSheet.create({
container: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
paddingVertical: 8,
paddingHorizontal: 16,
},
label: {
fontSize: 16,
},
});