UNPKG

ze-react-component-library

Version:
181 lines (153 loc) 5.18 kB
import "antd/es/message/style"; import _message from "antd/es/message"; var __assign = this && this.__assign || function () { __assign = Object.assign || function (t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) { if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } } return t; }; return __assign.apply(this, arguments); }; var __spreadArray = this && this.__spreadArray || function (to, from) { for (var i = 0, il = from.length, j = to.length; i < il; i++, j++) { to[j] = from[i]; } return to; }; import { useState, useEffect } from "react"; import { nanoid } from "nanoid"; import useService from "../hooks/useService"; export var loopTree = function loopTree(arr, id, formatItem, matchRule) { return arr.map(function (d) { if (matchRule ? matchRule(d, id) : d.id === id) { return formatItem(d); } else { if (d.children) { var children = loopTree(d.children, id, formatItem, matchRule); if ((children === null || children === void 0 ? void 0 : children.length) >= 2) { return __assign(__assign({}, d), { children: children }); } return __assign({}, children[0]); } return d; } }).filter(function (f) { return f !== null; }); }; export var useZETreeFilter = function useZETreeFilter(_a) { var value = _a.value, initialValue = _a.initialValue, onChange = _a.onChange, conditions = _a.conditions, maxCount = _a.maxCount, itemInitialProps = _a.itemInitialProps; var _b = useState([]), data = _b[0], setData = _b[1]; var actualValue = loopTree(value || data, "", function (item) { return __assign(__assign({}, item), { props: __assign(__assign({}, item.props), itemInitialProps) }); }, function (item) { return !item.children; }); var actualChange = function actualChange(v) { setData(v); onChange === null || onChange === void 0 ? void 0 : onChange(v); }; var onAdd = function onAdd(parentId, extraProps, mode) { var newItem = { id: nanoid(), props: __assign(__assign({}, itemInitialProps), extraProps) }; var firstItem = actualValue[0]; var id; if (parentId) { id = parentId; } else if (firstItem) { id = firstItem.id; } if (!firstItem) { actualChange([newItem]); } else { var newData = loopTree(actualValue, id, function (item) { var _a, _b; if (item.children) { var arr = __spreadArray([], item.children); var lastItem = item.children[item.children.length - 1]; if (mode === "drop" && lastItem && !((_a = lastItem.props) === null || _a === void 0 ? void 0 : _a.value) && !lastItem.children) { newItem.disableActions = lastItem.disableActions; newItem.disabled = lastItem.disabled; newItem.resetOnDelete = lastItem.resetOnDelete; arr.pop(); } return __assign(__assign({}, item), { children: __spreadArray(__spreadArray([], arr), [newItem]) }); } return { id: nanoid(), condition: (_b = conditions === null || conditions === void 0 ? void 0 : conditions[0]) === null || _b === void 0 ? void 0 : _b.value, children: [__assign({}, item), newItem] }; }); actualChange(newData); } }; var onUpdate = function onUpdate(id, changedProps) { actualChange(loopTree(actualValue, id, function (item) { return changedProps ? __assign(__assign({}, item), { props: __assign(__assign({}, item.props), changedProps) }) : item; })); }; var onRemove = function onRemove(id) { actualChange(loopTree(actualValue, id, function () { return null; })); }; var onReset = function onReset(id) { actualChange(loopTree(actualValue, id, function (item) { return __assign(__assign({}, item), { props: itemInitialProps }); })); }; var onSwitch = function onSwitch(id, newCondition) { actualChange(loopTree(actualValue, id, function (item) { return __assign(__assign({}, item), { condition: newCondition }); })); }; var onRootAdd = function onRootAdd(extraProps, mode) { var _a, _b, _c; if (!maxCount || !((_a = actualValue[0]) === null || _a === void 0 ? void 0 : _a.children) && actualValue.length < maxCount || ((_b = actualValue[0]) === null || _b === void 0 ? void 0 : _b.children) && actualValue[0].children.length < maxCount) { onAdd((_c = actualValue[0]) === null || _c === void 0 ? void 0 : _c.id, extraProps, mode); } else { _message.error("\u6700\u591A\u65B0\u589E" + maxCount + "\u4E2A"); } }; useEffect(function () { if (initialValue && !value) { actualChange(initialValue); } }, []); return { data: actualValue, onAdd: onAdd, onRemove: onRemove, onReset: onReset, onRootAdd: onRootAdd, onSwitch: onSwitch, onUpdate: onUpdate }; }; export var ZETreeFilterContext = useService(useZETreeFilter);