You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ant-design-vue/components/tabs/Tabs.vue

219 lines
5.5 KiB

7 years ago
<script>
import Icon from '../icon'
import KeyCode from './KeyCode'
import TabContent from './TabContent'
7 years ago
import ScrollableInkTabBar from './ScrollableInkTabBar'
7 years ago
function getDefaultActiveKey (t) {
let activeKey
7 years ago
t.$slots.default && t.$slots.default.forEach(({ componentOptions = {}, key: tabKey }) => {
7 years ago
const child = componentOptions.propsData
7 years ago
if (child && !activeKey && !child.disabled) {
7 years ago
activeKey = tabKey
7 years ago
}
})
return activeKey
}
function activeKeyIsValid (t, key) {
7 years ago
const keys = t.$slots.default && t.$slots.default.map(({ componentOptions = {}, key: tabKey }) => {
7 years ago
const child = componentOptions.propsData
if (child) {
7 years ago
return tabKey
7 years ago
}
})
return key !== undefined && keys.indexOf(key) >= 0
7 years ago
}
7 years ago
function noop () {
}
7 years ago
export default {
name: 'Tabs',
components: { Icon },
7 years ago
model: {
prop: 'activeKey',
event: 'change',
},
7 years ago
props: {
prefixCls: {
default: 'ant-tabs',
type: String,
},
tabBarPosition: {
default: 'top',
validator (value) {
7 years ago
return ['top', 'bottom', 'left', 'right'].includes(value)
7 years ago
},
},
7 years ago
tabBarProps: Object,
7 years ago
tabContentProps: Object,
7 years ago
destroyInactiveTabPane: Boolean,
activeKey: String,
defaultActiveKey: String,
7 years ago
type: {
validator (value) {
return ['line', 'card', 'editable-card'].includes(value)
},
},
7 years ago
onChange: { type: Function, default: noop },
onTabClick: { type: Function, default: noop },
7 years ago
},
data () {
return {
stateActiveKey: this.getStateActiveKey(),
}
},
computed: {
classes () {
const { prefixCls, tabBarPosition } = this
return {
[`${prefixCls}`]: true,
[`${prefixCls}-${tabBarPosition}`]: true,
}
},
},
beforeUpdate () {
7 years ago
if (this.activeKey) {
7 years ago
this.stateActiveKey = this.activeKey
} else if (!activeKeyIsValid(this, this.stateActiveKey)) {
this.stateActiveKey = getDefaultActiveKey(this)
}
},
methods: {
getStateActiveKey () {
let activeKey
7 years ago
if (this.activeKey) {
7 years ago
activeKey = this.activeKey
7 years ago
} else if (this.defaultActiveKey) {
7 years ago
activeKey = this.defaultActiveKey
} else {
activeKey = getDefaultActiveKey(this)
}
return activeKey
},
7 years ago
handleTabClick (activeKey) {
this.onTabClick(activeKey)
7 years ago
this.setActiveKey(activeKey)
},
onNavKeyDown (e) {
const eventKeyCode = e.keyCode
if (eventKeyCode === KeyCode.RIGHT || eventKeyCode === KeyCode.DOWN) {
e.preventDefault()
const nextKey = this.getNextActiveKey(true)
7 years ago
this.handleTabClick(nextKey)
7 years ago
} else if (eventKeyCode === KeyCode.LEFT || eventKeyCode === KeyCode.UP) {
e.preventDefault()
const previousKey = this.getNextActiveKey(false)
7 years ago
this.handleTabClick(previousKey)
7 years ago
}
},
setActiveKey (activeKey) {
if (this.stateActiveKey !== activeKey) {
7 years ago
if (!this.activeKey) {
7 years ago
this.stateActiveKey = activeKey
}
7 years ago
this.onChange(activeKey)
7 years ago
}
},
getNextActiveKey (next) {
const activeKey = this.stateActiveKey
const children = []
7 years ago
this.$slots.default && this.$slots.default.forEach(({ componentOptions = {}, key: tabKey }) => {
7 years ago
const c = componentOptions.propsData
7 years ago
7 years ago
if (c && !c.disabled && c.disabled !== '') {
7 years ago
if (next) {
7 years ago
children.push({ ...c, tabKey })
7 years ago
} else {
7 years ago
children.unshift({ ...c, tabKey })
7 years ago
}
}
})
const length = children.length
7 years ago
let ret = length && children[0].tabKey
7 years ago
children.forEach((child, i) => {
7 years ago
if (child.tabKey === activeKey) {
7 years ago
if (i === length - 1) {
7 years ago
ret = children[0].tabKey
7 years ago
} else {
7 years ago
ret = children[i + 1].tabKey
7 years ago
}
}
})
return ret
},
},
beforeDestroy () {
},
render () {
const {
prefixCls,
tabBarPosition,
destroyInactiveTabPane,
onNavKeyDown,
7 years ago
handleTabClick,
7 years ago
stateActiveKey,
classes,
setActiveKey,
$slots,
} = this
7 years ago
const panels = []
7 years ago
7 years ago
$slots.default && $slots.default.forEach(({ componentOptions, key: tabKey }) => {
if (componentOptions) {
if (componentOptions.propsData.tab === undefined) {
componentOptions.propsData.tab = $slots[`tab_${tabKey}`]
7 years ago
? $slots[`tab_${tabKey}`]
7 years ago
: null
}
panels.push({ ...componentOptions.propsData, tabKey })
}
7 years ago
})
7 years ago
const tabContentProps = {
props: {
7 years ago
...this.tabContentProps,
7 years ago
prefixCls,
tabBarPosition,
activeKey: stateActiveKey,
destroyInactiveTabPane,
onChange: setActiveKey,
key: 'tabContent',
7 years ago
},
}
const tabBarProps = {
props: {
7 years ago
...this.tabBarProps,
7 years ago
panels: panels,
prefixCls: prefixCls,
onKeyDown: onNavKeyDown,
tabBarPosition: tabBarPosition,
7 years ago
onTabClick: handleTabClick,
7 years ago
activeKey: stateActiveKey,
key: 'tabBar',
},
7 years ago
style: this.tabBarProps.style || {},
7 years ago
}
const contents = [
7 years ago
<ScrollableInkTabBar {...tabBarProps}>
7 years ago
{$slots.tabBarExtraContent ? <template slot='extraContent'>
7 years ago
{$slots.tabBarExtraContent}
7 years ago
</template> : null}
7 years ago
</ScrollableInkTabBar>,
7 years ago
<TabContent {...tabContentProps}>
{$slots.default}
</TabContent>,
]
if (tabBarPosition === 'bottom') {
contents.reverse()
}
7 years ago
return (
<div
class={classes}
>
7 years ago
{contents}
7 years ago
</div>
)
},
}
</script>