Files
CT/src/View/Components/propertyValueList.tsx
T

72 lines
2.1 KiB
TypeScript
Raw Normal View History

2024-07-25 01:28:55 +02:00
import * as React from "bloatless-react";
import {
2026-04-01 08:29:39 +00:00
StringEntryObject,
collectObjectValuesForKey,
localeCompare,
2024-07-25 01:28:55 +02:00
} from "../../Model/Utility/utility";
export function PropertyValueList<T>(
2026-04-01 08:29:39 +00:00
propertyKey: string,
stringEntryObjectConverter: (object: T) => StringEntryObject,
objects: React.ListState<T> | React.MapState<T>,
viewBuilder: (
keys: React.ListState<string>,
sortedKeys: React.State<string[]>,
) => HTMLElement,
2024-07-25 01:28:55 +02:00
) {
2026-04-01 08:29:39 +00:00
const propertyValues: React.ListState<string> = new React.ListState();
const sortedPropertyValues: React.State<string[]> =
createSortedPropertyValueState(propertyValues);
objects.subscribe(() => {
collectPropertyValuesToState(
propertyKey,
stringEntryObjectConverter,
objects,
propertyValues,
);
});
2024-07-25 18:42:06 +02:00
2026-04-01 08:29:39 +00:00
return viewBuilder(propertyValues, sortedPropertyValues);
2024-07-25 18:42:06 +02:00
}
export function collectPropertyValuesToState<T>(
2026-04-01 08:29:39 +00:00
propertyKey: string,
stringEntryObjectConverter: (object: T) => StringEntryObject,
objects: React.ListState<T> | React.MapState<T>,
propertyValues: React.ListState<string>,
2024-07-25 18:42:06 +02:00
) {
2026-04-01 08:29:39 +00:00
const values: string[] = collectObjectValuesForKey(
propertyKey,
stringEntryObjectConverter,
[...objects.value.values()],
);
for (const existingValue of values) {
if (propertyValues.value.has(existingValue)) continue;
propertyValues.add(existingValue);
}
2026-04-01 08:29:39 +00:00
for (const displayedValue of propertyValues.value.values()) {
if (values.includes(displayedValue) == false) {
propertyValues.remove(displayedValue);
}
}
2024-07-25 18:42:06 +02:00
}
2024-07-25 01:28:55 +02:00
2024-07-25 18:42:06 +02:00
export function createSortedPropertyValueState(
2026-04-01 08:29:39 +00:00
propertyValues: React.ListState<string>,
2024-07-25 18:42:06 +02:00
): React.State<string[]> {
2026-04-01 08:29:39 +00:00
return React.createProxyState([propertyValues], () =>
[...propertyValues.value.values()].sort(localeCompare),
);
2024-07-25 18:42:06 +02:00
}
2024-07-25 01:28:55 +02:00
2024-07-25 18:42:06 +02:00
export function createPropertyValueIndexState(
2026-04-01 08:29:39 +00:00
sortedKeys: React.State<string[]>,
key: string,
2024-07-25 18:42:06 +02:00
): React.State<number> {
2026-04-01 08:29:39 +00:00
return React.createProxyState([sortedKeys], () =>
sortedKeys.value.indexOf(key),
);
2024-07-25 01:28:55 +02:00
}