{"name":"@solid-primitives/keyed","version":"1.0.2","description":"Control Flow primitives and components that require specifying explicit keys to identify or rerender elements.","author":{"name":"Damian Tarnawski @thetarnav","email":"gthetarnav@gmail.com"},"license":"MIT","homepage":"https://github.com/solidjs-community/solid-primitives/tree/main/packages/keyed#readme","repository":{"type":"git","url":"git+https://github.com/solidjs-community/solid-primitives.git"},"primitive":{"name":"keyed","stage":3,"list":["keyArray","Key","Rerun"],"category":"Control Flow"},"private":false,"sideEffects":false,"type":"module","main":"./dist/index.cjs","module":"./dist/index.js","types":"./dist/index.d.ts","keywords":["key","control-flow","solid","primitives"],"devDependencies":{"@solid-primitives/immutable":"^0.1.2","@solid-primitives/refs":"^0.3.2","@solid-primitives/utils":"^3.0.2","jsdom":"^20.0.0","prettier":"^2.7.1","solid-app-router":"^0.2.1","solid-js":"^1.5.1","solid-register":"^0.2.5","solid-transition-group":"^0.0.8","tslib":"^2.4.0","tsup":"^6.2.2","typescript":"^4.7.4","unocss":"^0.44.7","uvu":"^0.5.6","vite":"^3.0.7","vite-plugin-solid":"^2.3.0"},"peerDependencies":{"solid-js":"^1.4.0"},"scripts":{"start":"vite serve dev","dev":"vite serve dev","build":"tsup","test":"uvu -r solid-register"},"bugs":{"url":"https://github.com/solidjs-community/solid-primitives/issues"},"_id":"@solid-primitives/keyed@1.0.2","_integrity":"sha512-UNgCksI/5D52NnkGNCHYoGbAv6ECGLTIKcrHqYWI2kZnfBK1FxMGTWfaKp1sbZOPBEx8K2gFHM9haeYxmLrFnw==","_resolved":"/tmp/5f1a5f1777737c3302f15b902dc5e37d/solid-primitives-keyed-1.0.2.tgz","_from":"file:solid-primitives-keyed-1.0.2.tgz","_nodeVersion":"17.9.1","_npmVersion":"8.11.0","dist":{"integrity":"sha512-UNgCksI/5D52NnkGNCHYoGbAv6ECGLTIKcrHqYWI2kZnfBK1FxMGTWfaKp1sbZOPBEx8K2gFHM9haeYxmLrFnw==","shasum":"64c334ffa36aebaa555912fac2fc9c86856f7bf1","tarball":"http://registry-npm.ywwl.com/@solid-primitives/keyed/download/@solid-primitives/keyed-1.0.2.tgz","fileCount":6,"unpackedSize":17342,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIA/X8ptdgnIs73o93sO4+UTb0eNlgnoB3AM4kTh+zu/RAiAqvNYs3G6OOmTH70vRpXmun/mjPfQVCCzWaPx9nCwLgQ=="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjCRwBACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmqjzw/6AxYGMRFI2BrNZj6q4vFNhno7rNnVm1GmeIItXiWDpqnHvsbr\r\nr7dzVG3AJ4Ekds7hvsqjZaavbKe2P8Ws5Maq4Qov4V1QYS7mP2OsdFA1kN51\r\nhB3uPd0TgIWvCr83OQhsHkDtU9ItaE1l1P3WoR7mnWyt1m349+ESSH8v95j3\r\nukzXpFb9zQy20rsoBD2L+P+/g9dqGDLY2VEV508bP8cF2IILTfd8q7iTC+Ri\r\neaUAWHS769XmMyLiDc09+wXMkAtB93uZRxnj5FoH9A/NRaI3bTnMNbuA/7Ir\r\n3qE10m8Uxde3DrPTBILjTFLFBvXBP/AQ8Dtnhm0RqKtf91atdRXlzU233gF+\r\n5brA71JYW7DAVz8Vz5blko49QB8CEpsdTeV47fbLeE3oMRtBHLyqwaeCVqu3\r\nKfY8JfPA/mx2VT/o7CSpiiadZqpLfTs1oqZRiXpqsTZMmp1l9TXsC6DQ2cfD\r\n6jpIV9s7DDfUkHmawqSx37R4qhYBQrOQje6KyDqOY1YNilw5kcv8CSULIIqp\r\ng3TwCI+FyR5W3uXp4zYSr+HXdiQ7cmf5uQICRnKfVCNyVSktFhJOZ0QPyjWZ\r\n/0LN4GPaykFc8BvVfxTbWuX0Y6ZYpJqJO8jHyE5xWMt6376C+lmgs/287efP\r\n1pzfdxJXdHrh+GjMbckH7iscLW9Xxty4xR0=\r\n=HhWr\r\n-----END PGP SIGNATURE-----\r\n","size":5432,"noattachment":false,"key":"/@solid-primitives/keyed/-/@solid-primitives/keyed-1.0.2.tgz"},"_npmUser":{"name":"thetarnav.","email":"gthetarnav@gmail.com"},"directories":{},"maintainers":[{"name":"thetarnav.","email":"gthetarnav@gmail.com"},{"name":"lexlohr","email":"alex.th.kloss.77@googlemail.com"},{"name":"davedbase","email":"dave.dibiase@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/keyed_1.0.2_1661541377296_0.41409807690970113"},"_hasShrinkwrap":false,"readme":"<p>\n  <img width=\"100%\" src=\"https://assets.solidjs.com/banner?type=Primitives&background=tiles&project=Keyed\" alt=\"Solid Primitives Keyed\">\n</p>\n\n# @solid-primitives/keyed\n\n[![turborepo](https://img.shields.io/badge/built%20with-turborepo-cc00ff.svg?style=for-the-badge&logo=turborepo)](https://turborepo.org/)\n[![size](https://img.shields.io/bundlephobia/minzip/@solid-primitives/keyed?style=for-the-badge&label=size)](https://bundlephobia.com/package/@solid-primitives/keyed)\n[![version](https://img.shields.io/npm/v/@solid-primitives/keyed?style=for-the-badge)](https://www.npmjs.com/package/@solid-primitives/keyed)\n[![stage](https://img.shields.io/endpoint?style=for-the-badge&url=https%3A%2F%2Fraw.githubusercontent.com%2Fsolidjs-community%2Fsolid-primitives%2Fmain%2Fassets%2Fbadges%2Fstage-3.json)](https://github.com/solidjs-community/solid-primitives#contribution-process)\n\nControl Flow primitives and components that require specifying explicit keys to identify or rerender elements.\n\n- [`keyArray`](#keyArray) - Reactively maps an array by specified key with a callback function - underlying helper for the `<Key>` control flow.\n- [`Key`](#Key) - Creates a list of elements by mapping items by provided key.\n- [`Entries`](#Entries) - Creates a list of elements by mapping object entries.\n- [`Rerun`](#Rerun) - Causes the children to rerender when the `on` changes.\n\n## Installation\n\n```bash\nnpm install @solid-primitives/keyed\n# or\nyarn add @solid-primitives/keyed\n```\n\n## `keyArray`\n\nReactively maps an array by specified key with a callback function - underlying helper for the [`<Key>`](#Key) control flow.\n\n### How to use it\n\n#### Import\n\n```ts\nimport { keyArray } from \"@solid-primitives/keyed\";\n```\n\n#### Basic usage\n\nThe `keyArray` primitive takes 4 arguments:\n\n- `list` - input list of values to map\n- `keyFn` - key getter, items will be identified by it's value. changing the value is changing the item.\n- `mapFn` - reactive function used to create mapped output item. Similar to `Array.prototype.map` but both item and index are signals, that could change over time.\n- `options` - a fallback for when the input list is empty or missing _(Optional)_\n\n```ts\nconst mapped = keyArray(source, (model, index) => {\n  const [name, setName] = createSignal(model().name);\n  const [description, setDescription] = createSignal(model().description);\n\n  createComputed(() => {\n    setName(model().name);\n    setDescription(model().description);\n  });\n\n  return {\n    id: model.id,\n    get name() {\n      return name();\n    },\n    get description() {\n      return description();\n    },\n    get index() {\n      return index();\n    },\n    setName,\n    setDescription,\n  };\n});\n```\n\nNotice that both the value and index arguments are signals. Items are identified only by keys, it means that the items could be copied, replaced, changed, but as long as the key is the same, `keyArray` will treat it as the same item.\n\n## `<Key>`\n\nCreates a list of elements by mapping items by provided key. Similar to Solid's `<For>` and `<Index>`, but here, both value and index arguments are signals.\n\nBut changing the value does not rerender the element, only where the value is being used.\n\n### How to use it\n\n#### Import\n\n```ts\nimport { Key } from \"@solid-primitives/keyed\";\n```\n\n#### Typical usage\n\nBoth `each` and `by` have to be provided. The `fallback` prop is optional, it will be displayed when the list in `each` is missing or empty.\n\n```tsx\n<Key each={items()} by={item => item.id} fallback={<div>No items</div>}>\n  {item => <div>{item()}</div>}\n</Key>\n```\n\n#### Key shortcut\n\nprop `by` can also be an object key\n\n```tsx\n<Key each={items()} by=\"id\">\n```\n\n#### Index argument\n\nSecond argument of the map function is an index signal.\n\n```tsx\n<Key each={items()} by=\"id\">\n  {(item, index) => <div data-index={index()}>{item()}</div>}\n</Key>\n```\n\n### Demo\n\nhttps://codesandbox.io/s/solid-primitives-keyed-key-demo-gh7gd?file=/index.tsx\n\n## `<Entries>`\n\nCreates a list of elements by mapping object entries. Similar to Solid's `<For>` and `<Index>`, but here, render function takes three arguments, and both value and index arguments are signals.\n\n### How to use it\n\n```tsx\nimport { Entries } from \"@solid-primitives/keyed\";\n\n<Entries of={object()} fallback={<div>No items</div>}>\n  {(key, value) => (\n    <div>\n      {key}: {value()}\n    </div>\n  )}\n</Entries>;\n```\n\n### Index argument\n\nThird argument of the map function is an index signal.\n\n```tsx\n<Entries of={object()} fallback={<div>No items</div>}>\n  {(key, value, index) => (\n    <div data-index={index()}>\n      {key}: {value()}\n    </div>\n  )}\n</Entries>\n```\n\n## `<Rerun>`\n\nCauses the children to rerender when the `on` key changes. Equivalent of `v-key` in vue, and `{#key}` in svelte.\n\n> **Note:** Since Solid 1.5.0 the `<Show>` component has a `keyed` prop that works very similarly to `<Rerun>`.\n\n### Import\n\n```ts\nimport { Rerun } from \"@solid-primitives/keyed\";\n```\n\n### How to use it\n\nYou have to provide a `on` prop. Changing it, will cause the children to rerender.\n\n```tsx\nconst [count, setCount] = createSignal(0);\n\n// will rerender whole <button>, instead of just text\n<Rerun on={count()}>\n  <button onClick={() => setCount(p => ++p)}>{count()}</button>\n</Rerun>;\n\n// or pass a function\n<Rerun on={() => count()}/>\n\n// or an array of dependencies\n<Rerun on={[count, name, length]}/>\n```\n\n#### Passing a function as children\n\nYou can treat `on` prop like sources argument of the Solid's `on` helper, and the children as the second, callback argument.\n\n```tsx\n<Rerun on={[count, className]}>\n  {([count, className]) => (\n    <button class={className} onClick={() => setCount(p => ++p)}>\n      {count}\n    </button>\n  )}\n</Rerun>\n```\n\n#### Using with Transition\n\n`<Rerun>` can be used together with [`solid-transition-group`](#https://github.com/solidjs/solid-transition-group) to animate single component's transition, on state change.\n\n```tsx\n<Transition name=\"your-animation\" mode=\"outin\">\n  <Rerun on={count()}>\n    <button onClick={() => setCount(p => ++p)}>{count()}</button>\n  </Rerun>\n</Transition>\n```\n\n### DEMO\n\nhttps://codesandbox.io/s/solid-primitives-keyed-rerun-demo-14vjr?file=/index.tsx\n\n## Changelog\n\nSee [CHANGELOG.md](./CHANGELOG.md)\n","publish_time":1661541377510,"_cnpm_publish_time":1661541377510,"dist-tags":{"latest":"3.0.0","next":"3.0.0-next.5"}}