You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
200 lines
4.9 KiB
200 lines
4.9 KiB
6 years ago
|
// Package imports.
|
||
|
import PropTypes from 'prop-types';
|
||
|
import React from 'react';
|
||
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
||
|
import classNames from 'classnames';
|
||
|
|
||
|
// Components.
|
||
|
import Icon from 'flavours/glitch/components/icon';
|
||
|
|
||
|
// Utils.
|
||
2 years ago
|
import { withPassive } from 'flavours/glitch/utils/dom_helpers';
|
||
|
import { assignHandlers } from 'flavours/glitch/utils/react_helpers';
|
||
6 years ago
|
|
||
|
// The component.
|
||
|
export default class ComposerOptionsDropdownContent extends React.PureComponent {
|
||
|
|
||
|
static propTypes = {
|
||
|
items: PropTypes.arrayOf(PropTypes.shape({
|
||
|
icon: PropTypes.string,
|
||
|
meta: PropTypes.node,
|
||
|
name: PropTypes.string.isRequired,
|
||
|
text: PropTypes.node,
|
||
|
})),
|
||
5 years ago
|
onChange: PropTypes.func.isRequired,
|
||
|
onClose: PropTypes.func.isRequired,
|
||
6 years ago
|
style: PropTypes.object,
|
||
|
value: PropTypes.string,
|
||
3 years ago
|
renderItemContents: PropTypes.func,
|
||
5 years ago
|
openedViaKeyboard: PropTypes.bool,
|
||
3 years ago
|
closeOnChange: PropTypes.bool,
|
||
6 years ago
|
};
|
||
|
|
||
|
static defaultProps = {
|
||
|
style: {},
|
||
3 years ago
|
closeOnChange: true,
|
||
6 years ago
|
};
|
||
|
|
||
|
state = {
|
||
5 years ago
|
value: this.props.openedViaKeyboard ? this.props.items[0].name : undefined,
|
||
6 years ago
|
};
|
||
|
|
||
|
// When the document is clicked elsewhere, we close the dropdown.
|
||
5 years ago
|
handleDocumentClick = (e) => {
|
||
|
if (this.node && !this.node.contains(e.target)) {
|
||
|
this.props.onClose();
|
||
6 years ago
|
}
|
||
2 years ago
|
};
|
||
6 years ago
|
|
||
|
// Stores our node in `this.node`.
|
||
2 years ago
|
setRef = (node) => {
|
||
6 years ago
|
this.node = node;
|
||
2 years ago
|
};
|
||
6 years ago
|
|
||
|
// On mounting, we add our listeners.
|
||
|
componentDidMount () {
|
||
|
document.addEventListener('click', this.handleDocumentClick, false);
|
||
|
document.addEventListener('touchend', this.handleDocumentClick, withPassive);
|
||
5 years ago
|
if (this.focusedItem) {
|
||
5 years ago
|
this.focusedItem.focus({ preventScroll: true });
|
||
5 years ago
|
} else {
|
||
5 years ago
|
this.node.firstChild.focus({ preventScroll: true });
|
||
5 years ago
|
}
|
||
6 years ago
|
}
|
||
|
|
||
|
// On unmounting, we remove our listeners.
|
||
|
componentWillUnmount () {
|
||
|
document.removeEventListener('click', this.handleDocumentClick, false);
|
||
|
document.removeEventListener('touchend', this.handleDocumentClick, withPassive);
|
||
|
}
|
||
|
|
||
3 years ago
|
handleClick = (e) => {
|
||
|
const i = Number(e.currentTarget.getAttribute('data-index'));
|
||
|
|
||
5 years ago
|
const {
|
||
|
onChange,
|
||
|
onClose,
|
||
3 years ago
|
closeOnChange,
|
||
5 years ago
|
items,
|
||
|
} = this.props;
|
||
|
|
||
3 years ago
|
const { name } = this.props.items[i];
|
||
5 years ago
|
e.preventDefault(); // Prevents change in focus on click
|
||
3 years ago
|
if (closeOnChange) {
|
||
5 years ago
|
onClose();
|
||
|
}
|
||
|
onChange(name);
|
||
2 years ago
|
};
|
||
5 years ago
|
|
||
|
// Handle changes differently whether the dropdown is a list of options or actions
|
||
|
handleChange = (name) => {
|
||
|
if (this.props.value) {
|
||
|
this.props.onChange(name);
|
||
|
} else {
|
||
|
this.setState({ value: name });
|
||
|
}
|
||
2 years ago
|
};
|
||
5 years ago
|
|
||
3 years ago
|
handleKeyDown = (e) => {
|
||
|
const index = Number(e.currentTarget.getAttribute('data-index'));
|
||
5 years ago
|
const { items } = this.props;
|
||
5 years ago
|
let element = null;
|
||
5 years ago
|
|
||
|
switch(e.key) {
|
||
|
case 'Escape':
|
||
|
this.props.onClose();
|
||
|
break;
|
||
|
case 'Enter':
|
||
|
case ' ':
|
||
|
this.handleClick(e);
|
||
|
break;
|
||
|
case 'ArrowDown':
|
||
5 years ago
|
element = this.node.childNodes[index + 1] || this.node.firstChild;
|
||
5 years ago
|
break;
|
||
|
case 'ArrowUp':
|
||
5 years ago
|
element = this.node.childNodes[index - 1] || this.node.lastChild;
|
||
5 years ago
|
break;
|
||
|
case 'Tab':
|
||
|
if (e.shiftKey) {
|
||
|
element = this.node.childNodes[index - 1] || this.node.lastChild;
|
||
|
} else {
|
||
|
element = this.node.childNodes[index + 1] || this.node.firstChild;
|
||
|
}
|
||
|
break;
|
||
|
case 'Home':
|
||
|
element = this.node.firstChild;
|
||
|
break;
|
||
|
case 'End':
|
||
|
element = this.node.lastChild;
|
||
|
break;
|
||
|
}
|
||
5 years ago
|
|
||
|
if (element) {
|
||
|
element.focus();
|
||
3 years ago
|
this.handleChange(this.props.items[Number(element.getAttribute('data-index'))].name);
|
||
5 years ago
|
e.preventDefault();
|
||
|
e.stopPropagation();
|
||
|
}
|
||
2 years ago
|
};
|
||
5 years ago
|
|
||
|
setFocusRef = c => {
|
||
|
this.focusedItem = c;
|
||
2 years ago
|
};
|
||
5 years ago
|
|
||
3 years ago
|
renderItem = (item, i) => {
|
||
3 years ago
|
const { name, icon, meta, text } = item;
|
||
5 years ago
|
|
||
|
const active = (name === (this.props.value || this.state.value));
|
||
|
|
||
2 years ago
|
const computedClass = classNames('privacy-dropdown__option', { active });
|
||
5 years ago
|
|
||
3 years ago
|
let contents = this.props.renderItemContents && this.props.renderItemContents(item, i);
|
||
5 years ago
|
|
||
3 years ago
|
if (!contents) {
|
||
|
contents = (
|
||
|
<React.Fragment>
|
||
|
{icon && <Icon className='icon' fixedWidth id={icon} />}
|
||
|
|
||
2 years ago
|
<div className='privacy-dropdown__option__content'>
|
||
3 years ago
|
<strong>{text}</strong>
|
||
|
{meta}
|
||
|
</div>
|
||
|
</React.Fragment>
|
||
|
);
|
||
5 years ago
|
}
|
||
|
|
||
|
return (
|
||
|
<div
|
||
|
className={computedClass}
|
||
3 years ago
|
onClick={this.handleClick}
|
||
|
onKeyDown={this.handleKeyDown}
|
||
5 years ago
|
role='option'
|
||
|
tabIndex='0'
|
||
|
key={name}
|
||
3 years ago
|
data-index={i}
|
||
5 years ago
|
ref={active ? this.setFocusRef : null}
|
||
|
>
|
||
3 years ago
|
{contents}
|
||
5 years ago
|
</div>
|
||
|
);
|
||
2 years ago
|
};
|
||
5 years ago
|
|
||
6 years ago
|
// Rendering.
|
||
|
render () {
|
||
|
const {
|
||
|
items,
|
||
|
onChange,
|
||
|
onClose,
|
||
|
style,
|
||
|
} = this.props;
|
||
|
|
||
|
// The result.
|
||
|
return (
|
||
2 years ago
|
<div style={{ ...style }} role='listbox' ref={this.setRef}>
|
||
|
{!!items && items.map((item, i) => this.renderItem(item, i))}
|
||
|
</div>
|
||
6 years ago
|
);
|
||
|
}
|
||
|
|
||
|
}
|