-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
CheckboxWithLabel.js
145 lines (121 loc) · 4.93 KB
/
CheckboxWithLabel.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
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
136
137
138
139
140
141
142
143
144
145
import PropTypes from 'prop-types';
import React, {useState} from 'react';
import {View} from 'react-native';
import _ from 'underscore';
import useThemeStyles from '@styles/useThemeStyles';
import variables from '@styles/variables';
import Checkbox from './Checkbox';
import FormHelpMessage from './FormHelpMessage';
import PressableWithFeedback from './Pressable/PressableWithFeedback';
import refPropTypes from './refPropTypes';
import Text from './Text';
/**
* Returns an error if the required props are not provided
* @param {Object} props
* @returns {Error|null}
*/
const requiredPropsCheck = (props) => {
if (!props.label && !props.LabelComponent) {
return new Error('One of "label" or "LabelComponent" must be provided');
}
if (props.label && typeof props.label !== 'string') {
return new Error('Prop "label" must be a string');
}
if (props.LabelComponent && typeof props.LabelComponent !== 'function') {
return new Error('Prop "LabelComponent" must be a function');
}
};
const propTypes = {
/** Whether the checkbox is checked */
isChecked: PropTypes.bool,
/** Called when the checkbox or label is pressed */
onInputChange: PropTypes.func.isRequired,
/** Container styles */
style: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.object), PropTypes.object]),
/** Text that appears next to check box */
label: requiredPropsCheck,
/** Component to display for label */
LabelComponent: requiredPropsCheck,
/** Error text to display */
errorText: PropTypes.string,
/** Value for checkbox. This prop is intended to be set by Form.js only */
value: PropTypes.bool,
/** The default value for the checkbox */
defaultValue: PropTypes.bool,
/** React ref being forwarded to the Checkbox input */
forwardedRef: refPropTypes,
/** The ID used to uniquely identify the input in a Form */
/* eslint-disable-next-line react/no-unused-prop-types */
inputID: PropTypes.string,
/** Saves a draft of the input value when used in a form */
/* eslint-disable-next-line react/no-unused-prop-types */
shouldSaveDraft: PropTypes.bool,
/** An accessibility label for the checkbox */
accessibilityLabel: PropTypes.string,
};
const defaultProps = {
inputID: undefined,
style: [],
label: undefined,
LabelComponent: undefined,
errorText: '',
shouldSaveDraft: false,
isChecked: false,
value: false,
defaultValue: false,
forwardedRef: () => {},
accessibilityLabel: undefined,
};
function CheckboxWithLabel(props) {
const styles = useThemeStyles();
// We need to pick the first value that is strictly a boolean
// https://github.com/Expensify/App/issues/16885#issuecomment-1520846065
const [isChecked, setIsChecked] = useState(() => _.find([props.value, props.defaultValue, props.isChecked], (value) => _.isBoolean(value)));
const toggleCheckbox = () => {
const newState = !isChecked;
props.onInputChange(newState);
setIsChecked(newState);
};
const LabelComponent = props.LabelComponent;
return (
<View style={props.style}>
<View style={[styles.flexRow, styles.alignItemsCenter, styles.breakWord]}>
<Checkbox
isChecked={isChecked}
onPress={toggleCheckbox}
label={props.label}
style={[styles.checkboxWithLabelCheckboxStyle]}
hasError={Boolean(props.errorText)}
forwardedRef={props.forwardedRef}
accessibilityLabel={props.accessibilityLabel || props.label}
/>
<PressableWithFeedback
tabIndex={-1}
accessible={false}
onPress={toggleCheckbox}
pressDimmingValue={variables.checkboxLabelActiveOpacity}
// We want to disable hover dimming
hoverDimmingValue={variables.checkboxLabelHoverOpacity}
style={[styles.flexRow, styles.alignItemsCenter, styles.noSelect, styles.w100]}
wrapperStyle={[styles.ml3, styles.pr2, styles.w100, styles.flexWrap, styles.flexShrink1]}
>
{props.label && <Text style={[styles.ml1]}>{props.label}</Text>}
{LabelComponent && <LabelComponent />}
</PressableWithFeedback>
</View>
<FormHelpMessage message={props.errorText} />
</View>
);
}
CheckboxWithLabel.propTypes = propTypes;
CheckboxWithLabel.defaultProps = defaultProps;
CheckboxWithLabel.displayName = 'CheckboxWithLabel';
const CheckboxWithLabelWithRef = React.forwardRef((props, ref) => (
<CheckboxWithLabel
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
forwardedRef={ref}
/>
));
CheckboxWithLabelWithRef.displayName = 'CheckboxWithLabelWithRef';
export default CheckboxWithLabelWithRef;