segmented-control.vue 2.7 KB
Newer Older
郭胜强 已提交
1 2
<template>
	<view class="segmented-control" :class="styleType" :style="wrapStyle">
X
xiaoyucoding 已提交
3 4
		<view v-for="(item, index) in values" class="segmented-control-item" :class="styleType" :key="index" :style="index === currentIndex ? activeStyle : itemStyle"
		 @click="onClick(index)">
郭胜强 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
			{{item}}
		</view>
	</view>
</template>

<script>
	export default {
		name: 'segmented-control',
		props: {
			current: {
				type: Number,
				default: 0
			},
			values: {
				type: Array,
				default () {
					return [];
				}
			},
			activeColor: {
				type: String,
				default: '#007aff'
			},
			styleType: {
				type: String,
				default: 'button'
			}
		},
X
xiaoyucoding 已提交
33 34 35 36 37 38 39 40 41 42 43 44
		data() {
			return {
				currentIndex: this.current
			}
		},
		watch: {
			current(val) {
				if (val !== this.currentIndex) {
					this.currentIndex = val;
				}
			}
		},
郭胜强 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
		computed: {
			wrapStyle() {
				let styleString = '';
				switch (this.styleType) {
					case 'text':
						styleString = `border:0;`;
						break;
					default:
						styleString = `border-color: ${this.activeColor}`;
						break;
				}
				return styleString;
			},
			itemStyle() {
				let styleString = '';
				switch (this.styleType) {
					case 'text':
						styleString = `color:#000;border-left:0;`;
						break;
					default:
						styleString = `color:${this.activeColor};border-color:${this.activeColor};`;
						break;
				}
				return styleString;
			},
			activeStyle() {
				let styleString = '';
				switch (this.styleType) {
					case 'text':
						styleString = `color:${this.activeColor};border-left:0;border-bottom-style:solid;`;
						break;
					default:
						styleString = `color:#fff;border-color:${this.activeColor};background-color:${this.activeColor}`;
						break;
				}
				return styleString;
			}
		},
		methods: {
			onClick(index) {
X
xiaoyucoding 已提交
85 86
				if (this.currentIndex !== index) {
					this.currentIndex = index;
郭胜强 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
					this.$emit('clickItem', index);
				}
			}
		},
	}
</script>

<style>
	.segmented-control {
		display: flex;
		flex-direction: row;
		justify-content: center;
		width: 100%;
		font-size: 30upx;
		border-radius: 12upx;
		box-sizing: border-box;
		margin: 0 auto;
		overflow: hidden;
	}
蓝色的小猫咪's avatar
蓝色的小猫咪 已提交
106
	
郭胜强 已提交
107 108 109
	.segmented-control.button {
		border: 1upx solid;
	}
蓝色的小猫咪's avatar
蓝色的小猫咪 已提交
110
	
郭胜强 已提交
111 112
	.segmented-control.text {
		border: 0;
X
xiaoyucoding 已提交
113
		border-radius: 0upx;
郭胜强 已提交
114
	}
蓝色的小猫咪's avatar
蓝色的小猫咪 已提交
115
	
郭胜强 已提交
116 117 118 119 120 121
	.segmented-control-item {
		flex: 1;
		text-align: center;
		line-height: 70upx;
		box-sizing: border-box;
	}
蓝色的小猫咪's avatar
蓝色的小猫咪 已提交
122
	
郭胜强 已提交
123 124 125
	.segmented-control-item.button {
		border-left: 1upx solid;
	}
蓝色的小猫咪's avatar
蓝色的小猫咪 已提交
126
	
郭胜强 已提交
127 128 129
	.segmented-control-item.text {
		border-left: 0;
	}
蓝色的小猫咪's avatar
蓝色的小猫咪 已提交
130
	
郭胜强 已提交
131 132 133
	.segmented-control-item:first-child {
		border-left-width: 0;
	}
蓝色的小猫咪's avatar
蓝色的小猫咪 已提交
134
</style>