• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

humanspeak / svelte-headless-table / 12913869200

22 Jan 2025 05:36PM UTC coverage: 34.206% (-25.6%) from 59.766%
12913869200

push

github

web-flow
Merge pull request #9 from humanspeak/feature-version

Feature: 5.x

404 of 538 branches covered (75.09%)

Branch coverage included in aggregate %.

2129 of 3542 new or added lines in 41 files covered. (60.11%)

1 existing line in 1 file now uncovered.

2639 of 8358 relevant lines covered (31.57%)

17.47 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/lib/plugins/addExpandedRows.ts
NEW
1
import type { BodyRow } from '../bodyRows.js'
×
NEW
2
import type { DeriveRowsFn, NewTablePropSet, TablePlugin } from '../types/TablePlugin.js'
×
NEW
3
import { recordSetStore, type RecordSetStore } from '../utils/store.js'
×
NEW
4
import { keyed } from '@humanspeak/svelte-keyed'
×
NEW
5
import { derived, readable, type Readable, type Writable } from 'svelte/store'
×
6

×
7
// eslint-disable-next-line @typescript-eslint/no-unused-vars
×
8
export interface ExpandedRowsConfig<Item> {
×
NEW
9
    initialExpandedIds?: Record<string, boolean>
×
10
}
×
11

×
12
export interface ExpandedRowsState<Item> {
×
NEW
13
    expandedIds: RecordSetStore<string>
×
NEW
14
    getRowState: (row: BodyRow<Item>) => ExpandedRowsRowState
×
15
}
×
16

×
17
export interface ExpandedRowsRowState {
×
NEW
18
    isExpanded: Writable<boolean>
×
NEW
19
    canExpand: Readable<boolean>
×
NEW
20
    isAllSubRowsExpanded: Readable<boolean>
×
21
}
×
22

×
23
const withExpandedRows = <Item, Row extends BodyRow<Item>>(
×
NEW
24
    row: Row,
×
NEW
25
    expandedIds: Record<string, boolean>
×
26
): Row[] => {
×
NEW
27
    if (row.subRows === undefined) {
×
NEW
28
        return [row]
×
NEW
29
    }
×
NEW
30
    if (expandedIds[row.id] !== true) {
×
NEW
31
        return [row]
×
NEW
32
    }
×
NEW
33
    const expandedSubRows = row.subRows.flatMap((subRow) =>
×
NEW
34
        withExpandedRows<Item, Row>(subRow as Row, expandedIds)
×
NEW
35
    )
×
NEW
36
    return [row, ...expandedSubRows]
×
NEW
37
}
×
38

×
39
export const addExpandedRows =
×
NEW
40
    <Item>({ initialExpandedIds = {} }: ExpandedRowsConfig<Item> = {}): TablePlugin<
×
NEW
41
        Item,
×
NEW
42
        ExpandedRowsState<Item>,
×
NEW
43
        Record<string, never>,
×
NEW
44
        NewTablePropSet<never>
×
NEW
45
    > =>
×
NEW
46
    () => {
×
NEW
47
        const expandedIds = recordSetStore(initialExpandedIds)
×
NEW
48
        const getRowState = (row: BodyRow<Item>): ExpandedRowsRowState => {
×
NEW
49
            const isExpanded = keyed(expandedIds, row.id) as Writable<boolean>
×
NEW
50
            const canExpand = readable((row.subRows?.length ?? 0) > 0)
×
NEW
51
            const subRowExpandedIds = derived(expandedIds, ($expandedIds) => {
×
NEW
52
                // Check prefix with '>' to match child ids while ignoring this row's id.
×
NEW
53
                return Object.entries($expandedIds).filter(
×
NEW
54
                    ([id, expanded]) => id.startsWith(`${row.id}>`) && expanded
×
NEW
55
                )
×
NEW
56
            })
×
NEW
57
            // If the number of expanded subRows is equal to the number of subRows
×
NEW
58
            // that can expand, then all subRows are expanded.
×
NEW
59
            const isAllSubRowsExpanded = derived(subRowExpandedIds, ($subRowExpandedIds) => {
×
NEW
60
                if (row.subRows === undefined) {
×
NEW
61
                    return true
×
NEW
62
                }
×
NEW
63
                // canExpand is derived from the presence of the `subRows` property.
×
NEW
64
                const expandableSubRows = row.subRows.filter(
×
NEW
65
                    (subRow) => subRow.subRows !== undefined
×
NEW
66
                )
×
NEW
67
                return $subRowExpandedIds.length === expandableSubRows.length
×
NEW
68
            })
×
NEW
69
            return {
×
NEW
70
                isExpanded,
×
NEW
71
                canExpand,
×
NEW
72
                isAllSubRowsExpanded
×
NEW
73
            }
×
NEW
74
        }
×
NEW
75
        const pluginState = { expandedIds, getRowState }
×
76

×
NEW
77
        const deriveRows: DeriveRowsFn<Item> = (rows) => {
×
NEW
78
            return derived([rows, expandedIds], ([$rows, $expandedIds]) => {
×
NEW
79
                return $rows.flatMap((row) => {
×
NEW
80
                    return withExpandedRows<Item, typeof row>(row, $expandedIds)
×
NEW
81
                })
×
NEW
82
            })
×
NEW
83
        }
×
84

×
NEW
85
        return {
×
NEW
86
            pluginState,
×
NEW
87
            deriveRows
×
NEW
88
        }
×
NEW
89
    }
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc