notCharles 61da930909 wip
2025-04-24 19:03:34 -04:00

2 lines
970 B
JavaScript

function r({state:o}){return{state:o,rows:[],shouldUpdateRows:!0,init:function(){this.updateRows(),this.rows.length<=0?this.rows.push({key:"",value:""}):this.updateState(),this.$watch("state",(e,t)=>{let i=s=>s===null?0:Array.isArray(s)?s.length:typeof s!="object"?0:Object.keys(s).length;i(e)===0&&i(t)===0||this.updateRows()})},addRow:function(){this.rows.push({key:"",value:""}),this.updateState()},deleteRow:function(e){this.rows.splice(e,1),this.rows.length<=0&&this.addRow(),this.updateState()},reorderRows:function(e){let t=Alpine.raw(this.rows);this.rows=[];let i=t.splice(e.oldIndex,1)[0];t.splice(e.newIndex,0,i),this.$nextTick(()=>{this.rows=t,this.updateState()})},updateRows:function(){if(!this.shouldUpdateRows){this.shouldUpdateRows=!0;return}this.rows=Alpine.raw(this.state)},updateState:function(){let e=[];this.rows.forEach(t=>{t.key===""||t.key===null||e.push({key:t.key,value:t.value})}),this.shouldUpdateRows=!1,this.state=e}}}export{r as default};