Skip to content

Commit 68f3ca7

Browse files
committed
refactor: clean up
1 parent 491259f commit 68f3ca7

10 files changed

Lines changed: 281 additions & 160 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,14 @@ The main switch component.
129129
<PillSwitch
130130
align?: 'left' | 'right' | 'center';
131131
onPressCallback?: (value: TValue) => void;
132-
customItemStyle?: ViewStyle;
132+
inactiveOptionContainerStyle?: ViewStyle;
133133
containerHeight?: number;
134-
itemHeight?: number;
134+
optionHeight?: number;
135135
inactiveBackgroundColor?: string;
136136
activeBackgroundColor?: string;
137137
inactiveTextColor?: string;
138138
activeTextColor?: string;
139-
customTextStyle?: TextStyle;
139+
inactiveTextStyle?: TextStyle;
140140
/>
141141
```
142142

example/src/examples/Align.tsx

Lines changed: 103 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
/* eslint-disable react-native/no-inline-styles */
21
import {
32
MultiswitchController,
43
type ControllerVariant,
54
} from 'react-native-multiswitch-controller';
65
import ExampleWrapper from './ExampleWrapper';
6+
import { StyleSheet } from 'react-native';
77

88
type AlignProps = {
99
variant: ControllerVariant;
@@ -19,14 +19,18 @@ export default function Align({ variant }: AlignProps) {
1919
]}
2020
defaultOption="First"
2121
variant={variant}
22-
styleProps={{
23-
align: 'left',
24-
inactiveBackgroundColor: 'rgb(21, 87, 21)',
25-
activeBackgroundColor: 'rgb(60, 180, 20)',
26-
inactiveTextColor: 'rgb(208, 249, 205)',
27-
containerHeight: 52,
28-
itemHeight: 46,
29-
}}
22+
align="left"
23+
containerHeight={52}
24+
optionHeight={46}
25+
containerPadding={variant === 'tabs' ? 10 : undefined}
26+
containerStyle={styles.containerStyle1}
27+
activeOptionContainerStyle={styles.activeOptionContainerStyle1}
28+
inactiveTextStyle={styles.inactiveCustomTextStyle1}
29+
activeTextStyle={
30+
variant === 'tabs'
31+
? styles.activeTextStyleTabs1
32+
: styles.activeTextStyleSegmentedControl1
33+
}
3034
/>
3135
<MultiswitchController<'First' | 'Second'>
3236
options={[
@@ -35,28 +39,18 @@ export default function Align({ variant }: AlignProps) {
3539
]}
3640
defaultOption="Second"
3741
variant={variant}
38-
styleProps={{
39-
align: 'center',
40-
inactiveBackgroundColor: 'rgba(220, 38, 38, 0.08)',
41-
activeBackgroundColor: 'rgb(185, 28, 28)',
42-
inactiveTextColor: 'rgb(185, 28, 28)',
43-
containerHeight: 28,
44-
itemHeight: 24,
45-
customTextStyle: {
46-
fontSize: 10,
47-
},
48-
customItemStyle: {
49-
paddingHorizontal: 8,
50-
paddingVertical: 2,
51-
borderRadius: 0,
52-
},
53-
customActiveOptionStyle: {
54-
borderRadius: 0,
55-
},
56-
customContainerStyle: {
57-
borderRadius: 0,
58-
},
59-
}}
42+
align="center"
43+
containerHeight={28}
44+
optionHeight={24}
45+
containerStyle={styles.containerStyle2}
46+
inactiveOptionContainerStyle={styles.inactiveOptionContainerStyle2}
47+
activeOptionContainerStyle={styles.activeOptionContainerStyle2}
48+
inactiveTextStyle={styles.inactiveCustomTextStyle2}
49+
activeTextStyle={
50+
variant === 'tabs'
51+
? styles.activeTextStyleTabs2
52+
: styles.activeTextStyleSegmentedControl2
53+
}
6054
/>
6155
<MultiswitchController<'First' | 'Second' | 'Third'>
6256
options={[
@@ -66,22 +60,85 @@ export default function Align({ variant }: AlignProps) {
6660
]}
6761
defaultOption="First"
6862
variant={variant}
69-
styleProps={{
70-
align: 'right',
71-
inactiveBackgroundColor: 'rgba(205, 197, 40, 0.08)',
72-
activeBackgroundColor: 'rgb(180, 170, 20)',
73-
inactiveTextColor: 'rgb(180, 170, 20)',
74-
containerHeight: 28,
75-
itemHeight: 24,
76-
customTextStyle: {
77-
fontSize: 10,
78-
},
79-
customItemStyle: {
80-
paddingHorizontal: 8,
81-
paddingVertical: 2,
82-
},
83-
}}
63+
align="right"
64+
containerHeight={44}
65+
optionHeight={24}
66+
containerStyle={styles.containerStyle3}
67+
inactiveTextStyle={styles.inactiveCustomTextStyle3}
68+
inactiveOptionContainerStyle={styles.inactiveOptionContainerStyle3}
69+
activeOptionContainerStyle={styles.activeOptionContainerStyle3}
70+
activeTextStyle={
71+
variant === 'tabs'
72+
? styles.activeTextStyleTabs3
73+
: styles.activeTextStyleSegmentedControl3
74+
}
8475
/>
8576
</ExampleWrapper>
8677
);
8778
}
79+
80+
const styles = StyleSheet.create({
81+
// 1st example
82+
containerStyle1: {
83+
backgroundColor: 'rgb(21, 87, 21)',
84+
},
85+
activeOptionContainerStyle1: {
86+
backgroundColor: 'rgb(60, 180, 20)',
87+
},
88+
inactiveCustomTextStyle1: {
89+
color: 'rgb(208, 249, 205)',
90+
},
91+
activeTextStyleTabs1: {
92+
color: 'rgb(60, 180, 20)',
93+
},
94+
activeTextStyleSegmentedControl1: {
95+
color: '#fff',
96+
},
97+
98+
// 2nd example
99+
containerStyle2: {
100+
borderRadius: 0,
101+
backgroundColor: 'rgba(220, 38, 38, 0.08)',
102+
},
103+
inactiveCustomTextStyle2: {
104+
color: 'rgba(185, 28, 28, 0.6)',
105+
fontSize: 10,
106+
},
107+
inactiveOptionContainerStyle2: {
108+
paddingHorizontal: 8,
109+
paddingVertical: 2,
110+
borderRadius: 0,
111+
},
112+
activeOptionContainerStyle2: {
113+
borderRadius: 0,
114+
backgroundColor: 'rgb(185, 28, 28)',
115+
},
116+
activeTextStyleTabs2: {
117+
color: 'rgb(185, 28, 28)',
118+
},
119+
activeTextStyleSegmentedControl2: {
120+
color: '#fff',
121+
},
122+
123+
// 3rd example
124+
containerStyle3: {
125+
backgroundColor: 'rgba(205, 197, 40, 0.4)',
126+
},
127+
inactiveCustomTextStyle3: {
128+
color: 'rgb(180, 170, 20)',
129+
fontSize: 10,
130+
},
131+
inactiveOptionContainerStyle3: {
132+
paddingHorizontal: 8,
133+
paddingVertical: 2,
134+
},
135+
activeOptionContainerStyle3: {
136+
backgroundColor: 'rgb(180, 170, 20)',
137+
},
138+
activeTextStyleTabs3: {
139+
color: 'rgb(180, 170, 20)',
140+
},
141+
activeTextStyleSegmentedControl3: {
142+
color: '#fff',
143+
},
144+
});

example/src/examples/DayOfTime.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,9 @@ export default function DayOfTime({
3535
onPressItem={(_value) => {
3636
// Instant callback, without waiting for animation to finish
3737
}}
38-
styleProps={{
39-
inactiveBackgroundColor: 'rgba(59, 130, 246, 0.08)',
40-
activeBackgroundColor: 'rgb(37, 99, 235)',
41-
}}
38+
optionGap={10}
39+
containerStyle={styles.containerStyle}
40+
activeOptionContainerStyle={styles.activeOptionContainerStyle}
4241
/>
4342
<View style={styles.buttonsContainer}>
4443
<Button
@@ -65,4 +64,11 @@ const styles = StyleSheet.create({
6564
justifyContent: 'center',
6665
alignItems: 'center',
6766
},
67+
68+
containerStyle: {
69+
backgroundColor: 'rgba(59, 130, 246, 0.08)',
70+
},
71+
activeOptionContainerStyle: {
72+
backgroundColor: 'rgb(37, 99, 235)',
73+
},
6874
});

example/src/examples/LongLabel.tsx

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable react-native/no-inline-styles */
21
import {
32
MultiswitchController,
43
type ControllerVariant,
@@ -20,21 +19,45 @@ export default function LongLabel({ variant }: LongLabelProps) {
2019
]}
2120
variant={variant}
2221
defaultOption="First"
23-
styleProps={{
24-
containerHeight: 48,
25-
itemHeight: 36,
26-
inactiveBackgroundColor: 'rgba(30, 64, 175, 0.08)',
27-
activeBackgroundColor: 'rgb(30, 64, 175)',
28-
inactiveTextColor: 'rgb(30, 64, 175)',
29-
customTextStyle: styles.smallText,
30-
}}
22+
containerHeight={48}
23+
optionHeight={36}
24+
optionGap={16}
25+
inactiveTextStyle={styles.inactiveTextStyle}
26+
containerStyle={styles.containerStyle}
27+
activeOptionContainerStyle={
28+
variant === 'tabs'
29+
? styles.activeOptionContainerStyleTabs
30+
: styles.activeOptionContainerStyleSegmentedControl
31+
}
32+
activeTextStyle={
33+
variant === 'tabs'
34+
? styles.activeTextStyleTabs
35+
: styles.activeTextStyleSegmentedControl
36+
}
3137
/>
3238
</ExampleWrapper>
3339
);
3440
}
3541

3642
const styles = StyleSheet.create({
37-
smallText: {
43+
inactiveTextStyle: {
3844
fontSize: 12,
45+
color: 'rgb(30, 64, 175)',
46+
},
47+
containerStyle: {
48+
backgroundColor: 'rgba(30, 64, 175, 0.08)',
49+
},
50+
activeOptionContainerStyleTabs: {
51+
backgroundColor: 'rgb(30, 64, 175)',
52+
height: 10,
53+
},
54+
activeOptionContainerStyleSegmentedControl: {
55+
backgroundColor: 'rgb(30, 64, 175)',
56+
},
57+
activeTextStyleTabs: {
58+
color: 'rgb(30, 64, 175)',
59+
},
60+
activeTextStyleSegmentedControl: {
61+
color: '#fff',
3962
},
4063
});

example/src/examples/Scrollable.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable react-native/no-inline-styles */
21
import {
32
MultiswitchController,
43
type ControllerVariant,
@@ -50,10 +49,6 @@ export default function Scrollable({ variant }: ScrollableProps) {
5049
]}
5150
defaultOption="First"
5251
variant={variant}
53-
styleProps={{
54-
containerHeight: 54,
55-
itemHeight: 48,
56-
}}
5752
/>
5853
</ExampleWrapper>
5954
);

src/MultiswitchController.tsx

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,30 @@ type MultiswitchControllerProps<TValue> = {
1010
defaultOption: TValue;
1111
variant?: ControllerVariant;
1212
onChangeOption?: (value: TValue) => void;
13-
styleProps?: SwitchListStylingProps;
1413
onPressItem?: (value: TValue) => void;
1514
ref?: Ref<ControlListRef<TValue>>;
16-
};
15+
} & Partial<SwitchListStylingProps>;
1716

1817
function MultiswitchController<TValue>({
1918
options,
2019
defaultOption,
2120
variant = 'segmentedControl',
2221
onChangeOption,
2322
onPressItem,
24-
styleProps,
23+
24+
// Style props
25+
containerStyle,
26+
inactiveOptionContainerStyle,
27+
activeOptionContainerStyle,
28+
inactiveTextStyle,
29+
activeTextStyle,
30+
containerHeight = 50,
31+
containerPadding,
32+
optionGap = 0,
33+
optionHeight = 48,
34+
optionPadding = 0,
35+
align = 'center',
36+
2537
ref,
2638
}: MultiswitchControllerProps<TValue>) {
2739
const {
@@ -36,7 +48,8 @@ function MultiswitchController<TValue>({
3648
{
3749
options,
3850
defaultOption,
39-
variant,
51+
optionGap,
52+
optionPadding,
4053
},
4154
ref
4255
);
@@ -47,6 +60,9 @@ function MultiswitchController<TValue>({
4760
}
4861
}, [activeOption, onChangeOption]);
4962

63+
const containerPaddingCalculated =
64+
containerPadding ?? (containerHeight - optionHeight) / 2;
65+
5066
return (
5167
<SwitchList
5268
options={options}
@@ -59,7 +75,17 @@ function MultiswitchController<TValue>({
5975
controlListRef={controlListRef}
6076
onPressItem={onPressItem}
6177
variant={variant}
62-
{...styleProps}
78+
containerStyle={containerStyle}
79+
inactiveOptionContainerStyle={inactiveOptionContainerStyle}
80+
activeOptionContainerStyle={activeOptionContainerStyle}
81+
inactiveTextStyle={inactiveTextStyle}
82+
activeTextStyle={activeTextStyle}
83+
containerHeight={containerHeight}
84+
containerPadding={containerPaddingCalculated}
85+
optionGap={optionGap}
86+
optionHeight={optionHeight}
87+
optionPadding={optionPadding}
88+
align={align}
6389
/>
6490
);
6591
}

0 commit comments

Comments
 (0)