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

humanspeak / svelte-headless-table / 12913219917

22 Jan 2025 04:59PM UTC coverage: 59.766%. First build
12913219917

push

github

jaysin586
Rolling it way back

404 of 483 branches covered (83.64%)

Branch coverage included in aggregate %.

2232 of 3955 new or added lines in 55 files covered. (56.43%)

2616 of 4570 relevant lines covered (57.24%)

34.43 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 '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

×
NEW
23
const withExpandedRows = <Item, Row extends BodyRow<Item>>(
×
NEW
24
        row: Row,
×
NEW
25
        expandedIds: Record<string, boolean>
×
NEW
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((subRow) => subRow.subRows !== undefined);
×
NEW
65
                                return $subRowExpandedIds.length === expandableSubRows.length;
×
NEW
66
                        });
×
NEW
67
                        return {
×
NEW
68
                                isExpanded,
×
NEW
69
                                canExpand,
×
NEW
70
                                isAllSubRowsExpanded,
×
NEW
71
                        };
×
NEW
72
                };
×
NEW
73
                const pluginState = { expandedIds, getRowState };
×
74

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

×
NEW
83
                return {
×
NEW
84
                        pluginState,
×
NEW
85
                        deriveRows,
×
NEW
86
                };
×
NEW
87
        };
×
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