@txdfe/at
Version:
一个设计体系组件库
336 lines (267 loc) • 14.8 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); }
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(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 _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; }
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 = /*#__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 (!_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() {
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 = _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"])((_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["default"].createElement("ol", _extends({}, others, {
className: stepCls,
ref: this._stepRefHandler
}), cloneChildren);
}
}]);
return Step;
}(_react.Component);
exports["default"] = Step;
_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
});