UNPKG

5.55 kBJavaScriptView Raw
1import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
2import _extends from 'babel-runtime/helpers/extends';
3import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
4import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
5import _inherits from 'babel-runtime/helpers/inherits';
6
7var _class, _temp;
8
9import React, { Component, Children } from 'react';
10import PropTypes from 'prop-types';
11import classNames from 'classnames';
12import { polyfill } from 'react-lifecycles-compat';
13
14import { obj } from '../../util';
15import ConfigProvider from '../../config-provider';
16import nextLocale from '../../locale/zh-cn';
17
18/** Timeline */
19var Timeline = (_temp = _class = function (_Component) {
20 _inherits(Timeline, _Component);
21
22 function Timeline(props, context) {
23 _classCallCheck(this, Timeline);
24
25 var _this = _possibleConstructorReturn(this, _Component.call(this, props, context));
26
27 _this.state = {
28 fold: props.fold
29 };
30 return _this;
31 }
32
33 Timeline.getDerivedStateFromProps = function getDerivedStateFromProps(nextProps, prevState) {
34 var innerUpdate = prevState.innerUpdate,
35 fold = prevState.fold;
36
37
38 if (innerUpdate) {
39 return {
40 fold: fold,
41 innerUpdate: false
42 };
43 }
44
45 if ('fold' in nextProps) {
46 return {
47 fold: nextProps.fold
48 };
49 }
50
51 return null;
52 };
53
54 Timeline.prototype.toggleFold = function toggleFold(folderIndex, total) {
55 var fold = this.state.fold.map(function (item) {
56 return _extends({}, item);
57 });
58
59 if (folderIndex) {
60 for (var i = 0; i < fold.length; i++) {
61 var _fold$i = fold[i],
62 foldArea = _fold$i.foldArea,
63 foldShow = _fold$i.foldShow;
64
65
66 if (foldArea[1] && folderIndex === foldArea[1] || !foldArea[1] && folderIndex === total - 1) {
67 fold[i].foldShow = !foldShow;
68 }
69 }
70
71 this.setState({ fold: fold, innerUpdate: true });
72 }
73 };
74
75 Timeline.prototype.render = function render() {
76 var _this2 = this,
77 _classNames;
78
79 var _props = this.props,
80 prefix = _props.prefix,
81 rtl = _props.rtl,
82 className = _props.className,
83 children = _props.children,
84 locale = _props.locale,
85 animation = _props.animation,
86 mode = _props.mode,
87 others = _objectWithoutProperties(_props, ['prefix', 'rtl', 'className', 'children', 'locale', 'animation', 'mode']);
88
89 var fold = this.state.fold;
90
91 // 修改子节点属性
92
93 var childrenCount = React.Children.count(children);
94 var isAlternateMode = mode === 'alternate';
95 var getPositionCls = function getPositionCls(idx) {
96 if (isAlternateMode) {
97 return idx % 2 === 0 ? prefix + 'timeline-item-left' : prefix + 'timeline-item-right';
98 }
99 return prefix + 'timeline-item-left';
100 };
101
102 var cloneChildren = Children.map(children, function (child, i) {
103 var folderIndex = null;
104 var foldNodeShow = false;
105
106 fold.forEach(function (item) {
107 var foldArea = item.foldArea,
108 foldShow = item.foldShow;
109
110
111 if (foldArea[0] && i >= foldArea[0] && (i <= foldArea[1] || !foldArea[1])) {
112 folderIndex = foldArea[1] || childrenCount - 1;
113 foldNodeShow = foldShow;
114 }
115 });
116
117 return React.cloneElement(child, {
118 prefix: prefix,
119 locale: locale,
120 total: childrenCount,
121 className: getPositionCls(i),
122 index: i,
123 folderIndex: folderIndex,
124 foldShow: foldNodeShow,
125 toggleFold: folderIndex === i ? _this2.toggleFold.bind(_this2, folderIndex, childrenCount) : function () {},
126 animation: animation
127 });
128 });
129
130 var timelineCls = classNames((_classNames = {}, _classNames[prefix + 'timeline'] = true, _classNames[prefix + 'alternate'] = isAlternateMode, _classNames), className);
131
132 if (rtl) {
133 others.dir = 'rtl';
134 }
135
136 return React.createElement(
137 'ul',
138 _extends({}, obj.pickOthers(Timeline.propTypes, others), { className: timelineCls }),
139 cloneChildren
140 );
141 };
142
143 return Timeline;
144}(Component), _class.propTypes = _extends({}, ConfigProvider.propTypes, {
145 /**
146 * 样式的品牌前缀
147 */
148 prefix: PropTypes.string,
149 rtl: PropTypes.bool,
150 /**
151 * 自定义折叠选项 示例`[{foldArea: [startIndex, endIndex], foldShow: boolean}]`
152 */
153 fold: PropTypes.array,
154 /**
155 * 自定义类名
156 */
157 className: PropTypes.string,
158 children: PropTypes.any,
159 locale: PropTypes.object,
160 animation: PropTypes.bool,
161 /**
162 * 展示的模式
163 * @enumdesc 左, 交错显示
164 * @version 1.23.18
165 */
166 mode: PropTypes.oneOf(['left', 'alternate'])
167}), _class.defaultProps = {
168 prefix: 'next-',
169 rtl: false,
170 fold: [],
171 locale: nextLocale.Timeline,
172 animation: true,
173 mode: 'left'
174}, _temp);
175Timeline.displayName = 'Timeline';
176
177
178export default ConfigProvider.config(polyfill(Timeline));
\No newline at end of file