import type { HTMLAttributes, Ref, TargetedMouseEvent } from 'preact';
import { forwardRef } from 'preact/compat';
import { useEffect, useState } from 'preact/hooks';
import shortenNumber from '../utils/shorten-number';
import Icon from './icon';
interface StatusButtonProps extends Omit<
HTMLAttributes,
'title' | 'class'
> {
checked?: boolean;
count?: number;
extraCount?: number;
class?: string;
title: string | [string, string];
alt: string | [string, string];
size?: string;
icon?: string;
iconSize?: string;
onClick?: (e: TargetedMouseEvent) => void;
}
const StatusButton = forwardRef(
(props: StatusButtonProps, ref: Ref) => {
let {
checked,
count,
extraCount,
class: className,
title,
alt,
size,
icon,
iconSize = 'l',
onClick,
...otherProps
} = props;
if (typeof title === 'string') {
title = [title, title];
}
if (typeof alt === 'string') {
alt = [alt, alt];
}
const [buttonTitle, setButtonTitle] = useState(title[0] || '');
const [iconAlt, setIconAlt] = useState(alt[0] || '');
useEffect(() => {
if (checked) {
setButtonTitle(title[1] || '');
setIconAlt(alt[1] || '');
} else {
setButtonTitle(title[0] || '');
setIconAlt(alt[0] || '');
}
}, [checked, title, alt]);
return (
);
},
);
export default StatusButton;