UNPKG

ukelli-ui

Version:

[![Build Status](https://travis-ci.org/ukelli/ukelli-ui.svg?branch=master)](https://travis-ci.org/ukelli/ukelli-ui) [![install size](https://packagephobia.now.sh/badge?p=ukelli-ui)](https://packagephobia.now.sh/result?p=ukelli-ui)

213 lines (173 loc) 8.54 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"); 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 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 SelectorBasic = /*#__PURE__*/ function (_Component) { _inherits(SelectorBasic, _Component); function SelectorBasic(props) { var _this; _classCallCheck(this, SelectorBasic); _this = _possibleConstructorReturn(this, _getPrototypeOf(SelectorBasic).call(this, props)); var value = props.value, defaultValue = props.defaultValue, isMultiple = props.isMultiple; // 如果是多选模式,value, defaultValue 必须为array,否则value, defaultValue必须为string _this.isControl = props.hasOwnProperty('value'); // 受控模式, 详情请查看 react control form // selectedValue = [...values]; // value 结构: ['values.value'] _this.value = value || defaultValue; _this.state = { selectedValue: _this.toArr(_this.value) }; _this.wrapValues(); return _this; } _createClass(SelectorBasic, [{ key: "toArr", value: function toArr(target) { return Array.isArray(target) ? target : [target]; } // shouldUpdateComponent(nextState, nextProps) { // // 如果是受控模式,必须是外部value改变才作出渲染 // const isChange = this.isControl ? JSON.stringify(this.props) !== JSON.stringify(nextProps) : JSON.stringify(this.state) !== JSON.stringify(nextState); // return isChange || this.state.isShow !== nextState.isShow; // } }, { key: "changeValue", value: function changeValue(value, idx) { var isMultiple = this.props.isMultiple; var selectedValue = this.getValue(); var nextValue = []; if (isMultiple) { nextValue = selectedValue || []; if (nextValue.indexOf(value) > -1) { nextValue = (0, _basicHelper.RemoveArrayItem)(nextValue, value); } else { nextValue.push(value); } } else { nextValue = [value]; } this.changeEvent(nextValue, { prevVal: selectedValue, idx: idx }); } }, { 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) { return Object.keys(values).map(function (valKey) { return { text: values[valKey], value: valKey }; }); } }, { key: "wrapArrayValToObj", value: function wrapArrayValToObj(values) { var result = {}; values.forEach(function (val) { result[val.value] = val.text; }); return result; } }, { key: "changeEvent", value: function changeEvent(nextValue) { var _this$props = this.props, isNum = _this$props.isNum, isMultiple = _this$props.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]; } this.emitChange.apply(this, [emitVal].concat(other)); if ((0, _basicHelper.IsFunc)(this.onChangeValue)) { this.onChangeValue.apply(this, [emitVal].concat(other)); } else { this.setState({ selectedValue: nextValue }); } } }, { key: "emitChange", value: function emitChange() { 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)); } }, { key: "selectAll", value: function selectAll() { this.changeEvent(Object.keys(this.props.values)); } }, { key: "checkIsSelectedAll", value: function checkIsSelectedAll() { var selectedValue = this.getValue(); return !!this.values && !!selectedValue && this.values.length == selectedValue.length; } }, { key: "getValue", value: function getValue() { // return this.state.selectedValue; return this.isControl ? this.props.value : this.state.selectedValue; } }]); return SelectorBasic; }(_react.Component); 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 });