Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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": "minor",
"comment": "chore: use Iterable instead of a Set for table selection API surface",
"packageName": "@fluentui/react-table",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,14 @@ export const useTableSelectionCell_unstable: (props: TableSelectionCellProps, re
// @public
export const useTableSelectionCellStyles_unstable: (state: TableSelectionCellState) => TableSelectionCellState;

// @public (undocumented)
export interface UseTableSelectionOptions {
defaultSelectedItems?: Iterable<TableRowId>;
onSelectionChange?(e: React_2.SyntheticEvent, data: OnSelectionChangeData): void;
selectedItems?: Iterable<TableRowId>;
selectionMode: SelectionMode_2;
}

// @public (undocumented)
export function useTableSort<TItem>(options: UseTableSortOptions): (tableState: TableFeaturesState<TItem>) => TableFeaturesState<TItem>;

Expand Down
4 changes: 2 additions & 2 deletions packages/react-components/react-table/src/hooks/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,11 @@ export interface UseTableSelectionOptions {
/**
* Used in uncontrolled mode to set initial selected rows on mount
*/
defaultSelectedItems?: Set<TableRowId>;
defaultSelectedItems?: Iterable<TableRowId>;
/**
* Used to control row selection
*/
selectedItems?: Set<TableRowId>;
selectedItems?: Iterable<TableRowId>;
/**
* Called when selection changes
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ export function useTableSelectionState<TItem>(
const { items, getRowId } = tableState;
const { selectionMode, defaultSelectedItems, selectedItems, onSelectionChange } = options;

const [selected, setSelected] = useControllableState({
initialState: new Set<TableRowId>(),
defaultState: defaultSelectedItems,
state: selectedItems,
const [selected, setSelected] = useControllableState<Set<TableRowId>>({
initialState: new Set(),
defaultState: React.useMemo(() => defaultSelectedItems && createSetFromIterable(defaultSelectedItems), [
defaultSelectedItems,
]),
state: React.useMemo(() => selectedItems && createSetFromIterable(selectedItems), [selectedItems]),
});

const selectionManager = React.useMemo(() => {
Expand Down Expand Up @@ -85,3 +87,10 @@ export function useTableSelectionState<TItem>(
},
};
}

/**
* Creates a set from a given iterable, in case the iterable is a set itself, returns the given set instead.
*/
function createSetFromIterable<V>(iterable: Iterable<V>): Set<V> {
return iterable instanceof Set ? iterable : new Set(iterable);
}
1 change: 1 addition & 0 deletions packages/react-components/react-table/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type {
TableSortState,
TableFeaturePlugin,
TableColumnSizingOptions,
UseTableSelectionOptions,
} from './hooks';

export {
Expand Down