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