folder-selection
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { useCallback, useRef, useState } from 'react';
|
||||
|
||||
const DEFAULT_INITIAL_SELECTION = [];
|
||||
const DEFAULT_INITIAL_ENTRIES = [];
|
||||
|
||||
export const useDocumentSelection = ({
|
||||
resolveDocumentRowKey,
|
||||
@@ -8,11 +8,11 @@ export const useDocumentSelection = ({
|
||||
isDocumentRowKey,
|
||||
isFolderRowKey,
|
||||
getRowId,
|
||||
initialSelection = DEFAULT_INITIAL_SELECTION,
|
||||
initialEntries = DEFAULT_INITIAL_ENTRIES,
|
||||
}) => {
|
||||
const [selectedRowKeys, setSelectedRowKeys] = useState(initialSelection);
|
||||
const [selectionOrder, setSelectionOrder] = useState(initialSelection);
|
||||
const selectionOrderRef = useRef(initialSelection);
|
||||
const [selectedEntries, setSelectedEntries] = useState(initialEntries);
|
||||
const [selectionOrder, setSelectionOrder] = useState(initialEntries);
|
||||
const selectionOrderRef = useRef(initialEntries);
|
||||
const selectionAnchorRef = useRef(null);
|
||||
const selectionInitializedRef = useRef(false);
|
||||
const [focusedDocumentId, setFocusedDocumentId] = useState(null);
|
||||
@@ -69,7 +69,7 @@ export const useDocumentSelection = ({
|
||||
const visibleRowKeySet = visibleRowKeySetRef.current;
|
||||
const unique = [];
|
||||
|
||||
(rowKeys || []).forEach((key) => {
|
||||
(rowKeys || []).forEach((key) => {
|
||||
if (!key) return;
|
||||
let canonicalKey = null;
|
||||
if (visibleRowKeySet.has(key)) {
|
||||
@@ -96,7 +96,7 @@ export const useDocumentSelection = ({
|
||||
resolvedAnchor = null;
|
||||
}
|
||||
|
||||
setSelectedRowKeys(unique);
|
||||
setSelectedEntries(unique);
|
||||
updateSelectionOrder(unique, interactedKeys);
|
||||
|
||||
const nextFocusedDocumentId = (() => {
|
||||
@@ -163,8 +163,8 @@ export const useDocumentSelection = ({
|
||||
}
|
||||
|
||||
let anchorKey = selectionAnchorRef.current;
|
||||
if (!anchorKey && shiftKey && selectedRowKeys.length) {
|
||||
anchorKey = selectedRowKeys[selectedRowKeys.length - 1];
|
||||
if (!anchorKey && shiftKey && selectedEntries.length) {
|
||||
anchorKey = selectedEntries[selectedEntries.length - 1];
|
||||
}
|
||||
if (!anchorKey) {
|
||||
anchorKey = rowKey;
|
||||
@@ -183,7 +183,7 @@ export const useDocumentSelection = ({
|
||||
const range = navigableRowKeys.slice(start, end + 1);
|
||||
nextKeys = range;
|
||||
|
||||
const previousSet = new Set(selectedRowKeys);
|
||||
const previousSet = new Set(selectedEntries);
|
||||
interactedKeys = range.filter((key) => key === rowKey || !previousSet.has(key));
|
||||
if (!interactedKeys.includes(rowKey)) {
|
||||
interactedKeys.push(rowKey);
|
||||
@@ -193,11 +193,11 @@ export const useDocumentSelection = ({
|
||||
interactedKeys = [rowKey];
|
||||
}
|
||||
} else if (additive) {
|
||||
if (selectedRowKeys.includes(rowKey)) {
|
||||
nextKeys = selectedRowKeys.filter((key) => key !== rowKey);
|
||||
if (selectedEntries.includes(rowKey)) {
|
||||
nextKeys = selectedEntries.filter((key) => key !== rowKey);
|
||||
interactedKeys = [];
|
||||
} else {
|
||||
nextKeys = [...selectedRowKeys, rowKey];
|
||||
nextKeys = [...selectedEntries, rowKey];
|
||||
interactedKeys = [rowKey];
|
||||
}
|
||||
anchorKey = rowKey;
|
||||
@@ -209,7 +209,7 @@ export const useDocumentSelection = ({
|
||||
|
||||
applySelection(nextKeys, { anchor: anchorKey, interactedKeys });
|
||||
},
|
||||
[applySelection, selectedRowKeys],
|
||||
[applySelection, selectedEntries],
|
||||
);
|
||||
|
||||
const promoteSelectionOrder = useCallback(
|
||||
@@ -218,18 +218,18 @@ export const useDocumentSelection = ({
|
||||
const rowKey = resolveDocumentRowKey(docId);
|
||||
if (!rowKey) return;
|
||||
|
||||
if (!selectedRowKeys.includes(rowKey)) {
|
||||
if (!selectedEntries.includes(rowKey)) {
|
||||
return;
|
||||
}
|
||||
|
||||
updateSelectionOrder(selectedRowKeys, [rowKey]);
|
||||
updateSelectionOrder(selectedEntries, [rowKey]);
|
||||
},
|
||||
[resolveDocumentRowKey, selectedRowKeys, updateSelectionOrder],
|
||||
[resolveDocumentRowKey, selectedEntries, updateSelectionOrder],
|
||||
);
|
||||
|
||||
return {
|
||||
selectedRowKeys,
|
||||
setSelectedRowKeys,
|
||||
selectedEntries,
|
||||
setSelectedEntries,
|
||||
selectionOrder,
|
||||
setSelectionOrder,
|
||||
selectionOrderRef,
|
||||
|
||||
Reference in New Issue
Block a user