UNPKG

ukelli-ui

Version:

ukelli-ui 是基于 React 的 UI 库,提供简约和功能齐全的组件,可高度定制的组件接口,灵活的配置,提供给开发者另一种开发思路,也致力于尝试不同的组件使用和开发方向。

282 lines (223 loc) 10.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = exports.selectorValuesType = void 0; var _react = _interopRequireWildcard(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _basicHelper = require("basic-helper"); var _ukeUtils = require("../uke-utils"); var _formControlBasic = _interopRequireDefault(require("../form-control/form-control-basic")); 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 _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); } function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } } 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 _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } 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 selectorValuesType = _propTypes["default"].oneOfType([_propTypes["default"].shape({ value: _propTypes["default"].any, text: _propTypes["default"].any }), _propTypes["default"].arrayOf(_propTypes["default"].shape({ text: _propTypes["default"].any, value: _propTypes["default"].any }))]); exports.selectorValuesType = selectorValuesType; var checkValuesIsEqual = function checkValuesIsEqual(value1, value2) { if (!value1 || !value2) return true; var res; if (Array.isArray(value1)) { res = value1.length === value2.length; } else { var valObj1 = Object.keys(value1); var valObj2 = Object.keys(value2); if (valObj1.length !== valObj2.length) { res = false; } else { res = JSON.stringify(valObj1) === JSON.stringify(valObj2); } } return res; }; var SelectorBasic = /*#__PURE__*/ function (_FormControlBasic) { _inherits(SelectorBasic, _FormControlBasic); function SelectorBasic(props) { var _this; _classCallCheck(this, SelectorBasic); _this = _possibleConstructorReturn(this, _getPrototypeOf(SelectorBasic).call(this, props)); _defineProperty(_assertThisInitialized(_this), "changeValue", function (value, idx) { var _this$props = _this.props, isNum = _this$props.isNum, isMultiple = _this$props.isMultiple; if (isNum) value = +value; if ((0, _basicHelper.HasValue)(value)) { var selectedValue = _this.getValue(); var nextValue = []; var targetVal = value; var removeItem; var addVal; if (isMultiple) { nextValue = Array.isArray(selectedValue) ? _toConsumableArray(selectedValue) : []; var valueIdx = nextValue.indexOf(value); if (valueIdx > -1) { removeItem = nextValue.splice(valueIdx, 1); // nextValue = RemoveArrayItem(nextValue, value); } else { nextValue.push(value); addVal = [value]; } } else { nextValue = [value]; addVal = value; } _this.changeEvent(nextValue, { prevVal: selectedValue, idx: idx, removeItem: removeItem, addVal: addVal, targetVal: targetVal }); } else { _this.emitChange(); } }); _defineProperty(_assertThisInitialized(_this), "changeEvent", function (nextValue) { var _this2; var _this$props2 = _this.props, isNum = _this$props2.isNum, isMultiple = _this$props2.isMultiple; if (isNum) { nextValue.forEach(function (_, idx) { nextValue[idx] = +_; }); } var emitVal = isMultiple ? nextValue : nextValue[0]; for (var _len = arguments.length, other = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { other[_key - 1] = arguments[_key]; } (_this2 = _this).emitChange.apply(_this2, [emitVal].concat(other)); if ((0, _basicHelper.IsFunc)(_this.onChangeValue)) { var _this3; (_this3 = _this).onChangeValue.apply(_this3, [emitVal].concat(other)); } else { _this.setState({ selectedValue: nextValue }); _this.value = nextValue; } }); _defineProperty(_assertThisInitialized(_this), "emitChange", function () { for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { args[_key2] = arguments[_key2]; } _basicHelper.Call.apply(void 0, [_this.props.onChange].concat(args)); }); var _value = props.value, defaultValue = props.defaultValue; // 受控模式, 详情请查看 react control form // selectedValue = [...values]; // value 结构: ['values.value'] _this.value = _this.toArr((0, _basicHelper.HasValue)(_value) ? _value : defaultValue); _this.state = { selectedValue: _this.value }; _this.wrapValues(); return _this; } _createClass(SelectorBasic, [{ key: "shouldComponentUpdate", value: function shouldComponentUpdate(nextProps, prevState) { /** 当 values 发生改变时,重新计算 this.values, 并且清空 value */ if (!checkValuesIsEqual(this.props.values, nextProps.values)) { this.wrapValues(nextProps.values); this.changeValue(); } return true; } }, { key: "toArr", value: function toArr(target) { if (!(0, _basicHelper.HasValue)(target)) return target; return Array.isArray(target) ? target : [target]; } }, { key: "wrapValues", value: function wrapValues() { var values = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.props.values; var isArrayValues = Array.isArray(values); this.values = isArrayValues ? values : this.wrapObjValToArr(values); this.valuesObj = isArrayValues ? this.wrapArrayValToObj(values) : values; return { valArr: this.values, valObj: this.valuesObj }; } }, { key: "wrapObjValToArr", value: function wrapObjValToArr(values) { if (!values) return {}; var isNum = this.props.isNum; return Object.keys(values).map(function (valKey) { return { text: values[valKey], value: isNum ? +valKey : valKey }; }); } }, { key: "wrapArrayValToObj", value: function wrapArrayValToObj(values) { var result = {}; values.forEach(function (val) { result[val.value] = val.text; }); return result; } /** * 选择器统一的更改 value 接口,会自动根据自身是否受控组件来更改 * @param {*} nextValue 下一个 value * @param {...any} other */ }, { key: "selectAll", value: function selectAll() { this.changeEvent(Object.keys(this.props.values), { preVal: this.getValue() }); } }, { key: "clearAll", value: function clearAll() { this.changeEvent([], { preVal: this.getValue() }); } }, { key: "checkIsSelectedAll", value: function checkIsSelectedAll() { var selectedValue = this.getValue(); return !!this.values && !!selectedValue && this.values.length == selectedValue.length; } }]); return SelectorBasic; }(_formControlBasic["default"]); exports["default"] = SelectorBasic; _defineProperty(SelectorBasic, "propTypes", { values: selectorValuesType.isRequired, defaultValue: _propTypes["default"].any, className: _propTypes["default"].string, value: _propTypes["default"].any, isNum: _propTypes["default"].bool, isMultiple: _propTypes["default"].bool, style: _propTypes["default"].object, onChange: _propTypes["default"].func });