Radarr/frontend/src/Components/Table/TableHeaderCell.tsx
Mark McDowall 8caa839d99 Convert Table to TypeScript
(cherry picked from commit 699120a8fd54be9e70fb9a83298f94c8cb6a80bb)
2025-04-27 20:29:10 +03:00

70 lines
1.8 KiB
TypeScript

import React, { useCallback } from 'react';
import Icon from 'Components/Icon';
import Link from 'Components/Link/Link';
import { icons, sortDirections } from 'Helpers/Props';
import { SortDirection } from 'Helpers/Props/sortDirections';
import styles from './TableHeaderCell.css';
interface TableHeaderCellProps {
className?: string;
name: string;
label?: string | (() => string) | React.ReactNode;
columnLabel?: string | (() => string);
isSortable?: boolean;
isVisible?: boolean;
isModifiable?: boolean;
sortKey?: string;
fixedSortDirection?: SortDirection;
sortDirection?: string;
children?: React.ReactNode;
onSortPress?: (name: string, sortDirection?: SortDirection) => void;
}
function TableHeaderCell({
className = styles.headerCell,
name,
label,
columnLabel,
isSortable = false,
isVisible,
isModifiable,
sortKey,
sortDirection,
fixedSortDirection,
children,
onSortPress,
...otherProps
}: TableHeaderCellProps) {
const isSorting = isSortable && sortKey === name;
const sortIcon =
sortDirection === sortDirections.ASCENDING
? icons.SORT_ASCENDING
: icons.SORT_DESCENDING;
const handlePress = useCallback(() => {
if (fixedSortDirection) {
onSortPress?.(name, fixedSortDirection);
} else {
onSortPress?.(name);
}
}, [name, fixedSortDirection, onSortPress]);
return isSortable ? (
<Link
{...otherProps}
component="th"
className={className}
// label={typeof label === 'function' ? label() : label}
title={typeof columnLabel === 'function' ? columnLabel() : columnLabel}
onPress={handlePress}
>
{children}
{isSorting && <Icon name={sortIcon} className={styles.sortIcon} />}
</Link>
) : (
<th className={className}>{children}</th>
);
}
export default TableHeaderCell;