|
|
@ -23,6 +23,7 @@ class DropdownMenu extends React.PureComponent { |
|
|
|
placement: PropTypes.string, |
|
|
|
placement: PropTypes.string, |
|
|
|
arrowOffsetLeft: PropTypes.string, |
|
|
|
arrowOffsetLeft: PropTypes.string, |
|
|
|
arrowOffsetTop: PropTypes.string, |
|
|
|
arrowOffsetTop: PropTypes.string, |
|
|
|
|
|
|
|
openedViaKeyboard: PropTypes.bool, |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
static defaultProps = { |
|
|
|
static defaultProps = { |
|
|
@ -42,13 +43,15 @@ class DropdownMenu extends React.PureComponent { |
|
|
|
|
|
|
|
|
|
|
|
componentDidMount () { |
|
|
|
componentDidMount () { |
|
|
|
document.addEventListener('click', this.handleDocumentClick, false); |
|
|
|
document.addEventListener('click', this.handleDocumentClick, false); |
|
|
|
|
|
|
|
document.addEventListener('keydown', this.handleKeyDown, false); |
|
|
|
document.addEventListener('touchend', this.handleDocumentClick, listenerOptions); |
|
|
|
document.addEventListener('touchend', this.handleDocumentClick, listenerOptions); |
|
|
|
if (this.focusedItem) this.focusedItem.focus(); |
|
|
|
if (this.focusedItem && this.props.openedViaKeyboard) this.focusedItem.focus(); |
|
|
|
this.setState({ mounted: true }); |
|
|
|
this.setState({ mounted: true }); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
componentWillUnmount () { |
|
|
|
componentWillUnmount () { |
|
|
|
document.removeEventListener('click', this.handleDocumentClick, false); |
|
|
|
document.removeEventListener('click', this.handleDocumentClick, false); |
|
|
|
|
|
|
|
document.removeEventListener('keydown', this.handleKeyDown, false); |
|
|
|
document.removeEventListener('touchend', this.handleDocumentClick, listenerOptions); |
|
|
|
document.removeEventListener('touchend', this.handleDocumentClick, listenerOptions); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -62,13 +65,10 @@ class DropdownMenu extends React.PureComponent { |
|
|
|
|
|
|
|
|
|
|
|
handleKeyDown = e => { |
|
|
|
handleKeyDown = e => { |
|
|
|
const items = Array.from(this.node.getElementsByTagName('a')); |
|
|
|
const items = Array.from(this.node.getElementsByTagName('a')); |
|
|
|
const index = items.indexOf(e.currentTarget); |
|
|
|
const index = items.indexOf(document.activeElement); |
|
|
|
let element; |
|
|
|
let element; |
|
|
|
|
|
|
|
|
|
|
|
switch(e.key) { |
|
|
|
switch(e.key) { |
|
|
|
case 'Enter': |
|
|
|
|
|
|
|
this.handleClick(e); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case 'ArrowDown': |
|
|
|
case 'ArrowDown': |
|
|
|
element = items[index+1]; |
|
|
|
element = items[index+1]; |
|
|
|
if (element) { |
|
|
|
if (element) { |
|
|
@ -96,6 +96,12 @@ class DropdownMenu extends React.PureComponent { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
handleItemKeyDown = e => { |
|
|
|
|
|
|
|
if (e.key === 'Enter') { |
|
|
|
|
|
|
|
this.handleClick(e); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
handleClick = e => { |
|
|
|
handleClick = e => { |
|
|
|
const i = Number(e.currentTarget.getAttribute('data-index')); |
|
|
|
const i = Number(e.currentTarget.getAttribute('data-index')); |
|
|
|
const { action, to } = this.props.items[i]; |
|
|
|
const { action, to } = this.props.items[i]; |
|
|
@ -120,7 +126,7 @@ class DropdownMenu extends React.PureComponent { |
|
|
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
return ( |
|
|
|
<li className='dropdown-menu__item' key={`${text}-${i}`}> |
|
|
|
<li className='dropdown-menu__item' key={`${text}-${i}`}> |
|
|
|
<a href={href} target='_blank' rel='noopener' role='button' tabIndex='0' ref={i === 0 ? this.setFocusRef : null} onClick={this.handleClick} onKeyDown={this.handleKeyDown} data-index={i}> |
|
|
|
<a href={href} target='_blank' rel='noopener' role='button' tabIndex='0' ref={i === 0 ? this.setFocusRef : null} onClick={this.handleClick} onKeyDown={this.handleItemKeyDown} data-index={i}> |
|
|
|
{text} |
|
|
|
{text} |
|
|
|
</a> |
|
|
|
</a> |
|
|
|
</li> |
|
|
|
</li> |
|
|
@ -170,6 +176,7 @@ export default class Dropdown extends React.PureComponent { |
|
|
|
onClose: PropTypes.func.isRequired, |
|
|
|
onClose: PropTypes.func.isRequired, |
|
|
|
dropdownPlacement: PropTypes.string, |
|
|
|
dropdownPlacement: PropTypes.string, |
|
|
|
openDropdownId: PropTypes.number, |
|
|
|
openDropdownId: PropTypes.number, |
|
|
|
|
|
|
|
openedViaKeyboard: PropTypes.bool, |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
static defaultProps = { |
|
|
|
static defaultProps = { |
|
|
@ -180,14 +187,14 @@ export default class Dropdown extends React.PureComponent { |
|
|
|
id: id++, |
|
|
|
id: id++, |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
handleClick = ({ target }) => { |
|
|
|
handleClick = ({ target, type }) => { |
|
|
|
if (this.state.id === this.props.openDropdownId) { |
|
|
|
if (this.state.id === this.props.openDropdownId) { |
|
|
|
this.handleClose(); |
|
|
|
this.handleClose(); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
const { top } = target.getBoundingClientRect(); |
|
|
|
const { top } = target.getBoundingClientRect(); |
|
|
|
const placement = top * 2 < innerHeight ? 'bottom' : 'top'; |
|
|
|
const placement = top * 2 < innerHeight ? 'bottom' : 'top'; |
|
|
|
|
|
|
|
|
|
|
|
this.props.onOpen(this.state.id, this.handleItemClick, placement); |
|
|
|
this.props.onOpen(this.state.id, this.handleItemClick, placement, type !== 'click'); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -197,6 +204,11 @@ export default class Dropdown extends React.PureComponent { |
|
|
|
|
|
|
|
|
|
|
|
handleKeyDown = e => { |
|
|
|
handleKeyDown = e => { |
|
|
|
switch(e.key) { |
|
|
|
switch(e.key) { |
|
|
|
|
|
|
|
case ' ': |
|
|
|
|
|
|
|
case 'Enter': |
|
|
|
|
|
|
|
this.handleClick(e); |
|
|
|
|
|
|
|
e.preventDefault(); |
|
|
|
|
|
|
|
break; |
|
|
|
case 'Escape': |
|
|
|
case 'Escape': |
|
|
|
this.handleClose(); |
|
|
|
this.handleClose(); |
|
|
|
break; |
|
|
|
break; |
|
|
@ -233,7 +245,7 @@ export default class Dropdown extends React.PureComponent { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
render () { |
|
|
|
render () { |
|
|
|
const { icon, items, size, title, disabled, dropdownPlacement, openDropdownId } = this.props; |
|
|
|
const { icon, items, size, title, disabled, dropdownPlacement, openDropdownId, openedViaKeyboard } = this.props; |
|
|
|
const open = this.state.id === openDropdownId; |
|
|
|
const open = this.state.id === openDropdownId; |
|
|
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
return ( |
|
|
@ -249,7 +261,7 @@ export default class Dropdown extends React.PureComponent { |
|
|
|
/> |
|
|
|
/> |
|
|
|
|
|
|
|
|
|
|
|
<Overlay show={open} placement={dropdownPlacement} target={this.findTarget}> |
|
|
|
<Overlay show={open} placement={dropdownPlacement} target={this.findTarget}> |
|
|
|
<DropdownMenu items={items} onClose={this.handleClose} /> |
|
|
|
<DropdownMenu items={items} onClose={this.handleClose} openedViaKeyboard={openedViaKeyboard} /> |
|
|
|
</Overlay> |
|
|
|
</Overlay> |
|
|
|
</div> |
|
|
|
</div> |
|
|
|
); |
|
|
|
); |
|
|
|