UNPKG

@txdfe/at

Version:

一个设计体系组件库

279 lines (271 loc) 13.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = _interopRequireWildcard(require("react")); var _reactDom = _interopRequireDefault(require("react-dom")); var _propTypes = _interopRequireDefault(require("prop-types")); var _classnames = _interopRequireDefault(require("classnames")); var _util = require("../../util"); var _excluded = ["className", "current", "labelPlacement", "shape", "readOnly", "animation", "itemRender", "rtl"]; 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 _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; } function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; } 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 getHeight = function getHeight(el) { return _util.dom.getStyle(el, 'height'); }; var setHeight = function setHeight(el, height) { return _util.dom.setStyle(el, 'height', height); }; /** Step */ var Step = exports["default"] = /*#__PURE__*/function (_Component) { function Step(props, context) { var _this; _classCallCheck(this, Step); _this = _callSuper(this, Step, [props, context]); _defineProperty(_this, "resize", function () { if (_this.step) { _this.setState({ parentWidth: _this.step.offsetWidth || 0, parentHeight: _this.step.offsetHeight || 0 }); } }); _defineProperty(_this, "_stepRefHandler", function (ref) { _this.step = ref; }); _this.state = { parentWidth: 'auto', parentHeight: 'auto', currentfocus: 0 }; return _this; } _inherits(Step, _Component); return _createClass(Step, [{ key: "componentDidMount", value: function componentDidMount() { /* istanbul ignore if */ if (!_util.support.flex) { this.resize(); _util.events.on(window, 'resize', this.resize); } this.adjustHeight(); } }, { key: "UNSAFE_componentWillReceiveProps", value: function UNSAFE_componentWillReceiveProps(newProps) { if ('current' in newProps) { this.setState({ current: newProps.current }); } } }, { key: "componentDidUpdate", value: function componentDidUpdate() { this.adjustHeight(); } }, { key: "componentWillUnmount", value: function componentWillUnmount() { /* istanbul ignore if */ if (!_util.support.flex) { _util.events.off(window, 'resize', this.resize); } } }, { key: "adjustHeight", value: function adjustHeight() { var _this$props = this.props, shape = _this$props.shape, direction = _this$props.direction, prefix = _this$props.prefix, labelPlacement = _this$props.labelPlacement; if (shape !== 'arrow' && (direction === 'horizontal' || direction === 'hoz') && (labelPlacement === 'vertical' || labelPlacement === 'ver')) { var step = _reactDom["default"].findDOMNode(this.step); // just resize when init if (step.style.height) return; var height = Array.prototype.slice.call(step.getElementsByClassName("".concat(prefix, "step-item"))).reduce(function (ret, re) { var itemHeight = getHeight(re) + getHeight(re.getElementsByClassName("".concat(prefix, "step-item-body"))[0]); return Math.max(itemHeight, ret); }, 0); setHeight(step, height); } } }, { key: "_getValidChildren", value: // set dir key for aria handle // handleKeyDown = e => { // const { shape, children } = this.props; // const { length: max } = children; // let { currentfocus } = this.state; // const initPosition = currentfocus; // switch (e.keyCode) { // case KEYCODE.RIGHT: // case KEYCODE.DOWN: // currentfocus++; // break; // case KEYCODE.LEFT: // case KEYCODE.UP: // currentfocus--; // break; // default: // break; // } // currentfocus = // currentfocus >= max ? 0 : currentfocus < 0 ? max - 1 : currentfocus; // this.setState({ currentfocus }, () => { // const child = this.step.children[currentfocus]; // if (!child) return; // const focusItem = // shape === 'arrow' // ? child // : child.querySelector('.next-step-item-body'); // focusItem && focusItem.focus(); // }); // if (initPosition !== currentfocus) { // e.preventDefault(); // } // }; function _getValidChildren(children) { var result = []; _react["default"].Children.forEach(children, function (child) { if (/*#__PURE__*/_react["default"].isValidElement(child)) { result.push(child); } }); return result; } }, { key: "render", value: function render() { // eslint-disable-next-line var _this$props2 = this.props, className = _this$props2.className, current = _this$props2.current, labelPlacement = _this$props2.labelPlacement, shape = _this$props2.shape, readOnly = _this$props2.readOnly, animation = _this$props2.animation, itemRender = _this$props2.itemRender, rtl = _this$props2.rtl, others = _objectWithoutProperties(_this$props2, _excluded); var _this$props3 = this.props, prefix = _this$props3.prefix, direction = _this$props3.direction, children = _this$props3.children; prefix = this.context.prefix || prefix; var _this$state = this.state, parentWidth = _this$state.parentWidth, parentHeight = _this$state.parentHeight; // type不同对应的direction不同 direction = shape === 'arrow' ? 'hoz' : direction; // children去除null children = this._getValidChildren(children); // 修改子节点属性 var cloneChildren = _react.Children.map(children, function (child, index) { var status = index < current ? 'finish' : index === current ? 'process' : 'wait'; return /*#__PURE__*/_react["default"].cloneElement(child, { prefix: prefix, key: index, index: index, total: children.length, status: child.props.status || status, shape: shape, direction: direction, labelPlacement: labelPlacement, parentWidth: parentWidth, parentHeight: parentHeight, readOnly: readOnly, animation: animation, tabIndex: 0, // tabIndex: this.state.currentfocus === index ? '0' : '-1', 'aria-current': status === 'process' ? 'step' : null, itemRender: child.props.itemRender ? child.props.itemRender : itemRender // 优先使用Item的itemRender }); }); var _direction = direction === 'ver' || direction === 'vertical' ? 'vertical' : 'horizontal'; var _labelPlacement = labelPlacement === 'ver' || labelPlacement === 'vertical' ? 'vertical' : 'horizontal'; var stepCls = (0, _classnames["default"])(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, "".concat(prefix, "step"), true), "".concat(prefix, "step-").concat(shape), shape), "".concat(prefix, "step-").concat(_direction), _direction), "".concat(prefix, "step-label-").concat(_labelPlacement), _labelPlacement), className, className)); if (rtl) { others.dir = 'rtl'; } // others.onKeyDown = makeChain(this.handleKeyDown, others.onKeyDown); return /*#__PURE__*/_react["default"].createElement("ol", _extends({}, others, { className: stepCls, ref: this._stepRefHandler }), cloneChildren); } }]); }(_react.Component); _defineProperty(Step, "propTypes", { prefix: _propTypes["default"].string, rtl: _propTypes["default"].bool, /** * 当前步骤 */ current: _propTypes["default"].number, /** * 展示方向 */ direction: _propTypes["default"].oneOf(['hoz', 'ver']), /** * 横向布局时的内容排列 */ labelPlacement: _propTypes["default"].oneOf(['hoz', 'ver']), /** * 类型 */ shape: _propTypes["default"].oneOf(['circle', 'arrow', 'dot']), /** * 是否只读模式 */ readOnly: _propTypes["default"].bool, /** * 是否开启动效 */ animation: _propTypes["default"].bool, /** * 自定义样式名 */ className: _propTypes["default"].string, /** * StepItem 的自定义渲染 * @param {Number} index 节点索引 * @param {String} status 节点状态 * @returns {Node} 节点的渲染结果 */ itemRender: _propTypes["default"].func }); _defineProperty(Step, "defaultProps", { prefix: 'next-', current: 0, direction: 'hoz', labelPlacement: 'ver', shape: 'circle', animation: true, itemRender: null }); _defineProperty(Step, "contextTypes", { prefix: _propTypes["default"].string });