UNPKG

deer-ui

Version:

React.js UI components for PC Web

332 lines (286 loc) 11.9 kB
"use strict"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized")); var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")); var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _react = _interopRequireWildcard(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _reactDom = require("react-dom"); var _scrollIntoViewIfNeeded = _interopRequireDefault(require("scroll-into-view-if-needed")); var _classnames = _interopRequireDefault(require("classnames")); var _input = _interopRequireDefault(require("../input")); var _empty = _interopRequireDefault(require("../empty")); var _utils = require("../utils"); var _icon = require("../icon"); function _createSuper(Derived) { return function () { var Super = (0, _getPrototypeOf2.default)(Derived), result; if (_isNativeReflectConstruct()) { var NewTarget = (0, _getPrototypeOf2.default)(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(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 { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } var sizes = { default: "default", small: "small", large: "large" }; var Select = /*#__PURE__*/function (_React$Component) { (0, _inherits2.default)(Select, _React$Component); var _super = _createSuper(Select); function Select(props) { var _this; (0, _classCallCheck2.default)(this, Select); _this = _super.call(this, props); (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "state", { selectValue: _this.props.defaultValue || _this.props.value || "", visible: _this.props.visible || false, left: 0, top: 0, width: 0 }); (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onResizeHandler", (0, _utils.debounce)(function () { _this.setWrapperBounding(); }, 600)); (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onClickOutsideHandler", function (e) { e.stopPropagation(); // .contains() 判断一个元素内是否包含另一个元素 if (_this.state.visible && !_this.props.visible && !_this.toggleContainer.current.contains(e.target) && !e.target.classList.contains("".concat(_this.props.prefixCls, "-option-disabled"))) { _this.setState({ visible: false }); } }); (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onClickHandler", function () { var visible = !_this.state.visible; _this.setState({ visible: visible }); if (visible) { _this.setWrapperBounding(function () { //将隐藏的内容显示出来 (0, _scrollIntoViewIfNeeded.default)(_this.wrapper.current, { scrollMode: "if-needed", behavior: "smooth", block: "nearest", inline: "nearest" }); }); } }); (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onClear", function () { _this.setState({ selectValue: "", visible: false }); _this.props.onChange(""); }); (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onChange", function (value, label) { var showLabel = _this.props.showLabel; _this.setState({ selectValue: value, visible: false, label: label }); if (showLabel) { _this.props.onChange({ key: value, label: label }); } else { _this.props.onChange(value); } }); _this.toggleContainer = (0, _react.createRef)(); _this.wrapper = (0, _react.createRef)(); return _this; } (0, _createClass2.default)(Select, [{ key: "componentDidMount", value: function componentDidMount() { window.addEventListener("click", this.onClickOutsideHandler, false); window.addEventListener("resize", this.onResizeHandler); if (this.props.visible) { this.setWrapperBounding(); } } }, { key: "setWrapperBounding", value: function setWrapperBounding() { var cb = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {}; var _this$getWrapperBound = this.getWrapperBounding(), left = _this$getWrapperBound.left, top = _this$getWrapperBound.top, width = _this$getWrapperBound.width; this.setState({ left: left, top: top, width: width }, cb); } }, { key: "getWrapperBounding", value: function getWrapperBounding() { var _this$toggleContainer = this.toggleContainer.current.getBoundingClientRect(), width = _this$toggleContainer.width, height = _this$toggleContainer.height, top = _this$toggleContainer.top, left = _this$toggleContainer.left; var _this$wrapper$current = this.wrapper.current.getBoundingClientRect(), wrapperHeight = _this$wrapper$current.height; var _window = window, scrollX = _window.scrollX, scrollY = _window.scrollY; var positions = { //显示在上 top: { top: top + scrollY - wrapperHeight - 10, left: left + scrollX, width: width }, //显示在下 bottom: { top: top + height + scrollY, left: left + scrollX, width: width } }; return positions[this.props.position]; } }, { key: "componentWillUnmount", value: function componentWillUnmount() { window.removeEventListener("click", this.onClickOutsideHandler, false); window.removeEventListener("resize", this.onResizeHandler); } }, { key: "render", value: function render() { var _cls2, _this2 = this; var _this$state = this.state, visible = _this$state.visible, left = _this$state.left, top = _this$state.top, width = _this$state.width; var _this$props = this.props, prefixCls = _this$props.prefixCls, className = _this$props.className, style = _this$props.style, disabled = _this$props.disabled, size = _this$props.size, allowClear = _this$props.allowClear, loading = _this$props.loading, children = _this$props.children, NoData = _this$props.NoData, placeholder = _this$props.placeholder, getPopupContainer = _this$props.getPopupContainer, showLabel = _this$props.showLabel, attr = (0, _objectWithoutProperties2.default)(_this$props, ["prefixCls", "className", "style", "disabled", "size", "allowClear", "loading", "children", "NoData", "placeholder", "getPopupContainer", "showLabel"]); return /*#__PURE__*/_react.default.createElement("div", (0, _extends2.default)({ className: (0, _classnames.default)("".concat(prefixCls), className), style: style, ref: this.toggleContainer }, attr), /*#__PURE__*/_react.default.createElement("div", { className: (0, _classnames.default)("".concat(prefixCls, "-inner"), (0, _defineProperty2.default)({}, "".concat(prefixCls, "-active"), visible)) }, /*#__PURE__*/_react.default.createElement(_input.default, { value: this.selectValue, disabled: disabled, readOnly: true, size: size, placeholder: placeholder, style: { width: style && style.width }, allowClear: allowClear, onClick: this.onClickHandler, onClear: this.onClear, suffix: loading ? /*#__PURE__*/_react.default.createElement(_icon.LoadingIcon, { className: "".concat(prefixCls, "-loading") }) : /*#__PURE__*/_react.default.createElement(_icon.DownIcon, { className: "".concat(prefixCls, "-arrow") }) })), (0, _reactDom.createPortal)( /*#__PURE__*/_react.default.createElement("div", { className: (0, _classnames.default)("".concat(prefixCls, "-content"), (_cls2 = {}, (0, _defineProperty2.default)(_cls2, "".concat(prefixCls, "-open"), visible), (0, _defineProperty2.default)(_cls2, "".concat(prefixCls, "-close"), !visible), _cls2)), ref: this.wrapper, style: { width: width, left: left, top: top } }, children ? _react.default.Children.map(children, function (element, key) { return _react.default.cloneElement(element, { key: key, onChange: _this2.onChange, selectValue: _this2.state.selectValue }); }) : NoData), getPopupContainer())); } }, { key: "selectValue", get: function get() { var selectValue = this.state.selectValue; if (this.props.children) { return (this.selectOptions.find(function (_ref) { var key = _ref.key; return key === selectValue; }) || {}).value || ""; } else { return selectValue; } } }, { key: "selectOptions", get: function get() { if (this.props.children) { return this.props.children.map(function (_ref2) { var props = _ref2.props; return { key: props.value, value: props.children }; }); } return {}; } }], [{ key: "getDerivedStateFromProps", value: function getDerivedStateFromProps(nextProps, prevState) { var defaultValue = nextProps.defaultValue; if (defaultValue === prevState.selectValue) { return { selectValue: defaultValue }; } return null; } }]); return Select; }(_react.default.Component); (0, _defineProperty2.default)(Select, "propTypes", { prefixCls: _propTypes.default.string.isRequired, onChange: _propTypes.default.func, defaultValue: _propTypes.default.string, value: _propTypes.default.string, size: _propTypes.default.oneOf(Object.values(sizes)), showLabel: _propTypes.default.bool, getPopupContainer: _propTypes.default.func, position: _propTypes.default.string }); (0, _defineProperty2.default)(Select, "defaultProps", { prefixCls: "deer-ui-select", onChange: function onChange() {}, size: "default", NoData: /*#__PURE__*/_react.default.createElement(_empty.default, null), showLabel: false, getPopupContainer: function getPopupContainer() { return document.body; }, position: "bottom" }); var _default = Select; exports.default = _default;