table-checkbox.vue 3.5 KB
Newer Older
1
<template>
雪洛's avatar
雪洛 已提交
2
	<view class="uni-table-checkbox" @click="selected">
3 4 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 33 34 35 36 37 38
		<view v-if="!indeterminate" class="checkbox__inner" :class="{'is-checked':isChecked,'is-disable':isDisabled}">
			<view class="checkbox__inner-icon"></view>
		</view>
		<view v-else class="checkbox__inner checkbox--indeterminate">
			<view class="checkbox__inner-icon"></view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'TableCheckbox',
		emits:['checkboxSelected'],
		props: {
			indeterminate: {
				type: Boolean,
				default: false
			},
			checked: {
				type: [Boolean,String],
				default: false
			},
			disabled: {
				type: Boolean,
				default: false
			},
			index: {
				type: Number,
				default: -1
			},
			cellData: {
				type: Object,
				default () {
					return {}
				}
			}
雪洛's avatar
雪洛 已提交
39 40 41 42 43 44 45 46 47 48 49 50
		},
		watch:{
			checked(newVal){
				if(typeof this.checked === 'boolean'){
					this.isChecked = newVal
				}else{
					this.isChecked = true
				}
			},
			indeterminate(newVal){
				this.isIndeterminate = newVal
			}
51 52 53 54
		},
		data() {
			return {
				isChecked: false,
雪洛's avatar
雪洛 已提交
55
				isDisabled: false,
56 57 58
				isIndeterminate:false
			}
		},
雪洛's avatar
雪洛 已提交
59 60 61
		created() {
			if(typeof this.checked === 'boolean'){
				this.isChecked = this.checked
62 63 64 65 66
			}
			this.isDisabled = this.disabled
		},
		methods: {
			selected() {
雪洛's avatar
雪洛 已提交
67
				if (this.isDisabled) return
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
				this.isIndeterminate = false
				this.isChecked = !this.isChecked
				this.$emit('checkboxSelected', {
					checked: this.isChecked,
					data: this.cellData
				})
			}
		}
	}
</script>

<style lang="scss">
	$checked-color: #007aff;
	$border-color: #DCDFE6;
	$disable:0.4;

	.uni-table-checkbox {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		position: relative;
		margin: 5px 0;
		cursor: pointer;

		// 多选样式
		.checkbox__inner {
			/* #ifndef APP-NVUE */
			flex-shrink: 0;
			box-sizing: border-box;
			/* #endif */
			position: relative;
			width: 16px;
			height: 16px;
			border: 1px solid $border-color;
			border-radius: 2px;
			background-color: #fff;
			z-index: 1;

			.checkbox__inner-icon {
				position: absolute;
				/* #ifdef APP-NVUE */
				top: 2px;
				/* #endif */
				/* #ifndef APP-NVUE */
				top: 2px;
				/* #endif */
				left: 5px;
				height: 7px;
				width: 3px;
				border: 1px solid #fff;
				border-left: 0;
				border-top: 0;
				opacity: 0;
				transform-origin: center;
雪洛's avatar
雪洛 已提交
123
				transform: rotate(45deg);
124 125 126
				box-sizing: content-box;
			}

雪洛's avatar
雪洛 已提交
127 128
			&.checkbox--indeterminate {
				border-color: $checked-color;
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
				background-color: $checked-color;

				.checkbox__inner-icon {
					position: absolute;
					opacity: 1;
					transform: rotate(0deg);
					height: 2px;
					top: 0;
					bottom: 0;
					margin: auto;
					left: 0px;
					right: 0px;
					bottom: 0;
					width: auto;
					border: none;
雪洛's avatar
雪洛 已提交
144
					border-radius: 2px;
145 146 147 148
					transform: scale(0.5);
					background-color: #fff;
				}
			}
雪洛's avatar
雪洛 已提交
149 150
			&:hover{
				border-color: $checked-color;
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
			}
			// 禁用
			&.is-disable {
				/* #ifdef H5 */
				cursor: not-allowed;
				/* #endif */
				background-color: #F2F6FC;
				border-color: $border-color;
			}

			// 选中
			&.is-checked {
				border-color: $checked-color;
				background-color: $checked-color;

				.checkbox__inner-icon {
					opacity: 1;
					transform: rotate(45deg);
				}

				// 选中禁用
				&.is-disable {
					opacity: $disable;
				}
			}
			
		}
	}
雪洛's avatar
雪洛 已提交
179
</style>