@jdcfe/yep-react
Version:
一套移动端的React组件库
198 lines (175 loc) • 6.9 kB
JavaScript
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
import * as React from 'react';
import classNames from 'classnames';
import Popup from '../popup';
import CheckOutlined from '@jdcfe/icons-react/CheckOutlined';
import Tabs from '../tabs';
import TabBar from './TabBar';
import Icon from '../icon';
var TabPanel = Tabs.TabPanel;
var CloseO = Icon.CloseO;
var AreaPicker = /*#__PURE__*/function (_React$PureComponent) {
_inherits(AreaPicker, _React$PureComponent);
var _super = _createSuper(AreaPicker);
function AreaPicker(props) {
var _this;
_classCallCheck(this, AreaPicker);
_this = _super.call(this, props);
_this.onClick = function (city, index) {
var _this$props = _this.props,
fetchAction = _this$props.fetchAction,
onOk = _this$props.onOk,
keyExtractor = _this$props.keyExtractor;
var _this$state = _this.state,
tabs = _this$state.tabs,
data = _this$state.data;
var id = keyExtractor(city, index);
var tempTabs = tabs;
var tempData = data;
if (tempTabs[index]) {
tempTabs.splice(index);
}
tempData.splice(index + 1);
if (tabs.filter(function (tab, i) {
return keyExtractor(tab, i) === id;
}).length === 0) {
tempTabs = tempTabs.concat(city);
}
fetchAction(city, index).then(function (res) {
if (res.length > 0) {
var defaultIndex = index + 1;
_this.setState({
tabs: tempTabs,
data: tempData.concat([res]),
defaultIndex: defaultIndex
});
_this.tabs.tabClickGoToTab(defaultIndex);
} else {
_this.setState({
tabs: tempTabs,
data: tempData
});
onOk(tempTabs);
}
});
};
_this.state = {
tabs: [],
data: [props.initialData],
defaultIndex: 0
};
return _this;
}
_createClass(AreaPicker, [{
key: "componentDidMount",
value: function componentDidMount() {
var _this2 = this;
var _this$props2 = this.props,
selected = _this$props2.selected,
fetchAction = _this$props2.fetchAction,
initialData = _this$props2.initialData;
if (selected.length > 0) {
Promise.all(selected.map(function (city, index) {
return fetchAction(city, index);
})).then(function (res) {
_this2.setState({
tabs: selected,
data: [initialData].concat(res.filter(function (item) {
return item.length > 0;
})),
defaultIndex: selected.length - 1
});
});
}
}
}, {
key: "render",
value: function render() {
var _this3 = this;
var _this$props3 = this.props,
show = _this$props3.show,
onCancel = _this$props3.onCancel,
prefixCls = _this$props3.prefixCls,
className = _this$props3.className,
style = _this$props3.style,
title = _this$props3.title,
maskCloseable = _this$props3.maskCloseable,
distanceToChangeTab = _this$props3.distanceToChangeTab,
keyExtractor = _this$props3.keyExtractor,
nameExtractor = _this$props3.nameExtractor,
chooseLabel = _this$props3.chooseLabel;
var _this$state2 = this.state,
data = _this$state2.data,
tabs = _this$state2.tabs,
defaultIndex = _this$state2.defaultIndex;
var cls = classNames(prefixCls, className);
return /*#__PURE__*/React.createElement(Popup, {
show: show,
onCancel: onCancel,
maskCloseable: maskCloseable
}, /*#__PURE__*/React.createElement("div", {
className: cls,
style: style
}, /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-title-wrapper")
}, /*#__PURE__*/React.createElement("h1", {
className: "".concat(prefixCls, "-title")
}, title), /*#__PURE__*/React.createElement(CloseO, {
className: "Yep-icon-sm",
onClick: onCancel
})), /*#__PURE__*/React.createElement(Tabs, {
defaultIndex: defaultIndex,
distanceToChangeTab: distanceToChangeTab,
ref: function ref(_ref) {
return _this3.tabs = _ref;
},
renderTabBar: function renderTabBar(props) {
return /*#__PURE__*/React.createElement(TabBar, props);
}
}, data.map(function (item, index) {
return /*#__PURE__*/React.createElement(TabPanel, {
tab: tabs[index] && nameExtractor(tabs[index]) || chooseLabel,
key: index
}, /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-content Yep-scroller")
}, /*#__PURE__*/React.createElement("ul", null, item.map(function (city) {
return /*#__PURE__*/React.createElement("li", {
key: keyExtractor(city, index),
onClick: function onClick() {
return _this3.onClick(city, index);
}
}, tabs[index] && keyExtractor(tabs[index]) === keyExtractor(city) && /*#__PURE__*/React.createElement(CheckOutlined, {
className: "".concat(prefixCls, "-area--selected")
}), nameExtractor(city));
}))));
}))));
}
}]);
return AreaPicker;
}(React.PureComponent);
export { AreaPicker as default };
AreaPicker.defaultProps = {
prefixCls: 'Yep-area-picker',
title: '配送至',
chooseLabel: '请选择',
style: {},
show: false,
maskCloseable: false,
distanceToChangeTab: 10,
keyExtractor: function keyExtractor(item) {
return item.id;
},
nameExtractor: function nameExtractor(item) {
return item.name;
},
selected: [],
initialData: [],
onOk: function onOk() {},
onCancel: function onCancel() {}
};