@apia/table 4.0.87 → 4.0.89

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1167,6 +1167,7 @@ function useDomProps(element, elementProps, stateSelector) {
1167
1167
  }
1168
1168
 
1169
1169
  const widthStorageName = "tablesWidths";
1170
+ const minColumnWidth = 50;
1170
1171
  function getColumnWidth(width) {
1171
1172
  if (typeof width === "string") {
1172
1173
  if (width.match(/(%|px|vw|vh|vmin|vmax|cm|mm|in|pt|pc)/))
@@ -1182,10 +1183,49 @@ function getColumnWidth(width) {
1182
1183
  return width;
1183
1184
  return width;
1184
1185
  }
1186
+ function getColumnWidthAsNumber(width) {
1187
+ const columnWidth = getColumnWidth(width);
1188
+ if (typeof columnWidth === "number")
1189
+ return columnWidth;
1190
+ if (typeof columnWidth === "string" && columnWidth.endsWith("px")) {
1191
+ const parsedWidth = Number.parseInt(columnWidth, 10);
1192
+ if (!Number.isNaN(parsedWidth))
1193
+ return parsedWidth;
1194
+ }
1195
+ return void 0;
1196
+ }
1197
+ function normalizeResizedWidth(width) {
1198
+ return Math.max(minColumnWidth, Math.round(width));
1199
+ }
1200
+ function setHeaderCellWidth(cell, width) {
1201
+ const normalizedWidth = normalizeResizedWidth(width);
1202
+ const cssWidth = `${normalizedWidth}px`;
1203
+ cell.dataset.resizedWidth = String(normalizedWidth);
1204
+ cell.style.width = cssWidth;
1205
+ cell.style.minWidth = cssWidth;
1206
+ cell.style.maxWidth = cssWidth;
1207
+ }
1208
+ function syncTableWidthFromHeaderCell(cell) {
1209
+ const row = cell.closest("tr");
1210
+ const table = cell.closest("table");
1211
+ if (!(row instanceof HTMLElement) || !(table instanceof HTMLElement))
1212
+ return;
1213
+ const tableWidth = Array.from(row.children).reduce((total, current) => {
1214
+ if (!(current instanceof HTMLElement))
1215
+ return total;
1216
+ const resizedWidth = Number.parseFloat(current.dataset.resizedWidth ?? "");
1217
+ return total + (Number.isFinite(resizedWidth) ? resizedWidth : current.getBoundingClientRect().width);
1218
+ }, 0);
1219
+ if (tableWidth <= 0)
1220
+ return;
1221
+ const normalizedTableWidth = `${normalizeResizedWidth(tableWidth)}px`;
1222
+ table.style.width = normalizedTableWidth;
1223
+ table.style.minWidth = normalizedTableWidth;
1224
+ }
1185
1225
  debugDispatcher.on(
1186
1226
  "cleanTableSizes",
1187
1227
  ([tableName]) => {
1188
- const currentWidths = persistentStorage[widthStorageName];
1228
+ const currentWidths = persistentStorage[widthStorageName] ?? {};
1189
1229
  delete currentWidths[tableName];
1190
1230
  persistentStorage[widthStorageName] = currentWidths;
1191
1231
  },
@@ -1210,18 +1250,31 @@ const NoMemoHeaderCell = ({
1210
1250
  return tableState?.columns;
1211
1251
  }, shallowEqual);
1212
1252
  const column = columns[columnIndex];
1213
- const additionalInfoColumns = columns.filter((c) => c.showAsAdditional);
1214
1253
  const storedWidth = persistentStorage[widthStorageName]?.[actualName]?.[column.name];
1215
- const [width, setWidth] = React.useState(storedWidth ?? column.width ?? 200);
1254
+ const [width, setStateWidth] = React.useState(
1255
+ normalizeResizedWidth(
1256
+ storedWidth ?? getColumnWidthAsNumber(column.width) ?? 200
1257
+ )
1258
+ );
1216
1259
  const [isResize, setIsResize] = React.useState(storedWidth !== void 0);
1260
+ const thRef = React.useRef(null);
1261
+ const dragWidth = React.useRef(null);
1262
+ const setWidth = React.useCallback((newWidth) => {
1263
+ setIsResize(true);
1264
+ setStateWidth(normalizeResizedWidth(newWidth));
1265
+ }, []);
1217
1266
  React.useEffect(() => {
1218
- return void thWidthEmitter.on("resize", (ev) => {
1267
+ return thWidthEmitter.on("resize", (ev) => {
1219
1268
  if (ev.name === makeColumnName(actualName, column.name)) {
1220
- setIsResize(true);
1221
1269
  setWidth(ev.width);
1222
1270
  }
1223
1271
  });
1224
- }, [column.name, actualName]);
1272
+ }, [column.name, actualName, setWidth]);
1273
+ React.useEffect(() => {
1274
+ if (!isResize || !thRef.current)
1275
+ return;
1276
+ syncTableWidthFromHeaderCell(thRef.current);
1277
+ }, [isResize, width]);
1225
1278
  const isFilterRequired = useResponsiveTable((global) => {
1226
1279
  const tableState = global.responsiveTableSlice[name];
1227
1280
  const columnFilter = tableState?.filters.find(
@@ -1246,24 +1299,17 @@ const NoMemoHeaderCell = ({
1246
1299
  );
1247
1300
  const sx = React.useMemo(
1248
1301
  () => !isResize ? {
1249
- minWidth: columnIndex === columns.length - 1 ? "100%" : getColumnWidth(column.minWidth ?? column.width),
1302
+ minWidth: getColumnWidth(column.minWidth ?? column.width),
1250
1303
  maxWidth: column.maxWidth !== void 0 ? getColumnWidth(column.maxWidth) : void 0,
1251
1304
  width: getColumnWidth(column.width),
1252
1305
  position: "relative"
1253
1306
  } : {
1254
1307
  width: `${width}px`,
1255
- minWidth: columnIndex === columns.length - 1 ? "100%" : `${width}px`,
1308
+ minWidth: `${width}px`,
1309
+ maxWidth: `${width}px`,
1256
1310
  position: "relative"
1257
1311
  },
1258
- [
1259
- column.maxWidth,
1260
- column.minWidth,
1261
- column.width,
1262
- columnIndex,
1263
- columns.length,
1264
- isResize,
1265
- width
1266
- ]
1312
+ [column.maxWidth, column.minWidth, column.width, isResize, width]
1267
1313
  );
1268
1314
  const lastIsLoading = useLatest(isLoading);
1269
1315
  const buttonProps = useOtherTagButton(
@@ -1296,12 +1342,14 @@ const NoMemoHeaderCell = ({
1296
1342
  as: "th",
1297
1343
  ...{ sx },
1298
1344
  "data-columnname": column.name,
1345
+ "data-resized-width": isResize ? width : void 0,
1299
1346
  title: column.title ?? column.label ?? column.name,
1300
1347
  ...buttonProps,
1301
1348
  ...domProps,
1302
1349
  role: "rowheader",
1303
1350
  className: `${isFilterRequired && window.SHOW_REQUIRED_POSITION ? "requiredFilterColumn" : ""}`,
1304
1351
  ref: (el) => {
1352
+ thRef.current = el instanceof HTMLTableCellElement ? el : null;
1305
1353
  if (!column?.autoAdjustWidth)
1306
1354
  return;
1307
1355
  if (el instanceof HTMLElement && !el.dataset.resized && storedWidth === void 0) {
@@ -1324,6 +1372,7 @@ const NoMemoHeaderCell = ({
1324
1372
  const diff = idealWidth - currentWidth;
1325
1373
  setWidth(elWidth + diff + 5);
1326
1374
  el.dataset.resized = "true";
1375
+ child.remove();
1327
1376
  });
1328
1377
  }
1329
1378
  },
@@ -1366,11 +1415,15 @@ const NoMemoHeaderCell = ({
1366
1415
  (current) => current.tagName === "TR"
1367
1416
  );
1368
1417
  trElement?.querySelectorAll("th").forEach((current) => {
1418
+ const columnName = current.dataset.columnname;
1419
+ if (!columnName)
1420
+ return;
1421
+ setHeaderCellWidth(
1422
+ current,
1423
+ current.clientWidth
1424
+ );
1369
1425
  thWidthEmitter.emit("resize", {
1370
- name: makeColumnName(
1371
- actualName,
1372
- current.dataset.columnname
1373
- ),
1426
+ name: makeColumnName(actualName, columnName),
1374
1427
  width: current.clientWidth
1375
1428
  });
1376
1429
  });
@@ -1384,33 +1437,53 @@ const NoMemoHeaderCell = ({
1384
1437
  throw new Error("There is no th");
1385
1438
  const initialX = ev.clientX;
1386
1439
  const initialWidth = th.clientWidth;
1440
+ let animationFrame = 0;
1441
+ const scheduleTableWidthSync = () => {
1442
+ if (animationFrame)
1443
+ return;
1444
+ animationFrame = window.requestAnimationFrame(() => {
1445
+ animationFrame = 0;
1446
+ syncTableWidthFromHeaderCell(th);
1447
+ });
1448
+ };
1387
1449
  const resize = (mousemoveEv) => {
1388
1450
  const dif = mousemoveEv.clientX - initialX;
1389
- setWidth(initialWidth + dif);
1451
+ const newWidth = normalizeResizedWidth(initialWidth + dif);
1452
+ dragWidth.current = newWidth;
1453
+ setHeaderCellWidth(th, newWidth);
1454
+ scheduleTableWidthSync();
1390
1455
  };
1391
1456
  const unsuscribe = () => {
1457
+ if (animationFrame) {
1458
+ window.cancelAnimationFrame(animationFrame);
1459
+ animationFrame = 0;
1460
+ }
1392
1461
  const trElement = getSpecificParent(
1393
1462
  ev.target,
1394
1463
  (current) => current.tagName === "TR"
1395
1464
  );
1396
1465
  trElement?.querySelectorAll("th").forEach((current) => {
1466
+ const columnName = current.dataset.columnname;
1467
+ if (!columnName)
1468
+ return;
1469
+ const resizedWidth = Number.parseFloat(
1470
+ current.dataset.resizedWidth ?? ""
1471
+ );
1397
1472
  persistentStorage[widthStorageName] = {
1398
1473
  ...persistentStorage[widthStorageName],
1399
1474
  [actualName]: {
1400
1475
  ...persistentStorage[widthStorageName]?.[actualName],
1401
- [current.dataset.columnname]: current.clientWidth
1402
- }
1403
- };
1404
- });
1405
- additionalInfoColumns.forEach((c) => {
1406
- persistentStorage[widthStorageName] = {
1407
- ...persistentStorage[widthStorageName],
1408
- [actualName]: {
1409
- ...persistentStorage[widthStorageName]?.[actualName],
1410
- [c.name]: 100
1476
+ [columnName]: normalizeResizedWidth(
1477
+ Number.isFinite(resizedWidth) ? resizedWidth : current.getBoundingClientRect().width
1478
+ )
1411
1479
  }
1412
1480
  };
1413
1481
  });
1482
+ if (dragWidth.current !== null) {
1483
+ setWidth(dragWidth.current);
1484
+ dragWidth.current = null;
1485
+ }
1486
+ syncTableWidthFromHeaderCell(th);
1414
1487
  document.removeEventListener("mousemove", resize);
1415
1488
  document.removeEventListener("mouseup", unsuscribe);
1416
1489
  };