ukelli-ui
Version:
[](https://travis-ci.org/ukelli/ukelli-ui) [](https://packagephobia.now.sh/result?p=ukelli-ui)
249 lines (204 loc) • 9.46 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _basicHelper = require("basic-helper");
var _icon = require("../icon");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var ideaTipsGroup = [1, 5, 10, 100];
var MultipleHelper =
/*#__PURE__*/
function (_PureComponent) {
_inherits(MultipleHelper, _PureComponent);
function MultipleHelper(props) {
var _this;
_classCallCheck(this, MultipleHelper);
_this = _possibleConstructorReturn(this, _getPrototypeOf(MultipleHelper).call(this, props));
var _props$range = props.range,
range = _props$range === void 0 ? ideaTipsGroup : _props$range,
defaultValue = props.defaultValue;
_this.state = {
isShowIdea: false,
value: defaultValue || range[0]
};
_this.defaultMax = 99999;
return _this;
}
_createClass(MultipleHelper, [{
key: "componentDidMount",
value: function componentDidMount() {
this.multipleHelper.defaultValue = '1';
}
}, {
key: "multipleOperation",
value: function multipleOperation(symbol) {
var _this$props$basicUnit = this.props.basicUnit,
basicUnit = _this$props$basicUnit === void 0 ? 1 : _this$props$basicUnit;
var value = this.state.value;
var _result = +value;
switch (symbol) {
case 'plus':
_result += basicUnit;
break;
case 'less':
_result -= basicUnit;
break;
}
this.changeValue(_result);
}
}, {
key: "changeValue",
value: function changeValue(val) {
var _this$props = this.props,
onChange = _this$props.onChange,
_this$props$min = _this$props.min,
min = _this$props$min === void 0 ? 1 : _this$props$min,
_this$props$max = _this$props.max,
max = _this$props$max === void 0 ? this.defaultMax : _this$props$max;
var value = +val || 1; // if(value < min || value === 0) {
// value = min;
// } else if(value > max) {
// value = max;
// }
this.setState({
value: value
});
(0, _basicHelper.Call)(onChange, value);
}
}, {
key: "checkValue",
value: function checkValue() {
var _this$props2 = this.props,
_this$props2$min = _this$props2.min,
min = _this$props2$min === void 0 ? 1 : _this$props2$min,
_this$props2$max = _this$props2.max,
max = _this$props2$max === void 0 ? this.defaultMax : _this$props2$max;
var value = this.state.value;
if (value < min || value === 0) {
value = min;
} else if (value > max) {
value = max;
}
this.changeValue(value);
}
}, {
key: "setIdea",
value: function setIdea(isShow) {
this.setState({
isShowIdea: isShow
});
} // shouldComponentUpdate(nextProps, nextState) {
// return this.props.defaultValue !== nextProps.defaultValue || this.state.isShowIdea !== nextState.isShowIdea;
// }
}, {
key: "render",
value: function render() {
var _this2 = this;
var _this$props3 = this.props,
onChange = _this$props3.onChange,
defaultValue = _this$props3.defaultValue,
suffix = _this$props3.suffix,
_this$props3$range = _this$props3.range,
range = _this$props3$range === void 0 ? ideaTipsGroup : _this$props3$range,
_this$props3$inputabl = _this$props3.inputable,
inputable = _this$props3$inputabl === void 0 ? true : _this$props3$inputabl;
var _this$state = this.state,
isShowIdea = _this$state.isShowIdea,
value = _this$state.value;
return _react.default.createElement("div", {
className: "multiple-helper" + (isShowIdea ? ' show' : '')
}, _react.default.createElement("div", {
className: "layout a-i-str j-c-b"
}, _react.default.createElement("span", {
className: "multiple-action-btn",
onClick: function onClick(e) {
return _this2.multipleOperation('less');
}
}, "-"), _react.default.createElement("input", {
type: "text",
name: "multiple",
className: "multiple-input-control",
onFocus: function onFocus(e) {
e.target.select();
},
onBlur: function onBlur(e) {
var self = _this2;
setTimeout(function () {
if (!isShowIdea) return;
self.setIdea(false); // self.checkValue();
}, 1 * 100);
},
value: value,
ref: function ref(e) {
return _this2.multipleHelper = e;
},
onChange: function onChange(e) {
if (!inputable) return;
_this2.changeValue(e.target.value);
}
}), _react.default.createElement("span", {
className: "multiple-tip"
}, suffix), _react.default.createElement("span", {
className: "multiple-action-btn",
onClick: function onClick(e) {
return _this2.multipleOperation('plus');
}
}, "+"), _react.default.createElement("span", {
className: "ps5 toggle-tip-btn",
onClick: function onClick(e) {
_this2.multipleHelper.focus();
_this2.setIdea(true);
}
}, _react.default.createElement(_icon.Icon, {
n: "more"
}))), _react.default.createElement("div", {
className: "idea-tip"
}, range.map(function (item) {
return _react.default.createElement("div", {
key: item,
className: "item",
onClick: function onClick(e) {
_this2.setIdea(false);
_this2.changeValue(item);
}
}, _react.default.createElement("span", {
className: "mul"
}, item), suffix);
})));
}
}]);
return MultipleHelper;
}(_react.PureComponent);
exports.default = MultipleHelper;
_defineProperty(MultipleHelper, "propTypes", {
/** onChange */
onChange: _propTypes.default.func,
/** 是否可输入 */
inputable: _propTypes.default.bool,
/** 每一次输入的单位 */
basicUnit: _propTypes.default.number,
/** 最小输入 */
min: _propTypes.default.number,
/** 最大输入 */
max: _propTypes.default.number,
/** 默认值 */
defaultValue: _propTypes.default.any,
/** 后缀 */
suffix: _propTypes.default.string,
/** 下拉提示的组合 */
range: _propTypes.default.array
});