import * as React from 'react'; import { I18nManager, Image, Text, View, StyleSheet, LayoutChangeEvent, } from 'react-native'; import TouchableItem from '../TouchableItem'; import defaultBackImage from '../assets/back-icon.png'; import { HeaderBackbuttonProps } from '../../types'; type Props = HeaderBackbuttonProps & { LabelContainerComponent: React.ComponentType; ButtonContainerComponent: React.ComponentType; }; type State = { initialTextWidth?: number; }; class ModularHeaderBackButton extends React.PureComponent { static defaultProps = { tintColor: '#037aff', truncatedTitle: 'Back', }; state: State = {}; private onTextLayout = (e: LayoutChangeEvent) => { if (this.state.initialTextWidth) { return; } this.setState({ initialTextWidth: e.nativeEvent.layout.x + e.nativeEvent.layout.width, }); }; private renderBackImage() { const { backImage, backTitleVisible, tintColor } = this.props; if (React.isValidElement(backImage)) { return backImage; } else if (backImage) { const BackImage = backImage; return ; } else { return ( ); } } private getTitleText = () => { const { width, title, truncatedTitle } = this.props; let { initialTextWidth } = this.state; if (title === null) { return null; } else if (!title) { return truncatedTitle; } else if (initialTextWidth && width && initialTextWidth > width) { return truncatedTitle; } else { return title.length > 8 ? truncatedTitle : title; } }; private maybeRenderTitle() { const { backTitleVisible, titleStyle, tintColor } = this.props; let backTitleText = this.getTitleText(); if (!backTitleVisible || backTitleText === null) { return null; } const { LabelContainerComponent } = this.props; return ( {this.getTitleText()} ); } render() { const { onPress, title } = this.props; const { ButtonContainerComponent } = this.props; return ( {this.renderBackImage()} {this.maybeRenderTitle()} ); } } const styles = StyleSheet.create({ container: { alignItems: 'center', flexDirection: 'row', backgroundColor: 'transparent', marginBottom: 1, overflow: 'visible', }, title: { fontSize: 17, paddingRight: 10, }, icon: { height: 21, width: 12, marginLeft: 9, marginRight: 22, marginVertical: 12, resizeMode: 'contain', transform: [{ scaleX: I18nManager.isRTL ? -1 : 1 }], }, iconWithTitle: { marginRight: 3, }, }); export default ModularHeaderBackButton;