Skip to content

Commit 9f8249d

Browse files
committed
used runes migrating to svelte 5
1 parent 37fb509 commit 9f8249d

File tree

1 file changed

+74
-42
lines changed

1 file changed

+74
-42
lines changed

src/lib/components/filters/content.svelte

Lines changed: 74 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,69 @@
88
InputSelectCheckbox,
99
InputDateTime
1010
} from '$lib/elements/forms';
11-
import { onMount } from 'svelte';
11+
import { onMount, createEventDispatcher } from 'svelte';
1212
import { operators, addFilter, queries, tags } from './store';
1313
import type { Column } from '$lib/helpers/types';
1414
import type { Writable } from 'svelte/store';
1515
import { TagList } from '.';
1616
import { Icon, Layout } from '@appwrite.io/pink-svelte';
1717
import { IconPlus } from '@appwrite.io/pink-icons-svelte';
1818
19-
// We cast to any to not cause type errors in the input components
20-
/* eslint @typescript-eslint/no-explicit-any: 'off' */
21-
export let value: any = null;
22-
export let columns: Writable<Column[]>;
23-
export let columnId: string | null = null;
24-
export let arrayValues: string[] = [];
25-
export let operatorKey: string | null = null;
26-
export let singleCondition = false;
19+
let {
20+
value = $bindable(null),
21+
columns,
22+
columnId = $bindable(null),
23+
arrayValues = $bindable([]),
24+
operatorKey = $bindable(null),
25+
singleCondition = false
26+
}: {
27+
value?: string | number | string[] | null;
28+
columns: Writable<Column[]>;
29+
columnId?: string | null;
30+
arrayValues?: string[];
31+
operatorKey?: string | null;
32+
singleCondition?: boolean;
33+
} = $props();
2734
28-
$: column = $columns.find((c) => c.id === columnId) as Column;
35+
let columnsArray = $derived($columns);
36+
let column = $derived(columnsArray.find((c) => c.id === columnId));
2937
30-
$: operatorsForColumn = Object.entries(operators)
31-
.filter(([, v]) => v.types.includes(column?.type))
32-
.map(([k]) => ({
33-
label: k,
34-
value: k
38+
let operatorsForColumn = $derived(() => {
39+
if (!column?.type) return [];
40+
return Object.entries(operators)
41+
.filter(([, v]) => v.types.includes(column.type))
42+
.map(([k]) => ({ label: k, value: k }));
43+
});
44+
45+
let operator = $derived(operatorKey ? operators[operatorKey] : null);
46+
let isDisabled = $derived(!operator);
47+
let appliedTags = $derived($tags);
48+
49+
let columnOptions = $derived(() =>
50+
columnsArray
51+
.filter((c) => c.filter !== false)
52+
.map((c) => ({
53+
label: c.title,
54+
value: c.id
55+
}))
56+
);
57+
58+
let enumOptions = $derived(() => {
59+
if (!column?.elements) return [];
60+
return column.elements.map((e) => ({
61+
label: e?.label ?? e,
62+
value: e?.value ?? e
3563
}));
64+
});
3665
37-
$: operator = operatorKey ? operators[operatorKey] : null;
38-
$: isDisabled = !operator;
66+
let enumOptionsWithChecked = $derived(() => {
67+
if (!column?.elements) return [];
68+
return column.elements.map((e) => ({
69+
label: e?.label ?? e,
70+
value: e?.value ?? e,
71+
checked: arrayValues.includes(e?.value ?? e)
72+
}));
73+
});
3974
4075
onMount(() => {
4176
value = column?.array ? [] : null;
@@ -45,12 +80,15 @@
4580
}
4681
});
4782
83+
const dispatch = createEventDispatcher<{ clear: void; apply: { applied: number } }>();
84+
4885
function addFilterAndReset() {
49-
addFilter($columns, columnId, operatorKey, value, arrayValues);
86+
addFilter(columnsArray, columnId, operatorKey, value, arrayValues);
5087
columnId = null;
5188
operatorKey = null;
5289
value = null;
5390
arrayValues = [];
91+
dispatch('apply', { applied: appliedTags.length });
5492
if (singleCondition) {
5593
queries.apply();
5694
}
@@ -62,18 +100,13 @@
62100
<Layout.Stack gap="s" direction="row" alignItems="flex-start">
63101
<InputSelect
64102
id="column"
65-
options={$columns
66-
.filter((c) => c.filter !== false)
67-
.map((c) => ({
68-
label: c.title,
69-
value: c.id
70-
}))}
103+
options={columnOptions()}
71104
placeholder="Select column"
72105
bind:value={columnId} />
73106
<InputSelect
74107
id="operator"
75108
disabled={!column}
76-
options={operatorsForColumn}
109+
options={operatorsForColumn()}
77110
placeholder="Select operator"
78111
bind:value={operatorKey} />
79112
</Layout.Stack>
@@ -84,11 +117,7 @@
84117
name="value"
85118
bind:tags={arrayValues}
86119
placeholder="Select value"
87-
options={column?.elements?.map((e) => ({
88-
label: e?.label ?? e,
89-
value: e?.value ?? e,
90-
checked: arrayValues.includes(e?.value ?? e)
91-
}))}>
120+
options={enumOptionsWithChecked()}>
92121
</InputSelectCheckbox>
93122
{:else}
94123
<InputTags
@@ -102,14 +131,14 @@
102131
{#if column.format === 'enum'}
103132
<InputSelect
104133
id="value"
105-
bind:value
134+
bind:value={value as string}
106135
placeholder="Select value"
107-
options={column?.elements?.map((e) => ({
108-
label: e?.label ?? e,
109-
value: e?.value ?? e
110-
}))} />
136+
options={enumOptions()} />
111137
{:else if column.type === 'integer' || column.type === 'double'}
112-
<InputNumber id="value" bind:value placeholder="Enter value" />
138+
<InputNumber
139+
id="value"
140+
bind:value={value as number}
141+
placeholder="Enter value" />
113142
{:else if column.type === 'boolean'}
114143
<InputSelect
115144
id="value"
@@ -118,14 +147,17 @@
118147
options={[
119148
{ label: 'True', value: true },
120149
{ label: 'False', value: false }
121-
].filter(Boolean)}
122-
bind:value />
150+
]}
151+
bind:value={value as unknown as boolean} />
123152
{:else if column.type === 'datetime'}
124153
{#key value}
125-
<InputDateTime id="value" bind:value step={60} />
154+
<InputDateTime id="value" bind:value={value as string} step={60} />
126155
{/key}
127156
{:else}
128-
<InputText id="value" bind:value placeholder="Enter value" />
157+
<InputText
158+
id="value"
159+
bind:value={value as string}
160+
placeholder="Enter value" />
129161
{/if}
130162
</ul>
131163
{/if}
@@ -138,10 +170,10 @@
138170
{/if}
139171
</form>
140172

141-
{#if !singleCondition && $tags.length > 0}
173+
{#if !singleCondition && appliedTags.length > 0}
142174
<ul class="u-flex u-flex-wrap u-cross-center u-gap-8 u-margin-block-start-16 tags">
143175
<TagList
144-
tags={$tags}
176+
tags={appliedTags}
145177
on:remove={(e) => {
146178
queries.removeFilter(e.detail);
147179
queries.apply();

0 commit comments

Comments
 (0)