Skip to content

Commit 244f8c8

Browse files
committed
COMPONENT
新增组件 ClVerticalTab、ClVerticalTabCell
1 parent da6ad0e commit 244f8c8

File tree

10 files changed

+491
-224
lines changed

10 files changed

+491
-224
lines changed

.idea/workspace.xml

+294-220
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

@types/index.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ export { default as ClScreenDrawer } from './screenDrawer';
2929
export { default as ClFlex } from './flex';
3030
export { default as ClGrid } from './grid';
3131
export { default as ClAnimation } from './animation';
32+
export {default as ClVerticalTab} from './verticalTab';
33+
export {default as CLVerticalTabCell} from './verticalTabCell';

@types/searchBar.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ export interface IProps {
3333
*
3434
* 默认值 `button`
3535
*
36-
* 可选参数 `button`, `text`, `none`, `list`
36+
* 可选参数 `button`, `text`, `none`
3737
*/
38-
searchType?: 'button' | 'text' | 'none' | 'list';
38+
searchType?: 'button' | 'text' | 'none';
3939
/**
4040
* 左边的图标组,可选类型请查看 Icon-iconName
4141
*/

@types/verticalTab.d.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {ComponentClass} from 'react';
2+
3+
export interface IProps {
4+
/**
5+
* 高度
6+
*/
7+
height: number;
8+
/**
9+
* 标签页
10+
*/
11+
tabs: {
12+
name: string;
13+
id: string;
14+
}[];
15+
/**
16+
* 当前激活的标签页 Id
17+
*/
18+
current?: string;
19+
children?: any;
20+
}
21+
22+
declare const VerticalTab: ComponentClass<IProps>;
23+
24+
export default VerticalTab;

@types/verticalTabCell.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {ComponentClass} from "react";
2+
3+
export interface IProps {
4+
children: any;
5+
}
6+
7+
declare const VerticalTabCell: ComponentClass<IProps>
8+
export default VerticalTabCell

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"name": "mp-colorui",
3-
"version": "0.1.7",
3+
"version": "0.2.0",
44
"description": "MP ColorUI 是一款基于 Taro 框架并且联合 Color-UI CSS 库开发的多端 UI 组件库(目前仅支持小程序端)",
55
"main": "dist/index.js",
66
"main:h5": "dist/h5/index.js",
77
"types": "./@types/index.d.ts",
8-
"github": "https://github.com/yinLiangDream/mp-colorui",
8+
"repository": "https://github.com/yinLiangDream/mp-colorui",
99
"homepage": "https://yinliangdream.github.io/mp-colorui-doc/#/",
1010
"files": [
1111
"dist",

src/components/verticalTab/index.scss

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
@import "../../style/vars";
2+
3+
.VerticalNav {
4+
&.nav {
5+
width: 200px;
6+
white-space: initial;
7+
8+
.cu-item {
9+
width: 100%;
10+
text-align: center;
11+
background-color: $white;
12+
margin: 0;
13+
border: none;
14+
height: 100px;
15+
position: relative;
16+
display: flex;
17+
justify-content: center;
18+
align-items: center;
19+
flex-wrap: wrap;
20+
21+
&.cur {
22+
background-color: #f1f1f1;
23+
color: $blue;
24+
25+
&::after {
26+
content: "";
27+
width: 8px;
28+
height: 30px;
29+
border-radius: 10px 0 0 10px;
30+
position: absolute;
31+
background-color: $blue;
32+
top: 0;
33+
right: 0;
34+
bottom: 0;
35+
margin: auto;
36+
}
37+
38+
}
39+
}
40+
}
41+
}

src/components/verticalTab/index.tsx

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Taro from '@tarojs/taro'
2+
import ClCard from "../../card";
3+
4+
import {IProps} from '../../../../@types/verticalTabCell'
5+
6+
export default function ClVerticalTabCell(props: IProps) {
7+
return (
8+
<ClCard>
9+
{this.props.children}
10+
</ClCard>
11+
)
12+
}
13+
14+
ClVerticalTabCell.options = {
15+
addGlobalClass: true
16+
}

src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,7 @@ export { default as ClImagePicker } from './components/imagePicker';
3434
export { default as ClFlex } from './components/flex';
3535
export { default as ClGrid } from './components/grid';
3636
export { default as ClAnimation } from './components/animation';
37+
export { default as ClVerticalTab } from './components/verticalTab'
38+
export { default as ClVerticalTabCell } from './components/verticalTab/verticalTabCell'
3739

3840
Taro.initPxTransform({ designWidth: 750, deviceRatio: {} });

0 commit comments

Comments
 (0)