import React from 'react'; import { SafeAreaView, ScrollView, StatusBar, StyleSheet, Text, Image, TouchableOpacity, View, Animated, Platform, Dimensions, ViewStyle, } from 'react-native'; import { colors, onePx, px2dp } from '../../utils'; import { BjTypeSampleLineResearch } from '../../utils/types'; const WINDOW_WIDTH = Dimensions.get('window').width; interface Props { goToPage: Function activeTab: number tabs: Array, backgroundColor: string activeTextColor: string inactiveTextColor: string scrollOffset: number style: ViewStyle, tabStyle: ViewStyle tabsContainerStyle: ViewStyle textStyle: ViewStyle renderTab: Function, underlineStyle: ViewStyle onScroll: Function scrollValue?: any handleSwitchTab?: Function handleAddContainer?: Function type: 'research' | 'species' researchInstance: BjTypeSampleLineResearch justView: boolean } interface State { _leftTabUnderline: any _widthTabUnderline: any _containerWidth: number } export default class CommonTableListScrollTabBar extends React.Component { constructor(props: Props) { super(props); this.renderTab = this.renderTab.bind(this); this.updateView = this.updateView.bind(this); this.updateTabPanel = this.updateTabPanel.bind(this); this.updateTabUnderline = this.updateTabUnderline.bind(this); this.measureTab = this.measureTab.bind(this); this.onTabContainerLayout = this.onTabContainerLayout.bind(this); this.onContainerLayout = this.onContainerLayout.bind(this) }; public _tabsMeasurements = []; public _scrollView = null; public _containerMeasurements = null; public _tabContainerMeasurements = null; public readonly state: Readonly = { _leftTabUnderline: new Animated.Value(0), _widthTabUnderline: new Animated.Value(0), _containerWidth: 0, }; componentDidMount() { this.props.scrollValue.addListener(this.updateView); }; updateView(offset) { const position = Math.floor(offset.value); const pageOffset = offset.value % 1; const tabCount = this.props.tabs.length; const lastTabPosition = tabCount - 1; if (tabCount === 0 || offset.value < 0 || offset.value > lastTabPosition) { return; } if (this.necessarilyMeasurementsCompleted(position, position === lastTabPosition)) { this.updateTabPanel(position, pageOffset); this.updateTabUnderline(position, pageOffset, tabCount); } }; necessarilyMeasurementsCompleted(position, isLastTab) { return this._tabsMeasurements[position] && (isLastTab || this._tabsMeasurements[position + 1]) && this._tabContainerMeasurements && this._containerMeasurements; }; updateTabPanel(position, pageOffset) { const containerWidth = this._containerMeasurements.width; const tabWidth = this._tabsMeasurements[position].width; const nextTabMeasurements = this._tabsMeasurements[position + 1]; const nextTabWidth = nextTabMeasurements && nextTabMeasurements.width || 0; const tabOffset = this._tabsMeasurements[position].left; const absolutePageOffset = pageOffset * tabWidth; let newScrollX = tabOffset + absolutePageOffset; // 切换 newScrollX -= (containerWidth - (1 - pageOffset) * tabWidth - pageOffset * nextTabWidth) / 2; newScrollX = newScrollX >= 0 ? newScrollX : 0; if (Platform.OS === 'android') { this._scrollView.scrollTo({ x: newScrollX, y: 0, animated: false, }); } else { const rightBoundScroll = this._tabContainerMeasurements.width - (this._containerMeasurements.width); newScrollX = newScrollX > rightBoundScroll ? rightBoundScroll : newScrollX; this._scrollView.scrollTo({ x: newScrollX, y: 0, animated: false, }); } }; updateTabUnderline(position, pageOffset, tabCount) { const lineLeft = this._tabsMeasurements[position].left + 24; const lineRight = this._tabsMeasurements[position].right - 24; if (position < tabCount - 1) { const nextTabLeft = this._tabsMeasurements[position + 1].left + 24; const nextTabRight = this._tabsMeasurements[position + 1].right - 24; const newLineLeft = (pageOffset * nextTabLeft + (1 - pageOffset) * lineLeft); const newLineRight = (pageOffset * nextTabRight + (1 - pageOffset) * lineRight); this.state._leftTabUnderline.setValue(newLineLeft); this.state._widthTabUnderline.setValue(newLineRight - newLineLeft); } else { this.state._leftTabUnderline.setValue(lineLeft); this.state._widthTabUnderline.setValue(lineRight - lineLeft); } }; renderTab(name, page, isTabActive, onPressHandler, onLayoutHandler) { const { handleSwitchTab } = this.props; const fontWeight = isTabActive ? '400' : '400'; const fontSize = isTabActive ? 14 : 14; const color = isTabActive ? colors.greenTextColor : '#7F7F7F'; return { if (handleSwitchTab) { handleSwitchTab && handleSwitchTab(page); onPressHandler(page); } else { onPressHandler(page) } }} onLayout={onLayoutHandler} > { page == 0 ? 样线 : {name} } ; }; measureTab(page, event) { const { x, width, height, } = event.nativeEvent.layout; this._tabsMeasurements[page] = { left: x, right: x + width, width, height, }; this.updateView({ value: this.props.scrollValue.__getValue(), }); }; render() { const { handleAddContainer, tabs, goToPage, type = 'research', researchInstance } = this.props; // let tabList = tabs; const firstTab = tabs[0]; const isFirstTabActive = this.props.activeTab === 0; return ( goToPage(0)} > 样线 { this._scrollView = scrollView; }} horizontal={true} showsHorizontalScrollIndicator={false} showsVerticalScrollIndicator={false} directionalLockEnabled={true} bounces={false} scrollsToTop={false} style={{ width: px2dp(362), height: '100%', marginLeft: px2dp(13) }} > {this.props.tabs.map((name, page) => { const isTabActive = this.props.activeTab === page; const renderTab = this.props.renderTab || this.renderTab; return renderTab(name, page, isTabActive, this.props.goToPage, this.measureTab.bind(this, page)); })} { type == 'research' ? handleAddContainer && handleAddContainer()} activeOpacity={researchInstance.finishDate ? 0.56 : 0.72} > 添加样方 : null } ) }; componentWillReceiveProps(nextProps) { // 更新state if (JSON.stringify(this.props.tabs) !== JSON.stringify(nextProps.tabs) && this.state._containerWidth) { this.setState({ _containerWidth: null, }); } }; onTabContainerLayout(e) { this._tabContainerMeasurements = e.nativeEvent.layout; let width = this._tabContainerMeasurements.width; if (width < WINDOW_WIDTH) { width = WINDOW_WIDTH; } this.setState({ _containerWidth: width, }); this.updateView({ value: this.props.scrollValue.__getValue(), }); }; onContainerLayout(e) { this._containerMeasurements = e.nativeEvent.layout; this.updateView({ value: this.props.scrollValue.__getValue(), }); }; } const styles = StyleSheet.create({ tab: { height: px2dp(32), width: px2dp(48), alignItems: 'center', justifyContent: 'center', overflow: 'hidden', marginRight: px2dp(3), backgroundColor: '#fff', borderTopLeftRadius: px2dp(5), borderTopRightRadius: px2dp(5), borderTopColor: '#d2d2d2', borderLeftColor: '#d2d2d2', borderRightColor: '#d2d2d2', borderTopWidth: onePx, borderLeftWidth: onePx, borderRightWidth: onePx }, fixedTabButton: { position: 'absolute', left: px2dp(13), bottom: 0, height: px2dp(32), width: px2dp(48), alignItems: 'center', justifyContent: 'center', overflow: 'hidden', marginRight: px2dp(3), borderTopLeftRadius: px2dp(5), borderTopRightRadius: px2dp(5), borderTopColor: '#d2d2d2', borderLeftColor: '#d2d2d2', borderRightColor: '#d2d2d2', borderTopWidth: onePx, borderLeftWidth: onePx, borderRightWidth: onePx, backgroundColor: colors.themeColor, zIndex: 999, elevation: 3 }, fixedTabButtonContent: { width: '100%', height: '100%', justifyContent: 'center', alignItems: 'center' }, fixedTab: { backgroundColor: colors.themeColor, }, content: { height: px2dp(36), width: px2dp(375), justifyContent: 'center', alignItems: 'center', }, container: { width: px2dp(375), height: px2dp(36), borderWidth: 0, borderTopWidth: 0, borderLeftWidth: 0, borderRightWidth: 0, zIndex: 999, backgroundColor: '#f2f5f8', }, tabs: { flexDirection: 'row', justifyContent: 'flex-start', alignItems: 'flex-end', paddingRight: px2dp(13), }, addBtn: { height: px2dp(32), width: px2dp(86), flexDirection: 'row', alignItems: 'center', justifyContent: 'center', overflow: 'hidden', marginRight: px2dp(3), backgroundColor: '#fff', borderTopLeftRadius: px2dp(5), borderTopRightRadius: px2dp(5), borderTopColor: '#e2e2e2', borderLeftColor: '#e2e2e2', borderRightColor: '#e2e2e2', borderBottomColor: '#e2e2e2', borderTopWidth: onePx, borderLeftWidth: onePx, borderRightWidth: onePx, borderBottomWidth: onePx }, addIcon: { width: px2dp(12), height: px2dp(12), tintColor: '#7f7f7f', marginRight: px2dp(2) }, addBtnText: { fontSize: 14, color: '#7f7f7f' } });