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-07-17 14:48:03 +08:00
|
|
|
import warning from '../../_util/warning';
|
|
|
|
|
|
|
|
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-08-30 22:09:32 +08:00
|
|
|
): InternalDescriptionsItemType {
|
2023-07-17 14:48:03 +08:00
|
|
|
let clone = rowItem;
|
|
|
|
|
|
|
|
if (span === undefined || span > rowRestCol) {
|
|
|
|
clone = {
|
|
|
|
...rowItem,
|
|
|
|
span: rowRestCol,
|
|
|
|
};
|
|
|
|
warning(
|
|
|
|
span === undefined,
|
|
|
|
'Descriptions',
|
|
|
|
'Sum of column `span` in a line not match `column` of Descriptions.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the sum of span in a row
|
2023-08-30 22:09:32 +08:00
|
|
|
function getCalcRows(rowItems: InternalDescriptionsItemType[], mergedColumn: number) {
|
|
|
|
const rows: InternalDescriptionsItemType[][] = [];
|
|
|
|
let tmpRow: InternalDescriptionsItemType[] = [];
|
2023-07-17 14:48:03 +08:00
|
|
|
let rowRestCol = mergedColumn;
|
|
|
|
|
|
|
|
rowItems
|
|
|
|
.filter((n) => n)
|
|
|
|
.forEach((rowItem, index) => {
|
|
|
|
const span = rowItem?.span;
|
|
|
|
const mergedSpan = span || 1;
|
|
|
|
|
|
|
|
// Additional handle last one
|
|
|
|
if (index === rowItems.length - 1) {
|
|
|
|
tmpRow.push(getFilledItem(rowItem, rowRestCol, span));
|
|
|
|
rows.push(tmpRow);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mergedSpan < rowRestCol) {
|
|
|
|
rowRestCol -= mergedSpan;
|
|
|
|
tmpRow.push(rowItem);
|
|
|
|
} else {
|
|
|
|
tmpRow.push(getFilledItem(rowItem, rowRestCol, mergedSpan));
|
|
|
|
rows.push(tmpRow);
|
|
|
|
rowRestCol = mergedColumn;
|
|
|
|
tmpRow = [];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return rows;
|
|
|
|
}
|
|
|
|
|
2023-08-30 22:09:32 +08:00
|
|
|
const useRow = (mergedColumn: number, items: InternalDescriptionsItemType[]) => {
|
|
|
|
const rows = useMemo(() => getCalcRows(items, mergedColumn), [items, mergedColumn]);
|
2023-07-17 14:48:03 +08:00
|
|
|
|
|
|
|
return rows;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default useRow;
|