-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathRadioButtonWithLabel.js
87 lines (75 loc) · 2.72 KB
/
RadioButtonWithLabel.js
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
import React from 'react';
import PropTypes from 'prop-types';
import {View, TouchableOpacity} from 'react-native';
import _ from 'underscore';
import styles from '../styles/styles';
import RadioButton from './RadioButton';
import Text from './Text';
import FormHelpMessage from './FormHelpMessage';
const propTypes = {
/** Whether the radioButton is checked */
isChecked: PropTypes.bool.isRequired,
/** Called when the radioButton or label is pressed */
onPress: PropTypes.func.isRequired,
/** Container styles */
style: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.object), PropTypes.object]),
/** Text that appears next to check box */
label: PropTypes.string,
/** Component to display for label */
LabelComponent: PropTypes.func,
/** Should the input be styled for errors */
hasError: PropTypes.bool,
/** Error text to display */
errorText: PropTypes.string,
};
const defaultProps = {
style: [],
label: undefined,
LabelComponent: undefined,
hasError: false,
errorText: '',
};
const RadioButtonWithLabel = (props) => {
const LabelComponent = props.LabelComponent;
const defaultStyles = [styles.flexRow, styles.alignItemsCenter];
const wrapperStyles = _.isArray(props.style) ? [...defaultStyles, ...props.style] : [...defaultStyles, props.style];
if (!props.label && !LabelComponent) {
throw new Error('Must provide at least label or LabelComponent prop');
}
return (
<>
<View style={wrapperStyles}>
<RadioButton
isChecked={props.isChecked}
onPress={props.onPress}
label={props.label}
hasError={props.hasError}
/>
<TouchableOpacity
onPress={() => props.onPress()}
style={[
styles.ml3,
styles.pr2,
styles.w100,
styles.flexRow,
styles.flexWrap,
styles.flexShrink1,
styles.alignItemsCenter,
]}
>
{props.label && (
<Text style={[styles.ml1]}>
{props.label}
</Text>
)}
{LabelComponent && (<LabelComponent />)}
</TouchableOpacity>
</View>
<FormHelpMessage message={props.errorText} />
</>
);
};
RadioButtonWithLabel.propTypes = propTypes;
RadioButtonWithLabel.defaultProps = defaultProps;
RadioButtonWithLabel.displayName = 'RadioButtonWithLabel';
export default RadioButtonWithLabel;