solid-primitives icon indicating copy to clipboard operation
solid-primitives copied to clipboard

Add selectors

Open ryoid opened this issue 1 year ago • 1 comments

Using createSelector is good for a single values. I found that there are a lot of use cases for array selectors. I have been using this on a few projects and I think it would be a good addition to the library.

const list: string[] = ["apple", "pear", "orange"]
const [selectedItems] = createSignal<string[]>(["apple"])
const isSelected = createArraySelector(selectedItems)

<For each={list}>
  {(item) => <li classList={{ active: isSelected(item) }}>{item}</li>}
</For>

Extending with a comparator function (instead of b.includes(a)) to support objects could be possible, but I decided to keep it simple. At that point it might be better to just create a custom selector directly.

ryoid avatar Sep 24 '23 08:09 ryoid