ant-design-vue/components/tabs/Tabs.vue

206 lines
5.0 KiB
Vue
Raw Normal View History

2017-11-21 11:15:41 +00:00
<script>
import Icon from '../icon'
import KeyCode from './KeyCode'
import TabContent from './TabContent'
2017-11-30 11:11:42 +00:00
import ScrollableInkTabBar from './ScrollableInkTabBar'
2017-11-21 11:15:41 +00:00
function getDefaultActiveKey (t) {
let activeKey
2017-12-01 04:21:43 +00:00
t.$slots.default.forEach(({ componentOptions = {}}) => {
const child = componentOptions.propsData
2017-11-21 11:15:41 +00:00
if (child && !activeKey && !child.disabled) {
2017-12-01 04:21:43 +00:00
activeKey = child.tabKey
2017-11-21 11:15:41 +00:00
}
})
return activeKey
}
function activeKeyIsValid (t, key) {
2017-12-01 04:21:43 +00:00
const keys = t.$slots.default.map(({ componentOptions = {}}) => {
const child = componentOptions.propsData
if (child) {
return child.tabKey
}
})
return key !== undefined && keys.indexOf(key) >= 0
2017-11-21 11:15:41 +00:00
}
export default {
name: 'Tabs',
components: { Icon },
2017-11-22 07:05:53 +00:00
model: {
prop: 'activeKey',
event: 'change',
},
2017-11-21 11:15:41 +00:00
props: {
prefixCls: {
default: 'ant-tabs',
type: String,
},
tabBarPosition: {
default: 'top',
validator (value) {
2017-11-22 07:05:53 +00:00
return ['top', 'bottom', 'left', 'right'].includes(value)
2017-11-21 11:15:41 +00:00
},
},
2017-11-30 11:11:42 +00:00
tabBarProps: Object,
2017-12-01 04:21:43 +00:00
tabContentProps: Object,
2017-11-21 11:15:41 +00:00
destroyInactiveTabPane: Boolean,
activeKey: String,
defaultActiveKey: String,
},
data () {
return {
stateActiveKey: this.getStateActiveKey(),
}
},
computed: {
classes () {
const { prefixCls, tabBarPosition } = this
return {
[`${prefixCls}`]: true,
[`${prefixCls}-${tabBarPosition}`]: true,
}
},
},
beforeUpdate () {
2017-12-01 04:21:43 +00:00
if (this.activeKey) {
2017-11-21 11:15:41 +00:00
this.stateActiveKey = this.activeKey
} else if (!activeKeyIsValid(this, this.stateActiveKey)) {
this.stateActiveKey = getDefaultActiveKey(this)
}
},
methods: {
getStateActiveKey () {
let activeKey
2017-12-01 04:21:43 +00:00
if (this.activeKey) {
2017-11-21 11:15:41 +00:00
activeKey = this.activeKey
2017-12-01 04:21:43 +00:00
} else if (this.defaultActiveKey) {
2017-11-21 11:15:41 +00:00
activeKey = this.defaultActiveKey
} else {
activeKey = getDefaultActiveKey(this)
}
return activeKey
},
onTabClick (activeKey) {
2017-11-22 07:05:53 +00:00
console.log('onTabClick', activeKey)
// if (this.tabBar.props.onTabClick) {
// this.tabBar.props.onTabClick(activeKey)
// }
2017-11-21 11:15:41 +00:00
this.setActiveKey(activeKey)
},
onNavKeyDown (e) {
const eventKeyCode = e.keyCode
if (eventKeyCode === KeyCode.RIGHT || eventKeyCode === KeyCode.DOWN) {
e.preventDefault()
const nextKey = this.getNextActiveKey(true)
this.onTabClick(nextKey)
} else if (eventKeyCode === KeyCode.LEFT || eventKeyCode === KeyCode.UP) {
e.preventDefault()
const previousKey = this.getNextActiveKey(false)
this.onTabClick(previousKey)
}
},
setActiveKey (activeKey) {
if (this.stateActiveKey !== activeKey) {
2017-12-01 04:21:43 +00:00
if (!this.activeKey) {
2017-11-21 11:15:41 +00:00
this.stateActiveKey = activeKey
}
2017-11-22 07:05:53 +00:00
// this.stateActiveKey = activeKey
2017-11-21 11:15:41 +00:00
this.$emit('change', activeKey)
}
},
getNextActiveKey (next) {
const activeKey = this.stateActiveKey
const children = []
2017-11-22 07:05:53 +00:00
this.$slots.default.forEach(({ componentOptions = {}}) => {
const c = componentOptions.propsData
2017-11-21 11:15:41 +00:00
if (c && !c.disabled) {
if (next) {
children.push(c)
} else {
children.unshift(c)
}
}
})
const length = children.length
let ret = length && children[0].key
children.forEach((child, i) => {
2017-12-01 04:21:43 +00:00
if (child.tabKey === activeKey) {
2017-11-21 11:15:41 +00:00
if (i === length - 1) {
2017-12-01 04:21:43 +00:00
ret = children[0].tabKey
2017-11-21 11:15:41 +00:00
} else {
2017-12-01 04:21:43 +00:00
ret = children[i + 1].tabKey
2017-11-21 11:15:41 +00:00
}
}
})
return ret
},
},
beforeDestroy () {
},
render () {
const {
prefixCls,
tabBarPosition,
destroyInactiveTabPane,
onNavKeyDown,
onTabClick,
stateActiveKey,
classes,
setActiveKey,
$slots,
} = this
const hasSlot = !!$slots.default
2017-11-22 07:05:53 +00:00
const panels = []
2017-11-21 11:15:41 +00:00
if (hasSlot) {
$slots.default.forEach(tab => {
2017-11-22 07:05:53 +00:00
tab.componentOptions && panels.push(tab.componentOptions.propsData)
2017-11-21 11:15:41 +00:00
})
}
const tabContentProps = {
props: {
2017-12-01 04:21:43 +00:00
...this.tabContentProps,
2017-11-21 11:15:41 +00:00
prefixCls,
tabBarPosition,
activeKey: stateActiveKey,
destroyInactiveTabPane,
onChange: setActiveKey,
key: 'tabContent',
2017-11-22 07:05:53 +00:00
},
}
const tabBarProps = {
props: {
2017-12-01 04:21:43 +00:00
...this.tabBarProps,
2017-11-22 07:05:53 +00:00
panels: panels,
prefixCls: prefixCls,
onKeyDown: onNavKeyDown,
tabBarPosition: tabBarPosition,
onTabClick: onTabClick,
activeKey: stateActiveKey,
key: 'tabBar',
},
2017-12-01 04:21:43 +00:00
style: this.tabBarProps.style || {},
2017-11-22 07:05:53 +00:00
}
const contents = [
2017-11-30 11:11:42 +00:00
<ScrollableInkTabBar {...tabBarProps}>
2017-11-22 07:05:53 +00:00
{this.$slots.tabBarExtraContent}
2017-11-30 11:11:42 +00:00
</ScrollableInkTabBar>,
2017-11-22 07:05:53 +00:00
<TabContent {...tabContentProps}>
{$slots.default}
</TabContent>,
]
if (tabBarPosition === 'bottom') {
contents.reverse()
}
2017-11-21 11:15:41 +00:00
return (
<div
class={classes}
>
2017-11-22 07:05:53 +00:00
{contents}
2017-11-21 11:15:41 +00:00
</div>
)
},
}
</script>