@txdfe/at
Version:
一个设计体系组件库
214 lines (176 loc) • 10.3 kB
JavaScript
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
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); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
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 } }); Object.defineProperty(subClass, "prototype", { writable: false }); 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 _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } 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 _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
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; }
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
import Icon from '../../icon';
import { func, obj, KEYCODE } from '../../util';
import Item from './item';
var bindCtx = func.bindCtx;
var pickOthers = obj.pickOthers;
/**
* Menu.Item
* @order 0
*/
var SelectableItem = /*#__PURE__*/function (_Component) {
_inherits(SelectableItem, _Component);
var _super = _createSuper(SelectableItem);
function SelectableItem(props) {
var _this;
_classCallCheck(this, SelectableItem);
_this = _super.call(this, props);
bindCtx(_assertThisInitialized(_this), ['handleKeyDown', 'handleClick']);
return _this;
}
_createClass(SelectableItem, [{
key: "getSelected",
value: function getSelected() {
var _this$props = this.props,
_key = _this$props._key,
root = _this$props.root,
selected = _this$props.selected;
var selectMode = root.props.selectMode;
var selectedKeys = root.state.selectedKeys;
return selected || !!selectMode && selectedKeys.indexOf(_key) > -1;
}
}, {
key: "handleSelect",
value: function handleSelect(e) {
var _this$props2 = this.props,
_key = _this$props2._key,
root = _this$props2.root,
onSelect = _this$props2.onSelect;
if (onSelect) {
onSelect(!this.getSelected(), this, e);
} else {
root.handleSelect(_key, !this.getSelected(), this);
}
}
}, {
key: "handleKeyDown",
value: function handleKeyDown(e) {
if (e.keyCode === KEYCODE.SPACE && !this.props.disabled) {
this.handleSelect(e);
}
this.props.onKeyDown && this.props.onKeyDown(e);
}
}, {
key: "handleClick",
value: function handleClick(e) {
this.handleSelect(e);
this.props.onClick && this.props.onClick(e);
}
}, {
key: "renderSelectedIcon",
value: function renderSelectedIcon(selected) {
var _cx;
var _this$props3 = this.props,
root = _this$props3.root,
inlineIndent = _this$props3.inlineIndent,
needIndent = _this$props3.needIndent,
hasSelectedIcon = _this$props3.hasSelectedIcon,
isSelectIconRight = _this$props3.isSelectIconRight,
type = _this$props3.type;
var _root$props = root.props,
prefix = _root$props.prefix,
rootSelectedIcon = _root$props.hasSelectedIcon,
rootSelectIconRight = _root$props.isSelectIconRight;
var cls = cx((_cx = {}, _defineProperty(_cx, "".concat(prefix, "menu-icon-selected"), true), _defineProperty(_cx, "".concat(prefix, "menu-icon-right"), ('isSelectIconRight' in this.props ? isSelectIconRight : rootSelectIconRight) && type !== 'submenu'), _cx));
return ('hasSelectedIcon' in this.props ? hasSelectedIcon : rootSelectedIcon) && selected ? /*#__PURE__*/React.createElement(Icon, {
style: needIndent && inlineIndent > 0 ? {
left: "".concat(inlineIndent, "px")
} : null,
className: cls,
type: "tick"
}) : null;
}
}, {
key: "render",
value: function render() {
var _cx2;
var _this$props4 = this.props,
_key = _this$props4._key,
root = _this$props4.root,
className = _this$props4.className,
disabled = _this$props4.disabled,
helper = _this$props4.helper,
children = _this$props4.children,
needIndent = _this$props4.needIndent,
checked = _this$props4.checked;
var prefix = root.props.prefix;
var others = pickOthers(Object.keys(SelectableItem.propTypes), this.props);
var selected = this.getSelected();
var newProps = _objectSpread({
_key: _key,
root: root,
disabled: disabled,
type: 'item',
className: cx((_cx2 = {}, _defineProperty(_cx2, "".concat(prefix, "selected"), selected), _defineProperty(_cx2, className, !!className), _defineProperty(_cx2, "".concat(prefix, "select-menu-item"), 'selectMode' in root.props), _defineProperty(_cx2, "".concat(prefix, "menu-item-checked"), checked), _cx2)),
onKeyDown: this.handleKeyDown,
onClick: !disabled ? this.handleClick : this.props.onClick,
needIndent: needIndent
}, others);
var textProps = {};
if ('selectMode' in root.props) {
textProps['aria-selected'] = selected;
}
return /*#__PURE__*/React.createElement(Item, newProps, this.renderSelectedIcon(selected), /*#__PURE__*/React.createElement("span", _extends({
className: "".concat(prefix, "menu-item-text")
}, textProps), children), helper ? /*#__PURE__*/React.createElement("div", {
className: "".concat(prefix, "menu-item-helper")
}, helper) : null, checked ? /*#__PURE__*/React.createElement(Icon, {
type: "tick",
className: "".concat(prefix, "menu-item-tick")
}) : null);
}
}]);
return SelectableItem;
}(Component);
_defineProperty(SelectableItem, "menuChildType", 'item');
_defineProperty(SelectableItem, "propTypes", {
_key: PropTypes.string,
root: PropTypes.object,
selected: PropTypes.bool,
onSelect: PropTypes.func,
inlineIndent: PropTypes.number,
/**
* 是否禁用
*/
disabled: PropTypes.bool,
/**
* 帮助文本
*/
helper: PropTypes.node,
/**
* 菜单项标签内容
*/
children: PropTypes.node,
className: PropTypes.string,
onKeyDown: PropTypes.func,
onClick: PropTypes.func,
needIndent: PropTypes.bool,
hasSelectedIcon: PropTypes.bool,
isSelectIconRight: PropTypes.bool,
/**
* 是否选中
*/
checked: PropTypes.bool
});
_defineProperty(SelectableItem, "defaultProps", {
disabled: false,
needIndent: true
});
export { SelectableItem as default };