From 3a646f6627eba4e1373a7740bcbbd98cf2ee96d8 Mon Sep 17 00:00:00 2001 From: chenjiajian <770230504@qq.com> Date: Mon, 27 Mar 2023 15:12:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8A=98=E5=8F=A0=E9=9D=A2?= =?UTF-8?q?=E6=9D=BF=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/collapse/index.js | 161 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 src/collapse/index.js diff --git a/src/collapse/index.js b/src/collapse/index.js new file mode 100644 index 0000000..38cda0c --- /dev/null +++ b/src/collapse/index.js @@ -0,0 +1,161 @@ +/** + * {} + * @author chensbox + * @date 2023/03/26 16:14:10 + */ + +import { css, html, Component } from '@bd/core' +import '../icon/index.js' + +class Collapse extends Component { + static props = { + value: { + type: String, + default: '', + attribute: false, + observer(newVal, oldVal) { + if (newVal !== oldVal && this.$children) { + this.updateView() + } + } + }, + accordion: false + } + static styles = [ + css` + :host { + display: block; + border-top: 1px solid #ebeef5; + } + ` + ] + updateView() { + const { accordion, value } = this + if (accordion) { + this.$children.forEach(item => { + item.show = item.name === value + }) + } else { + this.$children.forEach(item => { + item.show = value.includes(item.name) + }) + } + } + updateValue(name) { + let { accordion, value } = this + if (accordion) { + this.value = value === name ? '' : name + } else { + value = value.split(',') + !value[0] && value.shift() + if (value.includes(name)) { + value = value.filter(item => item !== name) + } else { + value.push(name) + } + this.value = value + } + this.$emit('change') + } + mounted() { + this.$children = Array.from(this.children) + this.updateView() + } +} +class CollapseItem extends Component { + static props = { + name: '', + title: '', + disabled: false + } + _show = false + static styles = [ + css` + :host { + display: block; + border-bottom: 1px solid #ebeef5; + } + .disabled { + cursor: not-allowed; + opacity: 0.6; + } + .title { + display: flex; + height: 48px; + align-items: center; + justify-content: space-between; + background-color: #fff; + // user-select: none; + color: #303133; + cursor: pointer; + font-size: 13px; + font-weight: 500; + + wc-icon { + margin: 0 8px; + --size: 12px; + color: #636465; + transform: rotate(0); + transition: transform 0.2s ease-in-out; + } + } + .wrapper { + overflow: hidden; + height: 0; + transition: height 0.2s linear; + .content { + padding-bottom: 20px; + font-size: 13px; + color: #303133; + line-height: 1.5; + } + } + ` + ] + + onClick() { + if (this.disabled) { + return + } + this.parentNode.updateValue(this.name) + } + get show() { + return this._show + } + set show(val) { + const { content, wrapper, icon } = this.$refs + if (!wrapper) { + this.onMounted = () => (this.show = val) + return + } + if (val) { + wrapper.style.height = content.offsetHeight + 'px' + icon.style.transform = 'rotate(90deg)' + } else { + wrapper.style.height = 0 + icon.style.transform = 'rotate(0deg)' + } + this._show = val + } + mounted() { + this.onMounted && this.onMounted() + } + render() { + return html` +
+
+ ${this.title ? this.title : html``} + +
+
+
+ +
+
+
+ ` + } +} + +Collapse.reg('collapse') +CollapseItem.reg('collapse-item')