pull/9/head
tangjinzhou 2017-12-01 12:21:43 +08:00
parent a51b25d920
commit 350001ea14
15 changed files with 162 additions and 72 deletions

View File

@ -5,9 +5,9 @@ import TabBarMixin from './TabBarMixin'
export default { export default {
name: 'InkTabBar', name: 'InkTabBar',
mixins: [TabBarMixin, InkTabBarMixin], mixins: [TabBarMixin, InkTabBarMixin],
render () { render (h) {
const inkBarNode = this.getInkBarNode() const inkBarNode = this.getInkBarNode()
const tabs = this.getTabs() const tabs = this.getTabs(h)
return this.getRootNode([inkBarNode, tabs]) return this.getRootNode([inkBarNode, tabs])
}, },
} }

View File

@ -35,7 +35,7 @@ function offset (elem) {
} }
function componentDidUpdate (component, init) { function componentDidUpdate (component, init) {
const { styles } = component.$props const { styles = {}} = component.$props
const wrapNode = component.$refs.nav || component.$refs.root const wrapNode = component.$refs.nav || component.$refs.root
const containerOffset = offset(wrapNode) const containerOffset = offset(wrapNode)
const inkBarNode = component.$refs.inkBar const inkBarNode = component.$refs.inkBar
@ -111,9 +111,8 @@ export default {
}, },
updated () { updated () {
this.$nextTick(function () { this.$nextTick(function () {
componentDidUpdate(this, true) componentDidUpdate(this)
}) })
componentDidUpdate(this)
}, },
mounted () { mounted () {

View File

@ -6,9 +6,9 @@ import TabBarMixin from './TabBarMixin'
export default { export default {
name: 'ScrollableInkTabBar', name: 'ScrollableInkTabBar',
mixins: [TabBarMixin, InkTabBarMixin, ScrollableTabBarMixin], mixins: [TabBarMixin, InkTabBarMixin, ScrollableTabBarMixin],
render () { render (h) {
const inkBarNode = this.getInkBarNode() const inkBarNode = this.getInkBarNode()
const tabs = this.getTabs() const tabs = this.getTabs(h)
const scrollbarNode = this.getScrollBarNode([inkBarNode, tabs]) const scrollbarNode = this.getScrollBarNode([inkBarNode, tabs])
return this.getRootNode(scrollbarNode) return this.getRootNode(scrollbarNode)
}, },

View File

@ -5,9 +5,9 @@ import TabBarMixin from './TabBarMixin'
export default { export default {
name: 'ScrollableTabBar', name: 'ScrollableTabBar',
mixins: [TabBarMixin, ScrollableTabBarMixin], mixins: [TabBarMixin, ScrollableTabBarMixin],
render () { render (h) {
const inkBarNode = this.getInkBarNode() const inkBarNode = this.getInkBarNode()
const tabs = this.getTabs() const tabs = this.getTabs(h)
const scrollbarNode = this.getScrollBarNode([inkBarNode, tabs]) const scrollbarNode = this.getScrollBarNode([inkBarNode, tabs])
return this.getRootNode(scrollbarNode) return this.getRootNode(scrollbarNode)
}, },

View File

@ -53,9 +53,9 @@ export default {
}, },
setNextPrev () { setNextPrev () {
const navNode = this.nav const navNode = this.$refs.nav
const navNodeWH = this.getOffsetWH(navNode) const navNodeWH = this.getOffsetWH(navNode)
const navWrapNode = this.navWrap const navWrapNode = this.$refs.navWrap
const navWrapNodeWH = this.getOffsetWH(navWrapNode) const navWrapNodeWH = this.getOffsetWH(navWrapNode)
let { offset } = this let { offset } = this
const minOffset = navWrapNodeWH - navNodeWH const minOffset = navWrapNodeWH - navNodeWH
@ -110,7 +110,7 @@ export default {
this.offset = target this.offset = target
let navOffset = {} let navOffset = {}
const tabBarPosition = this.$props.tabBarPosition const tabBarPosition = this.$props.tabBarPosition
const navStyle = this.nav.style const navStyle = this.$refs.nav.style
const transformSupported = isTransformSupported(navStyle) const transformSupported = isTransformSupported(navStyle)
if (tabBarPosition === 'left' || tabBarPosition === 'right') { if (tabBarPosition === 'left' || tabBarPosition === 'right') {
if (transformSupported) { if (transformSupported) {
@ -177,7 +177,8 @@ export default {
}, },
scrollToActiveTab (e) { scrollToActiveTab (e) {
const { activeTab, navWrap } = this const { activeTab } = this
const navWrap = this.$refs.navWrap
if (e && e.target !== e.currentTarget || !activeTab) { if (e && e.target !== e.currentTarget || !activeTab) {
return return
} }
@ -205,7 +206,7 @@ export default {
prevClick (e) { prevClick (e) {
this.$props.onPrevClick(e) this.$props.onPrevClick(e)
const navWrapNode = this.navWrap const navWrapNode = this.$refs.navWrap
const navWrapNodeWH = this.getOffsetWH(navWrapNode) const navWrapNodeWH = this.getOffsetWH(navWrapNode)
const { offset } = this const { offset } = this
this.setOffset(offset + navWrapNodeWH) this.setOffset(offset + navWrapNodeWH)
@ -213,7 +214,7 @@ export default {
nextClick (e) { nextClick (e) {
this.$props.onNextClick(e) this.$props.onNextClick(e)
const navWrapNode = this.navWrap const navWrapNode = this.$refs.navWrap
const navWrapNodeWH = this.getOffsetWH(navWrapNode) const navWrapNodeWH = this.getOffsetWH(navWrapNode)
const { offset } = this const { offset } = this
this.setOffset(offset - navWrapNodeWH) this.setOffset(offset - navWrapNodeWH)

View File

@ -26,9 +26,9 @@ export default {
activeKey: String, activeKey: String,
panels: Array, panels: Array,
}, },
render (createElement) { render (h) {
const tabs = this.getTabs() const tabs = this.getTabs(h)
return this.getRootNode(tabs, createElement) return this.getRootNode(tabs, h)
}, },
} }
</script> </script>

View File

@ -21,14 +21,16 @@ export default {
panels: Array, panels: Array,
}, },
methods: { methods: {
getTabs () { getTabs (h) {
const { panels: children, activeKey, prefixCls } = this const { panels: children, activeKey, prefixCls } = this
const rst = [] const rst = []
children.forEach((child) => { children.forEach((child) => {
if (!child) { if (!child) {
return return
} }
const key = child.pKey // componentOptions.propsData中获取的值disabled没有根据类型初始化, 会出现空字符串
child.disabled = child.disabled === '' || child.disabled
const key = child.tabKey
let cls = activeKey === key ? `${prefixCls}-tab-active` : '' let cls = activeKey === key ? `${prefixCls}-tab-active` : ''
cls += ` ${prefixCls}-tab` cls += ` ${prefixCls}-tab`
if (child.disabled) { if (child.disabled) {
@ -52,7 +54,7 @@ export default {
onClick={onClick} onClick={onClick}
ref={activeKey === key ? 'activeTab' : undefined} ref={activeKey === key ? 'activeTab' : undefined}
> >
{child.tab} {typeof child.tab === 'function' ? child.tab(h, key) : child.tab}
</div> </div>
) )
}) })
@ -71,7 +73,7 @@ export default {
let children = contents let children = contents
if ($slots.default) { if ($slots.default) {
children = [ children = [
<div key='extra' class='`${prefixCls}-extra-content`' style={tabBarExtraContentStyle}> <div key='extra' class={`${prefixCls}-extra-content`} style={tabBarExtraContentStyle}>
{$slots.default} {$slots.default}
</div>, </div>,
contents, contents,

View File

@ -12,12 +12,14 @@
export default { export default {
name: 'TabPane', name: 'TabPane',
props: { props: {
pKey: [String, Number], tabKey: [String, Number],
tab: [String, Number], tab: [String, Number, Function],
forceRender: Boolean, forceRender: Boolean,
disabled: Boolean,
// placeholder: [Function, String, Number], // placeholder: [Function, String, Number],
}, },
data () { data () {
console.log(this.disabled)
return { return {
} }
}, },
@ -33,7 +35,7 @@ export default {
}, },
active () { active () {
const { activeKey } = this.$parent const { activeKey } = this.$parent
return activeKey === this.pKey return activeKey === this.tabKey
}, },
isRender () { isRender () {
const { const {

View File

@ -1,21 +1,26 @@
<script> <script>
import Icon from '../icon' import Icon from '../icon'
import KeyCode from './KeyCode' import KeyCode from './KeyCode'
import TabBar from './TabBar'
import TabContent from './TabContent' import TabContent from './TabContent'
import ScrollableInkTabBar from './ScrollableInkTabBar' import ScrollableInkTabBar from './ScrollableInkTabBar'
function getDefaultActiveKey (t) { function getDefaultActiveKey (t) {
let activeKey let activeKey
t.$slot.default.forEach((child) => { t.$slots.default.forEach(({ componentOptions = {}}) => {
const child = componentOptions.propsData
if (child && !activeKey && !child.disabled) { if (child && !activeKey && !child.disabled) {
activeKey = child.pKey activeKey = child.tabKey
} }
}) })
return activeKey return activeKey
} }
function activeKeyIsValid (t, key) { function activeKeyIsValid (t, key) {
const keys = t.$slot.default.map(child => child && child.pKey) const keys = t.$slots.default.map(({ componentOptions = {}}) => {
return keys.indexOf(key) >= 0 const child = componentOptions.propsData
if (child) {
return child.tabKey
}
})
return key !== undefined && keys.indexOf(key) >= 0
} }
export default { export default {
name: 'Tabs', name: 'Tabs',
@ -35,11 +40,8 @@ export default {
return ['top', 'bottom', 'left', 'right'].includes(value) return ['top', 'bottom', 'left', 'right'].includes(value)
}, },
}, },
renderTabBar: {
type: Function,
default: () => {},
},
tabBarProps: Object, tabBarProps: Object,
tabContentProps: Object,
destroyInactiveTabPane: Boolean, destroyInactiveTabPane: Boolean,
activeKey: String, activeKey: String,
defaultActiveKey: String, defaultActiveKey: String,
@ -59,7 +61,7 @@ export default {
}, },
}, },
beforeUpdate () { beforeUpdate () {
if ('activeKey' in this) { if (this.activeKey) {
this.stateActiveKey = this.activeKey this.stateActiveKey = this.activeKey
} else if (!activeKeyIsValid(this, this.stateActiveKey)) { } else if (!activeKeyIsValid(this, this.stateActiveKey)) {
this.stateActiveKey = getDefaultActiveKey(this) this.stateActiveKey = getDefaultActiveKey(this)
@ -68,9 +70,9 @@ export default {
methods: { methods: {
getStateActiveKey () { getStateActiveKey () {
let activeKey let activeKey
if ('activeKey' in this) { if (this.activeKey) {
activeKey = this.activeKey activeKey = this.activeKey
} else if ('defaultActiveKey' in this) { } else if (this.defaultActiveKey) {
activeKey = this.defaultActiveKey activeKey = this.defaultActiveKey
} else { } else {
activeKey = getDefaultActiveKey(this) activeKey = getDefaultActiveKey(this)
@ -100,7 +102,7 @@ export default {
setActiveKey (activeKey) { setActiveKey (activeKey) {
if (this.stateActiveKey !== activeKey) { if (this.stateActiveKey !== activeKey) {
if (!('activeKey' in this)) { if (!this.activeKey) {
this.stateActiveKey = activeKey this.stateActiveKey = activeKey
} }
// this.stateActiveKey = activeKey // this.stateActiveKey = activeKey
@ -124,11 +126,11 @@ export default {
const length = children.length const length = children.length
let ret = length && children[0].key let ret = length && children[0].key
children.forEach((child, i) => { children.forEach((child, i) => {
if (child.pKey === activeKey) { if (child.tabKey === activeKey) {
if (i === length - 1) { if (i === length - 1) {
ret = children[0].pKey ret = children[0].tabKey
} else { } else {
ret = children[i + 1].pKey ret = children[i + 1].tabKey
} }
} }
}) })
@ -158,6 +160,7 @@ export default {
} }
const tabContentProps = { const tabContentProps = {
props: { props: {
...this.tabContentProps,
prefixCls, prefixCls,
tabBarPosition, tabBarPosition,
activeKey: stateActiveKey, activeKey: stateActiveKey,
@ -168,7 +171,7 @@ export default {
} }
const tabBarProps = { const tabBarProps = {
props: { props: {
...tabBarProps, ...this.tabBarProps,
panels: panels, panels: panels,
prefixCls: prefixCls, prefixCls: prefixCls,
onKeyDown: onNavKeyDown, onKeyDown: onNavKeyDown,
@ -177,6 +180,7 @@ export default {
activeKey: stateActiveKey, activeKey: stateActiveKey,
key: 'tabBar', key: 'tabBar',
}, },
style: this.tabBarProps.style || {},
} }
const contents = [ const contents = [
<ScrollableInkTabBar {...tabBarProps}> <ScrollableInkTabBar {...tabBarProps}>

View File

@ -1,23 +1,20 @@
<template> <template>
<div> <Tabs defaultActiveKey="1" @change="callback">
<Tabs v-model="activeKey"> <TabPane tab="Tab 1" tabKey="1">Content of Tab Pane 1</TabPane>
<span slot="tabBarExtraContent">kkk</span> <TabPane tab="Tab 2" tabKey="2">Content of Tab Pane 2</TabPane>
<TabPane pKey="test1" tab="tab1">hello</TabPane> <TabPane tab="Tab 3" tabKey="3">Content of Tab Pane 3</TabPane>
<TabPane pKey="test2" tab="tab2">world</TabPane> </Tabs>
</Tabs>
</div>
</template> </template>
<script> <script>
import { Tabs } from 'antd' import { Tabs } from 'antd'
export default { export default {
data () { data () {
return { return {
activeKey: 'test1',
} }
}, },
methods: { methods: {
tabBarExtraContent (h) { callback (key) {
return h('span', 'hhhh') console.log(key)
}, },
}, },
components: { components: {

View File

@ -0,0 +1,16 @@
<template>
<Tabs defaultActiveKey="1">
<TabPane tab="Tab 1" tabKey="1">Tab 1</TabPane>
<TabPane tab="Tab 2" disabled tabKey="2">Tab 2</TabPane>
<TabPane tab="Tab 3" tabKey="3">Tab 3</TabPane>
</Tabs>
</template>
<script>
import { Tabs } from 'antd'
export default {
components: {
Tabs,
TabPane: Tabs.TabPane,
},
}
</script>

View File

@ -0,0 +1,25 @@
<template>
<Tabs defaultActiveKey="2">
<TabPane :tab="(h, tabKey) => renderTab(h, tabKey, 'apple')" tabKey="1">
Tab 1
</TabPane>
<TabPane :tab="(h, tabKey) => renderTab(h, tabKey, 'android')" tabKey="2">
Tab 2
</TabPane>
</Tabs>
</template>
<script>
import { Tabs, Icon } from 'antd'
export default {
methods: {
renderTab (h, tabKey, iconType) {
return h('span', [<Icon type={iconType} />, `Tab ${tabKey}`])
},
},
components: {
Tabs,
TabPane: Tabs.TabPane,
Icon,
},
}
</script>

View File

@ -0,0 +1,41 @@
<template>
<div style="width: 500px">
<RadioGroup v-model="mode" :style="{ marginBottom: 8 }">
<RadioButton value="top">Horizontal</RadioButton>
<RadioButton value="left">Vertical</RadioButton>
</RadioGroup>
<Tabs defaultActiveKey="1" :tabPosition="mode" :style="{ height: '200px'}">
<TabPane tab="Tab 1" tabKey="1">Content of tab 1</TabPane>
<TabPane tab="Tab 2" tabKey="2">Content of tab 2</TabPane>
<TabPane tab="Tab 3" tabKey="3">Content of tab 3</TabPane>
<TabPane tab="Tab 4" tabKey="4">Content of tab 4</TabPane>
<TabPane tab="Tab 5" tabKey="5">Content of tab 5</TabPane>
<TabPane tab="Tab 6" tabKey="6">Content of tab 6</TabPane>
<TabPane tab="Tab 7" tabKey="7">Content of tab 7</TabPane>
<TabPane tab="Tab 8" tabKey="8">Content of tab 8</TabPane>
<TabPane tab="Tab 9" tabKey="9">Content of tab 9</TabPane>
<TabPane tab="Tab 10" tabKey="10">Content of tab 10</TabPane>
<TabPane tab="Tab 11" tabKey="11">Content of tab 11</TabPane>
</Tabs>
</div>
</template>
<script>
import { Tabs, Radio } from 'antd'
export default {
data () {
return {
mode: 'top',
}
},
methods: {
},
components: {
Tabs,
TabPane: Tabs.TabPane,
Radio,
RadioGroup: Radio.Group,
RadioButton: Radio.Button,
},
}
</script>

View File

@ -56,10 +56,11 @@ export default {
}, },
handleChange (activeKey) { handleChange (activeKey) {
const onChange = this.$props.onChange // const onChange = this.$props.onChange
if (onChange) { // if (onChange) {
onChange(activeKey) // onChange(activeKey)
} // }
this.$emit('change', activeKey)
}, },
}, },
@ -77,7 +78,6 @@ export default {
size, size,
type = 'line', type = 'line',
tabPosition, tabPosition,
children,
tabBarStyle, tabBarStyle,
// hideAdd, // hideAdd,
onTabClick, onTabClick,
@ -143,16 +143,16 @@ export default {
</div> </div>
) : null ) : null
const renderTabBar = () => ( // const renderTabBar = () => (
<ScrollableInkTabBar // <ScrollableInkTabBar
inkBarAnimated={inkBarAnimated} // inkBarAnimated={inkBarAnimated}
extraContent={tabBarExtraContent} // extraContent={tabBarExtraContent}
onTabClick={onTabClick} // onTabClick={onTabClick}
onPrevClick={onPrevClick} // onPrevClick={onPrevClick}
onNextClick={onNextClick} // onNextClick={onNextClick}
style={tabBarStyle} // style={tabBarStyle}
/> // />
) // )
const tabBarProps = { const tabBarProps = {
inkBarAnimated, inkBarAnimated,
extraContent: tabBarExtraContent, extraContent: tabBarExtraContent,
@ -161,15 +161,18 @@ export default {
onNextClick, onNextClick,
style: tabBarStyle, style: tabBarStyle,
} }
const tabContentProps = {
animated: tabPaneAnimated,
animatedWithMargin: true,
}
return ( return (
<Tabs <Tabs
{...this.$props} {...this.$props}
class={cls} class={cls}
tabBarPosition={tabPosition} tabBarPosition={tabPosition}
renderTabBar={renderTabBar}
// renderTabContent={() => <TabContent animated={tabPaneAnimated} animatedWithMargin />}
onChange={this.handleChange} onChange={this.handleChange}
tabBarProps={tabBarProps} tabBarProps={tabBarProps}
tabContentProps={tabContentProps}
> >
{childrenWithClose || this.$slots.default} {childrenWithClose || this.$slots.default}
</Tabs> </Tabs>

View File

@ -12,8 +12,8 @@ export function toArray (children) {
export function getActiveIndex (children, activeKey) { export function getActiveIndex (children, activeKey) {
const c = toArray(children) const c = toArray(children)
for (let i = 0; i < c.length; i++) { for (let i = 0; i < c.length; i++) {
const pKey = c[i].pKey || c[i].componentOptions.propsData.pKey const tabKey = c[i].tabKey || c[i].componentOptions.propsData.tabKey
if (pKey === activeKey) { if (tabKey === activeKey) {
return i return i
} }
} }
@ -22,7 +22,7 @@ export function getActiveIndex (children, activeKey) {
export function getActiveKey (children, index) { export function getActiveKey (children, index) {
const c = toArray(children) const c = toArray(children)
return c[index].pKey return c[index].tabKey
} }
export function setTransform (style, v) { export function setTransform (style, v) {