Skip to content
Merged
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
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ Color of the active content.
| ------ | -------- | --------- |
| string | No | `#000000` |

### `disabled`

Disable the segmented control.

| Type | Required | Default |
| ------- | -------- | ------- |
| boolean | No | false |

### `disabledStyle`

Style of the disabled segmented control. Uses the same styles as a `View` component.

| Type | Required | Default |
| --------- | -------- | ------------------ |
| ViewStyle | No | `{ opacity: 0.5 }` |

### `inactiveTintColor`

Color of the inactive content.
Expand Down Expand Up @@ -117,6 +133,22 @@ Element for the segment.
| ------- | -------- |
| Element | Yes |

### `disabled`

Disable the segment.

| Type | Required | Default |
| ------- | -------- | ------- |
| boolean | No | false |

### `disabledStyle`

Style of the disabled segment. Uses the same styles as a `View` component.

| Type | Required | Default |
| --------- | -------- | ------------------ |
| ViewStyle | No | `{ opacity: 0.5 }` |

### `name`

Unique name used to identify each segment.
Expand Down
5 changes: 5 additions & 0 deletions __tests__/__snapshots__/SegmentedControl.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ exports[`SegmentedControl should render 1`] = `
"position": "relative",
},
undefined,
false,
false,
]
}
>
Expand All @@ -35,11 +37,14 @@ exports[`SegmentedControl should render 1`] = `
"zIndex": 2,
},
undefined,
false,
false,
]
}
>
<TouchableOpacity
activeOpacity={0.2}
disabled={false}
testID="Segment_Button"
>
<View
Expand Down
24 changes: 20 additions & 4 deletions src/Segment/Segment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ import { SegmentedContext } from '../SegmentedContext';
import styles from './SegmentStyles';

export interface SegmentContentProps {
active: boolean;
activeTintColor: string;
disabled: boolean;
inactiveTintColor: string;
active: boolean;
}

export interface SegmentProps {
activeTintColor?: string;
content: React.ReactNode;
disabled?: boolean;
disabledStyle?: ViewStyle;
inactiveTintColor?: string;
name: string;
style?: StyleProp<ViewStyle>;
Expand All @@ -22,6 +25,8 @@ export interface SegmentProps {
export const Segment: FC<SegmentProps> = ({
activeTintColor,
content,
disabled,
disabledStyle,
inactiveTintColor,
name,
style,
Expand Down Expand Up @@ -63,15 +68,26 @@ export const Segment: FC<SegmentProps> = ({
}

if (typeof content === 'function') {
return content({ activeTintColor, inactiveTintColor, active });
return content({ activeTintColor, inactiveTintColor, active, disabled });
}

return content;
};

return (
<View style={[styles.container, style as ViewStyle]}>
<TouchableOpacity onPress={handlePress} testID={`Segment_Button`}>
<View
style={[
styles.container,
style as ViewStyle,
disabled && styles.disabled,
disabled && disabledStyle,
]}
>
<TouchableOpacity
disabled={disabled}
onPress={handlePress}
testID={`Segment_Button`}
>
<View style={styles.segment}>{renderContent()}</View>
</TouchableOpacity>
</View>
Expand Down
3 changes: 3 additions & 0 deletions src/Segment/SegmentStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const styles = StyleSheet.create({
alignItems: 'center',
zIndex: 2,
},
disabled: {
opacity: 0.5,
},
segment: {
flex: 1,
flexDirection: 'row',
Expand Down
33 changes: 27 additions & 6 deletions src/SegmentedControl/SegmentedControl.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import React, { useEffect, useState } from 'react';
import { LayoutChangeEvent, View, ViewStyle } from 'react-native';
import { PanGestureHandler, PanGestureHandlerGestureEvent } from 'react-native-gesture-handler';
import {
PanGestureHandler,
PanGestureHandlerGestureEvent
} from 'react-native-gesture-handler';
import Animated, { Easing } from 'react-native-reanimated';
import { timing } from 'react-native-redash';

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

export interface SegmentedControlProps {
activeTintColor?: string;
inactiveTintColor?: string;
initialSelectedName?: string;
children:
| React.ReactElement<SegmentProps>
| React.ReactElement<SegmentProps>[];
disabled?: boolean;
disabledStyle?: ViewStyle;
inactiveTintColor?: string;
initialSelectedName?: string;
onChangeValue?: (name: string) => void;
sliderStyle?: ViewStyle;
style?: ViewStyle;
Expand All @@ -24,6 +29,8 @@ export interface SegmentedControlProps {
export const SegmentedControl = ({
activeTintColor = '#000000',
children,
disabled = false,
disabledStyle,
inactiveTintColor = '#000000',
initialSelectedName,
onChangeValue,
Expand Down Expand Up @@ -115,6 +122,8 @@ export const SegmentedControl = ({
};

const handleGestureEvent = (event: PanGestureHandlerGestureEvent): void => {
if (disabled) return;

const { x } = event.nativeEvent;

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

return (
<SegmentedContext.Provider
value={{ selectedName: _activeName, onChange: handleChangeValue }}
value={{
selectedName: _activeName,
onChange: handleChangeValue,
}}
>
<PanGestureHandler onGestureEvent={handleGestureEvent}>
<View onLayout={handleLayout} style={[styles.container, style]}>
<View
onLayout={handleLayout}
style={[
styles.container,
style,
disabled && styles.disabledContainer,
disabled && disabledStyle,
]}
>
{typeof _activeName !== 'undefined' && (
<Animated.View
testID="SegmentedControl_Slider"
Expand Down Expand Up @@ -159,9 +179,10 @@ export const SegmentedControl = ({
{{
...child,
props: {
...child.props,
disabled,
inactiveTintColor,
activeTintColor,
...child.props,
},
}}
</React.Fragment>
Expand Down
3 changes: 3 additions & 0 deletions src/SegmentedControl/SegmentedControlStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const styles = StyleSheet.create({
height: 28,
position: 'relative',
},
disabledContainer: {
opacity: 0.5,
},
slider: {
position: 'absolute',
top: 0,
Expand Down