Skip to content

Commit 61cc717

Browse files
authored
Merge pull request #98 from BEXIS2/table
Table
2 parents f5e9f35 + a7f0eef commit 61cc717

File tree

6 files changed

+60
-59
lines changed

6 files changed

+60
-59
lines changed

src/lib/components/Facets/Facets.svelte

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,26 @@
11
<script lang="ts">
22
import { createEventDispatcher } from 'svelte';
33
import { getModalStore, Modal, TreeView, TreeViewItem } from '@skeletonlabs/skeleton';
4+
import type { Writable } from 'svelte/store';
45
56
import ShowMore from './ShowMore.svelte';
67
import type { FacetGroup, SelectedFacetGroup } from '$models/Models';
78
89
export let groupSelection = false;
9-
export let groups: FacetGroup[];
10+
export let groups: Writable<FacetGroup[]>;
1011
export let showAll = false;
1112
export let open = false;
1213
13-
let displayedGroups = structuredClone(groups);
14-
15-
let selected: { [key: string]: SelectedFacetGroup } = groups.reduce((acc, g) => {
16-
const children = g.children.reduce((acc, c) => {
17-
acc[c.name] = {
18-
...c,
19-
selected: false
20-
};
21-
return acc;
22-
}, {});
23-
24-
acc[g.name] = {
25-
...g,
26-
children,
27-
selected: false
28-
};
29-
30-
return acc;
31-
}, {});
14+
let selected: { [key: string]: SelectedFacetGroup };
3215
let selectedItems: {
3316
[key: string]: {
3417
[key: string]: boolean;
3518
};
3619
} = {};
3720
let selectedGroups: { [key: string]: boolean } = {};
3821
39-
Object.keys(selected).forEach((groupName) => {
40-
selectedItems[groupName] = {};
41-
Object.keys(selected[groupName].children).forEach((itemName) => {
42-
selectedItems[groupName][itemName] = false;
43-
});
44-
selectedGroups[groupName] = false;
45-
});
46-
4722
const dispatch = createEventDispatcher();
48-
23+
4924
const modalStore = getModalStore();
5025
const showMore = (group: SelectedFacetGroup) => {
5126
modalStore.trigger({
@@ -64,7 +39,7 @@
6439
6540
const handleSave = (group: SelectedFacetGroup) => {
6641
Object.keys(group.children).forEach((key) => {
67-
selectedItems[group.name][key] = group.children[key].selected;
42+
selectedItems[group.name][key] = group.children[key].selected || false;
6843
});
6944
modalStore.close();
7045
};
@@ -100,7 +75,7 @@
10075
});
10176
}
10277
103-
dispatch('change', changed);
78+
changed.length && dispatch('change', changed);
10479
};
10580
10681
const sortOptions = () => {
@@ -134,6 +109,36 @@
134109
});
135110
};
136111
112+
groups.subscribe((data) => {
113+
selected = data.reduce((acc, g) => {
114+
const children = g.children.reduce((acc, c) => {
115+
acc[c.name] = {
116+
...c,
117+
selected: c.selected || false
118+
};
119+
return acc;
120+
}, {});
121+
122+
acc[g.name] = {
123+
...g,
124+
children,
125+
selected: g.selected || false
126+
};
127+
128+
return acc;
129+
}, {});
130+
131+
Object.keys(selected).forEach((groupName) => {
132+
selectedItems[groupName] = {};
133+
Object.keys(selected[groupName].children).forEach((itemName) => {
134+
selectedItems[groupName][itemName] =
135+
selected[groupName].children[itemName].selected || false;
136+
});
137+
selectedGroups[groupName] = selected[groupName].selected || false;
138+
});
139+
});
140+
141+
$: displayedGroups = structuredClone($groups);
137142
$: selectedItems, mapSelected('items'), sortOptions();
138143
$: selectedGroups, mapSelected('groups');
139144
</script>
@@ -148,7 +153,9 @@
148153
bind:checked={selectedGroups[group.name]}
149154
bind:group={selectedGroups}
150155
>
151-
<p class="font-semibold">{group.displayName}</p>
156+
<p class="font-semibold">
157+
{group.displayName}{group.count !== undefined ? ` (${group.count})` : ''}
158+
</p>
152159

153160
<svelte:fragment slot="children">
154161
<!-- If more than 5 choices, show the remaining in the Modal -->

src/lib/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import type { TableConfig, Columns, Column } from './models/Models';
3232

3333
//Facets
3434
import Facets from './components/Facets/Facets.svelte';
35+
import type { FacetGroup, FacetOption, SelectedFacetGroup } from './models/Models';
3536

3637
// CodeEditor
3738
import CodeEditor from './components/CodeEditor/CodeEditor.svelte';
@@ -108,6 +109,7 @@ export { Table, TableFilter, columnFilter, searchFilter };
108109

109110
// Facets
110111
export { Facets };
112+
export type { FacetGroup, FacetOption, SelectedFacetGroup };
111113

112114
// CodeEditor
113115
export { CodeEditor };

src/lib/models/Models.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,23 +195,20 @@ export interface FacetOption {
195195
name: string;
196196
displayName: string;
197197
count?: number;
198+
selected?: boolean;
198199
}
199200

200201
export interface FacetGroup {
201202
name: string;
202203
displayName: string;
204+
selected?: boolean;
203205
children: FacetOption[];
204206
count?: number;
205207
}
206208

207-
export interface SelectedFacetOption extends FacetOption {
208-
selected: boolean;
209-
}
210-
211209
export interface SelectedFacetGroup extends Omit<FacetGroup, 'children'> {
212-
selected: boolean;
213210
children: {
214-
[key: string]: SelectedFacetOption;
211+
[key: string]: FacetOption;
215212
};
216213
}
217214

src/routes/components/facets/data/data.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@ export const groups: FacetGroup[] = [
6464
{ name: 'Stephen King', displayName: 'Stephen King', count: 10 },
6565
{ name: 'J.K. Rowling', displayName: 'J.K. Rowling', count: 3 },
6666
{ name: 'Agatha Christie', displayName: 'Agatha Christie', count: 4 },
67-
{ name: 'Dan Brown', displayName: 'Dan Brown', count: 7 }
67+
{ name: 'Dan Brown', displayName: 'Dan Brown', count: 7, selected: true },
6868
],
6969
name: 'authors',
70-
displayName: 'Authors'
70+
displayName: 'Authors',
71+
count: 24
7172
}
7273
];
Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
<script lang="ts">
2+
import { writable } from 'svelte/store';
3+
24
import CodeContainer from '$docs/components/CodeContainer.svelte';
35
import Facets from '$lib/components/Facets/Facets.svelte';
46
import { facetsNoGroupSelectionData, facetsGroupSelectionSvelte } from '../data/codeBlocks';
57
import { groups } from '../data/data';
68
7-
let selected = {
8-
Mediums: [],
9-
Genres: [],
10-
Authors: []
11-
};
12-
13-
let selectedGroups = {
14-
Mediums: true,
15-
Genres: false,
16-
Authors: false
17-
};
9+
const groupsStore = writable(groups);
1810
</script>
1911

2012
<div id="facetsGroupSelection">
@@ -23,6 +15,6 @@
2315
svelte={facetsGroupSelectionSvelte}
2416
data={facetsNoGroupSelectionData}
2517
>
26-
<Facets {groups} bind:selected bind:selectedGroups groupSelection open />
18+
<Facets groups={groupsStore} groupSelection open on:change={(e) => console.log(e)} />
2719
</CodeContainer>
2820
</div>
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
<script lang="ts">
2+
import { writable } from 'svelte/store';
3+
24
import CodeContainer from '$docs/components/CodeContainer.svelte';
35
import Facets from '$lib/components/Facets/Facets.svelte';
46
import { facetsNoGroupSelectionData, facetsNoGroupSelectionSvelte } from '../data/codeBlocks';
57
import { groups } from '../data/data';
68
7-
let selected = {
8-
Mediums: [],
9-
Genres: [],
10-
Authors: []
11-
};
9+
const groupsStore = writable(groups);
1210
</script>
1311

1412
<div id="facetsNoGroupSelection">
15-
<CodeContainer title="No group selection, closed by default and all options displayed in UI" svelte={facetsNoGroupSelectionSvelte} data={facetsNoGroupSelectionData}>
16-
<Facets {groups} bind:selected showAll />
13+
<CodeContainer
14+
title="No group selection, closed by default and all options displayed in UI"
15+
svelte={facetsNoGroupSelectionSvelte}
16+
data={facetsNoGroupSelectionData}
17+
>
18+
<Facets groups={groupsStore} showAll on:change={(e) => console.log(e)} />
1719
</CodeContainer>
1820
</div>

0 commit comments

Comments
 (0)