|
| 1 | +import React from 'react' |
| 2 | +import { View, ViewPropTypes, TouchableOpacity, StyleSheet, Text } from 'react-native' |
| 3 | +import PropTypes from 'prop-types' |
| 4 | + |
| 5 | +const handleTabPress = (index, selectedIndex, onTabPress) => { |
| 6 | + selectedIndex !== index && onTabPress(index) |
| 7 | +} |
| 8 | + |
| 9 | +const TabOption = ({ |
| 10 | + isTabActive, index, badge, text, |
| 11 | + firstTabStyle, lastTabStyle, |
| 12 | + tabStyle, activeTabStyle, |
| 13 | + tabTextStyle, activeTabTextStyle, |
| 14 | + tabBadgeContainerStyle, activeTabBadgeContainerStyle, |
| 15 | + tabBadgeStyle, activeTabBadgeStyle, |
| 16 | + onTabPress, textNumberOfLines, |
| 17 | + allowFontScaling, |
| 18 | + accessible, |
| 19 | + accessibilityLabel, |
| 20 | +}) => { |
| 21 | + return ( |
| 22 | + <TouchableOpacity style={[ |
| 23 | + styles.tabStyle, |
| 24 | + tabStyle, |
| 25 | + isTabActive ? [styles.activeTabStyle, activeTabStyle] : {}, |
| 26 | + firstTabStyle, |
| 27 | + lastTabStyle]} |
| 28 | + accessible={accessible} |
| 29 | + accessibilityLabel={accessibilityLabel} |
| 30 | + accessibilityTraits={isTabActive ? "selected" : "button"} |
| 31 | + accessibilityComponentType={"button"} |
| 32 | + onPress={() => onTabPress(index)} |
| 33 | + activeOpacity={1}> |
| 34 | + <View style={{ flexDirection: "row" }}> |
| 35 | + <Text style={[ |
| 36 | + styles.tabTextStyle, |
| 37 | + tabTextStyle, |
| 38 | + isTabActive ? [styles.activeTabTextStyle, activeTabTextStyle] : {}]} |
| 39 | + numberOfLines={textNumberOfLines} |
| 40 | + allowFontScaling={allowFontScaling} |
| 41 | + ellipsizeMode="tail"> |
| 42 | + {text} |
| 43 | + </Text> |
| 44 | + { |
| 45 | + badge ? |
| 46 | + <View style={[ |
| 47 | + styles.tabBadgeContainerStyle, |
| 48 | + tabBadgeContainerStyle, |
| 49 | + isTabActive ? [styles.activeTabBadgeContainerStyle, activeTabBadgeContainerStyle] : {}]}> |
| 50 | + <Text style={[ |
| 51 | + styles.tabBadgeStyle, |
| 52 | + tabBadgeStyle, |
| 53 | + isTabActive ? [styles.activeTabBadgeStyle, activeTabBadgeStyle] : {}]} |
| 54 | + allowFontScaling={allowFontScaling}> |
| 55 | + {badge} |
| 56 | + </Text> |
| 57 | + </View> : false |
| 58 | + } |
| 59 | + </View> |
| 60 | + </TouchableOpacity> |
| 61 | + ) |
| 62 | +} |
| 63 | + |
| 64 | +const getAccessibilityLabelByIndex = (accessibilityLabels, index) => { |
| 65 | + return accessibilityLabels && accessibilityLabels.length > 0 && accessibilityLabels[index] ? accessibilityLabels[index] : undefined |
| 66 | +} |
| 67 | + |
| 68 | +const SegmentedControlTab = ({ |
| 69 | + selectedIndex, selectedIndices, values, |
| 70 | + badges, borderRadius, tabsContainerStyle, |
| 71 | + tabStyle, activeTabStyle, |
| 72 | + tabTextStyle, activeTabTextStyle, |
| 73 | + tabBadgeContainerStyle, activeTabBadgeContainerStyle, |
| 74 | + tabBadgeStyle, activeTabBadgeStyle, |
| 75 | + onTabPress, textNumberOfLines, |
| 76 | + allowFontScaling, |
| 77 | + accessible, |
| 78 | + accessibilityLabels, |
| 79 | +}) => { |
| 80 | + |
| 81 | + const firstTabStyle = [{ borderRightWidth: values.length == 2 ? 1 : 0, borderTopLeftRadius: borderRadius, borderBottomLeftRadius: borderRadius }] |
| 82 | + const lastTabStyle = [{ borderLeftWidth: 0, borderTopRightRadius: borderRadius, borderBottomRightRadius: borderRadius }] |
| 83 | + |
| 84 | + return ( |
| 85 | + <View |
| 86 | + style={[styles.tabsContainerStyle, tabsContainerStyle]} |
| 87 | + removeClippedSubviews={false}> |
| 88 | + { |
| 89 | + values.map((item, index) => { |
| 90 | + const accessibilityText = getAccessibilityLabelByIndex(accessibilityLabels, index) |
| 91 | + return ( |
| 92 | + <TabOption |
| 93 | + key={index} |
| 94 | + index={index} |
| 95 | + badge={badges && badges[index] ? badges[index] : false} |
| 96 | + isTabActive={selectedIndex === index} |
| 97 | + text={item} |
| 98 | + textNumberOfLines={textNumberOfLines} |
| 99 | + onTabPress={(index) => handleTabPress(index, selectedIndex, onTabPress)} |
| 100 | + firstTabStyle={index === 0 ? [{ borderRightWidth: 0 }, firstTabStyle] : {}} |
| 101 | + lastTabStyle={index === values.length - 1 ? [{ borderLeftWidth: 0 }, lastTabStyle] : {}} |
| 102 | + tabStyle={[tabStyle, index !== 0 && index !== values.length - 1 ? { marginLeft: -1 } : {}]} |
| 103 | + activeTabStyle={activeTabStyle} |
| 104 | + tabTextStyle={tabTextStyle} |
| 105 | + activeTabTextStyle={activeTabTextStyle} |
| 106 | + tabBadgeContainerStyle={tabBadgeContainerStyle} |
| 107 | + activeTabBadgeContainerStyle={activeTabBadgeContainerStyle} |
| 108 | + tabBadgeStyle={tabBadgeStyle} |
| 109 | + activeTabBadgeStyle={activeTabBadgeStyle} |
| 110 | + allowFontScaling={allowFontScaling} |
| 111 | + accessible={accessible} |
| 112 | + accessibilityLabel={accessibilityText ? accessibilityText : item } |
| 113 | + /> |
| 114 | + ) |
| 115 | + }) |
| 116 | + } |
| 117 | + </View> |
| 118 | + ) |
| 119 | +} |
| 120 | + |
| 121 | +SegmentedControlTab.propTypes = { |
| 122 | + accessibilityLabels: PropTypes.arrayOf(PropTypes.string), |
| 123 | + accessible: PropTypes.bool, |
| 124 | + activeTabBadgeContainerStyle: Text.propTypes.style, |
| 125 | + activeTabBadgeStyle: Text.propTypes.style, |
| 126 | + activeTabStyle: ViewPropTypes.style, |
| 127 | + activeTabTextStyle: Text.propTypes.style, |
| 128 | + allowFontScaling: PropTypes.bool, |
| 129 | + badges: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.number, PropTypes.string])), |
| 130 | + borderRadius: PropTypes.number, |
| 131 | + onTabPress: PropTypes.func, |
| 132 | + selectedIndex: PropTypes.number, |
| 133 | + selectedIndices: PropTypes.arrayOf(PropTypes.number), |
| 134 | + tabBadgeContainerStyle: Text.propTypes.style, |
| 135 | + tabBadgeStyle: Text.propTypes.style, |
| 136 | + tabStyle: ViewPropTypes.style, |
| 137 | + tabTextStyle: Text.propTypes.style, |
| 138 | + tabsContainerStyle: ViewPropTypes.style, |
| 139 | + textNumberOfLines: PropTypes.number, |
| 140 | + values: PropTypes.arrayOf(PropTypes.string), |
| 141 | +} |
| 142 | + |
| 143 | +TabOption.propTypes = { |
| 144 | + accessibilityLabel: PropTypes.string, |
| 145 | + accessible: PropTypes.bool, |
| 146 | + activeTabBadgeContainerStyle: Text.propTypes.style, |
| 147 | + activeTabBadgeStyle: Text.propTypes.style, |
| 148 | + activeTabStyle: ViewPropTypes.style, |
| 149 | + activeTabTextStyle: Text.propTypes.style, |
| 150 | + allowFontScaling: PropTypes.bool, |
| 151 | + badge: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), |
| 152 | + firstTabStyle: PropTypes.arrayOf(PropTypes.object), |
| 153 | + index: PropTypes.number, |
| 154 | + isTabActive: PropTypes.bool, |
| 155 | + lastTabStyle: PropTypes.arrayOf(PropTypes.object), |
| 156 | + onTabPress: PropTypes.func, |
| 157 | + tabBadgeContainerStyle: Text.propTypes.style, |
| 158 | + tabBadgeStyle: Text.propTypes.style, |
| 159 | + tabStyle: ViewPropTypes.style, |
| 160 | + tabTextStyle: Text.propTypes.style, |
| 161 | + tabsContainerStyle: ViewPropTypes.style, |
| 162 | + text: PropTypes.string, |
| 163 | + textNumberOfLines: PropTypes.number, |
| 164 | +} |
| 165 | + |
| 166 | +SegmentedControlTab.defaultProps = { |
| 167 | + values: ['One', 'Two', 'Three'], |
| 168 | + accessible: true, |
| 169 | + accessibilityLabels: [], |
| 170 | + badges: ['', '', ''], |
| 171 | + selectedIndex: 0, |
| 172 | + selectedIndices: [0], |
| 173 | + onTabPress: () => { }, |
| 174 | + tabsContainerStyle: {}, |
| 175 | + tabStyle: {}, |
| 176 | + activeTabStyle: {}, |
| 177 | + tabTextStyle: {}, |
| 178 | + activeTabTextStyle: {}, |
| 179 | + tabBadgeContainerStyle: {}, |
| 180 | + activeTabBadgeContainerStyle: {}, |
| 181 | + tabBadgeStyle: {}, |
| 182 | + activeTabBadgeStyle: {}, |
| 183 | + borderRadius: 5, |
| 184 | + textNumberOfLines: 1, |
| 185 | + allowFontScaling: true, |
| 186 | +} |
| 187 | + |
| 188 | +const styles = StyleSheet.create({ |
| 189 | + tabsContainerStyle: { |
| 190 | + backgroundColor: 'transparent', |
| 191 | + flexDirection: 'row', |
| 192 | + }, |
| 193 | + tabStyle: { |
| 194 | + paddingVertical: 5, |
| 195 | + flex: 1, |
| 196 | + justifyContent: 'center', |
| 197 | + alignItems: 'center', |
| 198 | + borderColor: '#0076FF', |
| 199 | + borderWidth: 1, |
| 200 | + backgroundColor: 'white', |
| 201 | + }, |
| 202 | + activeTabStyle: { |
| 203 | + backgroundColor: '#0076FF' |
| 204 | + }, |
| 205 | + tabTextStyle: { |
| 206 | + color: '#0076FF' |
| 207 | + }, |
| 208 | + activeTabTextStyle: { |
| 209 | + color: 'white' |
| 210 | + }, |
| 211 | + tabBadgeContainerStyle: { |
| 212 | + borderRadius: 20, |
| 213 | + backgroundColor: 'red', |
| 214 | + paddingLeft: 5, |
| 215 | + paddingRight: 5, |
| 216 | + marginLeft: 5, |
| 217 | + marginBottom: 3 |
| 218 | + }, |
| 219 | + activeTabBadgeContainerStyle: { |
| 220 | + backgroundColor: 'white' |
| 221 | + }, |
| 222 | + tabBadgeStyle: { |
| 223 | + color: 'white', |
| 224 | + fontSize: 11, |
| 225 | + fontWeight: 'bold' |
| 226 | + }, |
| 227 | + activeTabBadgeStyle: { |
| 228 | + color: 'black' |
| 229 | + } |
| 230 | +}) |
| 231 | + |
| 232 | +export default SegmentedControlTab |
0 commit comments