@txdfe/at
Version:
一个设计体系组件库
315 lines (259 loc) • 13.1 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); }
var _excluded = ["className", "current", "labelPlacement", "shape", "readOnly", "animation", "itemRender", "rtl"];
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 _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = 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, Children } from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { support, events, dom } from '../../util';
var getHeight = function getHeight(el) {
return dom.getStyle(el, 'height');
};
var setHeight = function setHeight(el, height) {
return dom.setStyle(el, 'height', height);
};
/** Step */
var Step = /*#__PURE__*/function (_Component) {
_inherits(Step, _Component);
var _super = _createSuper(Step);
function Step(props, context) {
var _this;
_classCallCheck(this, Step);
_this = _super.call(this, props, context);
_defineProperty(_assertThisInitialized(_this), "resize", function () {
if (_this.step) {
_this.setState({
parentWidth: _this.step.offsetWidth || 0,
parentHeight: _this.step.offsetHeight || 0
});
}
});
_defineProperty(_assertThisInitialized(_this), "_stepRefHandler", function (ref) {
_this.step = ref;
});
_this.state = {
parentWidth: 'auto',
parentHeight: 'auto',
currentfocus: 0
};
return _this;
}
_createClass(Step, [{
key: "componentDidMount",
value: function componentDidMount() {
/* istanbul ignore if */
if (!support.flex) {
this.resize();
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 (!support.flex) {
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.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.Children.forEach(children, function (child) {
if ( /*#__PURE__*/React.isValidElement(child)) {
result.push(child);
}
});
return result;
}
}, {
key: "render",
value: function render() {
var _classNames;
// 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 = Children.map(children, function (child, index) {
var status = index < current ? 'finish' : index === current ? 'process' : 'wait';
return /*#__PURE__*/React.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 = classNames((_classNames = {}, _defineProperty(_classNames, "".concat(prefix, "step"), true), _defineProperty(_classNames, "".concat(prefix, "step-").concat(shape), shape), _defineProperty(_classNames, "".concat(prefix, "step-").concat(_direction), _direction), _defineProperty(_classNames, "".concat(prefix, "step-label-").concat(_labelPlacement), _labelPlacement), _defineProperty(_classNames, className, className), _classNames));
if (rtl) {
others.dir = 'rtl';
} // others.onKeyDown = makeChain(this.handleKeyDown, others.onKeyDown);
return /*#__PURE__*/React.createElement("ol", _extends({}, others, {
className: stepCls,
ref: this._stepRefHandler
}), cloneChildren);
}
}]);
return Step;
}(Component);
_defineProperty(Step, "propTypes", {
prefix: PropTypes.string,
rtl: PropTypes.bool,
/**
* 当前步骤
*/
current: PropTypes.number,
/**
* 展示方向
*/
direction: PropTypes.oneOf(['hoz', 'ver']),
/**
* 横向布局时的内容排列
*/
labelPlacement: PropTypes.oneOf(['hoz', 'ver']),
/**
* 类型
*/
shape: PropTypes.oneOf(['circle', 'arrow', 'dot']),
/**
* 是否只读模式
*/
readOnly: PropTypes.bool,
/**
* 是否开启动效
*/
animation: PropTypes.bool,
/**
* 自定义样式名
*/
className: PropTypes.string,
/**
* StepItem 的自定义渲染
* @param {Number} index 节点索引
* @param {String} status 节点状态
* @returns {Node} 节点的渲染结果
*/
itemRender: PropTypes.func
});
_defineProperty(Step, "defaultProps", {
prefix: 'next-',
current: 0,
direction: 'hoz',
labelPlacement: 'ver',
shape: 'circle',
animation: true,
itemRender: null
});
_defineProperty(Step, "contextTypes", {
prefix: PropTypes.string
});
export { Step as default };