accounts-frontend/packages/app/components/ui/form/Dropdown.tsx

154 lines
4.1 KiB
TypeScript
Raw Normal View History

import React, { InputHTMLAttributes, MouseEventHandler } from 'react';
2016-05-28 04:44:28 +05:30
import ReactDOM from 'react-dom';
2019-12-28 15:35:41 +05:30
import { MessageDescriptor } from 'react-intl';
2019-12-08 01:13:08 +05:30
import clsx from 'clsx';
2019-12-28 15:35:41 +05:30
import { COLOR_GREEN, Color } from 'app/components/ui';
2016-05-30 10:10:59 +05:30
2016-05-28 04:44:28 +05:30
import styles from './dropdown.scss';
2016-06-18 17:43:28 +05:30
import FormInputComponent from './FormInputComponent';
2019-12-28 15:35:41 +05:30
type I18nString = string | MessageDescriptor;
type ItemLabel = I18nString | React.ReactElement;
interface Props extends InputHTMLAttributes<HTMLInputElement> {
2020-05-24 04:38:24 +05:30
label: I18nString;
items: { [value: string]: ItemLabel };
block?: boolean;
color: Color;
2019-12-28 15:35:41 +05:30
}
2019-12-28 15:35:41 +05:30
interface OptionItem {
2020-05-24 04:38:24 +05:30
label: ItemLabel;
value: string;
2019-12-28 15:35:41 +05:30
}
interface State {
2020-05-24 04:38:24 +05:30
isActive: boolean;
activeItem: OptionItem | null;
2019-12-28 15:35:41 +05:30
}
export default class Dropdown extends FormInputComponent<Props, State> {
2020-05-24 04:38:24 +05:30
static defaultProps: Partial<Props> = {
color: COLOR_GREEN,
2016-05-30 10:10:59 +05:30
};
2020-05-24 04:38:24 +05:30
state: State = {
isActive: false,
activeItem: null,
};
2020-05-24 04:38:24 +05:30
componentDidMount() {
// listen to capturing phase to ensure, that our event handler will be
// called before all other
// @ts-ignore
document.addEventListener('click', this.onBodyClick, true);
}
2020-05-24 04:38:24 +05:30
componentWillUnmount() {
// @ts-ignore
document.removeEventListener('click', this.onBodyClick);
}
render() {
const { color, block, items, ...restProps } = this.props;
const { isActive } = this.state;
delete restProps.label;
const activeItem = this.getActiveItem();
const label = React.isValidElement(activeItem.label) ? activeItem.label : this.formatMessage(activeItem.label);
return (
<div>
<div
className={clsx(styles[color], {
[styles.block]: block,
[styles.opened]: isActive,
})}
data-e2e-select-name={restProps.name}
{...restProps}
onClick={this.onToggle}
>
<span className={styles.label} data-testid="select-label">
{label}
</span>
<span className={styles.toggleIcon} />
<div className={styles.menu}>
{Object.entries(items).map(([value, label]) => (
<div className={styles.menuItem} key={value} onClick={this.onSelectItem({ value, label })}>
{label}
</div>
))}
</div>
</div>
{this.renderError()}
</div>
);
}
2019-12-07 16:58:52 +05:30
2020-05-24 04:38:24 +05:30
toggle() {
this.setState({
isActive: !this.state.isActive,
});
}
onSelectItem(item: OptionItem): MouseEventHandler<HTMLDivElement> {
return (event) => {
event.preventDefault();
this.setState({
activeItem: item,
});
};
2016-05-28 04:44:28 +05:30
}
2020-05-24 04:38:24 +05:30
getActiveItem(): OptionItem {
const { items } = this.props;
let { activeItem } = this.state;
2016-05-28 04:44:28 +05:30
2020-05-24 04:38:24 +05:30
if (!activeItem) {
activeItem = {
label: this.props.label,
value: '',
};
2016-05-28 04:44:28 +05:30
2020-05-24 04:38:24 +05:30
if (!activeItem.label) {
const [[value, label]] = Object.entries(items);
2016-05-28 04:44:28 +05:30
2020-05-24 04:38:24 +05:30
activeItem = {
label,
value,
};
}
}
2016-05-28 04:44:28 +05:30
2020-05-24 04:38:24 +05:30
return activeItem;
}
2016-05-28 04:44:28 +05:30
2020-05-24 04:38:24 +05:30
getValue() {
return this.getActiveItem()?.value;
}
onToggle = (event: React.MouseEvent<HTMLDivElement>) => {
2016-05-28 04:44:28 +05:30
event.preventDefault();
this.toggle();
2020-05-24 04:38:24 +05:30
};
onBodyClick: MouseEventHandler = (event) => {
if (this.state.isActive) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const el = ReactDOM.findDOMNode(this)!;
if (!el.contains(event.target as HTMLElement) && el !== event.target) {
event.preventDefault();
event.stopPropagation();
this.toggle();
}
}
};
}