|
| 1 | +import React from 'react'; |
| 2 | +import {StyleSheet, Text, View} from 'react-native'; |
| 3 | +import {NavigationService} from '../../services/navigation-service/navigationService'; |
| 4 | +import {ColorDefault} from '../../themes/colors'; |
| 5 | +import {TouchableOpacity} from 'react-native-gesture-handler'; |
| 6 | +import {IIconTypes} from './Header.props'; |
| 7 | +import {Ionicons} from '@expo/vector-icons'; |
| 8 | + |
| 9 | +type HeaderProps = { |
| 10 | + leftIcon?: IIconTypes; |
| 11 | + onPressLeft?: () => void; |
| 12 | + rightIcon?: IIconTypes; |
| 13 | + onPressRight?: () => void; |
| 14 | + title?: string; |
| 15 | + backEnabled?: boolean; |
| 16 | + subRightIcon?: IIconTypes; |
| 17 | + onPressSubRight?: () => void; |
| 18 | + children?: React.ReactNode; |
| 19 | +}; |
| 20 | + |
| 21 | +export const Header = (props: HeaderProps) => { |
| 22 | + const { |
| 23 | + leftIcon, |
| 24 | + rightIcon, |
| 25 | + title, |
| 26 | + onPressLeft, |
| 27 | + onPressRight, |
| 28 | + backEnabled, |
| 29 | + subRightIcon, |
| 30 | + onPressSubRight, |
| 31 | + children, |
| 32 | + } = props; |
| 33 | + |
| 34 | + const iconLeft = leftIcon || (backEnabled ? 'chevron-back-sharp' : undefined); |
| 35 | + |
| 36 | + const onLeftPress = |
| 37 | + onPressLeft || backEnabled ? NavigationService.goBack : undefined; |
| 38 | + |
| 39 | + return ( |
| 40 | + <View |
| 41 | + style={[styles.header, {backgroundColor: ColorDefault.navigationBar}]}> |
| 42 | + <TouchableOpacity |
| 43 | + disabled={!iconLeft} |
| 44 | + onPress={onLeftPress} |
| 45 | + style={styles.leftButton}> |
| 46 | + {!!iconLeft && ( |
| 47 | + <Ionicons |
| 48 | + name={iconLeft} |
| 49 | + size={32} |
| 50 | + style={{color: ColorDefault.primary}} |
| 51 | + /> |
| 52 | + )} |
| 53 | + </TouchableOpacity> |
| 54 | + {!!subRightIcon && <View style={styles.leftButton} />} |
| 55 | + |
| 56 | + {/* Title */} |
| 57 | + {children || ( |
| 58 | + <Text style={[styles.titleText, {color: ColorDefault.primary}]}> |
| 59 | + {title} |
| 60 | + </Text> |
| 61 | + )} |
| 62 | + {!!subRightIcon && ( |
| 63 | + <TouchableOpacity |
| 64 | + disabled={!rightIcon} |
| 65 | + onPress={onPressSubRight} |
| 66 | + style={styles.subRightButton}> |
| 67 | + {!!subRightIcon && ( |
| 68 | + <Ionicons |
| 69 | + name={subRightIcon} |
| 70 | + size={32} |
| 71 | + style={{color: ColorDefault.primary}} |
| 72 | + /> |
| 73 | + )} |
| 74 | + </TouchableOpacity> |
| 75 | + )} |
| 76 | + {/* Button right */} |
| 77 | + <TouchableOpacity |
| 78 | + disabled={!rightIcon} |
| 79 | + onPress={onPressRight} |
| 80 | + style={styles.rightButton}> |
| 81 | + {!!rightIcon && ( |
| 82 | + <Ionicons |
| 83 | + name={rightIcon} |
| 84 | + size={32} |
| 85 | + style={{color: ColorDefault.primary}} |
| 86 | + /> |
| 87 | + )} |
| 88 | + </TouchableOpacity> |
| 89 | + </View> |
| 90 | + ); |
| 91 | +}; |
| 92 | + |
| 93 | +const styles = StyleSheet.create({ |
| 94 | + header: { |
| 95 | + height: 50, |
| 96 | + flexDirection: 'row', |
| 97 | + alignItems: 'center', |
| 98 | + }, |
| 99 | + leftButton: { |
| 100 | + width: 50, |
| 101 | + alignItems: 'center', |
| 102 | + }, |
| 103 | + rightButton: { |
| 104 | + width: 50, |
| 105 | + alignItems: 'center', |
| 106 | + }, |
| 107 | + subRightButton: { |
| 108 | + width: 50, |
| 109 | + alignItems: 'center', |
| 110 | + }, |
| 111 | + titleText: { |
| 112 | + flex: 1, |
| 113 | + textAlign: 'center', |
| 114 | + }, |
| 115 | + searchContainer: { |
| 116 | + height: 30, |
| 117 | + flex: 1, |
| 118 | + borderRadius: 5, |
| 119 | + paddingHorizontal: 5, |
| 120 | + flexDirection: 'row', |
| 121 | + alignItems: 'center', |
| 122 | + backgroundColor: '#FFF', |
| 123 | + marginRight: 8, |
| 124 | + }, |
| 125 | +}); |
0 commit comments