From 7c02b84d213d97a05a54751719e4512aecf73932 Mon Sep 17 00:00:00 2001 From: tangjinzhou <415800467@qq.com> Date: Wed, 22 Nov 2017 15:05:53 +0800 Subject: [PATCH] fix --- components/tabs/RefMixin.js | 10 +++-- components/tabs/TabBar.vue | 76 ++++++++-------------------------- components/tabs/TabBarMixin.js | 72 ++++++++++++++++++++++++++++++++ components/tabs/TabPane.vue | 17 ++++---- components/tabs/Tabs.vue | 62 +++++++++++++++++---------- components/tabs/demo/basic.vue | 13 +++++- 6 files changed, 159 insertions(+), 91 deletions(-) create mode 100644 components/tabs/TabBarMixin.js diff --git a/components/tabs/RefMixin.js b/components/tabs/RefMixin.js index fdb8792ab..bb8bddfb9 100644 --- a/components/tabs/RefMixin.js +++ b/components/tabs/RefMixin.js @@ -1,7 +1,9 @@ export default { - saveRef (name) { - return node => { - this[name] = node - } + methods: { + saveRef (name) { + return node => { + this[name] = node + } + }, }, } diff --git a/components/tabs/TabBar.vue b/components/tabs/TabBar.vue index 253b443ce..a3ea6acd8 100644 --- a/components/tabs/TabBar.vue +++ b/components/tabs/TabBar.vue @@ -1,12 +1,10 @@ - diff --git a/components/tabs/TabBarMixin.js b/components/tabs/TabBarMixin.js new file mode 100644 index 000000000..ddbc03df7 --- /dev/null +++ b/components/tabs/TabBarMixin.js @@ -0,0 +1,72 @@ +export default { + methods: { + getTabs () { + const { panels: children, activeKey, prefixCls } = this + const rst = [] + children.forEach((child) => { + if (!child) { + return + } + const key = child.pKey + let cls = activeKey === key ? `${prefixCls}-tab-active` : '' + cls += ` ${prefixCls}-tab` + if (child.disabled) { + cls += ` ${prefixCls}-tab-disabled` + } else { + } + const onClick = () => { + !child.disabled && this.onTabClick(key) + } + const ref = {} + if (activeKey === key) { + ref.ref = this.saveRef('activeTab') + } + rst.push( + + ) + }) + + return rst + }, + getRootNode (contents, createElement) { + const { + prefixCls, onKeyDown, tabBarPosition, $slots, + } = this + const cls = { + [`${prefixCls}-bar`]: true, + } + const topOrBottom = (tabBarPosition === 'top' || tabBarPosition === 'bottom') + const tabBarExtraContentStyle = topOrBottom ? { float: 'right' } : {} + let children = contents + if ($slots.default) { + children = [ +
+ {$slots.default} +
, + contents, + ] + children = topOrBottom ? children : children.reverse() + } + return ( +
+ {children} +
+ ) + }, + }, +} diff --git a/components/tabs/TabPane.vue b/components/tabs/TabPane.vue index 66d8c55a9..33eaf3f2c 100644 --- a/components/tabs/TabPane.vue +++ b/components/tabs/TabPane.vue @@ -13,31 +13,34 @@ export default { name: 'TabPane', props: { pKey: [String, Number], + tab: [String, Number], forceRender: Boolean, // placeholder: [Function, String, Number], }, data () { - const { prefixCls, destroyInactiveTabPane, activeKey } = this.$parent return { - rootPrefixCls: prefixCls, - destroyInactiveTabPane, - active: this.pKey === activeKey, } }, computed: { classes () { - const { rootPrefixCls, active } = this - const prefixCls = `${rootPrefixCls}-tabpane` + const { $parent, active } = this + const prefixCls = `${$parent.prefixCls}-tabpane` return { [`${prefixCls}`]: true, [`${prefixCls}-inactive`]: !active, [`${prefixCls}-active`]: active, } }, + active () { + const { activeKey } = this.$parent + return activeKey === this.pKey + }, isRender () { const { - destroyInactiveTabPane, active, + active, + $parent, } = this + const destroyInactiveTabPane = $parent.destroyInactiveTabPane this._isActived = this._isActived || active return destroyInactiveTabPane ? active : this._isActived }, diff --git a/components/tabs/Tabs.vue b/components/tabs/Tabs.vue index a25534b96..b73e4aede 100644 --- a/components/tabs/Tabs.vue +++ b/components/tabs/Tabs.vue @@ -19,6 +19,10 @@ function activeKeyIsValid (t, key) { export default { name: 'Tabs', components: { Icon }, + model: { + prop: 'activeKey', + event: 'change', + }, props: { prefixCls: { default: 'ant-tabs', @@ -27,7 +31,7 @@ export default { tabBarPosition: { default: 'top', validator (value) { - return ['top', 'bottom'].includes(value) + return ['top', 'bottom', 'left', 'right'].includes(value) }, }, destroyInactiveTabPane: Boolean, @@ -68,9 +72,10 @@ export default { return activeKey }, onTabClick (activeKey) { - if (this.tabBar.props.onTabClick) { - this.tabBar.props.onTabClick(activeKey) - } + console.log('onTabClick', activeKey) + // if (this.tabBar.props.onTabClick) { + // this.tabBar.props.onTabClick(activeKey) + // } this.setActiveKey(activeKey) }, @@ -92,6 +97,7 @@ export default { if (!('activeKey' in this)) { this.stateActiveKey = activeKey } + // this.stateActiveKey = activeKey this.$emit('change', activeKey) } }, @@ -99,7 +105,8 @@ export default { getNextActiveKey (next) { const activeKey = this.stateActiveKey const children = [] - this.$slot.default.forEach((c) => { + this.$slots.default.forEach(({ componentOptions = {}}) => { + const c = componentOptions.propsData if (c && !c.disabled) { if (next) { children.push(c) @@ -113,9 +120,9 @@ export default { children.forEach((child, i) => { if (child.pKey === activeKey) { if (i === length - 1) { - ret = children[0].key + ret = children[0].pKey } else { - ret = children[i + 1].key + ret = children[i + 1].pKey } } }) @@ -137,17 +144,10 @@ export default { $slots, } = this const hasSlot = !!$slots.default - const tabBarProps = [] + const panels = [] if (hasSlot) { $slots.default.forEach(tab => { - tab.data && tabBarProps.push( - ) + tab.componentOptions && panels.push(tab.componentOptions.propsData) }) } const tabContentProps = { @@ -158,15 +158,35 @@ export default { destroyInactiveTabPane, onChange: setActiveKey, key: 'tabContent', - }} + }, + } + const tabBarProps = { + props: { + panels: panels, + prefixCls: prefixCls, + onKeyDown: onNavKeyDown, + tabBarPosition: tabBarPosition, + onTabClick: onTabClick, + activeKey: stateActiveKey, + key: 'tabBar', + }, + } + const contents = [ + + {this.$slots.tabBarExtraContent} + , + + {$slots.default} + , + ] + if (tabBarPosition === 'bottom') { + contents.reverse() + } return (
- {tabBarProps} - - {$slots.default} - + {contents}
) }, diff --git a/components/tabs/demo/basic.vue b/components/tabs/demo/basic.vue index c8d38a1f9..b8bc434a8 100644 --- a/components/tabs/demo/basic.vue +++ b/components/tabs/demo/basic.vue @@ -1,6 +1,7 @@