import React from 'react' import { Animated, Pressable, StyleSheet, TextStyle, useWindowDimensions, View, ViewStyle, } from 'react-native' import { CONTROL_HEIGHT, IS_IOS } from './helpers' import { ControlProps } from './types' export type Props = ControlProps & { containerStyle?: ViewStyle tabStyle?: ViewStyle indicatorStyle?: ViewStyle pressColor?: string pressOpacity?: number labelStyle?: TextStyle inactiveOpacity?: number } /** * Default android control. * * Example usage: * * ```tsx * import { * Segmented, * MaterialTabBar * } from 'react-native-collapsible-segmented-view * * ... * * } * > * ... * ``` */ export const MaterialTabBar: React.FC = ({ setIndex, labels, floatIndex, containerStyle, tabStyle, indicatorStyle, pressColor = 'DDDDDD', pressOpacity = IS_IOS ? 0.2 : 1, labelStyle, inactiveOpacity = 0.7, }) => { const windowWidth = useWindowDimensions().width const onTabPress = React.useCallback( (nextIndex: number) => { setIndex(nextIndex) }, [setIndex] ) const [translateX, setTraslateX] = React.useState( floatIndex.interpolate({ inputRange: labels.map((_, i) => i), outputRange: labels.map((_, i) => (windowWidth / labels.length) * i), }) ) React.useEffect(() => { setTraslateX( floatIndex.interpolate({ inputRange: labels.map((_, i) => i), outputRange: labels.map((_, i) => (windowWidth / labels.length) * i), }) ) }, [floatIndex, labels, windowWidth]) return ( {labels.map((label, index) => { return ( onTabPress(index)} android_ripple={{ borderless: true, color: pressColor, }} style={({ pressed }) => [ { opacity: pressed ? pressOpacity : 1 }, styles.tab, tabStyle, ]} > i), outputRange: labels.map((_, i) => i === index ? 1 : inactiveOpacity ), }), }, labelStyle, ]} > {label.toUpperCase()} ) })} ) } const styles = StyleSheet.create({ container: { flexDirection: 'row', }, tab: { flex: 1, alignItems: 'center', justifyContent: 'center', paddingHorizontal: 16, height: CONTROL_HEIGHT, }, label: { color: 'black', }, indicator: { position: 'absolute', bottom: 0, height: 2, backgroundColor: '#2196f3', }, })