mobile-more
Version:
基于 antd-mobile v5 扩展移动端 UI 组件
49 lines • 1.86 kB
JavaScript
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
var _excluded = ["value", "mapKeys", "options"];
import * as React from 'react';
import { isArray, omit } from 'ut2';
import SuperCascadePicker from '../ItemCascadePicker/SuperCascadePicker';
import transformKeys from '../utils/transformKeys';
import parseAreaCode from './parseAreaCode';
var AreaCodePicker = function AreaCodePicker(_ref) {
var value = _ref.value,
mapKeys = _ref.mapKeys,
_ref$options = _ref.options,
outOptions = _ref$options === void 0 ? [] : _ref$options,
restProps = _objectWithoutProperties(_ref, _excluded);
var options = React.useMemo(function () {
if (mapKeys) {
return transformKeys(outOptions, mapKeys);
}
return outOptions;
}, [mapKeys, outOptions]);
var data = React.useMemo(function () {
var ret = [];
function recursion(opts) {
var index = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
opts.forEach(function (item) {
var currItem = omit(item, 'children');
ret.push(currItem);
if (isArray(item === null || item === void 0 ? void 0 : item.children) && item.children.length > 0) {
recursion(item.children, index + 1);
}
});
}
recursion(options);
return ret;
}, [options]);
var realValue = React.useMemo(function () {
if (typeof value === 'string') {
return parseAreaCode(value, data).map(function (item) {
return (item === null || item === void 0 ? void 0 : item.value) || null;
});
}
return value;
}, [data, value]);
return /*#__PURE__*/React.createElement(SuperCascadePicker, _objectSpread({
value: realValue,
options: options
}, restProps));
};
export default AreaCodePicker;