UNPKG

@meleon/uni-ui

Version:

A uniapp components library written in vue3 and typescript

1 lines 1.36 kB
import{isNodeCheckable}from"./tree-is";export function updateParentCheckedKeys(e){var t;const{node:d,checkedKeySet:n,indeterminateKeySet:r}=e;let c=d.parentNode;for(;c;){if(isNodeCheckable(c)){const e=c.key,d=(null===(t=c.children)||void 0===t?void 0:t.filter(isNodeCheckable))||[];let o=0;const a=d.length;d.forEach((({key:e})=>{n.has(e)?o+=1:r.has(e)&&(o+=.5)})),o&&o!==a?r.add(e):r.delete(e),o&&o===a?n.add(e):n.delete(e)}c=c.parentNode}}export function getChildrenKeys(e){var t;const d=[];return null===(t=e.children)||void 0===t||t.forEach((e=>{isNodeCheckable(e)&&d.push(e.key,...getChildrenKeys(e))})),d}export function getCheckState(e){const{node:t,checked:d,checkedKeys:n,indeterminateKeys:r}=e,{key:c}=t,o=new Set(n),a=new Set(r);d?o.add(c):o.delete(c),a.delete(c);const h=getChildrenKeys(t);return d?h.forEach((e=>{o.add(e)})):h.forEach((e=>{o.delete(e)})),h.forEach((e=>{a.delete(e)})),updateParentCheckedKeys({node:t,checkedKeySet:o,indeterminateKeySet:a}),[[...o],[...a]]}export function getCheckStateWithIntialKeys(e){const{initialCheckedKeys:t,key2TreeNode:d}=e,n=new Set,r=new Set,c=new Set;return t.forEach((e=>{const t=d.get(e);if(!t||r.has(e))return;getChildrenKeys(t).forEach((e=>{r.add(e)})),n.forEach((e=>{c.delete(e)})),n.add(e),c.delete(e),updateParentCheckedKeys({node:t,checkedKeySet:n,indeterminateKeySet:c})})),[[...n,...r],[...c]]}