Skip to content

Commit b08115d

Browse files
author
Artur Bien
committed
feat(radio,checkbox): add flat variant
1 parent c3edde4 commit b08115d

File tree

3 files changed

+105
-16
lines changed

3 files changed

+105
-16
lines changed

example/src/examples/CheckboxExample.tsx

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable no-console */
22
import React, { useState } from 'react';
3-
import { StyleSheet } from 'react-native';
4-
import { Checkbox, Panel, Fieldset } from 'react95-native';
3+
import { StyleSheet, View } from 'react-native';
4+
import { Checkbox, Panel, Fieldset, Cutout } from 'react95-native';
55

66
const CheckboxExample = () => {
77
const [isChecked, setIsChecked] = useState(true);
@@ -35,6 +35,40 @@ const CheckboxExample = () => {
3535
disabled
3636
/>
3737
</Fieldset>
38+
<Cutout>
39+
<View style={{ padding: 20 }}>
40+
<Fieldset variant='flat' label='Default'>
41+
<Checkbox
42+
variant='flat'
43+
status={isChecked ? 'checked' : 'unchecked'}
44+
onPress={() => setIsChecked(prevState => !prevState)}
45+
label='Potato'
46+
/>
47+
<Checkbox
48+
variant='flat'
49+
status='checked'
50+
onPress={() => console.warn('pressed')}
51+
label='Disabled'
52+
disabled
53+
/>
54+
</Fieldset>
55+
<Fieldset variant='flat' label='Indeterminate'>
56+
<Checkbox
57+
variant='flat'
58+
status={isIndeterminate ? 'indeterminate' : 'unchecked'}
59+
onPress={() => setIsIndeterminate(prevState => !prevState)}
60+
label='Potato'
61+
/>
62+
<Checkbox
63+
variant='flat'
64+
status='indeterminate'
65+
onPress={() => console.warn('pressed')}
66+
label='Disabled'
67+
disabled
68+
/>
69+
</Fieldset>
70+
</View>
71+
</Cutout>
3872
</Panel>
3973
);
4074
};

example/src/examples/RadioExample.tsx

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable no-console */
22
import React, { useState } from 'react';
3-
import { StyleSheet } from 'react-native';
4-
import { Radio, Panel, Fieldset } from 'react95-native';
3+
import { StyleSheet, View } from 'react-native';
4+
import { Radio, Panel, Fieldset, Cutout } from 'react95-native';
55

66
const RadioExample = () => {
77
const [value, setValue] = useState('Apple');
@@ -39,6 +39,46 @@ const RadioExample = () => {
3939
disabled
4040
/>
4141
</Fieldset>
42+
<Cutout>
43+
<View style={{ padding: 20 }}>
44+
<Fieldset variant='flat' label='Default'>
45+
<Radio
46+
variant='flat'
47+
status={value === 'Apple' ? 'checked' : 'unchecked'}
48+
onPress={() => setValue('Apple')}
49+
label='Apple'
50+
/>
51+
<Radio
52+
variant='flat'
53+
status={value === 'Orange' ? 'checked' : 'unchecked'}
54+
onPress={() => setValue('Orange')}
55+
label='Orange'
56+
/>
57+
<Radio
58+
variant='flat'
59+
status={value === 'Watermelon' ? 'checked' : 'unchecked'}
60+
onPress={() => setValue('Watermelon')}
61+
label='Watermelon'
62+
/>
63+
</Fieldset>
64+
<Fieldset variant='flat' label='Disabled'>
65+
<Radio
66+
variant='flat'
67+
status='checked'
68+
onPress={() => console.warn('pressed')}
69+
label='Apple'
70+
disabled
71+
/>
72+
<Radio
73+
variant='flat'
74+
status='unchecked'
75+
onPress={() => console.warn('pressed')}
76+
label='Pear'
77+
disabled
78+
/>
79+
</Fieldset>
80+
</View>
81+
</Cutout>
4282
</Panel>
4383
);
4484
};

src/SwitchBase/SwitchBase.tsx

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export type SwitchProps = {
4444
onPress?: () => void;
4545
status: SwitchStatus;
4646
style?: StyleProp<ViewStyle>;
47+
variant?: 'default' | 'flat';
4748
};
4849

4950
type Props = SwitchProps &
@@ -57,6 +58,7 @@ export const SwitchBase = ({
5758
component,
5859
disabled = false,
5960
label = '',
61+
variant = 'default',
6062
onPress = () => {},
6163
status,
6264
style = {},
@@ -65,9 +67,11 @@ export const SwitchBase = ({
6567
const theme = useContext(ThemeContext);
6668
const [isPressed, setIsPressed] = React.useState(false);
6769
const isRadio = component === 'radio';
68-
const borderRadius = isRadio ? switchSize / 2 : 0;
70+
const boxSize = variant === 'flat' ? switchSize - 4 : switchSize;
71+
const borderRadius = isRadio ? boxSize / 2 : 0;
6972

70-
const renderBox = () => {
73+
const renderCheckmark = () => {
74+
const symbolOffset = variant === 'flat' ? 2 : 4;
7175
if (status === 'checked') {
7276
const symbol = symbols[component][disabled ? 'disabled' : 'default'];
7377

@@ -76,9 +80,10 @@ export const SwitchBase = ({
7680
// border to compensate for Border
7781
style={[
7882
{
79-
width: '100%',
80-
height: '100%',
81-
borderWidth: 4,
83+
width: boxSize,
84+
height: boxSize,
85+
borderWidth: symbolOffset,
86+
borderColor: 'transparent',
8287
},
8388
]}
8489
imageStyle={{
@@ -110,9 +115,16 @@ export const SwitchBase = ({
110115
return <Text> </Text>;
111116
};
112117

118+
const getBackgroundColor = () => {
119+
if (variant === 'flat') {
120+
return disabled ? theme.flatLight : theme.canvas;
121+
}
122+
return disabled ? theme.material : theme.canvas;
123+
};
124+
113125
return (
114126
<TouchableHighlight
115-
style={[styles.wrapper, { backgroundColor: theme.material }]}
127+
style={[styles.wrapper]}
116128
onPress={onPress}
117129
activeOpacity={1}
118130
disabled={disabled}
@@ -131,14 +143,19 @@ export const SwitchBase = ({
131143
style={[
132144
styles.switchSymbol,
133145
{
134-
backgroundColor: disabled ? theme.material : theme.canvas,
146+
width: boxSize,
147+
height: boxSize,
148+
backgroundColor: getBackgroundColor(),
135149
borderRadius,
136150
overflow: 'hidden',
137151
},
138152
]}
139153
>
140-
{renderBox()}
141-
<Border variant='cutout' radius={borderRadius} />
154+
{renderCheckmark()}
155+
<Border
156+
variant={variant === 'flat' ? 'flat' : 'cutout'}
157+
radius={borderRadius}
158+
/>
142159
</View>
143160
{Boolean(label) && (
144161
<View
@@ -171,9 +188,7 @@ const styles = StyleSheet.create({
171188
width: 'auto',
172189
},
173190
switchSymbol: {
174-
width: switchSize,
175-
height: switchSize,
176-
marginRight: 8,
191+
marginRight: 4,
177192
},
178193
labelWrapper: {
179194
paddingHorizontal: 4,

0 commit comments

Comments
 (0)