Skip to content

Commit 49c1721

Browse files
fix: table total width while resizing (#423)
1 parent 6de7f69 commit 49c1721

File tree

1 file changed

+77
-13
lines changed

1 file changed

+77
-13
lines changed

src/ui/layouts/common/Table/index.tsx

Lines changed: 77 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export interface TableProps {
3535
headerCols: HeaderCol[];
3636
tableRows: any[];
3737
minCellWidth?: any;
38+
maxCellWidth?: any;
3839
activeSorting?: any;
3940
paginated?: any;
4041
filters?: any[];
@@ -61,7 +62,8 @@ export const Table: React.FC<TableProps> = ({
6162
headerCols,
6263
tableRows,
6364
paginated,
64-
minCellWidth = 120,
65+
minCellWidth = 150,
66+
maxCellWidth = 240,
6567
activeSorting,
6668
filters,
6769
showHeader = true,
@@ -73,8 +75,10 @@ export const Table: React.FC<TableProps> = ({
7375
}) => {
7476
const [tableHeight, setTableHeight] = useState('auto');
7577
const [activeIndex, setActiveIndex] = useState(null);
78+
const [hoverIndex, setHoverIndex] = useState(null);
7679
const tableElement = useRef(document.createElement('table'));
7780
const columns = createHeaders(headerCols);
81+
const [hover, setHover] = useState(false);
7882

7983
useEffect(() => {
8084
// console.log(tableElement.current.style.gridTemplateColumns, 'offsetHeight');
@@ -86,23 +90,48 @@ export const Table: React.FC<TableProps> = ({
8690
const mouseDown = (index: any) => {
8791
setActiveIndex(index);
8892
};
89-
console.log('paginatomnanas', paginated, pagination);
9093

9194
const mouseMove = useCallback(
9295
(e) => {
93-
// debugger;
9496
const gridColumns = columns.map((col, i) => {
95-
// debugger;
96-
if (i === activeIndex) {
97-
const width = e.clientX - col.ref.current.offsetLeft;
97+
// we must need to optimize this logic later
98+
if (i === (activeIndex as any) + 1 && i <= columns.length - 1) {
99+
const newWidth =
100+
e.clientX -
101+
columns[activeIndex as any].ref.current.offsetLeft -
102+
70 <=
103+
minCellWidth
104+
? minCellWidth
105+
: e.clientX -
106+
columns[activeIndex as any].ref.current.offsetLeft -
107+
70;
108+
const widthDifference =
109+
newWidth <= minCellWidth
110+
? 0
111+
: columns[activeIndex as any].ref.current.offsetWidth - newWidth;
112+
const width = col.ref.current.offsetWidth + widthDifference;
98113

99-
if (width >= minCellWidth) {
114+
if (width >= minCellWidth && newWidth >= minCellWidth) {
115+
return `${width}px`;
116+
}
117+
} else if (i === activeIndex && i < columns.length - 1) {
118+
const width =
119+
e.clientX - col.ref.current.offsetLeft - 70 <= minCellWidth
120+
? minCellWidth
121+
: e.clientX - col.ref.current.offsetLeft - 70;
122+
const widthDifference =
123+
width <= minCellWidth ? 0 : col.ref.current.offsetWidth - width;
124+
const newWidth =
125+
columns[(activeIndex as any) + 1].ref.current.offsetWidth +
126+
widthDifference;
127+
128+
if (width >= minCellWidth && newWidth > minCellWidth) {
100129
return `${width}px`;
101130
}
102131
}
132+
103133
return `${col.ref.current.offsetWidth}px`;
104134
});
105-
106135
tableElement.current.style.gridTemplateColumns = `${gridColumns.join(
107136
' ',
108137
)}`;
@@ -161,6 +190,7 @@ export const Table: React.FC<TableProps> = ({
161190
ref={tableElement as any}
162191
style={{
163192
gridTemplateColumns: gridTemplateColumns,
193+
overflow: 'auto',
164194
}}
165195
>
166196
<thead>
@@ -176,6 +206,10 @@ export const Table: React.FC<TableProps> = ({
176206
backgroundColor: '#F5F3F9',
177207
fontSize: '14px',
178208
fontWeight: 700,
209+
borderRight:
210+
i === columns.length - 1
211+
? '0'
212+
: '1px solid #cacaca',
179213
}}
180214
key={i}
181215
>
@@ -188,11 +222,41 @@ export const Table: React.FC<TableProps> = ({
188222
</Box>
189223

190224
<div
191-
style={{ height: tableHeight }}
192-
onMouseDown={() => mouseDown(i)}
193-
className={`resize-handle ${
194-
activeIndex === i ? 'active' : 'idle'
195-
}`}
225+
className="resize-handle"
226+
style={{
227+
height: tableHeight,
228+
display: 'block',
229+
position: 'absolute',
230+
cursor:
231+
i < columns.length - 1
232+
? 'col-resize'
233+
: 'pointer',
234+
width: '7px',
235+
right: '0',
236+
top: '0',
237+
zIndex: 1,
238+
borderRight:
239+
hover &&
240+
hoverIndex === i &&
241+
i < columns.length - 1
242+
? '2px solid #431d93'
243+
: '0',
244+
}}
245+
onMouseEnter={() => {
246+
setHover(true);
247+
setHoverIndex(i as any);
248+
}}
249+
onMouseLeave={() => {
250+
setHover(false);
251+
setHoverIndex(null);
252+
}}
253+
// style={{ height: tableHeight }}
254+
onMouseDown={(e) => {
255+
if (i < columns.length - 1) mouseDown(i);
256+
}}
257+
// className={`resize-handle ${
258+
// activeIndex === i ? 'active' : 'idle'
259+
// }`}
196260
/>
197261
</th>
198262
))}

0 commit comments

Comments
 (0)