UNPKG

@txdfe/at

Version:

一个设计体系组件库

360 lines (346 loc) 12.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.escapeForReg = escapeForReg; exports.filter = filter; exports.filterDataSource = filterDataSource; exports.flattingDataSource = flattingDataSource; exports.getValueDataSource = getValueDataSource; exports.isNull = isNull; exports.isSingle = isSingle; exports.loopMap = loopMap; exports.normalizeDataSource = normalizeDataSource; exports.parseDataSourceFromChildren = parseDataSourceFromChildren; exports.valueToSelectKey = valueToSelectKey; var _react = require("react"); var _excluded = ["value", "label", "title", "disabled"]; 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; } function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; } function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; } 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); } function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } 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); } /** * util module */ /** * 是否是单选模式 * @param {string} mode * @return {boolean} is single mode */ function isSingle(mode) { return !mode || mode === 'single' || mode === 'combobox'; } /** * 在 Select 中,认为 null 和 undefined 都是空值 * @param {*} n any object * @return {boolean} */ function isNull(n) { return n === null || n === undefined; } /** * 将字符串中的正则表达式关键字符添加转义 * @param {string} str * @return {string} */ function escapeForReg(str) { return str.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&'); } /** * filter by key * @param {string} key filter key * @param {object} item item object * @return {boolean} it's filtered */ function filter(key, item) { var _key = escapeForReg("".concat(key)); var regExp = new RegExp("(".concat(_key, ")"), 'ig'); return regExp.test("".concat(item.value)) || regExp.test("".concat(item.label)); } /** * loop map * @param {Array} dataSource * @param {function} callback * @return {Array} * ---- * @callback ~loopCallback * @param {object} option */ function loopMap(dataSource, callback) { var result = []; dataSource.forEach(function (option) { if (option.children) { var children = loopMap(option.children, callback); children.length && result.push(_objectSpread(_objectSpread({}, option), {}, { children: children })); } else { // eslint-disable-next-line callback-return var tmp = callback(option); tmp && result.push(tmp); } }); return result; } /** * Parse dataSource from MenuItem * @static * @param {Array<Element>} children * @param {number} [deep=0] recursion deep level */ function parseDataSourceFromChildren(children) { var deep = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; var source = []; _react.Children.forEach(children, function (child, index) { if (!child) { return; } var type = child.type, childProps = child.props; var item2 = { deep: deep }; var isOption = false; var isOptionGroup = false; if (typeof type === 'function' && type._typeMark === 'next_select_option' || type === 'option') { isOption = true; } if (typeof type === 'function' && type._typeMark === 'next_select_option_group' || type === 'optgroup') { isOptionGroup = true; } if (!isOption && !isOptionGroup) { return; } if (isOption) { // option // If children is a string, it can be used as value var isStrChild = typeof childProps.children === 'string'; // value > key > string children > index item2.value = 'value' in childProps ? childProps.value : 'key' in childProps ? childProps.key : isStrChild ? childProps.children : "".concat(index); item2.label = childProps.label || childProps.children || "".concat(item2.value); item2.title = childProps.title; childProps.disabled === true && (item2.disabled = true); // You can put your extra data here, and use it in `itemRender` or `labelRender` _extends(item2, childProps['data-extra'] || {}); } else if (isOptionGroup && deep < 1) { // option group item2.label = childProps.label || 'Group'; // parse children nodes item2.children = parseDataSourceFromChildren(childProps.children, deep + 1); } source.push(item2); }); return source; } /** * Normalize dataSource * @static * @param {Array} dataSource * @param {number} [deep=0] recursion deep level * ---- * value priority: value > 'index' * label priority: label > 'value' > 'index' * disabled: disabled === true */ function normalizeDataSource(dataSource) { var deep = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; var source = []; dataSource.forEach(function (item, index) { // enable array of basic type if (/string|boolean|number/.test(_typeof(item))) { item = { label: "".concat(item), value: item }; } // filter off addon item if (item.__isAddon) { return; } var item2 = { deep: deep }; // deep < 1: only 2 level allowed if (Array.isArray(item.children) && deep < 1) { // handle group item2.label = item.label || item.value || "Group ".concat(index); // parse children item2.children = normalizeDataSource(item.children, deep + 1); } else { var _item = item, value = _item.value, label = _item.label, title = _item.title, disabled = _item.disabled, others = _objectWithoutProperties(_item, _excluded); item2.value = !isNull(value) ? value : "".concat(index); item2.label = label || "".concat(item2.value); item2.title = title; disabled === true && (item2.disabled = true); _extends(item2, others); } source.push(item2); }); return source; } /** * Get flatten dataSource * @static * @param {Array} dataSource structured dataSource * @return {Array} */ function flattingDataSource(dataSource) { var source = []; dataSource.forEach(function (item) { if (Array.isArray(item.children)) { source.push.apply(source, _toConsumableArray(flattingDataSource(item.children))); } else { source.push(item); } }); return source; } function filterDataSource(dataSource, key, filter, addonKey) { if (!Array.isArray(dataSource)) { return []; } if (typeof key === 'undefined' || key === null) { return [].concat(dataSource); } var addKey = true; var menuDataSource = loopMap(dataSource, function (option) { if (key === "".concat(option.value)) { addKey = false; } return filter(key, option) && !option.__isAddon && option; }); // if key not in menuDataSource, add key to dataSource if (addonKey && key && addKey) { menuDataSource.unshift({ value: key, label: key, title: key, __isAddon: true }); } return menuDataSource; } function getKeyItemByValue(value, valueMap) { var item; if (_typeof(value) === 'object' && value.hasOwnProperty('value')) { item = value; } else { item = valueMap["".concat(value)] || { value: value, label: value }; } return item; } /** * compute valueDataSource by new value * @param {Array/String} value 数据 * @param {Object} mapValueDS 上个value的缓存数据 value => {value,label} 的映射关系表 * @param {*} mapMenuDS 通过 dataSource 建立 value => {value,label} 的映射关系表 * @returns {Object} value: [value]; valueDS: [{value,label}]; mapValueDS: {value: {value,label}} */ function getValueDataSource(value, mapValueDS, mapMenuDS) { if (isNull(value)) { return {}; } var newValue = []; var newValueDS = []; var newMapValueDS = {}; var _newMapDS = _extends({}, mapValueDS, mapMenuDS); if (Array.isArray(value)) { value.forEach(function (v) { var item = getKeyItemByValue(v, _newMapDS); newValueDS.push(item); newMapValueDS["".concat(item.value)] = item; newValue.push(item.value); }); return { value: newValue, // [value] valueDS: newValueDS, // [{value,label}] mapValueDS: newMapValueDS // {value: {value,label}} }; } else { var item = getKeyItemByValue(value, _newMapDS); return { value: item.value, valueDS: item, mapValueDS: _defineProperty({}, "".concat(item.value), item) }; } } /** * Get flatten dataSource * @static * @param {any} value structured dataSource * @return {String} */ function valueToSelectKey(value) { var val; if (_typeof(value) === 'object' && value.hasOwnProperty('value')) { val = value.value; } else { val = value; } return "".concat(val); } /** * UP Down 改进双向链表方法 */ // function DoubleLinkList(element){ // this.prev = null; // this.next = null; // this.element = element; // } // // export function mapDoubleLinkList(dataSource){ // // const mapDS = {}; // let doubleLink = null; // // let head = null; // let tail = null; // // function append(element) { // if (!doubleLink) { // doubleLink = new DoubleLinkList(element); // head = doubleLink; // tail = doubleLink; // return doubleLink; // } // // const node = new DoubleLinkList(element); // tail.next = node; // node.prev = tail; // tail = node; // // return tail; // } // // dataSource.forEach((item => { // if (item.disabled) { // return; // } // mapDS[`${item.value}`] = append(item); // })); // // return mapDS; // } //