123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <template>
- <view :class="'van-collapse-item custom-class ' + (index !== 0 ? 'van-hairline--top' : '')">
- <van-cell
- :title="title"
- title-class="title-class"
- :icon="icon"
- :value="value"
- :label="label"
- :is-link="isLink"
- :clickable="clickable"
- :border="border && expanded"
- :class="utils.bem('collapse-item__title', { disabled, expanded })"
- right-icon-class="van-cell__right-icon"
- custom-class="van-cell"
- hover-class="van-cell--hover"
- @click="onClick"
- >
- <slot name="title" slot="title" />
- <slot name="icon" slot="icon" />
- <slot name="value" />
- <slot name="right-icon" slot="right-icon" />
- </van-cell>
- <view :class="utils.bem('collapse-item__wrapper', { transition })" :style="'height: ' + contentHeight + ';'" @transitionend="onTransitionEnd">
- <view class="van-collapse-item__content content-class">
- <slot />
- </view>
- </view>
- </view>
- </template>
- <script module="utils" lang="wxs" src="@/miniprogram_npm/@vant/weapp/wxs/utils.wxs"></script>
- <script>
- 'use strict';
- Object.defineProperty(exports, '__esModule', {
- value: true
- });
- var component_1 = require('../common/component');
- var nextTick = function () {
- return new Promise(function (resolve) {
- return setTimeout(resolve, 20);
- });
- };
- component_1.VantComponent({
- classes: ['title-class', 'content-class'],
- relation: {
- name: 'collapse',
- type: 'ancestor',
- current: 'collapse-item'
- },
- props: {
- name: null,
- title: null,
- value: null,
- icon: String,
- label: String,
- disabled: Boolean,
- clickable: Boolean,
- border: {
- type: Boolean,
- value: true
- },
- isLink: {
- type: Boolean,
- value: true
- }
- },
- data: {
- contentHeight: 0,
- expanded: false,
- transition: false
- },
- mounted: function () {
- var that = this;
- this.updateExpanded()
- .then(nextTick)
- .then(function () {
- var data = {
- transition: true
- };
- if (that.expanded) {
- data.contentHeight = 'auto';
- }
- that.setData(data);
- });
- },
- methods: {
- updateExpanded: function () {
- if (!this.parent) {
- return Promise.resolve();
- }
- var _a = this.parent.data;
- var value = _a.value;
- var accordion = _a.accordion;
- var _b = this.parent.children;
- var children = _b === void 0 ? [] : _b;
- var name = this.name;
- var index = children.indexOf(this);
- var currentName = name == null ? index : name;
- var expanded = accordion
- ? value === currentName
- : (value || []).some(function (name) {
- return name === currentName;
- });
- var stack = [];
- if (expanded !== this.expanded) {
- stack.push(this.updateStyle(expanded));
- }
- stack.push(
- this.set({
- index: index,
- expanded: expanded
- })
- );
- return Promise.all(stack);
- },
- updateStyle: function (expanded) {
- var that = this;
- return this.getRect('.van-collapse-item__content')
- .then(function (rect) {
- return rect.height;
- })
- .then(function (height) {
- if (expanded) {
- return that.set({
- contentHeight: height ? height + 'px' : 'auto'
- });
- }
- return that
- .set({
- contentHeight: height + 'px'
- })
- .then(nextTick)
- .then(function () {
- return that.set({
- contentHeight: 0
- });
- });
- });
- },
- onClick: function () {
- if (this.disabled) {
- return;
- }
- var _a = this;
- var name = _a.name;
- var expanded = _a.expanded;
- var index = this.parent.children.indexOf(this);
- var currentName = name == null ? index : name;
- this.parent.switch(currentName, !expanded);
- },
- onTransitionEnd: function () {
- if (this.expanded) {
- this.setData({
- contentHeight: 'auto'
- });
- }
- }
- }
- });
- </script>
- <style>
- @import './index.css';
- </style>
|