@solid-primitives/keyed
Advanced tools
Comparing version 1.3.0 to 1.4.0
@@ -78,2 +78,22 @@ import { Accessor, JSX, AccessorArray } from 'solid-js'; | ||
}): JSX.Element; | ||
/** | ||
* Creates a list of elements from the values of provided Set | ||
* | ||
* @param props | ||
* @param props.of set to iterate values of (`mySet.values()`) | ||
* @param props.children | ||
* a map render function that receives a Set value and **index signal** and returns a JSX-Element; if the list is empty, an optional fallback is returned: | ||
* ```tsx | ||
* <SetValues of={set()} fallback={<div>No items</div>}> | ||
* {(value, index) => <div data-index={index()}>{value)}</div>} | ||
* </SetValues> | ||
* ``` | ||
* | ||
* @see https://github.com/solidjs-community/solid-primitives/tree/main/packages/keyed#setvalues | ||
*/ | ||
declare function SetValues<T>(props: { | ||
of: Set<T> | undefined | null | false; | ||
fallback?: JSX.Element; | ||
children: (value: T, i: Accessor<number>) => JSX.Element; | ||
}): JSX.Element; | ||
type RerunChildren<T> = ((input: T, prevInput: T | undefined) => JSX.Element) | JSX.Element; | ||
@@ -95,2 +115,2 @@ /** | ||
export { Entries, Key, MapEntries, Rerun, type RerunChildren, keyArray }; | ||
export { Entries, Key, MapEntries, Rerun, type RerunChildren, SetValues, keyArray }; |
@@ -122,2 +122,12 @@ import { onCleanup, $TRACK, untrack, createRoot, createSignal, createMemo, mapArray, on } from 'solid-js'; | ||
} | ||
function SetValues(props) { | ||
const mapFn = props.children; | ||
return createMemo( | ||
mapArray( | ||
() => props.of && Array.from(props.of.values()), | ||
mapFn.length < 2 ? (value) => mapFn(value) : mapFn, | ||
"fallback" in props ? { fallback: () => props.fallback } : void 0 | ||
) | ||
); | ||
} | ||
function Rerun(props) { | ||
@@ -133,2 +143,2 @@ const key = typeof props.on === "function" || Array.isArray(props.on) ? props.on : () => props.on; | ||
export { Entries, Key, MapEntries, Rerun, keyArray }; | ||
export { Entries, Key, MapEntries, Rerun, SetValues, keyArray }; |
{ | ||
"name": "@solid-primitives/keyed", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Control Flow primitives and components that require specifying explicit keys to identify or rerender elements.", | ||
@@ -19,3 +19,4 @@ "author": "Damian Tarnawski @thetarnav <gthetarnav@gmail.com>", | ||
"Entries", | ||
"MapEntries" | ||
"MapEntries", | ||
"SetValues" | ||
], | ||
@@ -22,0 +23,0 @@ "category": "Control Flow" |
@@ -18,2 +18,3 @@ <p> | ||
- [`MapEntries`](#mapentries) - Creates a list of elements by mapping Map entries. | ||
- [`SetValues`](#setvalues) - Creates a list of elements by mapping Set values. | ||
- [`Rerun`](#rerun) - Causes the children to rerender when the `on` changes. | ||
@@ -181,3 +182,3 @@ | ||
`MapEntries` is using [`Map#key()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/keys) so the index and resulting JSX will follow the insertion order. | ||
`MapEntries` is using [`Map#keys()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/keys) so the index and resulting JSX will follow the insertion order. | ||
@@ -194,2 +195,30 @@ ```tsx | ||
## `<SetValues>` | ||
Creates a list of elements by mapping Set values. Similar to Solid's `<For>` and `<Index>`, but here, render function takes two arguments, the value and the index argument as a signal. | ||
### How to use it | ||
```tsx | ||
import { SetValues } from "@solid-primitives/keyed"; | ||
const [set, setSet] = createSignal(new Set()); | ||
<SetValues of={set()} fallback={<div>No items</div>}> | ||
{value => <div>{value}</div>} | ||
</SetValues>; | ||
``` | ||
### Index argument | ||
Second argument of the map function is an index signal. | ||
`SetValues` is using [`Set#values()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/values) so the index and resulting JSX will follow the insertion order. | ||
```tsx | ||
<SetValues of={set()} fallback={<div>No items</div>}> | ||
{(value, index) => <div data-index={index()}>{value}</div>} | ||
</SetValues> | ||
``` | ||
## `<Rerun>` | ||
@@ -196,0 +225,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
29376
398
285