Skip to content

Commit

Permalink
bugfix: removes default prevention verification on selection (#28365)
Browse files Browse the repository at this point in the history
  • Loading branch information
bsunderhus authored Jun 29, 2023
1 parent 104c81e commit 7d519bd
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "bugfix: removes default prevention verification on selection",
"packageName": "@fluentui/react-utilities",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as React from 'react';

const isDefaultPrevented = () => false;
export function mockSyntheticEvent() {
return { isDefaultPrevented } as unknown as React.SyntheticEvent;
return {} as unknown as React.SyntheticEvent;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { SelectionHookParams, SelectionItemId, SelectionMethods } from './types'
import { useControllableState } from '../hooks/useControllableState';
import { createSetFromIterable } from '../utils/createSetFromIterable';

function useSingleSelection(params: Omit<SelectionHookParams, 'selectionMode'>) {
function useSelectionState(params: Omit<SelectionHookParams, 'selectionMode'>) {
const [selected, setSelected] = useControllableState<Set<SelectionItemId>>({
initialState: new Set(),
defaultState: React.useMemo(
Expand All @@ -17,10 +17,13 @@ function useSingleSelection(params: Omit<SelectionHookParams, 'selectionMode'>)
});
const changeSelection = (event: React.SyntheticEvent, nextSelectedItems: Set<SelectionItemId>) => {
params.onSelectionChange?.(event, { selectedItems: nextSelectedItems });
if (!event.isDefaultPrevented()) {
setSelected(nextSelectedItems);
}
setSelected(nextSelectedItems);
};
return [selected, changeSelection] as const;
}

function useSingleSelection(params: Omit<SelectionHookParams, 'selectionMode'>) {
const [selected, changeSelection] = useSelectionState(params);
const methods: SelectionMethods = {
deselectItem: event => changeSelection(event, new Set()),
selectItem: (event, itemId) => changeSelection(event, new Set([itemId])),
Expand All @@ -37,23 +40,7 @@ function useSingleSelection(params: Omit<SelectionHookParams, 'selectionMode'>)
}

function useMultipleSelection(params: Omit<SelectionHookParams, 'selectionMode'>) {
const [selected, setSelected] = useControllableState<Set<SelectionItemId>>({
initialState: new Set(),
defaultState: React.useMemo(
() => params.defaultSelectedItems && createSetFromIterable(params.defaultSelectedItems),
[params.defaultSelectedItems],
),
state: React.useMemo(
() => params.selectedItems && createSetFromIterable(params.selectedItems),
[params.selectedItems],
),
});
const changeSelection = (event: React.SyntheticEvent, nextSelectedItems: Set<SelectionItemId>) => {
params.onSelectionChange?.(event, { selectedItems: nextSelectedItems });
if (!event.isDefaultPrevented()) {
setSelected(nextSelectedItems);
}
};
const [selected, changeSelection] = useSelectionState(params);
const methods: SelectionMethods = {
toggleItem: (event, itemId) => {
const nextSelectedItems = new Set(selected);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as React from 'react';

const isDefaultPrevented = () => false;
export function mockSyntheticEvent() {
return { isDefaultPrevented } as unknown as React.SyntheticEvent;
return {} as unknown as React.SyntheticEvent;
}

0 comments on commit 7d519bd

Please sign in to comment.