Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(useTable): selection manager to avoid calling multiple hooks #24377

Merged
merged 6 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "refactor(useTable): selection manager to avoid calling multiple hooks",
"packageName": "@fluentui/react-table",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ export interface RowState<TItem> {
// @public (undocumented)
export interface SelectionState {
allRowsSelected: boolean;
clearSelection: () => void;
clearRows: () => void;
deSelectRow: (rowId: RowId) => void;
isRowSelected: (rowId: RowId) => boolean;
selectedRows: RowId[];
selectRow: (rowId: RowId) => void;
someRowsSelected: boolean;
toggleRowSelect: (rowId: RowId) => void;
toggleSelectAllRows: () => void;
toggleAllRows: () => void;
toggleRow: (rowId: RowId) => void;
}

// @public (undocumented)
Expand Down Expand Up @@ -349,7 +349,7 @@ export interface UseTableOptions<TItem, TRowState extends RowState<TItem> = RowS
// (undocumented)
rowEnhancer?: RowEnhancer<TItem, TRowState>;
// (undocumented)
selectionMode?: 'single' | 'multiselect';
selectionMode?: SelectionMode_2;
}

// @public
Expand Down
113 changes: 113 additions & 0 deletions packages/react-components/react-table/src/hooks/selectionManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { SelectionMode } from './types';

type OnSelectionChangeCallback = (selectedItems: Set<SelectionItemId>) => void;

export interface SelectionManager {
toggleItem(id: SelectionItemId): void;
selectItem(id: SelectionItemId): void;
deSelectItem(id: SelectionItemId): void;
clearItems(): void;
isSelected(id: SelectionItemId): boolean;
toggleAllItems(itemIds: SelectionItemId[]): void;
}

export type SelectionItemId = string | number;

export function createSelectionManager(
mode: SelectionMode,
onSelectionChange: OnSelectionChangeCallback = () => undefined,
): SelectionManager {
const managerFactory = mode === 'multiselect' ? createMultipleSelectionManager : createSingleSelectionManager;

return managerFactory(onSelectionChange);
}

function createMultipleSelectionManager(onSelectionChange: OnSelectionChangeCallback): SelectionManager {
const selectedItems = new Set<SelectionItemId>();
const toggleAllItems = (itemIds: SelectionItemId[]) => {
const allItemsSelected = itemIds.every(itemId => selectedItems.has(itemId));

if (allItemsSelected) {
selectedItems.clear();
} else {
itemIds.forEach(itemId => selectedItems.add(itemId));
}

onSelectionChange(new Set(selectedItems));
};

const toggleItem = (itemId: SelectionItemId) => {
if (selectedItems.has(itemId)) {
selectedItems.delete(itemId);
} else {
selectedItems.add(itemId);
}

onSelectionChange(new Set(selectedItems));
};

const selectItem = (itemId: SelectionItemId) => {
selectedItems.add(itemId);
onSelectionChange(new Set(selectedItems));
};

const deSelectItem = (itemId: SelectionItemId) => {
selectedItems.delete(itemId);
onSelectionChange(new Set(selectedItems));
};

const clearItems = () => {
selectedItems.clear();
onSelectionChange(new Set(selectedItems));
};

const isSelected = (itemId: SelectionItemId) => {
return selectedItems.has(itemId);
};

return {
toggleItem,
selectItem,
deSelectItem,
clearItems,
isSelected,
toggleAllItems,
};
}

function createSingleSelectionManager(onSelectionChange: OnSelectionChangeCallback): SelectionManager {
let selectedItem: SelectionItemId | undefined = undefined;
const toggleItem = (itemId: SelectionItemId) => {
selectedItem = itemId;
onSelectionChange(new Set([selectedItem]));
};

const clearItems = () => {
selectedItem = undefined;
onSelectionChange(new Set<SelectionItemId>());
};

const isSelected = (itemId: SelectionItemId) => {
return selectedItem === itemId;
};

const selectItem = (itemId: SelectionItemId) => {
selectedItem = itemId;
onSelectionChange(new Set([selectedItem]));
};

return {
deSelectItem: clearItems,
selectItem,
toggleAllItems: () => {
if (process.env.NODE_ENV !== 'production') {
throw new Error('[react-table]: `toggleAllItems` should not be used in single selection mode');
}

return undefined;
},
toggleItem,
clearItems,
isSelected,
};
}
15 changes: 8 additions & 7 deletions packages/react-components/react-table/src/hooks/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { SortDirection } from '../components/Table/Table.types';
export type RowId = string | number;
export type ColumnId = string | number;
export type GetRowIdInternal<TItem> = (rowId: TItem, index: number) => RowId;
export type SelectionMode = 'single' | 'multiselect';

export interface ColumnDefinition<TItem> {
columnId: ColumnId;
Expand All @@ -29,17 +30,17 @@ export interface SortStateInternal<TItem> {
export interface UseTableOptions<TItem, TRowState extends RowState<TItem> = RowState<TItem>> {
columns: ColumnDefinition<TItem>[];
items: TItem[];
selectionMode?: 'single' | 'multiselect';
selectionMode?: SelectionMode;
getRowId?: (item: TItem) => RowId;
rowEnhancer?: RowEnhancer<TItem, TRowState>;
}

export interface SelectionStateInternal {
clearSelection: () => void;
clearRows: () => void;
deSelectRow: (rowId: RowId) => void;
selectRow: (rowId: RowId) => void;
toggleSelectAllRows: () => void;
toggleRowSelect: (rowId: RowId) => void;
toggleAllRows: () => void;
toggleRow: (rowId: RowId) => void;
isRowSelected: (rowId: RowId) => boolean;
selectedRows: Set<RowId>;
allRowsSelected: boolean;
Expand Down Expand Up @@ -74,7 +75,7 @@ export interface SelectionState {
/**
* Clears all selected rows
*/
clearSelection: () => void;
clearRows: () => void;
/**
* Selects single row
*/
Expand All @@ -86,11 +87,11 @@ export interface SelectionState {
/**
* Toggle selection of all rows
*/
toggleSelectAllRows: () => void;
toggleAllRows: () => void;
/**
* Toggle selection of single row
*/
toggleRowSelect: (rowId: RowId) => void;
toggleRow: (rowId: RowId) => void;
/**
* Collection of row ids corresponding to selected rows
*/
Expand Down
Loading