|
| 1 | +import Taro, {pxTransform, useState} from '@tarojs/taro' |
| 2 | +import {ScrollView, View} from "@tarojs/components"; |
| 3 | +import {IProps} from '../../../@types/verticalTab' |
| 4 | + |
| 5 | +import './index.scss' |
| 6 | + |
| 7 | +export default function ClVerticalTab(props: IProps) { |
| 8 | + let scrollTab = false; |
| 9 | + let id = ''; |
| 10 | + const currentId = props.current ? props.current : props.tabs[0].id; |
| 11 | + const tabs: { |
| 12 | + name: string; |
| 13 | + id: string; |
| 14 | + top: number; |
| 15 | + bottom: number; |
| 16 | + }[] = props.tabs || []; |
| 17 | + const [current, setCurrent] = useState(currentId); |
| 18 | + const [verticalNavTop, setVerticalNavTop] = useState(tabs.findIndex(item => item.name === props.tabs[0].name)); |
| 19 | + const [scrollId, setScrollId] = useState(currentId); |
| 20 | + const [scrollContent, setScrollContent] = useState(0); |
| 21 | + const onScroll = (e) => { |
| 22 | + let tabHeight = 0; |
| 23 | + for (let i = 0; i < tabs.length; i++) { |
| 24 | + const query = Taro.createSelectorQuery() |
| 25 | + const view = query.select('#' + tabs[i].id); |
| 26 | + view.fields({ |
| 27 | + size: true |
| 28 | + }, (data: any) => { |
| 29 | + tabs[i].top = tabHeight; |
| 30 | + tabHeight = tabHeight + data.height; |
| 31 | + tabs[i].bottom = tabHeight; |
| 32 | + }).exec(); |
| 33 | + } |
| 34 | + let scrollTop = e.detail.scrollTop + 20; |
| 35 | + if (!scrollTab) { |
| 36 | + for (let i = 0; i < tabs.length; i++) { |
| 37 | + if (scrollTop > tabs[i].top && scrollTop < tabs[i].bottom) { |
| 38 | + setVerticalNavTop(i); |
| 39 | + setCurrent(tabs[i].id); |
| 40 | + return false |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + const tabsComponent = tabs.map((item, index) => ( |
| 46 | + <View key={index} className={`cu-item ${current === item.id ? 'cur' : ''}`} onClick={() => { |
| 47 | + id = item.id; |
| 48 | + scrollTab = true; |
| 49 | + changeTop(id); |
| 50 | + setCurrent(item.id); |
| 51 | + setVerticalNavTop(tabs.findIndex(tab => tab.name === item.name)); |
| 52 | + setScrollId(item.id); |
| 53 | + }} |
| 54 | + >{item.name}</View> |
| 55 | + )); |
| 56 | + const changeTop = (id) => { |
| 57 | + const query = Taro.createSelectorQuery() |
| 58 | + const view = query.select('#' + id); |
| 59 | + const topView = query.select('#' + tabs[0].id); |
| 60 | + let top = 0; |
| 61 | + topView.fields({ |
| 62 | + rect: true |
| 63 | + }, (data: any) => { |
| 64 | + top = data.top; |
| 65 | + }) |
| 66 | + view.fields({ |
| 67 | + rect: true |
| 68 | + }, (data: any) => { |
| 69 | + setTimeout(() => { |
| 70 | + const endTop = Math.abs(top - data.top); |
| 71 | + setScrollContent(endTop); |
| 72 | + scrollTab = false; |
| 73 | + }, 300) |
| 74 | + |
| 75 | + }).exec(); |
| 76 | + }; |
| 77 | + return ( |
| 78 | + <View className='flex'> |
| 79 | + <ScrollView scrollY scrollWithAnimation className='VerticalNav nav' style={{height: pxTransform(props.height)}} |
| 80 | + scrollTop={(verticalNavTop - 1) * 50}> |
| 81 | + {tabsComponent} |
| 82 | + </ScrollView> |
| 83 | + <ScrollView scrollY scrollWithAnimation style={{height: pxTransform(props.height)}} scrollIntoView={scrollId} |
| 84 | + onScroll={onScroll} scrollTop={scrollContent}> |
| 85 | + {this.props.children} |
| 86 | + </ScrollView> |
| 87 | + </View> |
| 88 | + |
| 89 | + ) |
| 90 | +} |
| 91 | + |
| 92 | +ClVerticalTab.options = { |
| 93 | + addGlobalClass: true |
| 94 | +} |
| 95 | + |
| 96 | +ClVerticalTab.defaultProps = { |
| 97 | + tabs: [{name: '', id: ''}], |
| 98 | + height: 0, |
| 99 | + current: '' |
| 100 | +} as IProps |
0 commit comments