@txdfe/at
Version:
一个设计体系组件库
295 lines (240 loc) • 13.9 kB
JavaScript
"use strict";
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); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactDom = require("react-dom");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _classnames = _interopRequireDefault(require("classnames"));
var _util = require("../../util");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
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; }
var bindCtx = _util.func.bindCtx;
var pickOthers = _util.obj.pickOthers;
var Item = /*#__PURE__*/function (_Component) {
_inherits(Item, _Component);
var _super = _createSuper(Item);
function Item(props) {
var _this;
_classCallCheck(this, Item);
_this = _super.call(this, props);
bindCtx(_assertThisInitialized(_this), ['handleClick', 'handleKeyDown']);
return _this;
}
_createClass(Item, [{
key: "componentDidMount",
value: function componentDidMount() {
this.itemNode = (0, _reactDom.findDOMNode)(this);
var _this$props = this.props,
parentMode = _this$props.parentMode,
root = _this$props.root,
menu = _this$props.menu;
if (menu) {
this.menuNode = (0, _reactDom.findDOMNode)(menu);
} else if (parentMode === 'popup') {
this.menuNode = this.itemNode.parentNode;
} else {
this.menuNode = (0, _reactDom.findDOMNode)(root);
var _root$props = root.props,
prefix = _root$props.prefix,
header = _root$props.header,
footer = _root$props.footer;
if (header || footer) {
this.menuNode = this.menuNode.querySelector(".".concat(prefix, "menu-content"));
}
}
this.setFocus();
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate() {
this.setFocus();
}
}, {
key: "focusable",
value: function focusable() {
var _this$props2 = this.props,
root = _this$props2.root,
type = _this$props2.type,
disabled = _this$props2.disabled;
var focusable = root.props.focusable;
return focusable && (type === 'submenu' || !disabled);
}
}, {
key: "getFocused",
value: function getFocused() {
var _this$props3 = this.props,
_key = _this$props3._key,
root = _this$props3.root;
var focusedKey = root.state.focusedKey;
return focusedKey === _key;
}
}, {
key: "setFocus",
value: function setFocus() {
var focused = this.getFocused();
if (focused) {
if (this.focusable()) {
this.itemNode.focus({
preventScroll: true
});
}
if (this.menuNode && this.menuNode.scrollHeight > this.menuNode.clientHeight) {
var scrollBottom = this.menuNode.clientHeight + this.menuNode.scrollTop;
var itemBottom = this.itemNode.offsetTop + this.itemNode.offsetHeight;
if (itemBottom > scrollBottom) {
this.menuNode.scrollTop = itemBottom - this.menuNode.clientHeight;
} else if (this.itemNode.offsetTop < this.menuNode.scrollTop) {
this.menuNode.scrollTop = this.itemNode.offsetTop;
}
}
}
}
}, {
key: "handleClick",
value: function handleClick(e) {
e.stopPropagation();
var _this$props4 = this.props,
_key = _this$props4._key,
root = _this$props4.root,
disabled = _this$props4.disabled;
if (!disabled) {
root.handleItemClick(_key, this, e);
this.props.onClick && this.props.onClick(e);
} else {
e.preventDefault();
}
}
}, {
key: "handleKeyDown",
value: function handleKeyDown(e) {
var _this$props5 = this.props,
_key = _this$props5._key,
root = _this$props5.root,
type = _this$props5.type;
if (this.focusable()) {
root.handleItemKeyDown(_key, type, this, e);
switch (e.keyCode) {
case _util.KEYCODE.ENTER:
{
if (!(type === 'submenu')) {
this.handleClick(e);
}
break;
}
}
}
this.props.onKeyDown && this.props.onKeyDown(e);
}
}, {
key: "getTitle",
value: function getTitle(children) {
var labelString = '';
var loop = function loop(children) {
_react.Children.forEach(children, function (child) {
if ( /*#__PURE__*/(0, _react.isValidElement)(child) && child.props.children) {
loop(child.props.children);
} else if (typeof child === 'string') {
labelString += child;
}
});
};
loop(children);
return labelString;
}
}, {
key: "render",
value: function render() {
var _cx;
var _this$props6 = this.props,
level = _this$props6.level,
root = _this$props6.root,
replaceClassName = _this$props6.replaceClassName,
groupIndent = _this$props6.groupIndent,
component = _this$props6.component,
disabled = _this$props6.disabled,
className = _this$props6.className,
children = _this$props6.children,
needIndent = _this$props6.needIndent,
parentMode = _this$props6.parentMode,
_key = _this$props6._key,
dangerous = _this$props6.dangerous;
var others = pickOthers(Object.keys(Item.propTypes), this.props);
var _root$props2 = root.props,
prefix = _root$props2.prefix,
focusable = _root$props2.focusable,
inlineIndent = _root$props2.inlineIndent,
itemClassName = _root$props2.itemClassName,
rtl = _root$props2.rtl;
var focused = this.getFocused();
var newClassName = replaceClassName ? className : (0, _classnames["default"])((_cx = {}, _defineProperty(_cx, "".concat(prefix, "menu-item"), true), _defineProperty(_cx, "".concat(prefix, "disabled"), disabled), _defineProperty(_cx, "".concat(prefix, "focused"), !focusable && focused), _defineProperty(_cx, "".concat(prefix, "menu-dangerous-item"), !!dangerous), _defineProperty(_cx, itemClassName, !!itemClassName), _defineProperty(_cx, className, !!className), _cx));
if (disabled) {
others['aria-disabled'] = true;
others['aria-hidden'] = true;
}
others.tabIndex = root.tabbableKey === _key ? '0' : '-1';
if (parentMode === 'inline' && level > 1 && inlineIndent > 0 && needIndent) {
var paddingProp = rtl ? 'paddingRight' : 'paddingLeft';
others.style = _objectSpread(_objectSpread({}, others.style || {}), {}, _defineProperty({}, paddingProp, "".concat(level * inlineIndent - (groupIndent || 0) * 0.4 * inlineIndent, "px")));
}
var TagName = component;
var role = 'menuitem';
if ('selectMode' in root.props) {
role = 'listitem';
}
return /*#__PURE__*/_react["default"].createElement(TagName, _extends({
role: role
}, others, {
className: newClassName,
onClick: this.handleClick,
onKeyDown: this.handleKeyDown
}), /*#__PURE__*/_react["default"].createElement("div", {
className: "".concat(prefix, "menu-item-inner")
}, children));
}
}]);
return Item;
}(_react.Component);
exports["default"] = Item;
_defineProperty(Item, "propTypes", {
_key: _propTypes["default"].string,
level: _propTypes["default"].number,
groupIndent: _propTypes["default"].number,
root: _propTypes["default"].object,
menu: _propTypes["default"].any,
parent: _propTypes["default"].object,
parentMode: _propTypes["default"].oneOf(['inline', 'popup']),
type: _propTypes["default"].oneOf(['submenu', 'item']),
component: _propTypes["default"].string,
disabled: _propTypes["default"].bool,
className: _propTypes["default"].string,
onClick: _propTypes["default"].func,
onKeyDown: _propTypes["default"].func,
needIndent: _propTypes["default"].bool,
replaceClassName: _propTypes["default"].bool,
dangerous: _propTypes["default"].bool
});
_defineProperty(Item, "defaultProps", {
component: 'li',
groupIndent: 0,
replaceClassName: false,
needIndent: true,
dangerous: false
});