Portal/rsconcept/frontend/src/components/view/value-icon.tsx

76 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-08-21 21:36:02 +03:00
import clsx from 'clsx';
2025-02-20 18:10:34 +03:00
import { globalIDs } from '@/utils/constants';
2024-08-22 22:41:29 +03:00
2025-03-12 12:04:23 +03:00
import { MiniButton } from '../control';
2025-02-22 14:03:13 +03:00
import { type Styling, type Titled } from '../props';
2024-08-21 21:36:02 +03:00
2025-02-20 20:22:05 +03:00
interface ValueIconProps extends Styling, Titled {
/** Id of the component. */
2024-08-21 21:36:02 +03:00
id?: string;
/** Value to display. */
2024-08-21 21:36:02 +03:00
value: string | number;
/** Icon to display. */
icon: React.ReactNode;
/** Classname for the text. */
2024-08-23 12:35:05 +03:00
textClassName?: string;
/** Callback to be called when the component is clicked. */
2025-02-22 14:03:13 +03:00
onClick?: (event: React.MouseEvent<Element>) => void;
/** Number of symbols to display in a small size. */
2024-08-23 12:35:05 +03:00
smallThreshold?: number;
/** Indicates that padding should be minimal. */
2024-08-21 21:36:02 +03:00
dense?: boolean;
/** Disable interaction. */
2024-08-21 21:36:02 +03:00
disabled?: boolean;
}
/**
* Displays a value with an icon that can be clicked.
*/
2025-02-07 10:53:49 +03:00
export function ValueIcon({
2024-08-21 21:36:02 +03:00
id,
dense,
icon,
2024-08-23 12:35:05 +03:00
value,
textClassName,
2024-08-21 21:36:02 +03:00
disabled = true,
title,
titleHtml,
hideTitle,
className,
2024-08-23 12:35:05 +03:00
smallThreshold,
2024-08-21 21:36:02 +03:00
onClick,
...restProps
2024-08-23 12:35:05 +03:00
}: ValueIconProps) {
// TODO: use CSS instead of threshold
const isSmall = !smallThreshold || String(value).length < smallThreshold;
2024-08-21 21:36:02 +03:00
return (
<div
2024-08-23 12:35:05 +03:00
className={clsx(
'flex items-center',
'text-right',
'hover:cursor-default',
{ 'justify-between gap-6': !dense, 'gap-1': dense },
className
)}
2024-08-21 21:36:02 +03:00
{...restProps}
2025-02-20 18:10:34 +03:00
data-tooltip-id={!!title || !!titleHtml ? globalIDs.tooltip : undefined}
2024-08-22 22:41:29 +03:00
data-tooltip-html={titleHtml}
data-tooltip-content={title}
data-tooltip-hidden={hideTitle}
2024-08-21 21:36:02 +03:00
>
2024-08-22 22:41:29 +03:00
<MiniButton noHover noPadding icon={icon} disabled={disabled} onClick={onClick} />
2024-08-23 12:35:05 +03:00
<span id={id} className={clsx({ 'text-xs': !isSmall }, textClassName)}>
2024-08-22 21:38:59 +03:00
{value}
</span>
2024-08-21 21:36:02 +03:00
</div>
);
}