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