From f79c18464299b12307b4a72bfa709e74b12389ef Mon Sep 17 00:00:00 2001 From: yutent Date: Thu, 13 Apr 2023 19:11:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0tabs=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/tabs/index.js | 132 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 116 insertions(+), 16 deletions(-) diff --git a/src/tabs/index.js b/src/tabs/index.js index 244be71..c1a0415 100644 --- a/src/tabs/index.js +++ b/src/tabs/index.js @@ -4,39 +4,139 @@ * @date 2023/03/06 15:17:25 */ -import { css, html, bind, Component } from '@bd/core' +import { css, html, bind, Component, nextTick } from '@bd/core' class Tabs extends Component { + static props = { + tab: { type: String, attribute: false }, + type: 'default', + 'tab-position': 'top', + labels: [] + } + + static styles = [ + css` + :host { + display: flex; + } + + :host([tab-position='top']), + :host([tab-position='bottom']) { + flex-direction: column; + } + :host([tab-position='left']), + :host([tab-position='right']) { + } + `, + + css` + .header { + user-select: none; + } + ` + ] + + #cache = {} + render() { return html` -

- 父组件内容 - dsds -

- +
+
+ ${this.labels.map( + (it, i) => + html`` + )} +
+
+ +
+
` } + // 处理子组件的slot,穿透到父组件来 + #parseSlot() { + this.labels.forEach((it, i) => { + let tab = this.$refs.tabs.children[i] + if (tab.lastElementChild) { + tab.replaceChild(it.el, tab.lastElementChild) + } else { + tab.append(it.el) + } + delete it.el + }) + } + + selectTab(ev) { + let elem = ev.target + let key + if (elem === ev.currentTarget) { + return + } + + while (elem.tagName !== 'LABEL') { + elem = elem.parentNode + } + + key = elem.dataset.key + + if (key === this.tab) { + return + } + + this.#cache[this.tab].tab + .$animate(true) + .then(_ => this.#cache[key].tab.$animate()) + + this.tab = key + } + created() { - console.log(this.root) bind(this.root, 'slotchange', ev => { let children = ev.target.assignedNodes() - children.forEach(it => { - let slot = it.querySelector('[slot=label]') - this.$refs.label.append( - slot.cloneNode(true), - this.$refs.label.firstChild - ) - slot.remove() + this.labels = children.map((it, i) => { + let tmp = { + label: it.label, + name: it.name || i, + el: null, + tab: null + } + this.#cache[tmp.name] = tmp + return tmp }) + + children.forEach((it, i) => { + let slot = it.querySelector('[slot=label]') + if (slot) { + this.labels[i].el = slot.cloneNode(true) + slot.remove() + } + this.labels[i].tab = it + }) + + nextTick(_ => this.#parseSlot()) + + if (!this.tab) { + this.tab = this.labels[0].name + this.labels[0].tab.$animate() + } }) } + + mounted() { + // + } } class Tab extends Component { - render() { - return html`
这是卡片内部
` + static animation = {} + + static props = { + name: '', + label: '', + closable: false, + disabled: false } }