import { CHILD_GROUP_PREFIX } from "./constants"; export const tranValueBySetChildGroup = (params) => { const { value, componentOptions } = params; if (!Array.isArray(value)) { return value; } let newValue = [...value]; componentOptions.forEach((item) => { if (String(item.value).startsWith(CHILD_GROUP_PREFIX) && item.children) { const allChildrenSelected = item.children.length && item.children.every((child) => newValue.includes(child.value)); if (allChildrenSelected) { newValue.push(item.value); } } if (item.children?.length > 0) { newValue = tranValueBySetChildGroup({ value: newValue, componentOptions: item.children }); } }); return newValue; }; export const getChildGroupChildValues = (params) => { const { value: childGroupValue, componentOptions } = params; for (let i = 0; i < componentOptions.length; i++) { const item = componentOptions[i]; if (item.value === childGroupValue) { return item.children ? item.children.map((child) => child.value) : []; } if (item.children?.length > 0) { const result = getChildGroupChildValues({ value: childGroupValue, componentOptions: item.children, }); if (result.length) { return result; } } } return []; }; export const getCalcNewValue = (params) => { const { oldValue, newValue, componentOptions } = params; if (!Array.isArray(newValue) || !Array.isArray(oldValue)) { return newValue; } const oldChildGroupValues = oldValue.filter((item) => item.startsWith(CHILD_GROUP_PREFIX)); const newChildGroupValues = newValue.filter((item) => item.startsWith(CHILD_GROUP_PREFIX)); // 说明是取消分组 if (oldChildGroupValues.length - newChildGroupValues.length >= 1) { let cancelChildValue; oldChildGroupValues.forEach((item) => { if (!newChildGroupValues.includes(item)) { cancelChildValue = item; } }); const childGroupChildValues = getChildGroupChildValues({ value: cancelChildValue, componentOptions, }); return newValue.filter( (item) => !childGroupChildValues.includes(item) && !item.startsWith(CHILD_GROUP_PREFIX), ); } // 说明是添加分组 if (newChildGroupValues.length - oldChildGroupValues.length >= 1) { let addChildValue; newChildGroupValues.forEach((item) => { if (!oldChildGroupValues.includes(item)) { addChildValue = item; } }); const childGroupChildValues = getChildGroupChildValues({ value: addChildValue, componentOptions, }); return newValue .concat(childGroupChildValues) .filter((item) => !item.startsWith(CHILD_GROUP_PREFIX)); } return newValue.filter((item) => !item.startsWith(CHILD_GROUP_PREFIX)); };