feat: update menu

pull/309/head
tangjinzhou 2018-12-09 20:04:00 +08:00
parent da2011e86b
commit 1e80e6c83f
5 changed files with 83 additions and 45 deletions

View File

@ -174,7 +174,7 @@ describe('DatePicker', () => {
const wrapper = mount(DatePicker, { sync: false, attachToDocument: true })
await asyncExpect(() => {
openPanel(wrapper)
})
}, 0)
await asyncExpect(() => {
selectDateFromBody(moment('2016-11-23'))
}, 0)

View File

@ -21,7 +21,7 @@ exports[`renders ./components/menu/demo/horizontal.md correctly 1`] = `
<div aria-haspopup="true" class="ant-menu-submenu-title"><i class="ant-menu-submenu-arrow"></i></div>
</li>
<li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal">
<div aria-haspopup="true" class="ant-menu-submenu-title"><span><i class="anticon anticon-setting"><svg viewBox="64 64 896 896" data-icon="setting" width="1em" height="1em" fill="currentColor" aria-hidden="true" class=""><path d="M924.8 625.7l-65.5-56c3.1-19 4.7-38.4 4.7-57.8s-1.6-38.8-4.7-57.8l65.5-56a32.03 32.03 0 0 0 9.3-35.2l-.9-2.6a443.74 443.74 0 0 0-79.7-137.9l-1.8-2.1a32.12 32.12 0 0 0-35.1-9.5l-81.3 28.9c-30-24.6-63.5-44-99.7-57.6l-15.7-85a32.05 32.05 0 0 0-25.8-25.7l-2.7-.5c-52.1-9.4-106.9-9.4-159 0l-2.7.5a32.05 32.05 0 0 0-25.8 25.7l-15.8 85.4a351.86 351.86 0 0 0-99 57.4l-81.9-29.1a32 32 0 0 0-35.1 9.5l-1.8 2.1a446.02 446.02 0 0 0-79.7 137.9l-.9 2.6c-4.5 12.5-.8 26.5 9.3 35.2l66.3 56.6c-3.1 18.8-4.6 38-4.6 57.1 0 19.2 1.5 38.4 4.6 57.1L99 625.5a32.03 32.03 0 0 0-9.3 35.2l.9 2.6c18.1 50.4 44.9 96.9 79.7 137.9l1.8 2.1a32.12 32.12 0 0 0 35.1 9.5l81.9-29.1c29.8 24.5 63.1 43.9 99 57.4l15.8 85.4a32.05 32.05 0 0 0 25.8 25.7l2.7.5a449.4 449.4 0 0 0 159 0l2.7-.5a32.05 32.05 0 0 0 25.8-25.7l15.7-85a350 350 0 0 0 99.7-57.6l81.3 28.9a32 32 0 0 0 35.1-9.5l1.8-2.1c34.8-41.1 61.6-87.5 79.7-137.9l.9-2.6c4.5-12.3.8-26.3-9.3-35zM788.3 465.9c2.5 15.1 3.8 30.6 3.8 46.1s-1.3 31-3.8 46.1l-6.6 40.1 74.7 63.9a370.03 370.03 0 0 1-42.6 73.6L721 702.8l-31.4 25.8c-23.9 19.6-50.5 35-79.3 45.8l-38.1 14.3-17.9 97a377.5 377.5 0 0 1-85 0l-17.9-97.2-37.8-14.5c-28.5-10.8-55-26.2-78.7-45.7l-31.4-25.9-93.4 33.2c-17-22.9-31.2-47.6-42.6-73.6l75.5-64.5-6.5-40c-2.4-14.9-3.7-30.3-3.7-45.5 0-15.3 1.2-30.6 3.7-45.5l6.5-40-75.5-64.5c11.3-26.1 25.6-50.7 42.6-73.6l93.4 33.2 31.4-25.9c23.7-19.5 50.2-34.9 78.7-45.7l37.9-14.3 17.9-97.2c28.1-3.2 56.8-3.2 85 0l17.9 97 38.1 14.3c28.7 10.8 55.4 26.2 79.3 45.8l31.4 25.8 92.8-32.9c17 22.9 31.2 47.6 42.6 73.6L781.8 426l6.5 39.9zM512 326c-97.2 0-176 78.8-176 176s78.8 176 176 176 176-78.8 176-176-78.8-176-176-176zm79.2 255.2A111.6 111.6 0 0 1 512 614c-29.9 0-58-11.7-79.2-32.8A111.6 111.6 0 0 1 400 502c0-29.9 11.7-58 32.8-79.2C454 401.6 482.1 390 512 390c29.9 0 58 11.6 79.2 32.8A111.6 111.6 0 0 1 624 502c0 29.9-11.7 58-32.8 79.2z"></path></svg></i>Navigation Three - Submenu</span><i class="ant-menu-submenu-arrow"></i></div>
<div aria-haspopup="true" class="ant-menu-submenu-title"><span class="submenu-title-wrapper"><i class="anticon anticon-setting"><svg viewBox="64 64 896 896" data-icon="setting" width="1em" height="1em" fill="currentColor" aria-hidden="true" class=""><path d="M924.8 625.7l-65.5-56c3.1-19 4.7-38.4 4.7-57.8s-1.6-38.8-4.7-57.8l65.5-56a32.03 32.03 0 0 0 9.3-35.2l-.9-2.6a443.74 443.74 0 0 0-79.7-137.9l-1.8-2.1a32.12 32.12 0 0 0-35.1-9.5l-81.3 28.9c-30-24.6-63.5-44-99.7-57.6l-15.7-85a32.05 32.05 0 0 0-25.8-25.7l-2.7-.5c-52.1-9.4-106.9-9.4-159 0l-2.7.5a32.05 32.05 0 0 0-25.8 25.7l-15.8 85.4a351.86 351.86 0 0 0-99 57.4l-81.9-29.1a32 32 0 0 0-35.1 9.5l-1.8 2.1a446.02 446.02 0 0 0-79.7 137.9l-.9 2.6c-4.5 12.5-.8 26.5 9.3 35.2l66.3 56.6c-3.1 18.8-4.6 38-4.6 57.1 0 19.2 1.5 38.4 4.6 57.1L99 625.5a32.03 32.03 0 0 0-9.3 35.2l.9 2.6c18.1 50.4 44.9 96.9 79.7 137.9l1.8 2.1a32.12 32.12 0 0 0 35.1 9.5l81.9-29.1c29.8 24.5 63.1 43.9 99 57.4l15.8 85.4a32.05 32.05 0 0 0 25.8 25.7l2.7.5a449.4 449.4 0 0 0 159 0l2.7-.5a32.05 32.05 0 0 0 25.8-25.7l15.7-85a350 350 0 0 0 99.7-57.6l81.3 28.9a32 32 0 0 0 35.1-9.5l1.8-2.1c34.8-41.1 61.6-87.5 79.7-137.9l.9-2.6c4.5-12.3.8-26.3-9.3-35zM788.3 465.9c2.5 15.1 3.8 30.6 3.8 46.1s-1.3 31-3.8 46.1l-6.6 40.1 74.7 63.9a370.03 370.03 0 0 1-42.6 73.6L721 702.8l-31.4 25.8c-23.9 19.6-50.5 35-79.3 45.8l-38.1 14.3-17.9 97a377.5 377.5 0 0 1-85 0l-17.9-97.2-37.8-14.5c-28.5-10.8-55-26.2-78.7-45.7l-31.4-25.9-93.4 33.2c-17-22.9-31.2-47.6-42.6-73.6l75.5-64.5-6.5-40c-2.4-14.9-3.7-30.3-3.7-45.5 0-15.3 1.2-30.6 3.7-45.5l6.5-40-75.5-64.5c11.3-26.1 25.6-50.7 42.6-73.6l93.4 33.2 31.4-25.9c23.7-19.5 50.2-34.9 78.7-45.7l37.9-14.3 17.9-97.2c28.1-3.2 56.8-3.2 85 0l17.9 97 38.1 14.3c28.7 10.8 55.4 26.2 79.3 45.8l31.4 25.8 92.8-32.9c17 22.9 31.2 47.6 42.6 73.6L781.8 426l6.5 39.9zM512 326c-97.2 0-176 78.8-176 176s78.8 176 176 176 176-78.8 176-176-78.8-176-176-176zm79.2 255.2A111.6 111.6 0 0 1 512 614c-29.9 0-58-11.7-79.2-32.8A111.6 111.6 0 0 1 400 502c0-29.9 11.7-58 32.8-79.2C454 401.6 482.1 390 512 390c29.9 0 58 11.6 79.2 32.8A111.6 111.6 0 0 1 624 502c0 29.9-11.7 58-32.8 79.2z"></path></svg></i>Navigation Three - Submenu</span><i class="ant-menu-submenu-arrow"></i></div>
</li>
<li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display: none;">
<div aria-haspopup="true" class="ant-menu-submenu-title"><i class="ant-menu-submenu-arrow"></i></div>

View File

@ -3,6 +3,13 @@ import { asyncExpect } from '@/tests/utils'
import Menu from '..'
import Icon from '../../icon'
jest.mock('mutationobserver-shim', () => {
global.MutationObserver = function MutationObserver () {
this.observe = () => {}
this.disconnect = () => {}
}
})
const { SubMenu } = Menu
function $$ (className) {
return document.body.querySelectorAll(className)
@ -291,6 +298,9 @@ describe('Menu', () => {
wrapper.vm.$refs.menu.switchModeFromInline = false
wrapper.vm.$forceUpdate()
})
await asyncExpect(() => {
wrapper.trigger('transitionend', { propertyName: 'width' })
})
await asyncExpect(() => {
expect(wrapper.findAll('ul.ant-menu-root').at(0).classes()).toContain('ant-menu-vertical')
expect(wrapper.findAll('ul.ant-menu-sub').length).toBe(0)
@ -338,6 +348,9 @@ describe('Menu', () => {
wrapper.vm.$refs.menu.switchModeFromInline = false
wrapper.vm.$forceUpdate()
})
await asyncExpect(() => {
wrapper.trigger('transitionend', { propertyName: 'width' })
})
await asyncExpect(() => {
wrapper.findAll('.ant-menu-submenu-title').at(0).trigger('mouseenter')
})

View File

@ -22,7 +22,7 @@ Horizontal top navigation menu.
<a-icon type="appstore" />Navigation Two
</a-menu-item>
<a-sub-menu>
<span slot="title"><a-icon type="setting" />Navigation Three - Submenu</span>
<span slot="title" class="submenu-title-wrapper"><a-icon type="setting" />Navigation Three - Submenu</span>
<a-menu-item-group title="Item 1">
<a-menu-item key="setting:1">Option 1</a-menu-item>
<a-menu-item key="setting:2">Option 2</a-menu-item>

View File

@ -54,33 +54,51 @@ const Menu = {
this.preProps = { ...this.$props }
},
watch: {
'$props': {
handler: function (nextProps) {
const { preProps, sOpenKeys } = this
const { prefixCls } = preProps
if (preProps.mode === 'inline' && nextProps.mode !== 'inline') {
this.switchModeFromInline = true
mode (val, oldVal) {
if (oldVal === 'inline' && val !== 'inline') {
this.switchingModeFromInline = true
}
if (hasProp(this, 'openKeys')) {
this.setState({ sOpenKeys: nextProps.openKeys })
this.preProps = { ...nextProps }
return
}
if (nextProps.inlineCollapsed && !preProps.inlineCollapsed) {
this.switchModeFromInline =
!!sOpenKeys.length && !!this.$el.querySelectorAll(`.${prefixCls}-submenu-open`).length
this.inlineOpenKeys = sOpenKeys
},
openKeys (val) {
this.setState({ sOpenKeys: val })
},
inlineCollapsed (val) {
if (!hasProp(this, 'openKeys')) {
if (val) {
this.switchingModeFromInline = true
this.inlineOpenKeys = this.sOpenKeys
this.setState({ sOpenKeys: [] })
}
if (!nextProps.inlineCollapsed && preProps.inlineCollapsed) {
} else {
this.setState({ sOpenKeys: this.inlineOpenKeys })
this.inlineOpenKeys = []
}
this.preProps = { ...nextProps }
},
deep: true,
}
},
// '$props': {
// handler: function (nextProps) {
// const { preProps, sOpenKeys } = this
// if (preProps.mode === 'inline' && nextProps.mode !== 'inline') {
// this.switchingModeFromInline = true
// }
// if (hasProp(this, 'openKeys')) {
// this.setState({ sOpenKeys: nextProps.openKeys })
// this.preProps = { ...nextProps }
// return
// }
// if (nextProps.inlineCollapsed && !preProps.inlineCollapsed) {
// this.switchingModeFromInline = true
// this.inlineOpenKeys = sOpenKeys
// this.setState({ sOpenKeys: [] })
// }
// if (!nextProps.inlineCollapsed && preProps.inlineCollapsed) {
// this.setState({ sOpenKeys: this.inlineOpenKeys })
// this.inlineOpenKeys = []
// }
// this.preProps = { ...nextProps }
// },
// deep: true,
// },
'layoutSiderContext.sCollapsed': function (val) {
const { openKeys, sOpenKeys = [], prefixCls } = this
if (hasProp(this, 'openKeys')) {
@ -88,7 +106,7 @@ const Menu = {
return
}
if (val) {
this.switchModeFromInline =
this.switchingModeFromInline =
!!sOpenKeys.length && !!this.$el.querySelectorAll(`.${prefixCls}-submenu-open`).length
this.inlineOpenKeys = sOpenKeys
this.setState({ sOpenKeys: [] })
@ -105,7 +123,7 @@ const Menu = {
!(hasProp(this, 'inlineCollapsed') && props.mode !== 'inline'),
'`inlineCollapsed` should only be used when Menu\'s `mode` is inline.',
)
this.switchModeFromInline = false
this.switchingModeFromInline = false
this.leaveAnimationExecutedWhenInlineCollapsed = false
this.inlineOpenKeys = []
let sOpenKeys
@ -120,6 +138,24 @@ const Menu = {
}
},
methods: {
restoreModeVerticalFromInline () {
if (this.switchingModeFromInline) {
this.switchingModeFromInline = false
this.$forceUpdate()
}
},
handleTransitionEnd (e) {
// console.log(111)
// when inlineCollapsed menu width animation finished
// https://github.com/ant-design/ant-design/issues/12864
const widthCollapsed = e.propertyName === 'width' && e.target === e.currentTarget
// Fix for <Menu style={{ width: '100%' }} />, the width transition won't trigger when menu is collapsed
// https://github.com/ant-design/ant-design-pro/issues/2783
const iconScaled = e.propertyName === 'font-size' && e.target.className.indexOf('anticon') >= 0
if (widthCollapsed || iconScaled) {
this.restoreModeVerticalFromInline()
}
},
handleClick (e) {
this.handleOpenChange([])
this.$emit('click', e)
@ -144,7 +180,7 @@ const Menu = {
},
getRealMenuMode () {
const inlineCollapsed = this.getInlineCollapsed()
if (this.switchModeFromInline && inlineCollapsed) {
if (this.switchingModeFromInline && inlineCollapsed) {
return 'inline'
}
const { mode } = this.$props
@ -170,29 +206,15 @@ const Menu = {
case 'vertical-right':
// When mode switch from inline
// submenu should hide without animation
if (this.switchModeFromInline) {
if (this.switchingModeFromInline) {
menuOpenAnimation = ''
this.switchModeFromInline = false
this.switchingModeFromInline = false
} else {
menuOpenAnimation = 'zoom-big'
}
break
case 'inline':
menuOpenAnimation = { on: {
...animation,
leave: (node, done) => animation.leave(node, () => {
// Make sure inline menu leave animation finished before mode is switched
this.switchModeFromInline = false
// this.setState({})
this.$forceUpdate()
// when inlineCollapsed change false to true, all submenu will be unmounted,
// so that we don't need handle animation leaving.
if (this.getRealMenuMode() === 'vertical') {
return
}
done()
}),
}}
menuOpenAnimation = { on: animation }
break
default:
}
@ -224,6 +246,9 @@ const Menu = {
deselect: this.handleDeselect,
openChange: this.handleOpenChange,
},
nativeOn: {
transitionend: this.handleTransitionEnd,
},
}
if (!hasProp(this, 'selectedKeys')) {
delete menuProps.props.selectedKeys