-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathCheckbox.js
132 lines (111 loc) · 4.03 KB
/
Checkbox.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
import React from 'react';
import {View} from 'react-native';
import PropTypes from 'prop-types';
import styles from '../styles/styles';
import themeColors from '../styles/themes/default';
import stylePropTypes from '../styles/stylePropTypes';
import Icon from './Icon';
import * as Expensicons from './Icon/Expensicons';
import * as StyleUtils from '../styles/StyleUtils';
import CONST from '../CONST';
import PressableWithFeedback from './Pressable/PressableWithFeedback';
import refPropTypes from './refPropTypes';
const propTypes = {
/** Whether checkbox is checked */
isChecked: PropTypes.bool,
/** A function that is called when the box/label is pressed */
onPress: PropTypes.func.isRequired,
/** Should the input be styled for errors */
hasError: PropTypes.bool,
/** Should the input be disabled */
disabled: PropTypes.bool,
/** Children (icon) for Checkbox */
children: PropTypes.node,
/** Additional styles to add to checkbox button */
style: stylePropTypes,
/** Additional styles to add to checkbox container */
containerStyle: stylePropTypes,
/** Callback that is called when mousedown is triggered. */
onMouseDown: PropTypes.func,
/** The size of the checkbox container */
containerSize: PropTypes.number,
/** The border radius of the checkbox container */
containerBorderRadius: PropTypes.number,
/** The size of the caret (checkmark) */
caretSize: PropTypes.number,
/** A ref to forward to the Pressable */
forwardedRef: refPropTypes,
/** An accessibility label for the checkbox */
accessibilityLabel: PropTypes.string.isRequired,
};
const defaultProps = {
isChecked: false,
hasError: false,
disabled: false,
style: [],
containerStyle: [],
forwardedRef: undefined,
children: null,
onMouseDown: undefined,
containerSize: 20,
containerBorderRadius: 4,
caretSize: 14,
};
function Checkbox(props) {
const handleSpaceKey = (event) => {
if (event.code !== 'Space') {
return;
}
props.onPress();
};
const firePressHandlerOnClick = (event) => {
// Pressable can be triggered with Enter key and by a click. As this is a checkbox,
// We do not want to toggle it, when Enter key is pressed.
if (event.type && event.type !== 'click') {
return;
}
props.onPress();
};
return (
<PressableWithFeedback
disabled={props.disabled}
onPress={firePressHandlerOnClick}
onMouseDown={props.onMouseDown}
ref={props.forwardedRef}
style={[props.style, styles.checkboxPressable]}
onKeyDown={handleSpaceKey}
accessibilityRole={CONST.ACCESSIBILITY_ROLE.CHECKBOX}
accessibilityState={{checked: props.isChecked}}
accessibilityLabel={props.accessibilityLabel}
pressDimmingValue={1}
>
{props.children ? (
props.children
) : (
<View
style={[
StyleUtils.getCheckboxContainerStyle(props.containerSize, props.containerBorderRadius),
props.containerStyle,
props.isChecked && styles.checkedContainer,
props.hasError && styles.borderColorDanger,
props.disabled && styles.cursorDisabled,
props.isChecked && styles.borderColorFocus,
]}
>
{props.isChecked && (
<Icon
src={Expensicons.Checkmark}
fill={themeColors.textLight}
height={props.caretSize}
width={props.caretSize}
/>
)}
</View>
)}
</PressableWithFeedback>
);
}
Checkbox.propTypes = propTypes;
Checkbox.defaultProps = defaultProps;
Checkbox.displayName = 'Checkbox';
export default Checkbox;