Skip to content

Commit bbed335

Browse files
authored
Merge pull request #12 from richardvclam/feature/disable-segment
feat: add disabled prop
2 parents 004b5b1 + f83780a commit bbed335

File tree

6 files changed

+90
-10
lines changed

6 files changed

+90
-10
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,22 @@ Color of the active content.
6363
| ------ | -------- | --------- |
6464
| string | No | `#000000` |
6565

66+
### `disabled`
67+
68+
Disable the segmented control.
69+
70+
| Type | Required | Default |
71+
| ------- | -------- | ------- |
72+
| boolean | No | false |
73+
74+
### `disabledStyle`
75+
76+
Style of the disabled segmented control. Uses the same styles as a `View` component.
77+
78+
| Type | Required | Default |
79+
| --------- | -------- | ------------------ |
80+
| ViewStyle | No | `{ opacity: 0.5 }` |
81+
6682
### `inactiveTintColor`
6783

6884
Color of the inactive content.
@@ -117,6 +133,22 @@ Element for the segment.
117133
| ------- | -------- |
118134
| Element | Yes |
119135

136+
### `disabled`
137+
138+
Disable the segment.
139+
140+
| Type | Required | Default |
141+
| ------- | -------- | ------- |
142+
| boolean | No | false |
143+
144+
### `disabledStyle`
145+
146+
Style of the disabled segment. Uses the same styles as a `View` component.
147+
148+
| Type | Required | Default |
149+
| --------- | -------- | ------------------ |
150+
| ViewStyle | No | `{ opacity: 0.5 }` |
151+
120152
### `name`
121153

122154
Unique name used to identify each segment.

__tests__/__snapshots__/SegmentedControl.test.tsx.snap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ exports[`SegmentedControl should render 1`] = `
2323
"position": "relative",
2424
},
2525
undefined,
26+
false,
27+
false,
2628
]
2729
}
2830
>
@@ -35,11 +37,14 @@ exports[`SegmentedControl should render 1`] = `
3537
"zIndex": 2,
3638
},
3739
undefined,
40+
false,
41+
false,
3842
]
3943
}
4044
>
4145
<TouchableOpacity
4246
activeOpacity={0.2}
47+
disabled={false}
4348
testID="Segment_Button"
4449
>
4550
<View

src/Segment/Segment.tsx

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ import { SegmentedContext } from '../SegmentedContext';
66
import styles from './SegmentStyles';
77

88
export interface SegmentContentProps {
9+
active: boolean;
910
activeTintColor: string;
11+
disabled: boolean;
1012
inactiveTintColor: string;
11-
active: boolean;
1213
}
1314

1415
export interface SegmentProps {
1516
activeTintColor?: string;
1617
content: React.ReactNode;
18+
disabled?: boolean;
19+
disabledStyle?: ViewStyle;
1720
inactiveTintColor?: string;
1821
name: string;
1922
style?: StyleProp<ViewStyle>;
@@ -22,6 +25,8 @@ export interface SegmentProps {
2225
export const Segment: FC<SegmentProps> = ({
2326
activeTintColor,
2427
content,
28+
disabled,
29+
disabledStyle,
2530
inactiveTintColor,
2631
name,
2732
style,
@@ -63,15 +68,26 @@ export const Segment: FC<SegmentProps> = ({
6368
}
6469

6570
if (typeof content === 'function') {
66-
return content({ activeTintColor, inactiveTintColor, active });
71+
return content({ activeTintColor, inactiveTintColor, active, disabled });
6772
}
6873

6974
return content;
7075
};
7176

7277
return (
73-
<View style={[styles.container, style as ViewStyle]}>
74-
<TouchableOpacity onPress={handlePress} testID={`Segment_Button`}>
78+
<View
79+
style={[
80+
styles.container,
81+
style as ViewStyle,
82+
disabled && styles.disabled,
83+
disabled && disabledStyle,
84+
]}
85+
>
86+
<TouchableOpacity
87+
disabled={disabled}
88+
onPress={handlePress}
89+
testID={`Segment_Button`}
90+
>
7591
<View style={styles.segment}>{renderContent()}</View>
7692
</TouchableOpacity>
7793
</View>

src/Segment/SegmentStyles.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ const styles = StyleSheet.create({
66
alignItems: 'center',
77
zIndex: 2,
88
},
9+
disabled: {
10+
opacity: 0.5,
11+
},
912
segment: {
1013
flex: 1,
1114
flexDirection: 'row',

src/SegmentedControl/SegmentedControl.tsx

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import React, { useEffect, useState } from 'react';
22
import { LayoutChangeEvent, View, ViewStyle } from 'react-native';
3-
import { PanGestureHandler, PanGestureHandlerGestureEvent } from 'react-native-gesture-handler';
3+
import {
4+
PanGestureHandler,
5+
PanGestureHandlerGestureEvent
6+
} from 'react-native-gesture-handler';
47
import Animated, { Easing } from 'react-native-reanimated';
58
import { timing } from 'react-native-redash';
69

@@ -11,11 +14,13 @@ import styles from './SegmentedControlStyles';
1114

1215
export interface SegmentedControlProps {
1316
activeTintColor?: string;
14-
inactiveTintColor?: string;
15-
initialSelectedName?: string;
1617
children:
1718
| React.ReactElement<SegmentProps>
1819
| React.ReactElement<SegmentProps>[];
20+
disabled?: boolean;
21+
disabledStyle?: ViewStyle;
22+
inactiveTintColor?: string;
23+
initialSelectedName?: string;
1924
onChangeValue?: (name: string) => void;
2025
sliderStyle?: ViewStyle;
2126
style?: ViewStyle;
@@ -24,6 +29,8 @@ export interface SegmentedControlProps {
2429
export const SegmentedControl = ({
2530
activeTintColor = '#000000',
2631
children,
32+
disabled = false,
33+
disabledStyle,
2734
inactiveTintColor = '#000000',
2835
initialSelectedName,
2936
onChangeValue,
@@ -115,6 +122,8 @@ export const SegmentedControl = ({
115122
};
116123

117124
const handleGestureEvent = (event: PanGestureHandlerGestureEvent): void => {
125+
if (disabled) return;
126+
118127
const { x } = event.nativeEvent;
119128

120129
const calculatedIndex = Math.floor((x / _width) * values.length);
@@ -126,10 +135,21 @@ export const SegmentedControl = ({
126135

127136
return (
128137
<SegmentedContext.Provider
129-
value={{ selectedName: _activeName, onChange: handleChangeValue }}
138+
value={{
139+
selectedName: _activeName,
140+
onChange: handleChangeValue,
141+
}}
130142
>
131143
<PanGestureHandler onGestureEvent={handleGestureEvent}>
132-
<View onLayout={handleLayout} style={[styles.container, style]}>
144+
<View
145+
onLayout={handleLayout}
146+
style={[
147+
styles.container,
148+
style,
149+
disabled && styles.disabledContainer,
150+
disabled && disabledStyle,
151+
]}
152+
>
133153
{typeof _activeName !== 'undefined' && (
134154
<Animated.View
135155
testID="SegmentedControl_Slider"
@@ -159,9 +179,10 @@ export const SegmentedControl = ({
159179
{{
160180
...child,
161181
props: {
162-
...child.props,
182+
disabled,
163183
inactiveTintColor,
164184
activeTintColor,
185+
...child.props,
165186
},
166187
}}
167188
</React.Fragment>

src/SegmentedControl/SegmentedControlStyles.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ const styles = StyleSheet.create({
99
height: 28,
1010
position: 'relative',
1111
},
12+
disabledContainer: {
13+
opacity: 0.5,
14+
},
1215
slider: {
1316
position: 'absolute',
1417
top: 0,

0 commit comments

Comments
 (0)