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
157 changes: 100 additions & 57 deletions example/src/Examples/SwitchExample.tsx
Original file line number Diff line number Diff line change
@@ -1,73 +1,106 @@
import * as React from 'react';
import { Platform, StyleSheet, View } from 'react-native';
import { StyleSheet, View } from 'react-native';

import { Palette, Switch, Text, TouchableRipple } from 'react-native-paper';
import { Switch, Text, useTheme } from 'react-native-paper';
import type { SwitchColors } from 'react-native-paper';

import ScreenWrapper from '../ScreenWrapper';

const Row = ({
label,
children,
}: {
label: string;
children: React.ReactNode;
}) => (
<View style={styles.row}>
<Text>{label}</Text>
<View style={styles.right}>{children}</View>
</View>
);

const SwitchExample = () => {
const [valueNormal, setNormalValue] = React.useState<boolean>(true);
const [valueCustom, setCustomValue] = React.useState<boolean>(true);
const theme = useTheme();
const [defaultOn, setDefaultOn] = React.useState(true);
const [defaultCheckedIconOn, setDefaultCheckedIconOn] = React.useState(true);
const [defaultIconOn, setDefaultIconOn] = React.useState(true);
const [customOn, setCustomOn] = React.useState(true);
const [customIconOn, setCustomIconOn] = React.useState(true);
const [disableAll, setDisableAll] = React.useState(false);

const switchValueNormalLabel = `switch ${
valueNormal === true ? 'on' : 'off'
}`;
const switchValueCustomlLabel = `switch ${
valueCustom === true ? 'on' : 'off'
}`;
const tertiaryColors: Partial<SwitchColors> = React.useMemo(
() => ({
checkedTrackColor: theme.colors.tertiary,
checkedHandleColor: theme.colors.onTertiary,
checkedHoverHandleColor: theme.colors.tertiaryContainer,
checkedFocusHandleColor: theme.colors.tertiaryContainer,
checkedPressedHandleColor: theme.colors.tertiaryContainer,
checkedIconColor: theme.colors.tertiary,
checkedStateLayerColor: theme.colors.tertiary,
focusIndicatorColor: theme.colors.tertiary,
}),
[theme]
);

return Platform.OS === 'android' ? (
return (
<ScreenWrapper style={styles.container}>
<TouchableRipple onPress={() => setNormalValue(!valueNormal)}>
<View style={styles.row}>
<Text>Normal {switchValueNormalLabel}</Text>
<View pointerEvents="none">
<Switch value={valueNormal} />
</View>
</View>
</TouchableRipple>
<TouchableRipple onPress={() => setCustomValue(!valueCustom)}>
<View style={styles.row}>
<Text>Custom {switchValueCustomlLabel}</Text>
<View pointerEvents="none">
<Switch value={valueCustom} color={Palette.tertiary50} />
</View>
</View>
</TouchableRipple>
<View style={styles.row}>
<Text>Switch on (disabled)</Text>
<Switch disabled value />
</View>
<View style={styles.row}>
<Text>Switch off (disabled)</Text>
<Switch disabled />
</View>
</ScreenWrapper>
) : (
<ScreenWrapper style={styles.container}>
<View style={styles.row}>
<Text>Normal {switchValueNormalLabel}</Text>
<Row label="Default">
<Switch
value={defaultOn}
onValueChange={setDefaultOn}
disabled={disableAll}
/>
</Row>

<Row label="Default with icon when on">
<Switch
value={defaultCheckedIconOn}
onValueChange={setDefaultCheckedIconOn}
checkedIcon="check"
disabled={disableAll}
/>
</Row>

<Row label="Default with icon">
<Switch
value={valueNormal}
onValueChange={() => setNormalValue(!valueNormal)}
value={defaultIconOn}
onValueChange={setDefaultIconOn}
checkedIcon="check"
uncheckedIcon="close"
disabled={disableAll}
/>
</View>
<View style={styles.row}>
<Text>Custom {switchValueCustomlLabel}</Text>
</Row>

<Row label="Custom">
<Switch
value={valueCustom}
onValueChange={() => setCustomValue(!valueCustom)}
color={Palette.tertiary50}
value={customOn}
onValueChange={setCustomOn}
colors={tertiaryColors}
disabled={disableAll}
/>
</View>
<View style={styles.row}>
<Text>Switch on (disabled)</Text>
<Switch value disabled />
</View>
<View style={styles.row}>
<Text>Switch off (disabled)</Text>
<Switch value={false} disabled />
</View>
</Row>

<Row label="Custom with icon">
<Switch
value={customIconOn}
onValueChange={setCustomIconOn}
checkedIcon="white-balance-sunny"
uncheckedIcon="moon-waning-crescent"
colors={tertiaryColors}
disabled={disableAll}
/>
</Row>

<View
style={[
styles.separator,
{ backgroundColor: theme.colors.outlineVariant },
]}
/>

<Row label="Disable all switches">
<Switch value={disableAll} onValueChange={setDisableAll} />
</Row>
</ScreenWrapper>
);
};
Expand All @@ -85,6 +118,16 @@ const styles = StyleSheet.create({
paddingVertical: 8,
paddingHorizontal: 16,
},
right: {
flexDirection: 'row',
alignItems: 'center',
gap: 12,
},
separator: {
height: 1,
marginHorizontal: 16,
marginVertical: 16,
},
});

export default SwitchExample;
4 changes: 4 additions & 0 deletions jest/testSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ jest.mock('react-native-worklets', () =>
require('react-native-worklets/lib/module/mock')
);

jest.mock('react-native-reanimated', () =>
require('react-native-reanimated/mock')
);

jest.mock('@react-native-vector-icons/material-design-icons', () => {
const React = require('react');
const { Text } = require('react-native');
Expand Down
Loading
Loading