uni-collapse-item.vue 3.1 KB
Newer Older
M
mehaotian 已提交
1 2
<template>
	<view class="uni-collapse-item">
3 4
		<view class="uni-collapse-item__title" @click="openCollapse(!this.is_open)">
			<text class="uni-collapse-item__title-text" :class="{'is-disabled':disabled,'open--active':is_open}">{{title}}</text>
M
mehaotian 已提交
5 6 7 8 9 10 11 12 13 14
			<view class="down_arrow" :class="{'down_arrow--active': is_open}"></view>
		</view>
		<view ref="boxRef" class="uni-collapse-item__content">
			<view ref="contentRef" class="uni-collapse-item__content-box">
				<slot></slot>
			</view>
		</view>
	</view>
</template>

15
<script lang="uts">
M
mehaotian 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
	import { $dispatch } from './util.uts'
	export default {
		name: "UniCollapseItem",
		props: {
			// 列表标题
			title: {
				type: String,
				default: ''
			},
			open: {
				type: Boolean,
				default: false
			},
			disabled: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				height: 0,
				is_open: this.open as boolean,
DCloud-yyl's avatar
DCloud-yyl 已提交
38 39
				boxNode: null as Element | null,
				contentNode: null as Element | null,
M
mehaotian 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53
			};
		},
		watch: {
			open(value: boolean) {
				// this.is_open = value
				if (this.boxNode != null) {
					this.openCollapse(value)
				}
			}
		},
		created() {
			$dispatch(this, 'UniCollapse', 'init', this)
		},
		mounted() {
DCloud-yyl's avatar
DCloud-yyl 已提交
54 55
			this.boxNode = this.$refs['boxRef'] as Element;
			this.contentNode = this.$refs['contentRef'] as Element;
M
mehaotian 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69
			// this.openCollapse(this.open)
		},
		methods: {
			// 开启或关闭折叠面板 
			openCollapse(open: boolean) {
				if (this.disabled) return
				// 关闭其他已打开
				$dispatch(this, 'UniCollapse', 'cloceAll')
				this.is_open = open
				this.oepnOrClose(open)
			},
			oepnOrClose(open: boolean) {
				const boxNode = this.boxNode?.style!;
				const contentNode = this.contentNode?.style!;
M
mehaotian 已提交
70 71
				let hide = open ? 'flex' : 'none';
				const opacity = open ? 1 : 0
M
mehaotian 已提交
72 73 74
				let ani_transform = open ? 'translateY(0)' : 'translateY(-100%)';
				boxNode.setProperty('display', hide);
				this.$nextTick(() => {
M
mehaotian 已提交
75 76
					contentNode.setProperty('transform', ani_transform);
					contentNode.setProperty('opacity', opacity);
M
mehaotian 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
				})
			}
		}
	}
</script>

<style scoped>
	.uni-collapse-item {
		background-color: #fff;
	}
	.uni-collapse-item__title {
		flex-direction: row;
		align-items: center;
		padding: 12px;
		background-color: #fff;
	}

94
	
M
mehaotian 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

	.down_arrow {
		width: 8px;
		height: 8px;
		transform: rotate(45deg);
		border-right: 1px #999 solid;
		border-bottom: 1px #999 solid;
		margin-top: -3px;
		transition-property: transform;
		transition-duration: 0.2s;
	}

	.down_arrow--active {
		transform: rotate(-135deg);
		margin-top: 0px;
	}

	.uni-collapse-item__title-text {
		flex: 1;
		color: #000;
		font-size: 14px;
		font-weight: 400;
	}
118 119 120 121 122 123
	
	.open--active {
		/* background-color: #f0f0f0; */
		color: #bbb;
	}
	
M
mehaotian 已提交
124 125 126 127 128 129 130 131 132 133 134
	.is-disabled {
		color: #999;
	}

	.uni-collapse-item__content {
		display: none;
		position: relative;
	}

	.uni-collapse-item__content-box {
		width: 100%;
135 136
		transition-property: transform , opacity;
		transition-duration: 0.2s;
M
mehaotian 已提交
137 138
		transform: translateY(-100%);
		opacity: 0;
M
mehaotian 已提交
139 140
	}
</style>