-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCheckboxLabel.tsx
98 lines (91 loc) · 2.69 KB
/
CheckboxLabel.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
import * as React from "react";
import { ComponentProps, useState } from "react";
import { Pressable, View } from "react-native";
import {
IOSelectionTickVisualParams,
IOSpacingScale,
useIOTheme
} from "../../core";
import { triggerHaptic } from "../../functions/haptic-feedback/hapticFeedback";
import { useIOFontDynamicScale } from "../../utils/accessibility";
import { HStack } from "../stack";
import { H6 } from "../typography/H6";
import { AnimatedCheckbox } from "./AnimatedCheckbox";
type Props = {
label: string;
// dispatch the new value after the checkbox changes state
onValueChange?: (newValue: boolean) => void;
};
const DISABLED_OPACITY = 0.5;
const CHECKBOX_MARGIN: IOSpacingScale = 8;
// disabled: the component is no longer touchable
// onPress:
type OwnProps = Props &
Pick<ComponentProps<typeof AnimatedCheckbox>, "disabled" | "checked"> &
Pick<ComponentProps<typeof Pressable>, "onPress">;
/**
* A checkbox with the automatic state management that uses a {@link AnimatedCheckBox}
* The toggleValue change when a `onPress` event is received and dispatch the `onValueChange`.
*
* @param props
* @constructor
*/
export const CheckboxLabel = ({
label,
checked,
disabled,
onValueChange
}: OwnProps) => {
const theme = useIOTheme();
const { dynamicFontScale } = useIOFontDynamicScale();
const [toggleValue, setToggleValue] = useState(checked ?? false);
const toggleCheckbox = () => {
triggerHaptic("impactLight");
setToggleValue(!toggleValue);
if (onValueChange !== undefined) {
onValueChange(!toggleValue);
}
};
return (
<Pressable
disabled={disabled}
onPress={toggleCheckbox}
testID="AnimatedCheckbox"
style={{
alignSelf: "flex-start",
opacity: disabled ? DISABLED_OPACITY : 1
}}
accessibilityRole="checkbox"
accessibilityState={{
checked: checked ?? toggleValue,
disabled: !!disabled
}}
// This is required to avoid opacity
// inheritance on Android
needsOffscreenAlphaCompositing={true}
>
<HStack
allowScaleSpacing
style={{ alignItems: "center", width: "100%" }}
space={CHECKBOX_MARGIN}
>
<View
pointerEvents="none"
accessibilityElementsHidden
importantForAccessibility="no-hide-descendants"
style={{
alignSelf: "flex-start"
}}
>
<AnimatedCheckbox
size={IOSelectionTickVisualParams.size * dynamicFontScale}
checked={checked ?? toggleValue}
/>
</View>
<H6 style={{ flexShrink: 1 }} color={theme["textBody-default"]}>
{label}
</H6>
</HStack>
</Pressable>
);
};