2023-07-17 14:48:03 +08:00
|
|
|
import { useMemo } from 'react';
|
2023-08-30 22:09:32 +08:00
|
|
|
|
|
|
|
import type { InternalDescriptionsItemType } from '..';
|
2023-09-11 17:28:04 +08:00
|
|
|
import { devUseWarning } from '../../_util/warning';
|
2023-07-17 14:48:03 +08:00
|
|
|
|
|
|
|
// Calculate the sum of span in a row
|
2023-09-11 17:28:04 +08:00
|
|
|
function getCalcRows(
|
|
|
|
rowItems: InternalDescriptionsItemType[],
|
|
|
|
mergedColumn: number,
|
|
|
|
): [rows: InternalDescriptionsItemType[][], exceed: boolean] {
|
2024-10-28 15:19:39 +08:00
|
|
|
let rows: InternalDescriptionsItemType[][] = [];
|
2023-08-30 22:09:32 +08:00
|
|
|
let tmpRow: InternalDescriptionsItemType[] = [];
|
2023-09-11 17:28:04 +08:00
|
|
|
let exceed = false;
|
2024-10-28 15:19:39 +08:00
|
|
|
let count = 0;
|
2023-07-17 14:48:03 +08:00
|
|
|
|
|
|
|
rowItems
|
|
|
|
.filter((n) => n)
|
2024-10-28 15:19:39 +08:00
|
|
|
.forEach((rowItem) => {
|
|
|
|
const { filled, ...restItem } = rowItem;
|
2023-09-11 17:28:04 +08:00
|
|
|
|
2024-10-28 15:19:39 +08:00
|
|
|
if (filled) {
|
|
|
|
tmpRow.push(restItem);
|
2023-07-17 14:48:03 +08:00
|
|
|
rows.push(tmpRow);
|
2024-10-28 15:19:39 +08:00
|
|
|
// reset
|
|
|
|
tmpRow = [];
|
|
|
|
count = 0;
|
2023-07-17 14:48:03 +08:00
|
|
|
return;
|
|
|
|
}
|
2024-10-28 15:19:39 +08:00
|
|
|
const restSpan = mergedColumn - count;
|
|
|
|
count += rowItem.span || 1;
|
|
|
|
if (count >= mergedColumn) {
|
|
|
|
if (count > mergedColumn) {
|
|
|
|
exceed = true;
|
|
|
|
tmpRow.push({ ...restItem, span: restSpan });
|
|
|
|
} else {
|
|
|
|
tmpRow.push(restItem);
|
|
|
|
}
|
2023-07-17 14:48:03 +08:00
|
|
|
rows.push(tmpRow);
|
2024-10-28 15:19:39 +08:00
|
|
|
// reset
|
2023-07-17 14:48:03 +08:00
|
|
|
tmpRow = [];
|
2024-10-28 15:19:39 +08:00
|
|
|
count = 0;
|
|
|
|
} else {
|
|
|
|
tmpRow.push(restItem);
|
2023-07-17 14:48:03 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-10-28 15:19:39 +08:00
|
|
|
if (tmpRow.length > 0) {
|
|
|
|
rows.push(tmpRow);
|
|
|
|
}
|
|
|
|
|
|
|
|
rows = rows.map((rows) => {
|
|
|
|
const count = rows.reduce((acc, item) => acc + (item.span || 1), 0);
|
|
|
|
if (count < mergedColumn) {
|
|
|
|
// If the span of the last element in the current row is less than the column, then add its span to the remaining columns
|
|
|
|
const last = rows[rows.length - 1];
|
|
|
|
last.span = mergedColumn - count + 1;
|
|
|
|
return rows;
|
|
|
|
}
|
|
|
|
return rows;
|
|
|
|
});
|
2023-09-11 17:28:04 +08:00
|
|
|
return [rows, exceed];
|
2023-07-17 14:48:03 +08:00
|
|
|
}
|
|
|
|
|
2023-08-30 22:09:32 +08:00
|
|
|
const useRow = (mergedColumn: number, items: InternalDescriptionsItemType[]) => {
|
2023-09-11 17:28:04 +08:00
|
|
|
const [rows, exceed] = useMemo(() => getCalcRows(items, mergedColumn), [items, mergedColumn]);
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2023-09-13 22:07:33 +08:00
|
|
|
const warning = devUseWarning('Descriptions');
|
|
|
|
|
|
|
|
warning(!exceed, 'usage', 'Sum of column `span` in a line not match `column` of Descriptions.');
|
2023-09-11 17:28:04 +08:00
|
|
|
}
|
2023-07-17 14:48:03 +08:00
|
|
|
|
|
|
|
return rows;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default useRow;
|