@txdfe/at
Version:
一个设计体系组件库
175 lines (174 loc) • 9.58 kB
JavaScript
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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) {
function SelectableItem(props) {
var _this;
_classCallCheck(this, SelectableItem);
_this = _callSuper(this, SelectableItem, [props]);
bindCtx(_this, ['handleKeyDown', 'handleClick']);
return _this;
}
_inherits(SelectableItem, _Component);
return _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 _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(_defineProperty(_defineProperty({}, "".concat(prefix, "menu-icon-selected"), true), "".concat(prefix, "menu-icon-right"), ('isSelectIconRight' in this.props ? isSelectIconRight : rootSelectIconRight) && type !== 'submenu'));
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 _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(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, "".concat(prefix, "selected"), selected), className, !!className), "".concat(prefix, "select-menu-item"), 'selectMode' in root.props), "".concat(prefix, "menu-item-checked"), checked)),
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);
}
}]);
}(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 };