|
1 | 1 | import * as React from 'react'; |
2 | 2 | import { StyleSheet, View } from 'react-native'; |
3 | 3 |
|
4 | | -import { Checkbox, Palette, Text, TouchableRipple } from 'react-native-paper'; |
| 4 | +import { Checkbox, Text, useTheme } from 'react-native-paper'; |
5 | 5 |
|
6 | 6 | import ScreenWrapper from '../ScreenWrapper'; |
7 | 7 |
|
8 | | -const CheckboxExample = () => { |
9 | | - const [checkedNormal, setCheckedNormal] = React.useState<boolean>(true); |
10 | | - const [checkedCustom, setCheckedCustom] = React.useState<boolean>(true); |
11 | | - const [indeterminate, setIndeterminate] = React.useState<boolean>(true); |
| 8 | +type Status = 'unchecked' | 'checked' | 'indeterminate'; |
| 9 | + |
| 10 | +const STATUSES: Status[] = ['unchecked', 'checked', 'indeterminate']; |
| 11 | + |
| 12 | +type Row = { |
| 13 | + label: string; |
| 14 | + render: (status: Status) => React.ReactNode; |
| 15 | +}; |
12 | 16 |
|
| 17 | +const ROWS: Row[] = [ |
| 18 | + { |
| 19 | + label: 'Default', |
| 20 | + render: (status) => <Checkbox status={status} onPress={() => {}} />, |
| 21 | + }, |
| 22 | + { |
| 23 | + label: 'Disabled', |
| 24 | + render: (status) => <Checkbox status={status} disabled />, |
| 25 | + }, |
| 26 | + { |
| 27 | + label: 'Custom color (tertiary)', |
| 28 | + render: (status) => <CustomColorCheckbox status={status} />, |
| 29 | + }, |
| 30 | + { |
| 31 | + label: 'Error', |
| 32 | + render: (status) => <Checkbox status={status} error onPress={() => {}} />, |
| 33 | + }, |
| 34 | +]; |
| 35 | + |
| 36 | +const CustomColorCheckbox = ({ status }: { status: Status }) => { |
| 37 | + const theme = useTheme(); |
13 | 38 | return ( |
14 | | - <ScreenWrapper style={styles.container}> |
15 | | - <TouchableRipple onPress={() => setCheckedNormal(!checkedNormal)}> |
16 | | - <View style={styles.row}> |
17 | | - <Text>Normal</Text> |
18 | | - <View pointerEvents="none"> |
19 | | - <Checkbox status={checkedNormal ? 'checked' : 'unchecked'} /> |
20 | | - </View> |
21 | | - </View> |
22 | | - </TouchableRipple> |
| 39 | + <Checkbox |
| 40 | + status={status} |
| 41 | + color={theme.colors.tertiary} |
| 42 | + uncheckedColor={theme.colors.tertiary} |
| 43 | + onPress={() => {}} |
| 44 | + /> |
| 45 | + ); |
| 46 | +}; |
23 | 47 |
|
24 | | - <TouchableRipple onPress={() => setCheckedCustom(!checkedCustom)}> |
25 | | - <View style={styles.row}> |
26 | | - <Text>Custom</Text> |
27 | | - <View pointerEvents="none"> |
28 | | - <Checkbox |
29 | | - color={Palette.error70} |
30 | | - status={checkedCustom ? 'checked' : 'unchecked'} |
31 | | - /> |
32 | | - </View> |
| 48 | +const Interactive = () => { |
| 49 | + const theme = useTheme(); |
| 50 | + const [aChecked, setAChecked] = React.useState(false); |
| 51 | + const [bIndeterminate, setBIndeterminate] = React.useState(false); |
| 52 | + return ( |
| 53 | + <View style={styles.interactive}> |
| 54 | + <Text style={[styles.rowLabel, { color: theme.colors.onSurfaceVariant }]}> |
| 55 | + Tap to toggle |
| 56 | + </Text> |
| 57 | + <View style={styles.interactiveRow}> |
| 58 | + <View style={styles.cell}> |
| 59 | + <Checkbox |
| 60 | + status={aChecked ? 'checked' : 'unchecked'} |
| 61 | + onPress={() => setAChecked((v) => !v)} |
| 62 | + /> |
33 | 63 | </View> |
34 | | - </TouchableRipple> |
35 | | - |
36 | | - <TouchableRipple onPress={() => setIndeterminate(!indeterminate)}> |
37 | | - <View style={styles.row}> |
38 | | - <Text>Indeterminate</Text> |
39 | | - <View pointerEvents="none"> |
40 | | - <Checkbox status={indeterminate ? 'indeterminate' : 'unchecked'} /> |
41 | | - </View> |
| 64 | + <Text |
| 65 | + style={[ |
| 66 | + styles.interactiveLabel, |
| 67 | + { color: theme.colors.onSurfaceVariant }, |
| 68 | + ]} |
| 69 | + > |
| 70 | + unchecked ↔ checked |
| 71 | + </Text> |
| 72 | + </View> |
| 73 | + <View style={[styles.interactiveRow, styles.interactiveRowSpacing]}> |
| 74 | + <View style={styles.cell}> |
| 75 | + <Checkbox |
| 76 | + status={bIndeterminate ? 'indeterminate' : 'unchecked'} |
| 77 | + onPress={() => setBIndeterminate((v) => !v)} |
| 78 | + /> |
42 | 79 | </View> |
43 | | - </TouchableRipple> |
| 80 | + <Text |
| 81 | + style={[ |
| 82 | + styles.interactiveLabel, |
| 83 | + { color: theme.colors.onSurfaceVariant }, |
| 84 | + ]} |
| 85 | + > |
| 86 | + unchecked ↔ indeterminate |
| 87 | + </Text> |
| 88 | + </View> |
| 89 | + </View> |
| 90 | + ); |
| 91 | +}; |
44 | 92 |
|
45 | | - <View style={styles.row}> |
46 | | - <Text>Checked (Disabled)</Text> |
47 | | - <Checkbox status="checked" disabled /> |
| 93 | +const IndeterminateParent = () => { |
| 94 | + const theme = useTheme(); |
| 95 | + const [child1, setChild1] = React.useState(true); |
| 96 | + const [child2, setChild2] = React.useState(false); |
| 97 | + const [child3, setChild3] = React.useState(true); |
| 98 | + const allChecked = child1 && child2 && child3; |
| 99 | + const noneChecked = !child1 && !child2 && !child3; |
| 100 | + const parentStatus: Status = allChecked |
| 101 | + ? 'checked' |
| 102 | + : noneChecked |
| 103 | + ? 'unchecked' |
| 104 | + : 'indeterminate'; |
| 105 | + const toggleAll = () => { |
| 106 | + const next = !allChecked; |
| 107 | + setChild1(next); |
| 108 | + setChild2(next); |
| 109 | + setChild3(next); |
| 110 | + }; |
| 111 | + return ( |
| 112 | + <View style={styles.row}> |
| 113 | + <Text style={[styles.rowLabel, { color: theme.colors.onSurfaceVariant }]}> |
| 114 | + Parent / children (indeterminate) |
| 115 | + </Text> |
| 116 | + <View style={styles.parentRow}> |
| 117 | + <Checkbox status={parentStatus} onPress={toggleAll} /> |
| 118 | + <Text |
| 119 | + style={[styles.interactiveLabel, { color: theme.colors.onSurface }]} |
| 120 | + > |
| 121 | + Select all |
| 122 | + </Text> |
| 123 | + </View> |
| 124 | + <View style={styles.childRow}> |
| 125 | + <Checkbox |
| 126 | + status={child1 ? 'checked' : 'unchecked'} |
| 127 | + onPress={() => setChild1((v) => !v)} |
| 128 | + /> |
| 129 | + <Text |
| 130 | + style={[styles.interactiveLabel, { color: theme.colors.onSurface }]} |
| 131 | + > |
| 132 | + Apples |
| 133 | + </Text> |
48 | 134 | </View> |
49 | | - <View style={styles.row}> |
50 | | - <Text>Unchecked (Disabled)</Text> |
51 | | - <Checkbox status="unchecked" disabled /> |
| 135 | + <View style={styles.childRow}> |
| 136 | + <Checkbox |
| 137 | + status={child2 ? 'checked' : 'unchecked'} |
| 138 | + onPress={() => setChild2((v) => !v)} |
| 139 | + /> |
| 140 | + <Text |
| 141 | + style={[styles.interactiveLabel, { color: theme.colors.onSurface }]} |
| 142 | + > |
| 143 | + Bananas |
| 144 | + </Text> |
52 | 145 | </View> |
53 | | - <View style={styles.row}> |
54 | | - <Text>Indeterminate (Disabled)</Text> |
55 | | - <Checkbox status="indeterminate" disabled /> |
| 146 | + <View style={styles.childRow}> |
| 147 | + <Checkbox |
| 148 | + status={child3 ? 'checked' : 'unchecked'} |
| 149 | + onPress={() => setChild3((v) => !v)} |
| 150 | + /> |
| 151 | + <Text |
| 152 | + style={[styles.interactiveLabel, { color: theme.colors.onSurface }]} |
| 153 | + > |
| 154 | + Cherries |
| 155 | + </Text> |
56 | 156 | </View> |
| 157 | + </View> |
| 158 | + ); |
| 159 | +}; |
| 160 | + |
| 161 | +const CheckboxExample = () => { |
| 162 | + const theme = useTheme(); |
| 163 | + return ( |
| 164 | + <ScreenWrapper contentContainerStyle={styles.scrollContent}> |
| 165 | + <Interactive /> |
| 166 | + {ROWS.map((row) => ( |
| 167 | + <View key={row.label} style={styles.row}> |
| 168 | + <Text |
| 169 | + style={[styles.rowLabel, { color: theme.colors.onSurfaceVariant }]} |
| 170 | + > |
| 171 | + {row.label} |
| 172 | + </Text> |
| 173 | + <View style={styles.cells}> |
| 174 | + {STATUSES.map((status) => ( |
| 175 | + <View key={status} style={styles.cell}> |
| 176 | + {row.render(status)} |
| 177 | + </View> |
| 178 | + ))} |
| 179 | + </View> |
| 180 | + </View> |
| 181 | + ))} |
| 182 | + <IndeterminateParent /> |
57 | 183 | </ScreenWrapper> |
58 | 184 | ); |
59 | 185 | }; |
60 | 186 |
|
61 | 187 | CheckboxExample.title = 'Checkbox'; |
62 | 188 |
|
63 | 189 | const styles = StyleSheet.create({ |
64 | | - container: { |
65 | | - paddingVertical: 8, |
| 190 | + scrollContent: { paddingHorizontal: 16, paddingTop: 16, paddingBottom: 32 }, |
| 191 | + row: { marginBottom: 20 }, |
| 192 | + rowLabel: { |
| 193 | + fontSize: 14, |
| 194 | + fontWeight: '500', |
| 195 | + marginBottom: 6, |
| 196 | + opacity: 0.7, |
| 197 | + }, |
| 198 | + cells: { |
| 199 | + flexDirection: 'row', |
| 200 | + alignItems: 'center', |
| 201 | + }, |
| 202 | + cell: { |
| 203 | + width: 60, |
| 204 | + alignItems: 'center', |
| 205 | + }, |
| 206 | + interactive: { marginBottom: 24 }, |
| 207 | + interactiveRow: { |
| 208 | + flexDirection: 'row', |
| 209 | + alignItems: 'center', |
| 210 | + gap: 12, |
| 211 | + }, |
| 212 | + interactiveRowSpacing: { marginTop: 8 }, |
| 213 | + interactiveLabel: { |
| 214 | + fontSize: 14, |
| 215 | + opacity: 0.7, |
| 216 | + }, |
| 217 | + parentRow: { |
| 218 | + flexDirection: 'row', |
| 219 | + alignItems: 'center', |
| 220 | + gap: 12, |
66 | 221 | }, |
67 | | - row: { |
| 222 | + childRow: { |
68 | 223 | flexDirection: 'row', |
69 | 224 | alignItems: 'center', |
70 | | - justifyContent: 'space-between', |
71 | | - paddingVertical: 8, |
72 | | - paddingHorizontal: 16, |
| 225 | + gap: 12, |
| 226 | + paddingLeft: 32, |
73 | 227 | }, |
74 | 228 | }); |
75 | 229 |
|
|
0 commit comments