@guruhotel/aura-hooks
Version:
🪝 Hooks library designed by the Guruhotel team for Aura UI
1 lines • 759 B
JavaScript
import{useState as e}from"react";export function useListState(t=[]){const[r,p]=e(t);return[r,{setState:p,append:(...e)=>p((t=>[...t,...e])),prepend:(...e)=>p((t=>[...e,...t])),insert:(e,...t)=>p((r=>[...r.slice(0,e),...t,...r.slice(e)])),pop:()=>p((e=>{const t=[...e];return t.pop(),t})),shift:()=>p((e=>{const t=[...e];return t.shift(),t})),apply:e=>p((t=>t.map(((t,r)=>e(t,r))))),applyWhere:(e,t)=>p((r=>r.map(((r,p)=>e(r,p)?t(r,p):r)))),remove:(...e)=>p((t=>t.filter(((t,r)=>!e.includes(r))))),reorder:({from:e,to:t})=>p((r=>{const p=[...r],s=r[e];return p.splice(e,1),p.splice(t,0,s),p})),setItem:(e,t)=>p((r=>{const p=[...r];return p[e]=t,p})),setItemProp:(e,t,r)=>p((p=>{const s=[...p];return s[e]={...s[e],[t]:r},s})),filter:e=>{p((t=>t.filter(e)))}}]}