/** * @file Santd tabs source file * @author mayihui@baidu.com **/ import './style/index'; import Base from 'santd/base'; import {Data} from 'san/types'; import type * as I from './interface'; import TabPane from './TabPane'; import KeyCode from '../core/util/keyCode'; import {classCreator} from '../core/util'; import ScrollableInkTabBar from './ScrollableInkTabBar'; const prefixCls = classCreator('tabs')(); export default class Tabs extends Base { static template = /* html */ `
`; static components = { 's-tabbar': ScrollableInkTabBar }; static computed: I.Computed = { classes(this: Tabs) { const tabPosition = this.data.get('tabPosition'); const size = this.data.get('size'); const type = this.data.get('type'); const centered = this.data.get('centered'); let classArr = [`${prefixCls}`, `${prefixCls}-${type}`, `${prefixCls}-${tabPosition}`]; (tabPosition === 'left' || tabPosition === 'right') && classArr.push(`${prefixCls}-vertical`); !!size && classArr.push(`${prefixCls}-${size}`); type.indexOf('card') >= 0 && classArr.push(`${prefixCls}-card`); centered && classArr.push(`${prefixCls}-center`); return classArr.join(' '); }, contentClasses(this: Tabs) { // @ts-ignore const animated = this.data.get('hasAnimated').tabPane; const tabPosition = this.data.get('tabPosition'); const type = this.data.get('type') || ''; let classArr = [`${prefixCls}-content`, `${prefixCls}-${tabPosition}-content`]; type.indexOf('card') >= 0 && classArr.push(prefixCls + '-card-content'); animated ? classArr.push(`${prefixCls}-content-animated`) : classArr.push(`${prefixCls}-content-no-animated`); return classArr; }, hasAnimated(this: Tabs) { const animated = this.data.get('animated'); return typeof animated === 'object' ? animated : { tabPane: animated, inkBar: animated }; }, props(this: Tabs) { const activeKey = this.data.get('__activeKey__'); const tabBarGutter = this.data.get('tabBarGutter'); const tabPosition = this.data.get('tabPosition'); const type = this.data.get('type'); const size = this.data.get('size'); const tabBarData = this.data.get('tabBarData'); return { activeKey, tabBarGutter, tabPosition, type, size, tabBarData }; }, }; static messages: I.Messages = { santd_tabs_addTabPane(payload) { this.tabPanes.push(payload.value); this.updateTab(); }, santd_tabs_removeTabPane(payload) { this.tabPanes.forEach((tabPane: {data: Data}, index: number) => { if (tabPane.data.get('key') === payload.value) { this.tabPanes.splice(index, 1); } }); this.updateTab(); }, santd_tabs_prevClick(payload) { this.fire('prevClick', payload.value); }, santd_tabs_nextClick(payload) { this.fire('nextClick', payload.value); } }; tabPanes!: any[]; // owner!: {data: Data}; static TabPane: typeof TabPane; static TabBar: typeof ScrollableInkTabBar; getNextActiveKey(next: boolean) { // @ts-ignore const {__activeKey__, tabBarData} = this.data.get(); const arr = tabBarData.filter((item: {disabled: boolean}) => !item.disabled); const len = arr.length; let ret = __activeKey__; let currentIndex = arr.findIndex((item: {key: string}) => item.key === __activeKey__); if (currentIndex > -1) { next ? ++currentIndex : --currentIndex; if (currentIndex < 0) { currentIndex = len - 1; } if (currentIndex > len - 1) { currentIndex = 0; } ret = arr[currentIndex].key; } return ret; }; handleTabClick(payload: { key: string; }) { this.fire('tabClick', payload.key); this.setActiveKey(payload.key); }; handleNavKeyDown(e: { keyCode: number; preventDefault: () => void; }) { if (!this.data.get('keyboard')) { return; } const eventKeyCode = e.keyCode; if (eventKeyCode === KeyCode.ARROW_RIGHT || eventKeyCode === KeyCode.ARROW_DOWN) { e.preventDefault(); const nextKey = this.getNextActiveKey(true); this.setActiveKey(nextKey); } else if (eventKeyCode === KeyCode.ARROW_LEFT || eventKeyCode === KeyCode.ARROW_UP) { e.preventDefault(); const previousKey = this.getNextActiveKey(false); this.setActiveKey(previousKey); } }; handleCreateNewTab() { this.fire('edit', { action: 'add' }); }; handleRemoveTab({key, e}: { key: string; e: Event }) { e.stopPropagation(); if (!key) { return; } this.fire('edit', { action: 'remove', key: key }); }; setActiveKey(key: string) { if (this.data.get('__activeKey__') !== key) { this.data.set('__activeKey__', key); this.updateTab(); this.fire('change', key); } }; updateTab() { const __activeKey__ = this.data.get('__activeKey__'); let tabBarData: any[] = []; this.tabPanes.forEach((tabPane: {data: Data}) => { const key = tabPane.data.get('key'); tabPane.data.set('active', __activeKey__ === key); tabBarData.push(tabPane.data.get()); }); this.data.set('tabBarData', tabBarData); this.data.set('tabPanes', this.tabPanes); }; inited() { this.data.set('__activeKey__', this.data.get('activeKey') || this.data.get('defaultActiveKey')); this.data.set('hasRenderTabBar', !!this.sourceSlots.named.renderTabBar); this.data.set('hasExtraContent', !!this.sourceSlots.named.tabBarExtraContent); this.data.set('hasAddIcon', !!this.sourceSlots.named.addIcon); this.data.set('ownerData', this.owner.data.get()); this.tabPanes = []; this.data.set('ownerData', this.owner.data.get()); }; attached() { this.updateTab(); this.watch('tabPosition', () => { this.nextTick(() => { this.updateTab(); }); }); this.watch('activeKey', (val: string) => this.setActiveKey(val)); }; initData(): I.State { return { type: 'line', hideAdd: false, tabPosition: 'top', children: [], centered: false, keyboard: true }; }; }; Tabs.TabPane = TabPane; Tabs.TabBar = ScrollableInkTabBar;