43341aa55c
* 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>
137 lines
3.7 KiB
TypeScript
137 lines
3.7 KiB
TypeScript
import { describe, it, expect } from 'vitest'
|
|
import { applyRangeSelection } from '@/lib/hooks/use-range-select'
|
|
|
|
const VISIBLE = ['a', 'b', 'c', 'd', 'e']
|
|
|
|
function sorted(ids: Set<string>): string[] {
|
|
return [...ids].sort()
|
|
}
|
|
|
|
describe('applyRangeSelection', () => {
|
|
it('toggles a single row when not extending', () => {
|
|
const next = applyRangeSelection({
|
|
selectedIds: new Set(),
|
|
visibleIds: VISIBLE,
|
|
anchorId: null,
|
|
targetId: 'b',
|
|
extend: false,
|
|
})
|
|
expect(sorted(next)).toEqual(['b'])
|
|
})
|
|
|
|
it('unselects a selected row when not extending', () => {
|
|
const next = applyRangeSelection({
|
|
selectedIds: new Set(['b']),
|
|
visibleIds: VISIBLE,
|
|
anchorId: 'b',
|
|
targetId: 'b',
|
|
extend: false,
|
|
})
|
|
expect(sorted(next)).toEqual([])
|
|
})
|
|
|
|
it('selects the range downwards from the anchor', () => {
|
|
const next = applyRangeSelection({
|
|
selectedIds: new Set(['b']),
|
|
visibleIds: VISIBLE,
|
|
anchorId: 'b',
|
|
targetId: 'd',
|
|
extend: true,
|
|
})
|
|
expect(sorted(next)).toEqual(['b', 'c', 'd'])
|
|
})
|
|
|
|
it('selects the range upwards from the anchor', () => {
|
|
const next = applyRangeSelection({
|
|
selectedIds: new Set(['d']),
|
|
visibleIds: VISIBLE,
|
|
anchorId: 'd',
|
|
targetId: 'b',
|
|
extend: true,
|
|
})
|
|
expect(sorted(next)).toEqual(['b', 'c', 'd'])
|
|
})
|
|
|
|
it('unselects the whole range when the target was selected', () => {
|
|
const next = applyRangeSelection({
|
|
selectedIds: new Set(['a', 'b', 'c', 'd']),
|
|
visibleIds: VISIBLE,
|
|
anchorId: 'a',
|
|
targetId: 'c',
|
|
extend: true,
|
|
})
|
|
expect(sorted(next)).toEqual(['d'])
|
|
})
|
|
|
|
it('keeps selections outside the range untouched', () => {
|
|
const next = applyRangeSelection({
|
|
selectedIds: new Set(['a', 'b']),
|
|
visibleIds: VISIBLE,
|
|
anchorId: 'b',
|
|
targetId: 'd',
|
|
extend: true,
|
|
})
|
|
expect(sorted(next)).toEqual(['a', 'b', 'c', 'd'])
|
|
})
|
|
|
|
it('degrades to a plain toggle when there is no anchor yet', () => {
|
|
const next = applyRangeSelection({
|
|
selectedIds: new Set(),
|
|
visibleIds: VISIBLE,
|
|
anchorId: null,
|
|
targetId: 'd',
|
|
extend: true,
|
|
})
|
|
expect(sorted(next)).toEqual(['d'])
|
|
})
|
|
|
|
it('degrades to a plain toggle when the selection was cleared', () => {
|
|
// The anchor row is still on screen, but the user cleared the selection
|
|
// (clear button, filter change, finished bulk action). Extending from it
|
|
// would sweep in rows they never picked.
|
|
const next = applyRangeSelection({
|
|
selectedIds: new Set(),
|
|
visibleIds: VISIBLE,
|
|
anchorId: 'a',
|
|
targetId: 'e',
|
|
extend: true,
|
|
})
|
|
expect(sorted(next)).toEqual(['e'])
|
|
})
|
|
|
|
it('degrades to a plain toggle when the anchor is no longer visible', () => {
|
|
// The anchor row was filtered away or is on another page.
|
|
const next = applyRangeSelection({
|
|
selectedIds: new Set(['z']),
|
|
visibleIds: VISIBLE,
|
|
anchorId: 'z',
|
|
targetId: 'c',
|
|
extend: true,
|
|
})
|
|
expect(sorted(next)).toEqual(['c', 'z'])
|
|
})
|
|
|
|
it('follows the rendered order, not the id order', () => {
|
|
// The anchor is always a row the user just clicked, so it is selected.
|
|
const next = applyRangeSelection({
|
|
selectedIds: new Set(['e']),
|
|
visibleIds: ['e', 'd', 'c', 'b', 'a'],
|
|
anchorId: 'e',
|
|
targetId: 'c',
|
|
extend: true,
|
|
})
|
|
expect(sorted(next)).toEqual(['c', 'd', 'e'])
|
|
})
|
|
|
|
it('unselects just the row when anchor and target are the same', () => {
|
|
const next = applyRangeSelection({
|
|
selectedIds: new Set(['c']),
|
|
visibleIds: VISIBLE,
|
|
anchorId: 'c',
|
|
targetId: 'c',
|
|
extend: true,
|
|
})
|
|
expect(sorted(next)).toEqual([])
|
|
})
|
|
})
|