UNPKG

10.5 kBJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4
5var _extends2 = require('babel-runtime/helpers/extends');
6
7var _extends3 = _interopRequireDefault(_extends2);
8
9var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
10
11var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
12
13var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
14
15var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
16
17var _inherits2 = require('babel-runtime/helpers/inherits');
18
19var _inherits3 = _interopRequireDefault(_inherits2);
20
21var _class, _temp;
22
23var _reactDom = require('react-dom');
24
25var _reactDom2 = _interopRequireDefault(_reactDom);
26
27var _propTypes = require('prop-types');
28
29var _propTypes2 = _interopRequireDefault(_propTypes);
30
31var _classnames = require('classnames');
32
33var _classnames2 = _interopRequireDefault(_classnames);
34
35var _react = require('react');
36
37var _react2 = _interopRequireDefault(_react);
38
39var _reactLifecyclesCompat = require('react-lifecycles-compat');
40
41var _configProvider = require('../../config-provider');
42
43var _configProvider2 = _interopRequireDefault(_configProvider);
44
45var _util = require('../../util');
46
47function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
48
49var getHeight = function getHeight(el) {
50 return _util.dom.getStyle(el, 'height');
51};
52var setHeight = function setHeight(el, height) {
53 return _util.dom.setStyle(el, 'height', height);
54};
55
56/** Step */
57var Step = (_temp = _class = function (_Component) {
58 (0, _inherits3.default)(Step, _Component);
59
60 Step.getDerivedStateFromProps = function getDerivedStateFromProps(newProps) {
61 if ('current' in newProps) {
62 return {
63 current: newProps.current
64 };
65 }
66 };
67
68 function Step(props, context) {
69 (0, _classCallCheck3.default)(this, Step);
70
71 var _this = (0, _possibleConstructorReturn3.default)(this, _Component.call(this, props, context));
72
73 _this._stepRefHandler = function (ref) {
74 _this.step = ref;
75 };
76
77 _this.state = {
78 parentWidth: 'auto',
79 parentHeight: 'auto',
80 currentfocus: 0
81 };
82 _this.resize = _this.resize.bind(_this);
83 return _this;
84 }
85
86 Step.prototype.componentDidMount = function componentDidMount() {
87 /* istanbul ignore if */
88 if (!_util.support.flex) {
89 this.resize();
90 _util.events.on(window, 'resize', this.resize);
91 }
92 this.adjustHeight();
93 };
94
95 Step.prototype.componentDidUpdate = function componentDidUpdate() {
96 this.adjustHeight();
97 };
98
99 Step.prototype.componentWillUnmount = function componentWillUnmount() {
100 /* istanbul ignore if */
101 if (!_util.support.flex) {
102 _util.events.off(window, 'resize', this.resize);
103 }
104 };
105
106 Step.prototype.adjustHeight = function adjustHeight() {
107 var _props = this.props,
108 shape = _props.shape,
109 direction = _props.direction,
110 prefix = _props.prefix,
111 labelPlacement = _props.labelPlacement;
112
113 var step = _reactDom2.default.findDOMNode(this.step);
114 if (shape !== 'arrow' && (direction === 'horizontal' || direction === 'hoz') && (labelPlacement === 'vertical' || labelPlacement === 'ver')) {
115 var height = Array.prototype.slice.call(step.getElementsByClassName(prefix + 'step-item')).reduce(function (ret, re) {
116 var itemHeight = getHeight(re.getElementsByClassName(prefix + 'step-item-container')[0]) + getHeight(re.getElementsByClassName(prefix + 'step-item-body')[0]);
117 return Math.max(itemHeight, ret);
118 }, 0);
119 setHeight(step, height);
120 } else {
121 setHeight(step, '');
122 }
123 };
124
125 Step.prototype.resize = function resize() {
126 if (this.step) {
127 this.setState({
128 parentWidth: this.step.offsetWidth || 0,
129 parentHeight: this.step.offsetHeight || 0
130 });
131 }
132 };
133
134 // set dir key for aria handle
135 // handleKeyDown = e => {
136 // const { shape, children } = this.props;
137 // const { length: max } = children;
138 // let { currentfocus } = this.state;
139 // const initPosition = currentfocus;
140 // switch (e.keyCode) {
141 // case KEYCODE.RIGHT:
142 // case KEYCODE.DOWN:
143 // currentfocus++;
144 // break;
145 // case KEYCODE.LEFT:
146 // case KEYCODE.UP:
147 // currentfocus--;
148 // break;
149 // default:
150 // break;
151 // }
152 // currentfocus =
153 // currentfocus >= max ? 0 : currentfocus < 0 ? max - 1 : currentfocus;
154 // this.setState({ currentfocus }, () => {
155 // const child = this.step.children[currentfocus];
156 // if (!child) return;
157 // const focusItem =
158 // shape === 'arrow'
159 // ? child
160 // : child.querySelector('.next-step-item-body');
161 // focusItem && focusItem.focus();
162 // });
163 // if (initPosition !== currentfocus) {
164 // e.preventDefault();
165 // }
166 // };
167
168 Step.prototype._getValidChildren = function _getValidChildren(children) {
169 var result = [];
170 _react2.default.Children.forEach(children, function (child) {
171 if (_react2.default.isValidElement(child)) {
172 result.push(child);
173 }
174 });
175 return result;
176 };
177
178 Step.prototype.render = function render() {
179 var _this2 = this,
180 _classNames;
181
182 // eslint-disable-next-line
183 var _props2 = this.props,
184 className = _props2.className,
185 current = _props2.current,
186 labelPlacement = _props2.labelPlacement,
187 shape = _props2.shape,
188 readOnly = _props2.readOnly,
189 animation = _props2.animation,
190 itemRender = _props2.itemRender,
191 rtl = _props2.rtl,
192 stretch = _props2.stretch;
193
194 var others = _util.obj.pickOthers(Step.propTypes, this.props);
195 var _props3 = this.props,
196 prefix = _props3.prefix,
197 direction = _props3.direction,
198 children = _props3.children;
199
200 prefix = this.context.prefix || prefix;
201 var _state = this.state,
202 parentWidth = _state.parentWidth,
203 parentHeight = _state.parentHeight;
204
205 // type不同对应的direction不同
206
207 direction = shape === 'arrow' ? 'hoz' : direction;
208
209 // children去除null
210 children = this._getValidChildren(children);
211
212 // 修改子节点属性
213 var cloneChildren = _react.Children.map(children, function (child, index) {
214 var status = index < current ? 'finish' : index === current ? 'process' : 'wait';
215
216 return _react2.default.cloneElement(child, {
217 prefix: prefix,
218 key: index,
219 index: index,
220 total: children.length,
221 status: child.props.status || status,
222 shape: shape,
223 direction: direction,
224 labelPlacement: labelPlacement,
225 parentWidth: parentWidth,
226 parentHeight: parentHeight,
227 readOnly: readOnly,
228 animation: animation,
229 tabIndex: 0,
230 // tabIndex: this.state.currentfocus === index ? '0' : '-1',
231 'aria-current': status === 'process' ? 'step' : null,
232 itemRender: child.props.itemRender ? child.props.itemRender : itemRender, // 优先使用Item的itemRender
233 onResize: function onResize() {
234 _this2.step && _this2.adjustHeight();
235 },
236 stretch: stretch
237 });
238 });
239
240 var _direction = direction === 'ver' || direction === 'vertical' ? 'vertical' : 'horizontal';
241 var _labelPlacement = labelPlacement === 'ver' || labelPlacement === 'vertical' ? 'vertical' : 'horizontal';
242 var stepCls = (0, _classnames2.default)((_classNames = {}, _classNames[prefix + 'step'] = true, _classNames[prefix + 'step-' + shape] = shape, _classNames[prefix + 'step-' + _direction] = _direction, _classNames[prefix + 'step-label-' + _labelPlacement] = _labelPlacement, _classNames[className] = className, _classNames));
243
244 if (rtl) {
245 others.dir = 'rtl';
246 }
247
248 // others.onKeyDown = makeChain(this.handleKeyDown, others.onKeyDown);
249 return _react2.default.createElement(
250 'ol',
251 (0, _extends3.default)({}, others, { className: stepCls, ref: this._stepRefHandler }),
252 cloneChildren
253 );
254 };
255
256 return Step;
257}(_react.Component), _class.propTypes = (0, _extends3.default)({}, _configProvider2.default.propTypes, {
258 prefix: _propTypes2.default.string,
259 rtl: _propTypes2.default.bool,
260 /**
261 * 当前步骤
262 */
263 current: _propTypes2.default.number,
264 /**
265 * 展示方向
266 */
267 direction: _propTypes2.default.oneOf(['hoz', 'ver']),
268 /**
269 * 横向布局时( direction 为 hoz )的内容排列
270 */
271 labelPlacement: _propTypes2.default.oneOf(['hoz', 'ver']),
272 /**
273 * 类型
274 */
275 shape: _propTypes2.default.oneOf(['circle', 'arrow', 'dot']),
276 /**
277 * 是否只读模式
278 */
279 readOnly: _propTypes2.default.bool,
280 /**
281 * 是否开启动效
282 */
283 animation: _propTypes2.default.bool,
284 /**
285 * 自定义样式名
286 */
287 className: _propTypes2.default.string,
288 /**
289 * StepItem 的自定义渲染
290 * @param {Number} index 节点索引
291 * @param {String} status 节点状态
292 * @returns {Node} 节点的渲染结果
293 */
294 itemRender: _propTypes2.default.func,
295 /**
296 * 宽度横向拉伸
297 */
298 stretch: _propTypes2.default.bool
299}), _class.defaultProps = {
300 prefix: 'next-',
301 current: 0,
302 direction: 'hoz',
303 labelPlacement: 'ver',
304 shape: 'circle',
305 animation: true,
306 itemRender: null,
307 stretch: false
308}, _class.contextTypes = {
309 prefix: _propTypes2.default.string
310}, _temp);
311Step.displayName = 'Step';
312exports.default = (0, _reactLifecyclesCompat.polyfill)(Step);
313module.exports = exports['default'];
\No newline at end of file