UNPKG

@wufengteam/wform

Version:

@wufengteam/wform

142 lines 6.53 kB
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } /* eslint-disable no-console */ import React, { useState, useEffect, useContext, useMemo } from 'react'; import { AddressPicker } from '@lingxiteam/factory/lib/index.export'; import { DFormContext } from '@lingxiteam/factory/lib/utils/form'; import { prefix } from '../../utils'; import { isString } from '../../utils/tool'; import "./index.css"; // 内部定义属性不全,导致value参数报错。这里类型强转下 var IAddressPicker = AddressPicker; var Cascader = function Cascader(props) { var _onChange = props.onChange, value = props.value, fieldName = props.fieldName, options = props.options, placeholder = props.placeholder, style = props.style, disabled = props.disabled, _props$defaultValue = props.defaultValue, defaultValue = _props$defaultValue === void 0 ? [] : _props$defaultValue; var _useState = useState({}), _useState2 = _slicedToArray(_useState, 2), labelMap = _useState2[0], setLabelMap = _useState2[1]; var cascValue = useMemo(function () { var val = value || defaultValue; if (val) { if (Array.isArray(val)) { if (val.length > 0) { var label = []; var queryValueTitle = function queryValueTitle(vals, data, index) { var _a, _b; var filter = data.filter(function (it) { return it.value === vals; }); if (filter && !!filter.length) { label.push((_a = filter[0]) === null || _a === void 0 ? void 0 : _a.title); if (val.length > index + 1) { queryValueTitle(val[index + 1], ((_b = filter[0]) === null || _b === void 0 ? void 0 : _b.children) || [], index + 1); } } }; queryValueTitle(val[0], options, 0); if (label.length > 0) { return { label: label, val: val }; } return val.join(); } return undefined; } return val; } return undefined; }, [value, defaultValue]); var mergedOptions = useMemo(function () { var generate = function generate(list) { if (!list) return undefined; return list.map(function (option) { return Object.assign(Object.assign({}, option), { label: option === null || option === void 0 ? void 0 : option.title, children: generate(option === null || option === void 0 ? void 0 : option.children) }); }); }; return generate(options); }, [options]); var getLabelMap = function getLabelMap() { var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; var result = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; // eslint-disable-next-line consistent-return data.forEach(function (item) { var key = item.key, title = item.title, _item$children = item.children, children = _item$children === void 0 ? [] : _item$children; // eslint-disable-next-line no-param-reassign result[key] = title; if (Array.isArray(children) && children.length > 0) { return getLabelMap(children, result); } }); return result; }; useEffect(function () { var result = getLabelMap(options); setLabelMap(result); }, [JSON.stringify(options)]); var _ref = useContext(DFormContext) || {}, form = _ref.form; useEffect(function () { if (value && Array.isArray(value)) { var label = value.map(function (item) { return labelMap[item]; }); form === null || form === void 0 ? void 0 : form.setFieldsValue({ useless: { label: label, value: value } }); } }, [JSON.stringify(value), labelMap]); useEffect(function () { var idNode = document.getElementById("alita-dform-".concat(fieldName)); idNode === null || idNode === void 0 ? void 0 : idNode.setAttribute('class', 'alita-dform-cascader'); }, []); if (isString(cascValue)) { return /*#__PURE__*/React.createElement("div", { className: "".concat(prefix, "-DformCascader") }, cascValue); } return /*#__PURE__*/React.createElement("div", { className: "".concat(prefix, "-DformCascader") }, /*#__PURE__*/React.createElement(IAddressPicker, { style: style, defaultValue: cascValue, formFlag: false, fieldProps: fieldName, disabled: disabled, data: mergedOptions, placeholder: placeholder, labelNumber: 0, placeholderList: [], onChange: function onChange(newValue) { var _a; if (newValue && ((_a = Object.keys(newValue)) === null || _a === void 0 ? void 0 : _a.length)) { _onChange === null || _onChange === void 0 ? void 0 : _onChange(newValue === null || newValue === void 0 ? void 0 : newValue.value); } else { _onChange === null || _onChange === void 0 ? void 0 : _onChange(undefined); } } })); }; export default Cascader;