refactor(createFilterStore): move from store pattern to svelte 5 runes usage

This commit is contained in:
Ilia Mashkov
2026-01-07 14:26:37 +03:00
parent 0692711726
commit 9fd98aca5d
16 changed files with 265 additions and 334 deletions
@@ -0,0 +1,120 @@
import { SvelteSet } from 'svelte/reactivity';
export interface Property {
/**
* Property identifier
*/
id: string;
/**
* Property name
*/
name: string;
/**
* Property selected state
*/
selected?: boolean;
}
export interface FilterModel {
/**
* Search query
*/
searchQuery?: string;
/**
* Properties
*/
properties: Property[];
}
/**
* Create a filter store.
* @param initialState - Initial state of the filter store
*/
export function createFilter<T extends FilterModel>(
initialState: T,
) {
let properties = $state(
initialState.properties.map(p => ({
...p,
selected: p.selected ?? false,
})),
);
const selectedProperties = $derived.by(() => {
const _ = properties;
return properties.filter(p => p.selected);
});
const selectedCount = $derived.by(() => {
const _ = properties;
return selectedProperties.length;
});
return {
/**
* Get all properties.
*/
get properties() {
return properties;
},
/**
* Get selected properties.
*/
get selectedProperties() {
return selectedProperties;
},
/**
* Get selected count.
*/
get selectedCount() {
return selectedCount;
},
/**
* Toggle property selection.
*/
toggleProperty: (id: string) => {
properties = properties.map(p => ({
...p,
selected: p.id === id ? !p.selected : p.selected,
}));
},
/**
* Select property.
*/
selectProperty(id: string) {
properties = properties.map(p => ({
...p,
selected: p.id === id ? true : p.selected,
}));
},
/**
* Deselect property.
*/
deselectProperty(id: string) {
properties = properties.map(p => ({
...p,
selected: p.id === id ? false : p.selected,
}));
},
/**
* Select all properties.
*/
selectAll: () => {
properties = properties.map(p => ({
...p,
selected: true,
}));
},
/**
* Deselect all properties.
*/
deselectAll: () => {
properties = properties.map(p => ({
...p,
selected: false,
}));
},
};
}
export type Filter = ReturnType<typeof createFilter>;
+8 -1
View File
@@ -7,4 +7,11 @@ export type {
QueryParams,
QueryParamValue,
} from './buildQueryString';
export { createVirtualizer } from './createVirtualizer/createVirtualizer';
export {
createVirtualizer,
type Virtualizer,
} from './createVirtualizer/createVirtualizer.svelte';
export {
createFilter,
type Filter,
} from './filter/createFilter/createFilter.svelte';