Skip to content
This repository was archived by the owner on Oct 27, 2021. It is now read-only.

Commit fc9f2c8

Browse files
author
pengshanglong
committed
feat: add SegmentedControl
1 parent 0cf733f commit fc9f2c8

File tree

5 files changed

+128
-1
lines changed

5 files changed

+128
-1
lines changed

src/components/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ import FloatLayout from './float-layout/index'
66
import Accordion from './accordion/index'
77
import NavBar from './nav-bar/index'
88
import TabBar from './tab-bar/index'
9+
import SegmentedControl from './segmented-control/index'
910

10-
export { Grid, List, ListItem, Card, FloatLayout, Accordion, NavBar, TabBar }
11+
export { Grid, List, ListItem, Card, FloatLayout, Accordion, NavBar, TabBar, SegmentedControl }
+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import classNames from 'classnames'
2+
import { pxTransform } from '../../utils/common'
3+
4+
export default {
5+
name: 'SegmentedControl',
6+
props: {
7+
customStyle: {
8+
type: [Object, String],
9+
default: '',
10+
},
11+
className: {
12+
type: [Array, String],
13+
default: '',
14+
},
15+
current: {
16+
type: Number,
17+
default: 0,
18+
},
19+
color: {
20+
type: String,
21+
default: '',
22+
},
23+
fontSize: {
24+
type: [Number, String],
25+
default: 0,
26+
},
27+
disabled: {
28+
type: Boolean,
29+
default: false,
30+
},
31+
selectedColor: {
32+
type: String,
33+
default: '',
34+
},
35+
values: {
36+
type: Array,
37+
default: () => [],
38+
},
39+
onClick: {
40+
type: Function,
41+
default: () => () => {},
42+
},
43+
},
44+
methods: {
45+
/**
46+
*
47+
* @param {number} index
48+
* @param {event} event
49+
*/
50+
handleClick(index, event) {
51+
if (this.disabled) return
52+
this.onClick(index, event)
53+
},
54+
},
55+
render() {
56+
const {
57+
customStyle,
58+
className,
59+
disabled,
60+
values,
61+
selectedColor,
62+
current,
63+
color,
64+
fontSize,
65+
} = this
66+
67+
const rootStyle = {
68+
borderColor: selectedColor,
69+
}
70+
const itemStyle = {
71+
color: selectedColor,
72+
fontSize: pxTransform(fontSize),
73+
borderColor: selectedColor,
74+
backgroundColor: color,
75+
}
76+
const selectedItemStyle = {
77+
color,
78+
fontSize: pxTransform(fontSize),
79+
borderColor: selectedColor,
80+
backgroundColor: selectedColor,
81+
}
82+
const rootCls = classNames(
83+
'at-segmented-control',
84+
{
85+
'at-segmented-control--disabled': disabled,
86+
},
87+
className
88+
)
89+
90+
return (
91+
<view class={rootCls} style={this.$mergeStyle(rootStyle, customStyle)}>
92+
{values.map((value, i) => (
93+
<view
94+
class={classNames('at-segmented-control__item', {
95+
'at-segmented-control__item--active': current === i,
96+
})}
97+
style={current === i ? selectedItemStyle : itemStyle}
98+
key={value}
99+
onTap={this.handleClick.bind(this, i)}>
100+
{value}
101+
</view>
102+
))}
103+
</view>
104+
)
105+
},
106+
}

src/pages/index/index.vue

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
<view
33
class="index"
44
>
5+
<SegmentedControl
6+
:on-click="segmentedClick"
7+
selected-color="#FF4949"
8+
font-size="30"
9+
:current="segmentedIndex"
10+
:values="segmentedList"
11+
/>
512
<TabBar
613
:tab-list="tabList"
714
:on-click="onTabClick"

src/pages/index/indexMixins.ts

+7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
Accordion,
3131
NavBar,
3232
TabBar,
33+
SegmentedControl,
3334
} from '../../components/index'
3435

3536
export default {
@@ -65,6 +66,7 @@ export default {
6566
Accordion,
6667
NavBar,
6768
TabBar,
69+
SegmentedControl,
6870
},
6971
data() {
7072
return {
@@ -124,6 +126,8 @@ export default {
124126
{ title: '拍照', iconType: 'camera' },
125127
{ title: '文件夹', iconType: 'folder', text: '100', max: 99 },
126128
],
129+
segmentedIndex: 0,
130+
segmentedList: ['标签页1', '标签页2', '标签页3'],
127131
}
128132
},
129133
methods: {
@@ -148,5 +152,8 @@ export default {
148152
onTabClick(i, e) {
149153
this.tabIndex = i
150154
},
155+
segmentedClick(i, e) {
156+
this.segmentedIndex = i
157+
},
151158
},
152159
}

src/utils/common.ts

+6
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ function handleTouchScroll(flag: any): void {
132132
}
133133
}
134134

135+
function pxTransform(size: number): string {
136+
if (!size) return ''
137+
return Taro.pxTransform(size)
138+
}
139+
135140
export {
136141
getEnvs,
137142
delayGetScrollOffset,
@@ -140,4 +145,5 @@ export {
140145
uuid,
141146
isTest,
142147
handleTouchScroll,
148+
pxTransform,
143149
}

0 commit comments

Comments
 (0)