feat: menu add title

pull/398/head
tangjinzhou 2019-01-05 10:54:18 +08:00
parent 7b5fb53dfe
commit 8933cc524e
6 changed files with 59 additions and 23 deletions

View File

@ -4,6 +4,7 @@ import { getOptionProps } from '../_util/props-util'
import Tooltip from '../tooltip' import Tooltip from '../tooltip'
function noop () {} function noop () {}
export default { export default {
inheritAttrs: false,
props: itemProps, props: itemProps,
name: 'MenuItem', name: 'MenuItem',
inject: { inject: {
@ -17,18 +18,27 @@ export default {
}, },
render (h) { render (h) {
const props = getOptionProps(this) const props = getOptionProps(this)
const { level, title, rootPrefixCls } = props
const { getInlineCollapsed, $slots, $attrs: attrs, $listeners } = this const { getInlineCollapsed, $slots, $attrs: attrs, $listeners } = this
const inlineCollapsed = getInlineCollapsed() const inlineCollapsed = getInlineCollapsed()
let titleNode
if (inlineCollapsed) {
titleNode = title || (level === 1 ? $slots.default : '')
}
const itemProps = { const itemProps = {
props, props: {
...props,
title: inlineCollapsed ? null : title,
},
attrs, attrs,
on: $listeners, on: $listeners,
} }
const toolTipProps = { const toolTipProps = {
props: { props: {
title: inlineCollapsed && props.level === 1 ? $slots.default : '', title: titleNode,
placement: 'right', placement: 'right',
overlayClassName: `${props.rootPrefixCls}-inline-collapsed-tooltip`, overlayClassName: `${rootPrefixCls}-inline-collapsed-tooltip`,
}, },
} }
return ( return (

View File

@ -455,4 +455,32 @@ describe('Menu', () => {
}, 500) }, 500)
}) })
}) })
it('inline title', async () => {
const wrapper = mount({
render () {
return (
<Menu mode='inline' inlineCollapsed>
<Menu.Item key='1' title='bamboo lucky'>
<Icon type='pie-chart' />
<span>
Option 1
<img
style={{ width: 20 }}
alt='test'
src='https://gw.alipayobjects.com/zos/rmsportal/KDpgvguMpGfqaHPjicRK.svg'
/>
</span>
</Menu.Item>
</Menu>
)
},
}, { sync: false, attachToDocument: true })
wrapper.find('.ant-menu-item').trigger('mouseenter')
await asyncExpect(() => {
const text = $$('.ant-tooltip-inner')[0].textContent
expect(text).toBe('bamboo lucky')
}, 500)
})
}) })

View File

@ -67,4 +67,4 @@ export default {
}, },
} }
</script> </script>
``` ```

View File

@ -46,6 +46,7 @@
| ----- | ----------- | ---- | ------------- | | ----- | ----------- | ---- | ------------- |
| disabled | whether menu item is disabled or not | boolean | false | | disabled | whether menu item is disabled or not | boolean | false |
| key | unique id of the menu item | string | | | key | unique id of the menu item | string | |
| title | set display title for collapsed item | string | |
### Menu.SubMenu ### Menu.SubMenu

View File

@ -45,6 +45,7 @@ const Menu = {
mixins: [BaseMixin], mixins: [BaseMixin],
inject: { inject: {
layoutSiderContext: { default: {}}, layoutSiderContext: { default: {}},
configProvider: { default: {}},
}, },
model: { model: {
prop: 'selectedKeys', prop: 'selectedKeys',
@ -176,26 +177,19 @@ const Menu = {
const { openAnimation, openTransitionName } = this.$props const { openAnimation, openTransitionName } = this.$props
let menuOpenAnimation = openAnimation || openTransitionName let menuOpenAnimation = openAnimation || openTransitionName
if (openAnimation === undefined && openTransitionName === undefined) { if (openAnimation === undefined && openTransitionName === undefined) {
switch (menuMode) { if (menuMode === 'horizontal') {
case 'horizontal': menuOpenAnimation = 'slide-up'
menuOpenAnimation = 'slide-up' } else if (menuMode === 'inline') {
break menuOpenAnimation = { on: animation }
case 'vertical': } else {
case 'vertical-left':
case 'vertical-right':
// When mode switch from inline // When mode switch from inline
// submenu should hide without animation // submenu should hide without animation
if (this.switchingModeFromInline) { if (this.switchingModeFromInline) {
menuOpenAnimation = '' menuOpenAnimation = ''
this.switchingModeFromInline = false this.switchingModeFromInline = false
} else { } else {
menuOpenAnimation = 'zoom-big' menuOpenAnimation = 'zoom-big'
} }
break
case 'inline':
menuOpenAnimation = { on: animation }
break
default:
} }
} }
return menuOpenAnimation return menuOpenAnimation
@ -204,7 +198,8 @@ const Menu = {
render () { render () {
const { layoutSiderContext, $slots, $listeners } = this const { layoutSiderContext, $slots, $listeners } = this
const { collapsedWidth } = layoutSiderContext const { collapsedWidth } = layoutSiderContext
const { prefixCls, theme } = this.$props const { getPopupContainer: getContextPopupContainer } = this.configProvider
const { prefixCls, theme, getPopupContainer } = this.$props
const menuMode = this.getRealMenuMode() const menuMode = this.getRealMenuMode()
const menuOpenAnimation = this.getMenuOpenAnimation(menuMode) const menuOpenAnimation = this.getMenuOpenAnimation(menuMode)
@ -216,6 +211,7 @@ const Menu = {
const menuProps = { const menuProps = {
props: { props: {
...omit(this.$props, ['inlineCollapsed']), ...omit(this.$props, ['inlineCollapsed']),
getPopupContainer: getPopupContainer || getContextPopupContainer,
openKeys: this.sOpenKeys, openKeys: this.sOpenKeys,
mode: menuMode, mode: menuMode,
}, },

View File

@ -44,6 +44,7 @@
| --- | --- | --- | --- | | --- | --- | --- | --- |
| disabled | 是否禁用 | boolean | false | | disabled | 是否禁用 | boolean | false |
| key | item 的唯一标志 | string | | | key | item 的唯一标志 | string | |
| title | 设置收缩时展示的悬浮标题 | string | |
### Menu.SubMenu ### Menu.SubMenu