@etsoo/react
Version:
TypeScript ReactJs UI Independent Framework
188 lines (187 loc) • 6.79 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import { Utils } from "@etsoo/shared";
import React from "react";
import { List, useListRef } from "react-window";
import { useCombinedRefs } from "../uses/useCombinedRefs";
// Calculate loadBatchSize
const calculateBatchSize = (height, rowHeight) => {
if (typeof height === "number" &&
typeof rowHeight === "number" &&
rowHeight > 0) {
return 1 + Math.ceil(height / rowHeight);
}
return 10;
};
/**
* Scroller vertical list
* @param props Props
* @returns Component
*/
export const ScrollerList = (props) => {
// Destruct
const { autoLoad = true, defaultOrderBy, height = "100%", width = "100%", mRef, style = {}, idField = "id", rowHeight, listRef, loadBatchSize = calculateBatchSize(height, rowHeight), loadData, threshold = 3, onRowsRendered, onInitLoad, onUpdateRows, ...rest } = props;
// Style
Object.assign(style, {
width,
height
});
const localRef = useListRef(null);
const refs = useCombinedRefs(listRef, localRef);
const [rows, updateRows] = React.useState([]);
const setRows = (rows, reset = false) => {
stateRefs.current.loadedItems = rows.length;
updateRows(rows);
onUpdateRows?.(rows, stateRefs.current, reset);
};
const batchSize = Utils.getResult(loadBatchSize, height);
const stateRefs = React.useRef({
queryPaging: {
currentPage: 0,
orderBy: defaultOrderBy,
batchSize
},
autoLoad,
loadedItems: 0,
hasNextPage: true,
isNextPageLoading: false,
selectedItems: [],
idCache: {}
});
// Load data
const loadDataLocal = (pageAdd = 1) => {
// Prevent multiple loadings
if (!stateRefs.current.hasNextPage ||
stateRefs.current.isNextPageLoading ||
stateRefs.current.isMounted === false)
return;
// Update state
stateRefs.current.isNextPageLoading = true;
// Parameters
const { queryPaging, data } = stateRefs.current;
const loadProps = {
queryPaging,
data
};
loadData(loadProps, stateRefs.current.lastItem).then((result) => {
if (result == null || stateRefs.current.isMounted === false) {
return;
}
stateRefs.current.isMounted = true;
const newItems = result.length;
stateRefs.current.lastLoadedItems = newItems;
stateRefs.current.lastItem = result.at(-1);
stateRefs.current.hasNextPage = newItems >= batchSize;
stateRefs.current.isNextPageLoading = false;
if (pageAdd === 0) {
// New items
const newRows = stateRefs.current.lastLoadedItems
? [...rows]
.splice(rows.length - stateRefs.current.lastLoadedItems, stateRefs.current.lastLoadedItems)
.concat(result)
: result;
stateRefs.current.idCache = {};
for (const row of newRows) {
const id = row[idField];
stateRefs.current.idCache[id] = null;
}
// Update rows
setRows(newRows);
}
else {
if (stateRefs.current.queryPaging.currentPage == null)
stateRefs.current.queryPaging.currentPage = pageAdd;
else
stateRefs.current.queryPaging.currentPage += pageAdd;
// Update rows, avoid duplicate items
const newRows = [...rows];
for (const item of result) {
const id = item[idField];
if (stateRefs.current.idCache[id] === undefined) {
newRows.push(item);
}
}
setRows(newRows);
}
});
};
// Reset the state and load again
const reset = (add, items = []) => {
const { queryPaging, ...rest } = add ?? {};
const resetState = {
autoLoad: true,
loadedItems: 0,
hasNextPage: true,
isNextPageLoading: false,
lastLoadedItems: undefined,
lastItem: undefined,
...rest
};
Object.assign(stateRefs.current, resetState);
Object.assign(stateRefs.current.queryPaging, {
currentPage: 0,
...queryPaging
});
// Reset
if (stateRefs.current.isMounted !== false)
setRows(items, true);
};
React.useImperativeHandle(mRef, () => {
return {
get element() {
return localRef.current?.element;
},
delete(index) {
const item = rows.at(index);
if (item) {
const newRows = [...rows];
newRows.splice(index, 1);
setRows(newRows);
}
return item;
},
insert(item, start) {
const newRows = [...rows];
newRows.splice(start, 0, item);
setRows(newRows);
},
refresh() {
loadDataLocal(0);
},
reset,
scrollToRow(param) {
localRef.current?.scrollToRow(param);
}
};
}, [rows]);
// When layout ready
React.useEffect(() => {
// Return clear function
return () => {
stateRefs.current.isMounted = false;
};
}, []);
React.useEffect(() => {
// Auto load data when current page is 0
if (stateRefs.current.queryPaging?.currentPage === 0 &&
stateRefs.current.autoLoad) {
const initItems = onInitLoad == null || localRef.current == null
? undefined
: onInitLoad(localRef.current);
if (initItems)
reset(initItems[1], initItems[0]);
else
loadDataLocal();
}
}, [onInitLoad, loadDataLocal]);
// Row count
const rowCount = rows.length;
// Layout
return (_jsx(List, { listRef: refs, onRowsRendered: (visibleCells, allCells) => {
// No items, means no necessary to load more data during reset
if (rowCount > 0 && visibleCells.stopIndex + threshold > rowCount) {
// Auto load next page
loadDataLocal();
}
onRowsRendered?.(visibleCells, allCells);
}, overscanCount: threshold, rowHeight: rowHeight, rowCount: rowCount, rowProps: { items: rows }, style: style, ...rest }));
};