UNPKG

lulouis-vant

Version:

Lightweight Mobile UI Components built on Vue

162 lines (141 loc) 3.94 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); exports.__esModule = true; exports.default = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _utils = require("../utils"); var _raf = require("../utils/raf"); var _cell = _interopRequireDefault(require("../cell")); var _cell2 = _interopRequireDefault(require("../mixins/cell")); var _findParent = _interopRequireDefault(require("../mixins/find-parent")); var _use = (0, _utils.use)('collapse-item'), sfc = _use[0], bem = _use[1]; var CELL_SLOTS = ['title', 'icon', 'right-icon']; var _default = sfc({ mixins: [_cell2.default, _findParent.default], props: { name: [String, Number], disabled: Boolean, isLink: { type: Boolean, default: true } }, data: function data() { return { show: null, inited: null }; }, computed: { items: function items() { return this.parent.items; }, index: function index() { return this.items.indexOf(this); }, currentName: function currentName() { return (0, _utils.isDef)(this.name) ? this.name : this.index; }, expanded: function expanded() { var _this = this; if (!this.parent) { return null; } var value = this.parent.value; return this.parent.accordion ? value === this.currentName : value.some(function (name) { return name === _this.currentName; }); } }, created: function created() { this.findParent('van-collapse'); this.items.push(this); this.show = this.expanded; this.inited = this.expanded; }, destroyed: function destroyed() { this.items.splice(this.index, 1); }, watch: { expanded: function expanded(_expanded, prev) { var _this2 = this; if (prev === null) { return; } if (_expanded) { this.show = true; this.inited = true; } this.$nextTick(function () { var _this2$$refs = _this2.$refs, content = _this2$$refs.content, wrapper = _this2$$refs.wrapper; if (!content || !wrapper) { return; } var contentHeight = content.clientHeight + "px"; wrapper.style.height = _expanded ? 0 : contentHeight; (0, _raf.raf)(function () { wrapper.style.height = _expanded ? contentHeight : 0; }); }); } }, methods: { onClick: function onClick() { if (this.disabled) { return; } var parent = this.parent; var name = parent.accordion && this.currentName === parent.value ? '' : this.currentName; var expanded = !this.expanded; this.parent.switch(name, expanded); }, onTransitionEnd: function onTransitionEnd() { if (!this.expanded) { this.show = false; } else { this.$refs.wrapper.style.height = null; } } }, render: function render(h) { var _this3 = this; var Title = h(_cell.default, { "class": bem('title', { disabled: this.disabled, expanded: this.expanded }), "on": { "click": this.onClick }, "props": (0, _extends2.default)({}, this.$props) }, [this.$slots.value, CELL_SLOTS.map(function (slot) { return h('template', { slot: slot }, _this3.$slots[slot]); })]); var Content = this.inited && h("div", { "directives": [{ name: "show", value: this.show }], "ref": "wrapper", "class": bem('wrapper'), "on": { "transitionend": this.onTransitionEnd } }, [h("div", { "ref": "content", "class": bem('content') }, [this.$slots.default])]); return h("div", { "class": [bem(), { 'van-hairline--top': this.index }] }, [Title, Content]); } }); exports.default = _default;