Merge branch 'master' of ssh://github.com/bd-js/wcui

master
chenjiajian 2023-04-14 11:53:59 +08:00
commit 3dd9f94d7e
1 changed files with 116 additions and 16 deletions

View File

@ -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`
<h1>
父组件内容
<slot ref="label" name="label">dsds</slot>
</h1>
<div class="tabs">
<header class="header" ref="tabs" @click=${this.selectTab}>
${this.labels.map(
(it, i) =>
html`<label class="label" data-key=${it.name}>${it.label}</label>`
)}
</header>
<div class="content">
<slot></slot>
</div>
</div>
`
}
// 处理子组件的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 => {
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]')
this.$refs.label.append(
slot.cloneNode(true),
this.$refs.label.firstChild
)
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`<div class="tab">这是卡片内部</div> `
static animation = {}
static props = {
name: '',
label: '',
closable: false,
disabled: false
}
}