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

135 lines
3.5 KiB
TypeScript
Raw Normal View History

import React, { InputHTMLAttributes, MouseEventHandler } from 'react';
2019-12-28 15:35:41 +05:30
import { MessageDescriptor } from 'react-intl';
import ClickAwayListener from 'react-click-away-listener';
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<HTMLDivElement> {
2020-05-24 04:38:24 +05:30
label: I18nString;
items: Record<string, ItemLabel>;
2020-05-24 04:38:24 +05:30
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
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 (
<ClickAwayListener onClickAway={this.onCloseClick}>
2020-05-24 04:38:24 +05:30
<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()}
</ClickAwayListener>
2020-05-24 04:38:24 +05:30
);
}
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
};
onCloseClick = () => {
2020-05-24 04:38:24 +05:30
if (this.state.isActive) {
this.toggle();
2020-05-24 04:38:24 +05:30
}
};
}