Skip to content

A full-featured CRM built with React, shadcn/ui, and Supabase.

Add ability to select a range of rows

fzaninotto8233d336dc0d
diff --git a/src/components/atomic-crm/contacts/ContactListContent.tsx b/src/components/atomic-crm/contacts/ContactListContent.tsx
index 9e474cb9..9e806e3d 100644
--- a/src/components/atomic-crm/contacts/ContactListContent.tsx
+++ b/src/components/atomic-crm/contacts/ContactListContent.tsx
@@ -1,6 +1,11 @@
import { formatRelative } from "date-fns";
-import { RecordContextProvider, useListContext } from "ra-core";
-import { type MouseEvent, useCallback } from "react";
+import { difference, union } from "lodash";
+import {
+ type Identifier,
+ RecordContextProvider,
+ useListContext,
+} from "ra-core";
+import { type MouseEvent, useCallback, useRef } from "react";
import { Link } from "react-router";
import { ReferenceField } from "@/components/admin/reference-field";
import { TextField } from "@/components/admin/text-field";
@@ -19,9 +24,43 @@ export const ContactListContent = () => {
error,
isPending,
onToggleItem,
+ onSelect,
selectedIds,
} = useListContext<Contact>();
const isSmall = useIsMobile();
+ const lastSelected = useRef<Identifier | null>(null);
+
+ // Handle shift+click to select a range of rows
+ const handleToggleItem = useCallback(
+ (id: Identifier, event: MouseEvent) => {
+ if (!contacts) return;
+
+ const ids = contacts.map((contact) => contact.id);
+ const lastSelectedIndex = lastSelected.current
+ ? ids.indexOf(lastSelected.current)
+ : -1;
+
+ if (event.shiftKey && lastSelectedIndex !== -1) {
+ const index = ids.indexOf(id);
+ const idsBetweenSelections = ids.slice(
+ Math.min(lastSelectedIndex, index),
+ Math.max(lastSelectedIndex, index) + 1,
+ );
+
+ const isClickedItemSelected = selectedIds?.includes(id);
+ const newSelectedIds = isClickedItemSelected
+ ? difference(selectedIds, idsBetweenSelections)
+ : union(selectedIds, idsBetweenSelections);
+
+ onSelect?.(newSelectedIds);
+ } else {
+ onToggleItem(id);
+ }
+
+ lastSelected.current = id;
+ },
+ [contacts, selectedIds, onSelect, onToggleItem],
+ );
// StopPropagation does not work for some reason on Checkbox, this handler is a workaround
const handleLinkClick = useCallback(function handleLinkClick(
@@ -48,12 +87,9 @@ export const ContactListContent = () => {
<div className="flex flex-row items-center py-2 hover:bg-muted transition-colors first:rounded-t-xl last:rounded-b-xl">
<div
className="px-4 py-3 flex items-center cursor-pointer"
- onClick={() => onToggleItem(contact.id)}
+ onClick={(e) => handleToggleItem(contact.id, e)}
>
- <Checkbox
- checked={selectedIds.includes(contact.id)}
- onCheckedChange={() => onToggleItem(contact.id)}
- />
+ <Checkbox checked={selectedIds.includes(contact.id)} />
</div>
<Link
to={`/contacts/${contact.id}/show`}