feat(ui): shift-click range selection on list row checkboxes (#2117)

* feat(ui): shift-click range selection on list row checkboxes

Click one checkbox, shift-click another, and every row between them
takes the clicked row's new state, the way mail clients work. Turns a
20-row bulk selection into two clicks.

New useRangeSelect hook (lib/hooks/use-range-select.ts) keeps the anchor
and applies the range over the rows as currently rendered, so it follows
filtering, sorting and paging rather than the underlying data order. A
shift-click with no valid anchor (first click, or the anchor filtered
away) degrades to a plain toggle. Select-all and clear reset the anchor.

Wired into the 8 selection surfaces: transaction inbox and skattekonto
inbox (separate ranges, since the two row types book through different
endpoints), journal entry list, invoices, supplier invoices, orders,
pending operations, invoice inbox workspace.

Radix' onCheckedChange carries no mouse event, so each row records
shiftKey from the click that precedes it; the checkbox cells get
select-none so shift-clicking does not smear a text selection.

The pure range rule is unit tested (10 cases: both directions, range
unselect, anchor invalidation, rendered-order independence).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015n8vUx9Nukr8mHC7CVNF7y

* fix(ui): void the range anchor on an empty selection, keep placeholders out

Review findings from CodeRabbit and the skeptic pass, all in the new
range-selection feature:

- Clearing a selection left the anchor behind, so the next shift-click
  extended from a row the user could no longer see selected (click a row,
  press "Rensa markering", shift-click 30 rows down, get 30 rows). The
  explicit resetAnchor() calls only covered the clear paths that were
  wired by hand; several others (period change, filter change, post-bulk
  success, "Avmarkera") were not. An empty selection now counts as having
  no anchor, which covers every clear path including ones added later.
- The invoice inbox passed optimistic upload placeholders into visibleIds
  even though they render no checkbox. Safe today only because
  placeholders are always prepended; filtering them out makes the
  invariant local instead of depending on insert order elsewhere.
- pending: "Godkänn alla" pre-selects a non-empty set, so it resets the
  anchor explicitly.

Two existing tests used a fixture the UI cannot reach (an anchor with an
empty selection); they now start from the state a real anchor implies.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015n8vUx9Nukr8mHC7CVNF7y

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Mattsson
2026-09-01 14:37:25 +02:00
committed by GitHub
co-authored by Claude Fable 5
parent 237bdd0366
commit 43341aa55c
11 changed files with 430 additions and 93 deletions
@@ -49,6 +49,7 @@ import {
import Link from 'next/link'
import { cn, formatCurrency, formatDate, formatDateLong } from '@/lib/utils'
import { QUIET_LINK_CLASS, CHECKBOX_REVEAL_CLASS } from '@/components/ui/dry-table'
import { useRangeSelect } from '@/lib/hooks/use-range-select'
import { GoogleMark, MicrosoftMark } from '@/components/ui/provider-marks'
import { StartCard } from '@/components/dashboard/StartCard'
import EditKonteringDialog from '@/components/extensions/general/EditKonteringDialog'
@@ -1130,16 +1131,23 @@ export default function InvoiceInboxWorkspace(_props: WorkspaceComponentProps) {
}
}, [fetchItems, selectedId, toast])
const toggleSelected = useCallback((id: string) => {
setSelectedIds((prev) => {
const next = new Set(prev)
if (next.has(id)) next.delete(id)
else next.add(id)
return next
})
}, [])
// Ranges walk the rendered inbox rows in order. Optimistic upload
// placeholders render no checkbox, so they stay out of the range: their
// temp-* ids are not server rows and must never reach a bulk action.
const range = useRangeSelect({
visibleIds: filteredItems.filter((item) => !item.isPlaceholder).map((item) => item.id),
selectedIds,
setSelectedIds,
})
const toggleSelected = useCallback(
(id: string, extend?: boolean) => range.toggle(id, extend),
[range],
)
const clearSelection = useCallback(() => setSelectedIds(new Set()), [])
const clearSelection = useCallback(() => {
setSelectedIds(new Set())
range.resetAnchor()
}, [range])
// The selected rows, and how many of them can actually be bulk-booked
// (matched to a transaction and not yet booked). Drives the "Bokför valda"
@@ -1757,7 +1765,7 @@ export default function InvoiceInboxWorkspace(_props: WorkspaceComponentProps) {
selected={item.id === selectedId}
onClick={() => handleSelect(item.id)}
isChecked={selectedIds.has(item.id)}
onToggleChecked={() => toggleSelected(item.id)}
onToggleChecked={(extend) => toggleSelected(item.id, extend)}
anyChecked={selectedIds.size > 0}
/>
))}
@@ -2124,12 +2132,15 @@ function InboxRow({
selected: boolean
onClick: () => void
isChecked: boolean
onToggleChecked: () => void
onToggleChecked: (extend?: boolean) => void
/** True when bulk-select mode is active anywhere in the list: keeps the
checkbox visible (otherwise it's hover-only on desktop). */
anyChecked: boolean
}) {
const t = useTranslations('inbox_workspace')
// Radix' onCheckedChange carries no mouse event: the preceding click records
// whether shift was held, for range selection.
const shiftHeld = useRef(false)
const amount = pickAmount(item)
const supplierName = pickSupplierName(item)
const invoiceDate = pickInvoiceDate(item)
@@ -2167,7 +2178,7 @@ function InboxRow({
{!isPlaceholder && (
<div
className={cn(
'flex items-center pl-2.5 pr-1.5 transition-opacity',
'flex select-none items-center pl-2.5 pr-1.5 transition-opacity',
// Solid on touch (pointer-coarse) or when any selection is active;
// otherwise muted-but-visible at rest. focus-within because this
// wraps the checkbox rather than being it.
@@ -2179,7 +2190,10 @@ function InboxRow({
>
<Checkbox
checked={isChecked}
onCheckedChange={onToggleChecked}
onClick={(e) => {
shiftHeld.current = e.shiftKey
}}
onCheckedChange={() => onToggleChecked(shiftHeld.current)}
aria-label="Markera post"
className="h-3.5 w-3.5 border-foreground"
/>