import { tag, h, WeElement, extractClass } from 'omi' import * as css from './index.scss' import { MDCTabBar } from '@material/tab-bar' import '../icon' import { domReady } from '../util/dom-ready' //@ts-ignore import '../theme.ts' // @ts-ignore import { extract, htmlToVdom } from '../util.ts' interface Props { defaultActive: string, width: string, align: 'start' | 'end' | 'center', // 三种可能值 start | end | center useMinWidth: boolean, stacked: boolean } interface Data { } @tag('m-tab') export default class Tab extends WeElement{ static css = css data = { active: null, tabBar: null } static propTypes = { defaultActive: String, width: String, align: String, useMinWidth: Boolean, stacked: Boolean } installed() { domReady(() => { //update first this.update() //init mdc tab this.data.tabBar = new MDCTabBar(this.shadowRoot.querySelector('.mdc-tab-bar')); this.data.tabBar.listen('MDCTabBar:activated', (e) => { let item = this.props.children[e.detail.index] this.fire('change', item.attributes) }) }) } uninstall() { this.data.tabBar.destroy() } activateTab(value) { // @ts-ignore let index = [].findIndex(this.props.children, (item => item.attributes.value === value)) this.data.tabBar.activateTab(index) } renderButton(vnode, activeProp) { const { attributes: props } = vnode //todo fix this? if (props['svg-icon']) { props.svgIcon = JSON.parse(props['svg-icon'].replace(/(['"])?([a-zA-Z0-9_]+)(['"])?:([^\/])/g, '"$2":$4').replace(/'([\s\S]*?)'/g, '"$1"')) } const isActive = activeProp === props.value return ( ) } render(props) { const { defaultActive, width, align } = props if (this.innerHTML && !props.children) { props.children = htmlToVdom(this.innerHTML) } const { children } = props const style = { width: width || '100%' } const alignClass = align && 'mdc-tab-scroller--align-' + align const scrollerClasses = extractClass(props, 'mdc-tab-scroller', alignClass) return (
{children && children.map(vnode => this.renderButton(vnode, defaultActive))}
) } }