2022-02-28 22:35:05 +08:00
|
|
|
import React, { useCallback, useState } from 'react';
|
2022-04-22 21:33:13 +08:00
|
|
|
|
2022-02-28 22:35:05 +08:00
|
|
|
import { IconSize } from '../../types/icon';
|
|
|
|
|
import { IconButton } from '../IconButton/IconButton';
|
|
|
|
|
import { HorizontalGroup } from '../Layout/Layout';
|
|
|
|
|
import { TooltipPlacement } from '../Tooltip';
|
2022-04-22 21:33:13 +08:00
|
|
|
|
2022-02-28 22:35:05 +08:00
|
|
|
import { TableCellInspectModal } from './TableCellInspectModal';
|
|
|
|
|
import { FILTER_FOR_OPERATOR, FILTER_OUT_OPERATOR, TableCellProps, TableFieldOptions } from './types';
|
|
|
|
|
import { getTextAlign } from './utils';
|
|
|
|
|
|
|
|
|
|
interface CellActionProps extends TableCellProps {
|
|
|
|
|
previewMode: 'text' | 'code';
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-07 22:22:21 +08:00
|
|
|
interface CommonButtonProps {
|
|
|
|
|
size: IconSize;
|
|
|
|
|
tooltipPlacement: TooltipPlacement;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-28 22:35:05 +08:00
|
|
|
export function CellActions({ field, cell, previewMode, onCellFilterAdded }: CellActionProps) {
|
|
|
|
|
const [isInspecting, setIsInspecting] = useState(false);
|
|
|
|
|
|
|
|
|
|
const isRightAligned = getTextAlign(field) === 'flex-end';
|
|
|
|
|
const showFilters = Boolean(field.config.filterable) && cell.value !== undefined;
|
|
|
|
|
const inspectEnabled = Boolean((field.config.custom as TableFieldOptions)?.inspect);
|
2022-10-07 22:22:21 +08:00
|
|
|
const commonButtonProps: CommonButtonProps = {
|
|
|
|
|
size: 'sm',
|
|
|
|
|
tooltipPlacement: 'top',
|
2022-02-28 22:35:05 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onFilterFor = useCallback(
|
2022-07-20 20:26:04 +08:00
|
|
|
(event: React.MouseEvent<HTMLButtonElement>) => {
|
|
|
|
|
if (onCellFilterAdded) {
|
|
|
|
|
onCellFilterAdded({ key: field.name, operator: FILTER_FOR_OPERATOR, value: cell.value });
|
|
|
|
|
}
|
|
|
|
|
},
|
2022-02-28 22:35:05 +08:00
|
|
|
[cell, field, onCellFilterAdded]
|
|
|
|
|
);
|
|
|
|
|
const onFilterOut = useCallback(
|
2022-07-20 20:26:04 +08:00
|
|
|
(event: React.MouseEvent<HTMLButtonElement>) => {
|
|
|
|
|
if (onCellFilterAdded) {
|
|
|
|
|
onCellFilterAdded({ key: field.name, operator: FILTER_OUT_OPERATOR, value: cell.value });
|
|
|
|
|
}
|
|
|
|
|
},
|
2022-02-28 22:35:05 +08:00
|
|
|
[cell, field, onCellFilterAdded]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<div className={`cellActions ${isRightAligned ? 'cellActionsLeft' : ''}`}>
|
|
|
|
|
<HorizontalGroup spacing="xs">
|
|
|
|
|
{inspectEnabled && (
|
|
|
|
|
<IconButton
|
|
|
|
|
name="eye"
|
|
|
|
|
tooltip="Inspect value"
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setIsInspecting(true);
|
|
|
|
|
}}
|
|
|
|
|
{...commonButtonProps}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
{showFilters && (
|
|
|
|
|
<IconButton name={'search-plus'} onClick={onFilterFor} tooltip="Filter for value" {...commonButtonProps} />
|
|
|
|
|
)}
|
|
|
|
|
{showFilters && (
|
|
|
|
|
<IconButton name={'search-minus'} onClick={onFilterOut} tooltip="Filter out value" {...commonButtonProps} />
|
|
|
|
|
)}
|
|
|
|
|
</HorizontalGroup>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{isInspecting && (
|
|
|
|
|
<TableCellInspectModal
|
|
|
|
|
mode={previewMode}
|
|
|
|
|
value={cell.value}
|
|
|
|
|
onDismiss={() => {
|
|
|
|
|
setIsInspecting(false);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|