优化 tabs

pull/9/head
tangjinzhou 2018-01-12 17:06:01 +08:00
parent 728e26548f
commit 8612331300
15 changed files with 51 additions and 59 deletions

View File

@ -1,6 +1,6 @@
import Tabs from './index.vue' import Tabs from './index.vue'
import TabPane from './TabPane' import TabPane from './src/TabPane'
import TabContent from './TabContent' import TabContent from './src/TabContent'
Tabs.TabPane = TabPane Tabs.TabPane = TabPane
export default Tabs export default Tabs
export { TabPane, TabContent } export { TabPane, TabContent }

View File

@ -1,5 +1,5 @@
<script> <script>
import Tabs from './Tabs' import Tabs from './src/Tabs'
import isFlexSupported from '../_util/isFlexSupported' import isFlexSupported from '../_util/isFlexSupported'
import hasProp from '../_util/props-util' import hasProp from '../_util/props-util'
export default { export default {
@ -75,7 +75,6 @@ export default {
tabPosition, tabPosition,
tabBarStyle, tabBarStyle,
hideAdd, hideAdd,
onTabClick,
onPrevClick, onPrevClick,
onNextClick, onNextClick,
animated, animated,
@ -114,36 +113,38 @@ export default {
} }
}) })
const tabBarProps = { const tabBarProps = {
inkBarAnimated, props: {
onTabClick, hideAdd,
onPrevClick, removeTab: this.removeTab,
onNextClick, createNewTab: this.createNewTab,
inkBarAnimated,
},
on: {
// tabClick: onTabClick,
prevClick: onPrevClick,
nextClick: onNextClick,
},
style: tabBarStyle, style: tabBarStyle,
hideAdd,
removeTab: this.removeTab,
createNewTab: this.createNewTab,
} }
const tabContentProps = { const tabContentProps = {
animated: tabPaneAnimated, props: {
animatedWithMargin: true, animated: tabPaneAnimated,
animatedWithMargin: true,
},
} }
const self = this
const tabsProps = { const tabsProps = {
props: { props: {
prefixCls, prefixCls,
tabBarPosition: tabPosition, tabBarPosition: tabPosition,
onChange: this.handleChange,
tabBarProps: tabBarProps, tabBarProps: tabBarProps,
tabContentProps: tabContentProps, tabContentProps: tabContentProps,
destroyInactiveTabPane, destroyInactiveTabPane,
defaultActiveKey, defaultActiveKey,
type, type,
onTabClick: this.onTabClick,
}, },
on: { on: {
change (val) { change: this.handleChange,
self.handleChange(val) tabClick: this.onTabClick,
},
}, },
} }
if (hasProp(this, 'activeKey')) { if (hasProp(this, 'activeKey')) {

View File

@ -2,10 +2,11 @@
import InkTabBarMixin from './InkTabBarMixin' import InkTabBarMixin from './InkTabBarMixin'
import ScrollableTabBarMixin from './ScrollableTabBarMixin' import ScrollableTabBarMixin from './ScrollableTabBarMixin'
import TabBarMixin from './TabBarMixin' import TabBarMixin from './TabBarMixin'
import BaseMixin from '../../_util/BaseMixin'
export default { export default {
name: 'ScrollableInkTabBar', name: 'ScrollableInkTabBar',
mixins: [TabBarMixin, InkTabBarMixin, ScrollableTabBarMixin], mixins: [TabBarMixin, InkTabBarMixin, ScrollableTabBarMixin, BaseMixin],
render (h) { render (h) {
const inkBarNode = this.getInkBarNode() const inkBarNode = this.getInkBarNode()
const tabs = this.getTabs(h) const tabs = this.getTabs(h)

View File

@ -1,10 +1,11 @@
<script> <script>
import ScrollableTabBarMixin from './ScrollableTabBarMixin' import ScrollableTabBarMixin from './ScrollableTabBarMixin'
import TabBarMixin from './TabBarMixin' import TabBarMixin from './TabBarMixin'
import BaseMixin from '../../_util/BaseMixin'
export default { export default {
name: 'ScrollableTabBar', name: 'ScrollableTabBar',
mixins: [TabBarMixin, ScrollableTabBarMixin], mixins: [TabBarMixin, ScrollableTabBarMixin, BaseMixin],
render (h) { render (h) {
const inkBarNode = this.getInkBarNode() const inkBarNode = this.getInkBarNode()
const tabs = this.getTabs(h) const tabs = this.getTabs(h)

View File

@ -6,8 +6,6 @@ function noop () {
export default { export default {
props: { props: {
scrollAnimated: { type: Boolean, default: true }, scrollAnimated: { type: Boolean, default: true },
onPrevClick: { type: Function, default: noop },
onNextClick: { type: Function, default: noop },
}, },
data () { data () {
@ -201,7 +199,7 @@ export default {
}, },
prevClick (e) { prevClick (e) {
this.$props.onPrevClick(e) this.__emit('prevClick', e)
const navWrapNode = this.$refs.navWrap const navWrapNode = this.$refs.navWrap
const navWrapNodeWH = this.getOffsetWH(navWrapNode) const navWrapNodeWH = this.getOffsetWH(navWrapNode)
const { offset } = this const { offset } = this
@ -209,7 +207,7 @@ export default {
}, },
nextClick (e) { nextClick (e) {
this.$props.onNextClick(e) this.__emit('nextClick', e)
const navWrapNode = this.$refs.navWrap const navWrapNode = this.$refs.navWrap
const navWrapNodeWH = this.getOffsetWH(navWrapNode) const navWrapNodeWH = this.getOffsetWH(navWrapNode)
const { offset } = this const { offset } = this

View File

@ -1,9 +1,8 @@
<script> <script>
import TabBarMixin from './TabBarMixin' import TabBarMixin from './TabBarMixin'
function noop () { import BaseMixin from '../../_util/BaseMixin'
}
export default { export default {
mixins: [TabBarMixin], mixins: [TabBarMixin, BaseMixin],
name: 'TabBar', name: 'TabBar',
props: { props: {
prefixCls: { prefixCls: {
@ -15,14 +14,6 @@ export default {
type: String, type: String,
}, },
disabled: Boolean, disabled: Boolean,
onKeyDown: {
default: noop,
type: Function,
},
onTabClick: {
default: noop,
type: Function,
},
activeKey: String, activeKey: String,
panels: Array, panels: Array,
}, },

View File

@ -1,4 +1,4 @@
import Icon from '../icon' import Icon from '../../icon'
function noop () { function noop () {
} }
export default { export default {
@ -12,14 +12,6 @@ export default {
type: String, type: String,
}, },
disabled: Boolean, disabled: Boolean,
onKeyDown: {
default: noop,
type: Function,
},
onTabClick: {
default: noop,
type: Function,
},
activeKey: String, activeKey: String,
panels: Array, panels: Array,
hideAdd: Boolean, hideAdd: Boolean,
@ -51,7 +43,7 @@ export default {
} else { } else {
} }
const onClick = () => { const onClick = () => {
!disabled && this.onTabClick(tabKey) !disabled && this.__emit('tabClick', tabKey)
} }
let tabC = typeof tab === 'function' ? child.tab(h, tabKey) : tab let tabC = typeof tab === 'function' ? child.tab(h, tabKey) : tab
@ -86,6 +78,9 @@ export default {
return rst return rst
}, },
onKeyDown (e) {
this.__emit('keydown', e)
},
getRootNode (contents, createElement) { getRootNode (contents, createElement) {
const { const {
prefixCls, onKeyDown, tabBarPosition, hideAdd, prefixCls, onKeyDown, tabBarPosition, hideAdd,

View File

@ -1,9 +1,10 @@
<script> <script>
import Icon from '../icon' import Icon from '../../icon'
import KeyCode from './KeyCode' import KeyCode from './KeyCode'
import TabContent from './TabContent' import TabContent from './TabContent'
import ScrollableInkTabBar from './ScrollableInkTabBar' import ScrollableInkTabBar from './ScrollableInkTabBar'
import hasProp from '../_util/props-util' import hasProp from '../../_util/props-util'
import BaseMixin from '../../_util/BaseMixin'
function getDefaultActiveKey (t) { function getDefaultActiveKey (t) {
let activeKey let activeKey
t.$slots.default && t.$slots.default.forEach(({ componentOptions = {}, key: tabKey }) => { t.$slots.default && t.$slots.default.forEach(({ componentOptions = {}, key: tabKey }) => {
@ -23,8 +24,7 @@ function activeKeyIsValid (t, key) {
}) })
return key !== undefined && keys.indexOf(key) >= 0 return key !== undefined && keys.indexOf(key) >= 0
} }
function noop () {
}
export default { export default {
name: 'Tabs', name: 'Tabs',
components: { Icon }, components: { Icon },
@ -32,6 +32,7 @@ export default {
prop: 'activeKey', prop: 'activeKey',
event: 'change', event: 'change',
}, },
mixins: [BaseMixin],
props: { props: {
prefixCls: { prefixCls: {
default: 'ant-tabs', default: 'ant-tabs',
@ -53,8 +54,6 @@ export default {
return ['line', 'card', 'editable-card'].includes(value) return ['line', 'card', 'editable-card'].includes(value)
}, },
}, },
onChange: { type: Function, default: noop },
onTabClick: { type: Function, default: noop },
}, },
data () { data () {
return { return {
@ -90,7 +89,7 @@ export default {
return activeKey return activeKey
}, },
handleTabClick (activeKey) { handleTabClick (activeKey) {
this.onTabClick(activeKey) this.__emit('tabClick', activeKey)
this.setActiveKey(activeKey) this.setActiveKey(activeKey)
}, },
@ -112,7 +111,7 @@ export default {
if (!hasProp(this, 'activeKey')) { if (!hasProp(this, 'activeKey')) {
this.stateActiveKey = activeKey this.stateActiveKey = activeKey
} }
this.onChange(activeKey) this.__emit('change', activeKey)
} }
}, },
@ -172,25 +171,31 @@ export default {
}) })
const tabContentProps = { const tabContentProps = {
props: { props: {
...this.tabContentProps, ...this.tabContentProps.props,
prefixCls, prefixCls,
tabBarPosition, tabBarPosition,
activeKey: stateActiveKey, activeKey: stateActiveKey,
destroyInactiveTabPane, destroyInactiveTabPane,
onChange: setActiveKey, // onChange: setActiveKey,
},
on: {
change: setActiveKey,
}, },
} }
const tabBarProps = { const tabBarProps = {
props: { props: {
...this.tabBarProps, ...this.tabBarProps.props,
panels: panels, panels: panels,
prefixCls: prefixCls, prefixCls: prefixCls,
onKeyDown: onNavKeyDown,
tabBarPosition: tabBarPosition, tabBarPosition: tabBarPosition,
onTabClick: handleTabClick,
activeKey: stateActiveKey, activeKey: stateActiveKey,
}, },
style: this.tabBarProps.style || {}, style: this.tabBarProps.style || {},
on: {
...this.tabBarProps.on,
keydown: onNavKeyDown,
tabClick: handleTabClick,
},
} }
const contents = [ const contents = [
<ScrollableInkTabBar <ScrollableInkTabBar