2023-05-23 11:52:27 +03:00
|
|
|
import { PureComponent } from 'react';
|
2023-05-10 13:59:29 +03:00
|
|
|
|
2017-10-16 10:30:09 +03:00
|
|
|
import classNames from 'classnames';
|
2023-05-10 13:59:29 +03:00
|
|
|
|
2023-05-02 13:53:32 +03:00
|
|
|
import { AnimatedNumber } from './animated_number';
|
2023-05-10 13:59:29 +03:00
|
|
|
import { Icon } from './icon';
|
2023-05-02 13:53:32 +03:00
|
|
|
|
2023-05-10 13:59:29 +03:00
|
|
|
interface Props {
|
2023-05-02 13:53:32 +03:00
|
|
|
className?: string;
|
|
|
|
title: string;
|
|
|
|
icon: string;
|
|
|
|
onClick?: React.MouseEventHandler<HTMLButtonElement>;
|
|
|
|
onMouseDown?: React.MouseEventHandler<HTMLButtonElement>;
|
|
|
|
onKeyDown?: React.KeyboardEventHandler<HTMLButtonElement>;
|
|
|
|
onKeyPress?: React.KeyboardEventHandler<HTMLButtonElement>;
|
|
|
|
size: number;
|
|
|
|
active: boolean;
|
|
|
|
expanded?: boolean;
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
activeStyle?: React.CSSProperties;
|
|
|
|
disabled: boolean;
|
|
|
|
inverted?: boolean;
|
|
|
|
animate: boolean;
|
|
|
|
overlay: boolean;
|
|
|
|
tabIndex: number;
|
|
|
|
counter?: number;
|
|
|
|
href?: string;
|
|
|
|
ariaHidden: boolean;
|
2023-05-10 13:59:29 +03:00
|
|
|
}
|
|
|
|
interface States {
|
2023-05-09 20:02:12 +03:00
|
|
|
activate: boolean;
|
|
|
|
deactivate: boolean;
|
2023-05-10 13:59:29 +03:00
|
|
|
}
|
2023-05-23 11:52:27 +03:00
|
|
|
export class IconButton extends PureComponent<Props, States> {
|
2017-05-12 15:44:10 +03:00
|
|
|
static defaultProps = {
|
|
|
|
size: 18,
|
|
|
|
active: false,
|
|
|
|
disabled: false,
|
|
|
|
animate: false,
|
2017-05-20 18:31:47 +03:00
|
|
|
overlay: false,
|
2023-04-04 17:33:44 +03:00
|
|
|
tabIndex: 0,
|
2023-01-05 14:36:42 +02:00
|
|
|
ariaHidden: false,
|
2017-05-12 15:44:10 +03:00
|
|
|
};
|
|
|
|
|
2019-10-24 23:47:48 +03:00
|
|
|
state = {
|
|
|
|
activate: false,
|
|
|
|
deactivate: false,
|
2023-01-30 02:45:35 +02:00
|
|
|
};
|
2019-10-24 23:47:48 +03:00
|
|
|
|
2023-05-09 20:02:12 +03:00
|
|
|
UNSAFE_componentWillReceiveProps(nextProps: Props) {
|
2019-10-24 23:47:48 +03:00
|
|
|
if (!nextProps.animate) return;
|
|
|
|
|
|
|
|
if (this.props.active && !nextProps.active) {
|
|
|
|
this.setState({ activate: false, deactivate: true });
|
|
|
|
} else if (!this.props.active && nextProps.active) {
|
|
|
|
this.setState({ activate: true, deactivate: false });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-09 20:02:12 +03:00
|
|
|
handleClick: React.MouseEventHandler<HTMLButtonElement> = (e) => {
|
2016-08-31 23:58:10 +03:00
|
|
|
e.preventDefault();
|
2016-12-23 00:03:57 +02:00
|
|
|
|
2023-05-02 13:53:32 +03:00
|
|
|
if (!this.props.disabled && this.props.onClick != null) {
|
2017-04-11 15:34:14 +03:00
|
|
|
this.props.onClick(e);
|
2016-12-23 00:03:57 +02:00
|
|
|
}
|
2023-01-30 02:45:35 +02:00
|
|
|
};
|
2016-08-31 23:58:10 +03:00
|
|
|
|
2023-05-02 13:53:32 +03:00
|
|
|
handleKeyPress: React.KeyboardEventHandler<HTMLButtonElement> = (e) => {
|
2019-08-07 14:58:53 +03:00
|
|
|
if (this.props.onKeyPress && !this.props.disabled) {
|
|
|
|
this.props.onKeyPress(e);
|
|
|
|
}
|
2023-01-30 02:45:35 +02:00
|
|
|
};
|
2019-08-07 14:58:53 +03:00
|
|
|
|
2023-05-02 13:53:32 +03:00
|
|
|
handleMouseDown: React.MouseEventHandler<HTMLButtonElement> = (e) => {
|
2019-08-06 12:59:58 +03:00
|
|
|
if (!this.props.disabled && this.props.onMouseDown) {
|
|
|
|
this.props.onMouseDown(e);
|
|
|
|
}
|
2023-01-30 02:45:35 +02:00
|
|
|
};
|
2019-08-06 12:59:58 +03:00
|
|
|
|
2023-05-02 13:53:32 +03:00
|
|
|
handleKeyDown: React.KeyboardEventHandler<HTMLButtonElement> = (e) => {
|
2019-08-06 12:59:58 +03:00
|
|
|
if (!this.props.disabled && this.props.onKeyDown) {
|
|
|
|
this.props.onKeyDown(e);
|
|
|
|
}
|
2023-01-30 02:45:35 +02:00
|
|
|
};
|
2019-08-06 12:59:58 +03:00
|
|
|
|
2023-05-09 20:02:12 +03:00
|
|
|
render() {
|
2017-05-19 12:42:54 +03:00
|
|
|
const style = {
|
2016-09-30 01:00:45 +03:00
|
|
|
fontSize: `${this.props.size}px`,
|
2016-11-02 23:29:19 +02:00
|
|
|
width: `${this.props.size * 1.28571429}px`,
|
2017-04-13 18:01:09 +03:00
|
|
|
height: `${this.props.size * 1.28571429}px`,
|
2016-11-02 23:29:19 +02:00
|
|
|
lineHeight: `${this.props.size}px`,
|
2017-05-19 12:42:54 +03:00
|
|
|
...this.props.style,
|
2017-05-20 18:31:47 +03:00
|
|
|
...(this.props.active ? this.props.activeStyle : {}),
|
2016-09-30 01:00:45 +03:00
|
|
|
};
|
|
|
|
|
2017-10-16 10:30:09 +03:00
|
|
|
const {
|
|
|
|
active,
|
|
|
|
className,
|
|
|
|
disabled,
|
|
|
|
expanded,
|
|
|
|
icon,
|
|
|
|
inverted,
|
|
|
|
overlay,
|
|
|
|
tabIndex,
|
|
|
|
title,
|
2020-09-28 14:29:43 +03:00
|
|
|
counter,
|
2022-02-25 02:20:41 +02:00
|
|
|
href,
|
2023-01-05 14:36:42 +02:00
|
|
|
ariaHidden,
|
2017-10-16 10:30:09 +03:00
|
|
|
} = this.props;
|
2017-04-13 18:01:09 +03:00
|
|
|
|
2023-05-09 20:02:12 +03:00
|
|
|
const { activate, deactivate } = this.state;
|
2019-10-24 23:47:48 +03:00
|
|
|
|
2017-10-16 10:30:09 +03:00
|
|
|
const classes = classNames(className, 'icon-button', {
|
|
|
|
active,
|
|
|
|
disabled,
|
|
|
|
inverted,
|
2019-10-24 23:47:48 +03:00
|
|
|
activate,
|
|
|
|
deactivate,
|
2017-10-16 10:30:09 +03:00
|
|
|
overlayed: overlay,
|
2020-10-04 16:02:36 +03:00
|
|
|
'icon-button--with-counter': typeof counter !== 'undefined',
|
2017-10-16 10:30:09 +03:00
|
|
|
});
|
2017-04-23 05:26:55 +03:00
|
|
|
|
2020-09-28 14:29:43 +03:00
|
|
|
if (typeof counter !== 'undefined') {
|
|
|
|
style.width = 'auto';
|
|
|
|
}
|
|
|
|
|
2022-02-25 02:20:41 +02:00
|
|
|
let contents = (
|
2023-05-23 11:52:27 +03:00
|
|
|
<>
|
2023-05-09 20:02:12 +03:00
|
|
|
<Icon id={icon} fixedWidth aria-hidden='true' />{' '}
|
|
|
|
{typeof counter !== 'undefined' && (
|
|
|
|
<span className='icon-button__counter'>
|
2023-09-06 00:57:03 +03:00
|
|
|
<AnimatedNumber value={counter} />
|
2023-05-09 20:02:12 +03:00
|
|
|
</span>
|
|
|
|
)}
|
2023-05-23 11:52:27 +03:00
|
|
|
</>
|
2022-02-25 02:20:41 +02:00
|
|
|
);
|
|
|
|
|
2023-05-02 13:53:32 +03:00
|
|
|
if (href != null) {
|
2022-08-25 05:30:53 +03:00
|
|
|
contents = (
|
|
|
|
<a href={href} target='_blank' rel='noopener noreferrer'>
|
2022-02-25 02:20:41 +02:00
|
|
|
{contents}
|
|
|
|
</a>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-08-31 23:58:10 +03:00
|
|
|
return (
|
2019-10-24 23:47:48 +03:00
|
|
|
<button
|
2022-11-05 14:43:37 +02:00
|
|
|
type='button'
|
2019-10-24 23:47:48 +03:00
|
|
|
aria-label={title}
|
|
|
|
aria-expanded={expanded}
|
2023-01-05 14:36:42 +02:00
|
|
|
aria-hidden={ariaHidden}
|
2019-10-24 23:47:48 +03:00
|
|
|
title={title}
|
|
|
|
className={classes}
|
|
|
|
onClick={this.handleClick}
|
|
|
|
onMouseDown={this.handleMouseDown}
|
|
|
|
onKeyDown={this.handleKeyDown}
|
|
|
|
onKeyPress={this.handleKeyPress}
|
|
|
|
style={style}
|
|
|
|
tabIndex={tabIndex}
|
|
|
|
disabled={disabled}
|
|
|
|
>
|
2022-02-25 02:20:41 +02:00
|
|
|
{contents}
|
2019-10-24 23:47:48 +03:00
|
|
|
</button>
|
2016-08-31 23:58:10 +03:00
|
|
|
);
|
|
|
|
}
|
2017-04-21 21:05:35 +03:00
|
|
|
}
|