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

[DataGrid] Fix disableMultipleSelection #286

Merged
merged 1 commit into from
Sep 15, 2020
Merged
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
24 changes: 12 additions & 12 deletions packages/grid/x-grid-modules/src/hooks/features/useSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const useSelection = (
): void => {
const logger = useLogger('useSelection');
const selectedItemsRef = React.useRef<RowId[]>([]);
const optionsRef = React.useRef<GridOptions>(options);
const allowMultipleSelectionKeyPressed = React.useRef<boolean>(false);
const [, forceUpdate] = React.useState();

Expand Down Expand Up @@ -110,10 +111,12 @@ export const useSelection = (

const selectRows = React.useCallback(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered using useEventCallback instead of useCallback?
It will remove the need for the new optionsRef.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes we can do if you want

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No strong preferences. If you find it easier in the future or solving bugs, I think that it will be great to consider it.

(ids: RowId[], isSelected = true, deSelectOthers = false) => {
if (options.disableMultipleSelection && ids.length > 1 && !options.checkboxSelection) {
throw new Error(
'Material-UI: `disableMultipleSelection` should be false to select more than 1 item.',
);
if (
optionsRef.current.disableMultipleSelection &&
ids.length > 1 &&
!optionsRef.current.checkboxSelection
) {
return;
}
let updates = ids.map((id) => ({ id, selected: isSelected }));

Expand All @@ -131,14 +134,7 @@ export const useSelection = (
};
apiRef.current.publishEvent(SELECTION_CHANGED, selectionChangeParam);
},
[
apiRef,
selectedItemsRef,
forceUpdate,
options.disableMultipleSelection,
getSelectedRows,
options.checkboxSelection,
],
[apiRef, selectedItemsRef, forceUpdate, getSelectedRows],
);

const rowClickHandler = React.useCallback(
Expand Down Expand Up @@ -187,6 +183,10 @@ export const useSelection = (
};
useApiMethod(apiRef, selectionApi, 'SelectionApi');

React.useEffect(() => {
optionsRef.current = options;
}, [options]);

React.useEffect(() => {
if (initialised && selectedItemsRef.current.length > 0) {
selectRows(selectedItemsRef.current);
Expand Down