2020-09-22 21:01:10 +08:00
|
|
|
import { EVENT_CODE, triggerEvent } from '../aria'
|
2020-07-28 17:48:40 +08:00
|
|
|
import SubMenu from './submenu'
|
|
|
|
|
|
|
|
|
|
|
|
class MenuItem {
|
|
|
|
public submenu: SubMenu = null
|
|
|
|
constructor(public domNode: HTMLElement) {
|
|
|
|
this.submenu = null
|
|
|
|
this.init()
|
|
|
|
}
|
|
|
|
|
|
|
|
init(): void {
|
|
|
|
this.domNode.setAttribute('tabindex', '0')
|
|
|
|
const menuChild = this.domNode.querySelector('.el-menu')
|
|
|
|
if (menuChild) {
|
|
|
|
this.submenu = new SubMenu(this, menuChild)
|
|
|
|
}
|
|
|
|
this.addListeners()
|
|
|
|
}
|
|
|
|
|
|
|
|
addListeners(): void {
|
|
|
|
this.domNode.addEventListener('keydown', (event: KeyboardEvent) => {
|
|
|
|
let prevDef = false
|
2020-09-22 21:01:10 +08:00
|
|
|
switch (event.code) {
|
|
|
|
case EVENT_CODE.down: {
|
2020-07-28 17:48:40 +08:00
|
|
|
triggerEvent(event.currentTarget as HTMLElement, 'mouseenter')
|
|
|
|
this.submenu && this.submenu.gotoSubIndex(0)
|
|
|
|
prevDef = true
|
|
|
|
break
|
|
|
|
}
|
2020-09-22 21:01:10 +08:00
|
|
|
case EVENT_CODE.up: {
|
2020-07-28 17:48:40 +08:00
|
|
|
triggerEvent(event.currentTarget as HTMLElement, 'mouseenter')
|
|
|
|
this.submenu && this.submenu.gotoSubIndex(this.submenu.subMenuItems.length - 1)
|
|
|
|
prevDef = true
|
|
|
|
break
|
|
|
|
}
|
2020-09-22 21:01:10 +08:00
|
|
|
case EVENT_CODE.tab: {
|
2020-07-28 17:48:40 +08:00
|
|
|
triggerEvent(event.currentTarget as HTMLElement, 'mouseleave')
|
|
|
|
break
|
|
|
|
}
|
2020-09-22 21:01:10 +08:00
|
|
|
case EVENT_CODE.enter:
|
|
|
|
case EVENT_CODE.space: {
|
2020-07-28 17:48:40 +08:00
|
|
|
prevDef = true
|
2020-08-28 11:44:04 +08:00
|
|
|
;(event.currentTarget as HTMLElement).click()
|
2020-07-28 17:48:40 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (prevDef) {
|
|
|
|
event.preventDefault()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default MenuItem
|