UNPKG

@txdfe/at

Version:

一个设计体系组件库

240 lines (239 loc) 12.9 kB
"use strict"; 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(e) { return e && e.__esModule ? e : { "default": e }; } function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; } 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 _readOnlyError(r) { throw new TypeError('"' + r + '" is read-only'); } 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); } var bindCtx = _util.func.bindCtx; var pickOthers = _util.obj.pickOthers; var Item = exports["default"] = /*#__PURE__*/function (_Component) { function Item(props) { var _this; _classCallCheck(this, Item); _this = _callSuper(this, Item, [props]); bindCtx(_this, ['handleClick', 'handleKeyDown']); return _this; } _inherits(Item, _Component); return _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 _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"])(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, "".concat(prefix, "menu-item"), true), "".concat(prefix, "disabled"), disabled), "".concat(prefix, "focused"), !focusable && focused), "".concat(prefix, "menu-dangerous-item"), !!dangerous), itemClassName, !!itemClassName), className, !!className)); 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)); } }]); }(_react.Component); _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 });