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
|
|
|
|
|
|
|
function getFilledItem(
|
2023-08-30 22:09:32 +08:00
|
|
|
rowItem: InternalDescriptionsItemType,
|
2023-07-17 14:48:03 +08:00
|
|
|
rowRestCol: number,
|
|
|
|
span?: number,
|
2023-09-11 17:28:04 +08:00
|
|
|
): [item: InternalDescriptionsItemType, exceed: boolean] {
|
2023-07-17 14:48:03 +08:00
|
|
|
let clone = rowItem;
|
2023-09-11 17:28:04 +08:00
|
|
|
let exceed = false;
|
2023-07-17 14:48:03 +08:00
|
|
|
|
|
|
|
if (span === undefined || span > rowRestCol) {
|
|
|
|
clone = {
|
|
|
|
...rowItem,
|
|
|
|
span: rowRestCol,
|
|
|
|
};
|
2023-09-11 17:28:04 +08:00
|
|
|
|
|
|
|
exceed = span !== undefined;
|
2023-07-17 14:48:03 +08:00
|
|
|
}
|
2023-09-11 17:28:04 +08:00
|
|
|
return [clone, exceed];
|
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] {
|
2023-08-30 22:09:32 +08:00
|
|
|
const rows: InternalDescriptionsItemType[][] = [];
|
|
|
|
let tmpRow: InternalDescriptionsItemType[] = [];
|
2023-07-17 14:48:03 +08:00
|
|
|
let rowRestCol = mergedColumn;
|
2023-09-11 17:28:04 +08:00
|
|
|
let exceed = false;
|
2023-07-17 14:48:03 +08:00
|
|
|
|
|
|
|
rowItems
|
|
|
|
.filter((n) => n)
|
|
|
|
.forEach((rowItem, index) => {
|
|
|
|
const span = rowItem?.span;
|
|
|
|
const mergedSpan = span || 1;
|
|
|
|
|
|
|
|
// Additional handle last one
|
|
|
|
if (index === rowItems.length - 1) {
|
2023-09-11 17:28:04 +08:00
|
|
|
const [item, itemExceed] = getFilledItem(rowItem, rowRestCol, span);
|
|
|
|
exceed = exceed || itemExceed;
|
|
|
|
|
|
|
|
tmpRow.push(item);
|
2023-07-17 14:48:03 +08:00
|
|
|
rows.push(tmpRow);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mergedSpan < rowRestCol) {
|
|
|
|
rowRestCol -= mergedSpan;
|
|
|
|
tmpRow.push(rowItem);
|
|
|
|
} else {
|
2023-09-11 17:28:04 +08:00
|
|
|
const [item, itemExceed] = getFilledItem(rowItem, rowRestCol, mergedSpan);
|
|
|
|
exceed = exceed || itemExceed;
|
|
|
|
|
|
|
|
tmpRow.push(item);
|
2023-07-17 14:48:03 +08:00
|
|
|
rows.push(tmpRow);
|
|
|
|
rowRestCol = mergedColumn;
|
|
|
|
tmpRow = [];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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;
|