From 9e5ca1a063d092dbd64c3f91569f83d38730811d Mon Sep 17 00:00:00 2001 From: marlon-erler Date: Thu, 25 Jul 2024 01:28:55 +0200 Subject: [PATCH] prepare PropertyValueList and FilteredList --- dist/index.js | 18 ---------------- src/View/Components/filteredList.tsx | 30 +++++++++++++++++++++++++++ src/View/Components/propertyList.tsx | 31 ++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 18 deletions(-) create mode 100644 src/View/Components/filteredList.tsx create mode 100644 src/View/Components/propertyList.tsx diff --git a/dist/index.js b/dist/index.js index bec7138f..a4c73f5f 100644 --- a/dist/index.js +++ b/dist/index.js @@ -330,17 +330,6 @@ } }; }; - function collectObjectValuesForKey(key, converter, objects) { - const values = /* @__PURE__ */ new Set(); - for (const object of objects) { - const stringEntryObject = converter(object); - const stringEntryObjectValue = stringEntryObject[key]; - if (stringEntryObjectValue == void 0 || stringEntryObjectValue == "") - continue; - values.add(stringEntryObjectValue); - } - return [...values.values()]; - } var IndexManager = class { itemToString; sortedStrings = []; @@ -3527,13 +3516,6 @@ }; // src/index.tsx - console.log( - collectObjectValuesForKey("test", (x) => x, [ - { test: "hello" }, - { test: "123" }, - { test: "hello" } - ]) - ); var storageModel = new StorageModel(); var settingsModel = new SettingsModel(storageModel); var connectionModel = new ConnectionModel(storageModel); diff --git a/src/View/Components/filteredList.tsx b/src/View/Components/filteredList.tsx new file mode 100644 index 00000000..e74b7238 --- /dev/null +++ b/src/View/Components/filteredList.tsx @@ -0,0 +1,30 @@ +import * as React from "bloatless-react"; + +import { + StringEntryObject, + checkDoesObjectMatchReference, +} from "../../Model/Utility/utility"; + +export function FilteredList( + reference: StringEntryObject, + stringEntryObjectConverter: (object: T) => StringEntryObject, + objects: React.ListState | React.MapState, + viewBuilder: (matchingObjects: React.ListState) => HTMLElement +) { + const matchingObjects: React.ListState = new React.ListState(); + + objects.handleAddition((newObject) => { + const doesMatch: boolean = checkDoesObjectMatchReference( + reference, + stringEntryObjectConverter(newObject) + ); + if (doesMatch == false) return; + + matchingObjects.add(newObject); + objects.handleRemoval(newObject, () => { + matchingObjects.remove(newObject); + }); + }); + + return viewBuilder(matchingObjects); +} diff --git a/src/View/Components/propertyList.tsx b/src/View/Components/propertyList.tsx new file mode 100644 index 00000000..8966d63a --- /dev/null +++ b/src/View/Components/propertyList.tsx @@ -0,0 +1,31 @@ +import * as React from "bloatless-react"; + +import { + StringEntryObject, + collectObjectValuesForKey, +} from "../../Model/Utility/utility"; + +export function PropertyValueList( + propertyKey: string, + stringEntryObjectConverter: (object: T) => StringEntryObject, + objects: React.ListState | React.MapState, + viewBuilder: (categories: React.ListState) => HTMLElement +) { + const propertyValues: React.ListState = new React.ListState(); + + function collectValues() { + propertyValues.clear(); + const values: string[] = collectObjectValuesForKey( + propertyKey, + stringEntryObjectConverter, + [...objects.value.values()] + ); + propertyValues.add(...values); + } + + objects.subscribe(() => { + collectValues(); + }); + + return viewBuilder(propertyValues); +}