UNPKG

tntd

Version:

tntd是基于 TNT Design 设计体系的 React UI 组件库,主要用于研发企业级中后台产品。

147 lines (145 loc) 5.62 kB
"use strict"; function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = exports.SEARCH_MARK = void 0; var React = _interopRequireWildcard(require("react")); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); } function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } var SEARCH_MARK = exports.SEARCH_MARK = '__rc_cascader_search_mark__'; var defaultFilter = function defaultFilter(search, options, _ref) { var label = _ref.label; return options.some(function (opt) { return String(opt[label]).toLowerCase().includes(search.toLowerCase()); }); }; var defaultRender = function defaultRender(inputValue, path, prefixCls, fieldNames) { return path.map(function (opt) { return opt[fieldNames.label]; }).join(' / '); }; var _default = exports["default"] = function _default(search, options, fieldNames, prefixCls, config, changeOnSelect) { var _config$filter = config.filter, filter = _config$filter === void 0 ? defaultFilter : _config$filter, _config$render = config.render, render = _config$render === void 0 ? defaultRender : _config$render, _config$limit = config.limit, limit = _config$limit === void 0 ? false : _config$limit, sort = config.sort; return React.useMemo(function () { var filteredOptions = []; if (!search) { return []; } function dig(list, pathOptions) { list.forEach(function (option) { // Perf saving when `sort` is disabled and `limit` is provided if (!sort && limit > 0 && filteredOptions.length >= limit) { return; } var connectedPathOptions = [].concat(_toConsumableArray(pathOptions), [option]); var children = option[fieldNames.children]; // If current option is filterable if ( // If is leaf option !children || children.length === 0 || // If is changeOnSelect changeOnSelect) { if (filter(search, connectedPathOptions, { label: fieldNames.label })) { filteredOptions.push(Object.assign(Object.assign({}, option), _defineProperty(_defineProperty({}, fieldNames.label, render(search, connectedPathOptions, prefixCls, fieldNames)), SEARCH_MARK, connectedPathOptions))); } } if (children) { dig(option[fieldNames.children], connectedPathOptions); } }); } dig(options, []); // Do sort if (sort) { filteredOptions.sort(function (a, b) { return sort(a[SEARCH_MARK], b[SEARCH_MARK], search, fieldNames); }); } return limit > 0 ? filteredOptions.slice(0, limit) : filteredOptions; }, [search, options, fieldNames, prefixCls, render, changeOnSelect, filter, sort, limit]); };