@zag-js/collection
Advanced tools
Comparing version 0.0.0-dev-20230905173145 to 0.0.0-dev-20230906161631
@@ -10,8 +10,7 @@ type CollectionSearchState = { | ||
}; | ||
type CollectionItem = { | ||
[key: string]: any; | ||
}; | ||
type CollectionNode<T extends CollectionItem> = { | ||
type CollectionItem = string | Record<string, any>; | ||
type CollectionNode<T extends CollectionItem = CollectionItem> = { | ||
item: T; | ||
index: number; | ||
label: string; | ||
value: string; | ||
@@ -21,3 +20,3 @@ previousValue: string | null; | ||
}; | ||
type CollectionOptions<T extends CollectionItem> = { | ||
type CollectionOptions<T extends CollectionItem = CollectionItem> = { | ||
/** | ||
@@ -94,2 +93,6 @@ * The options of the select | ||
/** | ||
* Whether an item is disabled | ||
*/ | ||
itemToDisabled: (item: T | null) => boolean; | ||
/** | ||
* Convert a value to a string | ||
@@ -96,0 +99,0 @@ */ |
@@ -33,2 +33,25 @@ "use strict"; | ||
// src/collection.ts | ||
var isObject = (v) => typeof v === "object" && v !== null && !Array.isArray(v); | ||
var hasKey = (obj, key) => Object.prototype.hasOwnProperty.call(obj, key); | ||
var fallback = { | ||
itemToValue(item) { | ||
if (typeof item === "string") | ||
return item; | ||
if (isObject(item) && hasKey(item, "value")) | ||
return item.value; | ||
return ""; | ||
}, | ||
itemToString(item) { | ||
if (typeof item === "string") | ||
return item; | ||
if (isObject(item) && hasKey(item, "label")) | ||
return item.label; | ||
return fallback.itemToValue(item); | ||
}, | ||
itemToDisabled(item) { | ||
if (isObject(item) && hasKey(item, "disabled")) | ||
return !!item.disabled; | ||
return false; | ||
} | ||
}; | ||
var Collection = class { | ||
@@ -57,3 +80,3 @@ constructor(options) { | ||
__publicField(this, "iterate", () => { | ||
const { items, isItemDisabled } = this.options; | ||
const { items } = this.options; | ||
for (let i = 0; i < items.length; i++) { | ||
@@ -63,5 +86,7 @@ const item = items[i]; | ||
const label = this.itemToString(item); | ||
const disabled = this.itemToDisabled(item); | ||
const node = { | ||
item: { ...item, label }, | ||
item, | ||
index: i, | ||
label, | ||
value, | ||
@@ -72,3 +97,3 @@ previousValue: this.itemToValue(items[i - 1]) ?? null, | ||
this.nodes.set(value, node); | ||
if (isItemDisabled?.(item)) { | ||
if (disabled) { | ||
this.disabledValues.add(value); | ||
@@ -134,3 +159,3 @@ } | ||
return ""; | ||
return this.options.itemToValue?.(item) ?? item?.value ?? ""; | ||
return this.options.itemToValue?.(item) ?? fallback.itemToValue(item); | ||
}); | ||
@@ -143,5 +168,13 @@ /** | ||
return ""; | ||
return this.options.itemToString?.(item) ?? item?.label ?? this.itemToValue(item); | ||
return this.options.itemToString?.(item) ?? fallback.itemToString(item); | ||
}); | ||
/** | ||
* Whether an item is disabled | ||
*/ | ||
__publicField(this, "itemToDisabled", (item) => { | ||
if (!item) | ||
return false; | ||
return this.options.isItemDisabled?.(item) ?? fallback.itemToDisabled(item); | ||
}); | ||
/** | ||
* Convert a value to a string | ||
@@ -152,3 +185,3 @@ */ | ||
return ""; | ||
return this.itemToString(this.item(value)); | ||
return this.nodes.get(value)?.label ?? ""; | ||
}); | ||
@@ -262,5 +295,5 @@ /** | ||
if (isSingleKey) { | ||
nodes = nodes.filter((item) => item.value !== currentValue); | ||
nodes = nodes.filter((node) => node.value !== currentValue); | ||
} | ||
return nodes.find((node) => match(this.itemToString(node.item), text)); | ||
return nodes.find((node) => match(node.label, text)); | ||
}); | ||
@@ -267,0 +300,0 @@ /** |
{ | ||
"name": "@zag-js/collection", | ||
"version": "0.0.0-dev-20230905173145", | ||
"version": "0.0.0-dev-20230906161631", | ||
"description": "Utilities to manage a collection of items.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
59148
794