-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathuseActionsColumnDefinition.tsx
More file actions
150 lines (135 loc) · 4.37 KB
/
Copy pathuseActionsColumnDefinition.tsx
File metadata and controls
150 lines (135 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
import * as React from 'react';
import { useCallback, useLayoutEffect, useMemo, useRef } from 'react';
import type { Row } from '@tanstack/react-table';
import { createColumnHelper } from '@tanstack/react-table';
import styled, { css } from 'styled-components';
import useResizeObserver from '@react-hook/resize-observer';
import { ButtonToolbar } from 'components/bootstrap';
import {
TABLE_ROW_HOVER_TRANSITION,
TABLE_ROW_PINNED_HOVER_BG_VAR,
flattenTableBackground,
} from 'components/bootstrap/Table';
import type { EntityBase } from 'components/common/EntityDataTable/types';
import { ACTIONS_COL_ID, CELL_PADDING } from 'components/common/EntityDataTable/Constants';
import { actionsHeaderWidthVar } from 'components/common/EntityDataTable/CSSVariables';
const AlignRight = styled.div`
display: flex;
justify-content: flex-end;
height: 100%;
`;
const BackgroundFoundation = styled.div`
height: 100%;
width: var(${actionsHeaderWidthVar});
`;
const HeaderBackground = styled(BackgroundFoundation)(
({ theme }) => css`
background-color: ${flattenTableBackground(theme, theme.colors.table.head.background)};
`,
);
const Actions = styled.div<{ $isEvenRow: boolean }>(
({ $isEvenRow, theme }) => css`
display: flex;
justify-content: flex-end;
padding: ${CELL_PADDING}px;
background-color: ${flattenTableBackground(
theme,
$isEvenRow ? theme.colors.table.row.background : theme.colors.table.row.backgroundStriped,
)};
height: 100%;
align-items: flex-start;
transition: ${TABLE_ROW_HOVER_TRANSITION};
tr:hover & {
background-color: var(${TABLE_ROW_PINNED_HOVER_BG_VAR});
}
`,
);
const ActionCell = <Entity extends EntityBase>({
row,
entityActions,
onWidthChange,
}: {
row: Row<Entity>;
entityActions: (entity: Entity) => React.ReactNode | undefined;
onWidthChange: (rowId: string, width: number) => void;
}) => {
const ref = useRef<HTMLDivElement>(null);
useLayoutEffect(() => {
if (ref.current) {
onWidthChange(row.id, ref.current.getBoundingClientRect().width);
}
}, [row.id, onWidthChange]);
useResizeObserver(ref, ({ contentRect: { width } }) => onWidthChange(row.id, width));
return (
<AlignRight>
<BackgroundFoundation>
<Actions $isEvenRow={row.index % 2 === 0}>
<ButtonToolbar ref={ref}>{entityActions(row.original)}</ButtonToolbar>
</Actions>
</BackgroundFoundation>
</AlignRight>
);
};
const useActionsColumnDefinition = <Entity extends EntityBase>({
colWidth,
entityActions,
hasRowActions,
onWidthChange,
}: {
colWidth: number;
entityActions: (entity: Entity) => React.ReactNode | undefined;
hasRowActions: boolean;
minWidth: number;
onWidthChange: (colId: string, width: number) => void;
}) => {
const columnHelper = createColumnHelper<Entity>();
const cell = useCallback(
({ row }: { row: Row<Entity> }) =>
entityActions ? (
<ActionCell<Entity> row={row} entityActions={entityActions} onWidthChange={onWidthChange} />
) : null,
[entityActions, onWidthChange],
);
const header = useCallback(
() => (
<AlignRight>
<HeaderBackground />
</AlignRight>
),
[],
);
return useMemo(
() =>
columnHelper.display({
id: ACTIONS_COL_ID,
size: colWidth,
enableHiding: false,
enablePinning: true,
enableResizing: false,
// The actions/tail column is always present. We only hide its content when there are no row actions.
header: hasRowActions ? header : undefined,
cell: hasRowActions ? cell : undefined,
meta: {
hideCellPadding: true,
},
}),
[colWidth, cell, columnHelper, hasRowActions, header],
);
};
export default useActionsColumnDefinition;