import React from 'react'; import classNames from 'classnames'; import Submenu from './Submenu'; interface IProps { label: string; submenu: Array; icon: string | null; } interface IState { open: boolean; } /** * Submenu within item actions. */ export default class SubmenuDropdown extends React.Component { refEl: HTMLDivElement; constructor(props) { super(props); this.state = { open: false, }; this.openSubmenu = this.openSubmenu.bind(this); this.closeSubmenu = this.closeSubmenu.bind(this); this.toggleState = this.toggleState.bind(this); } openSubmenu() { function focusFirstItem() { const btn = this.refEl.querySelectorAll('ul')[0]?.querySelectorAll('button:not([disabled])')[0]; if (btn instanceof HTMLElement) { btn.focus(); } } this.setState({open: true}, focusFirstItem); } closeSubmenu() { this.setState({open: false}); } toggleState() { if (this.state.open) { this.closeSubmenu(); } else { this.openSubmenu(); } } render() { return (
{ this.refEl = el; }} > {this.state.open && ( {this.props.submenu} )}
); } }