|
| 1 | +/* @flow */ |
| 2 | +import React, { PureComponent } from 'react'; |
| 3 | +import { View, StyleSheet, TouchableWithoutFeedback } from 'react-native'; |
| 4 | + |
| 5 | +import Icon from './Icon'; |
| 6 | +import { Caption2 } from './Typography'; |
| 7 | +import { withTheme } from '../'; |
| 8 | +import type { Theme } from '../types/Theme'; |
| 9 | + |
| 10 | +type TabItem = { |
| 11 | + icon: string, |
| 12 | + title: string, |
| 13 | + onPress: (tabIndex: number) => void, |
| 14 | + isActive: boolean, |
| 15 | + disabled?: boolean, |
| 16 | +}; |
| 17 | + |
| 18 | +type Props = { |
| 19 | + /** |
| 20 | + * Array of Tabs. Each TabItem needs to have below shape: |
| 21 | + * icon: Icon name, one of https://github.com/oblador/react-native-vector-icons/blob/master/glyphmaps/Ionicons.json |
| 22 | + * title: string, |
| 23 | + * onPress: function to be called when Tab is tapped |
| 24 | + * isActive: boolean, indicating wheter Tab is active |
| 25 | + * disabled?: boolean, (optional), diasables a Tab |
| 26 | + */ |
| 27 | + tabs: Array<TabItem>, |
| 28 | + /** |
| 29 | + * Provided by the ThemeProvider |
| 30 | + */ |
| 31 | + theme: Theme, |
| 32 | +}; |
| 33 | + |
| 34 | +class TabBar extends PureComponent<Props> { |
| 35 | + render() { |
| 36 | + const { theme, tabs } = this.props; |
| 37 | + const tabBarStyle = { |
| 38 | + backgroundColor: theme.barColor, |
| 39 | + borderTopColor: theme.borderColor, |
| 40 | + }; |
| 41 | + const activeColor = theme.buttonColor; |
| 42 | + const inactiveColor = theme.buttonDisabledColor; |
| 43 | + return ( |
| 44 | + <View style={[styles.wrapper, tabBarStyle]}> |
| 45 | + {tabs.map((tab, idx) => ( |
| 46 | + <TouchableWithoutFeedback |
| 47 | + key={`tabItem_${idx}`} |
| 48 | + onPress={() => tab.onPress(idx)} |
| 49 | + disabled={tab.disabled || tab.isActive} |
| 50 | + > |
| 51 | + <View style={styles.tabItem}> |
| 52 | + <Icon |
| 53 | + name={tab.icon} |
| 54 | + size={30} |
| 55 | + color={tab.isActive ? activeColor : inactiveColor} |
| 56 | + /> |
| 57 | + <Caption2 |
| 58 | + style={{ |
| 59 | + color: tab.isActive ? activeColor : inactiveColor, |
| 60 | + }} |
| 61 | + > |
| 62 | + {tab.title} |
| 63 | + </Caption2> |
| 64 | + </View> |
| 65 | + </TouchableWithoutFeedback> |
| 66 | + ))} |
| 67 | + </View> |
| 68 | + ); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +export default withTheme(TabBar); |
| 73 | + |
| 74 | +const styles = StyleSheet.create({ |
| 75 | + wrapper: { |
| 76 | + position: 'absolute', |
| 77 | + bottom: 0, |
| 78 | + left: 0, |
| 79 | + right: 0, |
| 80 | + flexDirection: 'row', |
| 81 | + borderTopWidth: 1, |
| 82 | + }, |
| 83 | + tabItem: { |
| 84 | + flexGrow: 1, |
| 85 | + justifyContent: 'center', |
| 86 | + alignItems: 'center', |
| 87 | + paddingVertical: 4, |
| 88 | + }, |
| 89 | +}); |
0 commit comments