UNPKG

choerodon-ui

Version:

An enterprise-class UI design language and React-based implementation

233 lines (201 loc) 7.2 kB
import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized"; import _inherits from "@babel/runtime/helpers/inherits"; import _createSuper from "@babel/runtime/helpers/createSuper"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; import React, { Component } from 'react'; import Select from './Select'; var formatOption = function formatOption(option, disabledOptions) { var value = "".concat(option); if (option < 10) { value = "0".concat(option); } var disabled = false; if (disabledOptions && disabledOptions.indexOf(option) >= 0) { disabled = true; } return { value: value, disabled: disabled }; }; var Combobox = /*#__PURE__*/function (_Component) { _inherits(Combobox, _Component); var _super = _createSuper(Combobox); function Combobox() { var _this; _classCallCheck(this, Combobox); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _super.call.apply(_super, [this].concat(args)); _defineProperty(_assertThisInitialized(_this), "onItemChange", function (type, itemValue) { var _this$props = _this.props, onChange = _this$props.onChange, defaultOpenValue = _this$props.defaultOpenValue, use12Hours = _this$props.use12Hours; var value = (_this.props.value || defaultOpenValue).clone(); if (type === 'hour') { if (use12Hours) { if (_this.props.isAM) { value.hour(+itemValue % 12); } else { value.hour(+itemValue % 12 + 12); } } else { value.hour(+itemValue); } } else if (type === 'minute') { value.minute(+itemValue); } else if (type === 'ampm') { var ampm = itemValue.toUpperCase(); if (use12Hours) { if (ampm === 'PM' && value.hour() < 12) { value.hour(value.hour() % 12 + 12); } if (ampm === 'AM') { if (value.hour() >= 12) { value.hour(value.hour() - 12); } } } } else { value.second(+itemValue); } onChange(value); }); _defineProperty(_assertThisInitialized(_this), "onEnterSelectPanel", function (range) { _this.props.onCurrentSelectPanelChange(range); }); return _this; } _createClass(Combobox, [{ key: "getHourSelect", value: function getHourSelect(hour) { var _this$props2 = this.props, prefixCls = _this$props2.prefixCls, hourOptions = _this$props2.hourOptions, disabledHours = _this$props2.disabledHours, showHour = _this$props2.showHour, use12Hours = _this$props2.use12Hours; if (!showHour) { return null; } var disabledOptions = disabledHours(); var hourOptionsAdj; var hourAdj; if (use12Hours) { hourOptionsAdj = [12].concat(hourOptions.filter(function (h) { return h < 12 && h > 0; })); hourAdj = hour % 12 || 12; } else { hourOptionsAdj = hourOptions; hourAdj = hour; } return /*#__PURE__*/React.createElement(Select, { prefixCls: prefixCls, options: hourOptionsAdj.map(function (option) { return formatOption(option, disabledOptions); }), selectedIndex: hourOptionsAdj.indexOf(hourAdj), type: "hour", onSelect: this.onItemChange, onMouseEnter: this.onEnterSelectPanel.bind(this, 'hour') }); } }, { key: "getMinuteSelect", value: function getMinuteSelect(minute) { var _this$props3 = this.props, prefixCls = _this$props3.prefixCls, minuteOptions = _this$props3.minuteOptions, disabledMinutes = _this$props3.disabledMinutes, defaultOpenValue = _this$props3.defaultOpenValue, showMinute = _this$props3.showMinute; if (!showMinute) { return null; } var value = this.props.value || defaultOpenValue; var disabledOptions = disabledMinutes(value.hour()); return /*#__PURE__*/React.createElement(Select, { prefixCls: prefixCls, options: minuteOptions.map(function (option) { return formatOption(option, disabledOptions); }), selectedIndex: minuteOptions.indexOf(minute), type: "minute", onSelect: this.onItemChange, onMouseEnter: this.onEnterSelectPanel.bind(this, 'minute') }); } }, { key: "getSecondSelect", value: function getSecondSelect(second) { var _this$props4 = this.props, prefixCls = _this$props4.prefixCls, secondOptions = _this$props4.secondOptions, disabledSeconds = _this$props4.disabledSeconds, showSecond = _this$props4.showSecond, defaultOpenValue = _this$props4.defaultOpenValue; if (!showSecond) { return null; } var value = this.props.value || defaultOpenValue; var disabledOptions = disabledSeconds(value.hour(), value.minute()); return /*#__PURE__*/React.createElement(Select, { prefixCls: prefixCls, options: secondOptions.map(function (option) { return formatOption(option, disabledOptions); }), selectedIndex: secondOptions.indexOf(second), type: "second", onSelect: this.onItemChange, onMouseEnter: this.onEnterSelectPanel.bind(this, 'second') }); } }, { key: "getAMPMSelect", value: function getAMPMSelect() { var _this$props5 = this.props, prefixCls = _this$props5.prefixCls, use12Hours = _this$props5.use12Hours, format = _this$props5.format; if (!use12Hours) { return null; } var AMPMOptions = ['am', 'pm'] // If format has A char, then we should uppercase AM/PM .map(function (c) { return format.match(/\sA/) ? c.toUpperCase() : c; }).map(function (c) { return { value: c }; }); var selected = this.props.isAM ? 0 : 1; return /*#__PURE__*/React.createElement(Select, { prefixCls: prefixCls, options: AMPMOptions, selectedIndex: selected, type: "ampm", onSelect: this.onItemChange, onMouseEnter: this.onEnterSelectPanel.bind(this, 'ampm') }); } }, { key: "render", value: function render() { var _this$props6 = this.props, prefixCls = _this$props6.prefixCls, defaultOpenValue = _this$props6.defaultOpenValue; var value = this.props.value || defaultOpenValue; return /*#__PURE__*/React.createElement("div", { className: "".concat(prefixCls, "-combobox") }, this.getHourSelect(value.hour()), this.getMinuteSelect(value.minute()), this.getSecondSelect(value.second()), this.getAMPMSelect(value.hour())); } }]); return Combobox; }(Component); export default Combobox; //# sourceMappingURL=Combobox.js.map