Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 68 additions & 4 deletions example/src/Examples/CheckboxExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,27 @@ import ScreenWrapper from '../ScreenWrapper';
const CheckboxExample = () => {
const [checkedNormal, setCheckedNormal] = React.useState<boolean>(true);
const [checkedCustom, setCheckedCustom] = React.useState<boolean>(true);
const [indeterminate, setIndeterminate] = React.useState<boolean>(true);
const [checkedError, setCheckedError] = React.useState<boolean>(false);

// Indeterminate parent: aggregates the state of its children.
const [child1, setChild1] = React.useState<boolean>(true);
const [child2, setChild2] = React.useState<boolean>(false);
const [child3, setChild3] = React.useState<boolean>(true);

const allChecked = child1 && child2 && child3;
const noneChecked = !child1 && !child2 && !child3;
const parentStatus: 'checked' | 'unchecked' | 'indeterminate' = allChecked
? 'checked'
: noneChecked
? 'unchecked'
: 'indeterminate';

const toggleAllChildren = () => {
const next = !allChecked;
setChild1(next);
setChild2(next);
setChild3(next);
};

return (
<ScreenWrapper style={styles.container}>
Expand All @@ -33,11 +53,11 @@ const CheckboxExample = () => {
</View>
</TouchableRipple>

<TouchableRipple onPress={() => setIndeterminate(!indeterminate)}>
<TouchableRipple onPress={() => setCheckedError(!checkedError)}>
<View style={styles.row}>
<Text>Indeterminate</Text>
<Text>Error</Text>
<View pointerEvents="none">
<Checkbox status={indeterminate ? 'indeterminate' : 'unchecked'} />
<Checkbox error status={checkedError ? 'checked' : 'unchecked'} />
</View>
</View>
</TouchableRipple>
Expand All @@ -54,6 +74,42 @@ const CheckboxExample = () => {
<Text>Indeterminate (Disabled)</Text>
<Checkbox status="indeterminate" disabled />
</View>

<View style={styles.sectionLabel}>
<Text variant="labelLarge">Parent / children (indeterminate)</Text>
</View>
<TouchableRipple onPress={toggleAllChildren}>
<View style={styles.row}>
<Text>Select all</Text>
<View pointerEvents="none">
<Checkbox status={parentStatus} />
</View>
</View>
</TouchableRipple>
<TouchableRipple onPress={() => setChild1(!child1)}>
<View style={[styles.row, styles.indent]}>
<Text>Apples</Text>
<View pointerEvents="none">
<Checkbox status={child1 ? 'checked' : 'unchecked'} />
</View>
</View>
</TouchableRipple>
<TouchableRipple onPress={() => setChild2(!child2)}>
<View style={[styles.row, styles.indent]}>
<Text>Bananas</Text>
<View pointerEvents="none">
<Checkbox status={child2 ? 'checked' : 'unchecked'} />
</View>
</View>
</TouchableRipple>
<TouchableRipple onPress={() => setChild3(!child3)}>
<View style={[styles.row, styles.indent]}>
<Text>Cherries</Text>
<View pointerEvents="none">
<Checkbox status={child3 ? 'checked' : 'unchecked'} />
</View>
</View>
</TouchableRipple>
</ScreenWrapper>
);
};
Expand All @@ -71,6 +127,14 @@ const styles = StyleSheet.create({
paddingVertical: 8,
paddingHorizontal: 16,
},
indent: {
paddingLeft: 32,
},
sectionLabel: {
paddingHorizontal: 16,
paddingTop: 16,
paddingBottom: 4,
},
});

export default CheckboxExample;
Loading
Loading