-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathdatabase-table.component.tsx
More file actions
114 lines (107 loc) · 3.08 KB
/
database-table.component.tsx
File metadata and controls
114 lines (107 loc) · 3.08 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
// Importaciones necesarias
import React from 'react';
import { GUID, Size } from '@/core/model';
import { TableVm, UpdatePositionFn } from '@/core/providers/canvas-schema';
import { useDraggable } from './table-drag.hook';
import { TABLE_CONST } from '@/core/providers/canvas-schema/canvas.const';
import {
DatabaseTableHeader,
DatabaseTableBorder,
DatabaseTableBody,
} from './components';
import { renderRows } from './database-table-render-rows.helper';
import classes from './database-table.module.css';
import { motion } from 'framer-motion';
// TODO: We should add an optional field to indicate FONT_SIZE in case we override the standard class
// TODO: There's is a solution more elaborated (using JS) to show elipsis ... if text is too long
interface Props {
tableInfo: TableVm;
updatePosition: UpdatePositionFn;
onToggleCollapse: (tableId: GUID, fieldId: GUID) => void;
onEditTable: (tableInfo: TableVm) => void;
canvasSize: Size;
isSelected: boolean;
selectTable: (tableId: GUID) => void;
isTabletOrMobileDevice: boolean;
viewBoxSize: Size;
zoomFactor: number;
}
export const DatabaseTable: React.FC<Props> = ({
tableInfo,
onEditTable,
updatePosition,
onToggleCollapse,
canvasSize,
isSelected,
selectTable,
isTabletOrMobileDevice,
viewBoxSize,
zoomFactor,
}) => {
const rowHeight = TABLE_CONST.FONT_SIZE + TABLE_CONST.ROW_PADDING;
const [renderedRows, totalHeight] = React.useMemo((): [
JSX.Element[],
number,
] => {
const [rows, totalY] = renderRows(
{
tableInfo,
fields: tableInfo.fields,
level: 0,
startY: TABLE_CONST.HEADER_HEIGHT,
rowHeight,
},
{
onToggleCollapse,
}
);
return [rows, totalY + TABLE_CONST.ROW_PADDING]; // Adjust for the last padding
}, [tableInfo.fields]);
const { onMouseDown, onTouchStart, ref } = useDraggable(
tableInfo.id,
tableInfo.x,
tableInfo.y,
updatePosition,
totalHeight,
canvasSize,
viewBoxSize,
zoomFactor
);
const handleSelectTable = () => {
if (!isSelected) {
selectTable(tableInfo.id);
}
};
const handleDoubleClick = () => {
onEditTable(tableInfo);
};
return (
<g transform={`translate(${tableInfo.x}, ${tableInfo.y})`}>
<motion.g
onMouseDown={onMouseDown}
onTouchStart={onTouchStart}
className={classes.tableContainer}
ref={ref as React.Ref<SVGGElement> | undefined}
initial={{ opacity: 0, scale: 0.8 }}
animate={{ opacity: [0, 1, 0, 1], scale: 1 }}
transition={{
opacity: { duration: 2 },
scale: { duration: 0.8 },
}}
>
<DatabaseTableBorder
totalHeight={totalHeight}
isSelected={isSelected}
/>
<DatabaseTableHeader
onEditTable={handleDoubleClick}
onSelectTable={handleSelectTable}
isSelected={isSelected}
tableName={tableInfo.tableName}
isTabletOrMobileDevice={isTabletOrMobileDevice}
/>
<DatabaseTableBody renderedRows={renderedRows} />
</motion.g>
</g>
);
};