|
| 1 | +import { JsxChildren } from 'dom-renderer'; |
| 2 | +import { observable } from 'mobx'; |
| 3 | +import { |
| 4 | + WebCell, |
| 5 | + attribute, |
| 6 | + component, |
| 7 | + observer, |
| 8 | + on, |
| 9 | + reaction |
| 10 | +} from 'web-cell'; |
| 11 | +import { CustomElement } from 'web-utility'; |
| 12 | + |
| 13 | +import { Nav, NavLink } from './Nav'; |
| 14 | + |
| 15 | +export interface TabProps { |
| 16 | + caption: JsxChildren; |
| 17 | +} |
| 18 | + |
| 19 | +export interface Tab extends WebCell<TabProps> {} |
| 20 | + |
| 21 | +@component({ tagName: 'tab-pane' }) |
| 22 | +export class Tab extends HTMLElement implements WebCell<TabProps> { |
| 23 | + caption: JsxChildren; |
| 24 | + |
| 25 | + connectedCallback() { |
| 26 | + this.classList.add('tab-pane', 'fade'); |
| 27 | + this.role = 'tabpanel'; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +@component({ |
| 32 | + tagName: 'tabs-box', |
| 33 | + mode: 'open' |
| 34 | +}) |
| 35 | +@observer |
| 36 | +export class Tabs extends HTMLElement implements CustomElement { |
| 37 | + @observable |
| 38 | + accessor tabMeta: TabProps[] = []; |
| 39 | + |
| 40 | + @attribute |
| 41 | + @observable |
| 42 | + accessor currentIndex = 0; |
| 43 | + |
| 44 | + connectedCallback() { |
| 45 | + this.turnPaneTo(this.currentIndex); |
| 46 | + } |
| 47 | + |
| 48 | + @on('slotchange', 'slot') |
| 49 | + handleSlotChange(_: Event, slot: HTMLSlotElement) { |
| 50 | + const tabs = slot.assignedElements() as Tab[]; |
| 51 | + |
| 52 | + if (this.tabMeta.length !== tabs.length) |
| 53 | + this.tabMeta = tabs.map(({ caption }) => ({ caption })); |
| 54 | + } |
| 55 | + |
| 56 | + @on('click', '.nav-tabs > .nav-link') |
| 57 | + handleTabClick( |
| 58 | + event: MouseEvent, |
| 59 | + { dataset: { index } }: HTMLAnchorElement |
| 60 | + ) { |
| 61 | + event.preventDefault(); |
| 62 | + event.stopPropagation(); |
| 63 | + |
| 64 | + this.currentIndex = +index; |
| 65 | + } |
| 66 | + |
| 67 | + @reaction(({ currentIndex }) => currentIndex) |
| 68 | + turnPaneTo(index: number) { |
| 69 | + const previous = this.querySelector<Tab>('tab-pane.active'); |
| 70 | + |
| 71 | + if (previous) { |
| 72 | + previous.hidden = true; |
| 73 | + previous.classList.remove('active', 'show'); |
| 74 | + } |
| 75 | + const next = this.children[index] as Tab; |
| 76 | + |
| 77 | + next.hidden = false; |
| 78 | + next.classList.add('active', 'show'); |
| 79 | + } |
| 80 | + |
| 81 | + renderContent() { |
| 82 | + const { tabMeta, currentIndex } = this; |
| 83 | + |
| 84 | + return ( |
| 85 | + <> |
| 86 | + <Nav className="nav-tabs" role="tablist"> |
| 87 | + {tabMeta.map(({ caption }, index) => ( |
| 88 | + <NavLink |
| 89 | + role="tab" |
| 90 | + data-index={index} |
| 91 | + className={currentIndex === index ? 'active' : ''} |
| 92 | + ariaSelected={`${currentIndex === index}`} |
| 93 | + > |
| 94 | + {caption} |
| 95 | + </NavLink> |
| 96 | + ))} |
| 97 | + </Nav> |
| 98 | + <div className="tab-content"> |
| 99 | + <slot /> |
| 100 | + </div> |
| 101 | + </> |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + render() { |
| 106 | + return ( |
| 107 | + <> |
| 108 | + <link |
| 109 | + rel="stylesheet" |
| 110 | + href="https://unpkg.com/[email protected]/dist/css/bootstrap.min.css" |
| 111 | + /> |
| 112 | + {this.renderContent()} |
| 113 | + </> |
| 114 | + ); |
| 115 | + } |
| 116 | +} |
0 commit comments