forked from ant-design/ant-design-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.web.tsx
141 lines (130 loc) · 3.36 KB
/
index.web.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import * as React from 'react';
import classNames from 'classnames';
import Button from '../button';
import Flex from '../flex';
import PaginationProps from './PaginationPropTypes';
function noop() {
}
export default class Pagination extends React.Component<PaginationProps, any> {
static defaultProps = {
prefixCls: 'am-pagination',
mode: 'button',
current: 0,
simple: false,
prevText: 'Prev',
nextText: 'Next',
onChange: noop,
};
constructor(props) {
super(props);
this.state = {
current: props.current,
};
this.onPrev = this.onPrev.bind(this);
this.onNext = this.onNext.bind(this);
}
componentWillReceiveProps(nextProps) {
this.setState({
current: nextProps.current,
});
}
_hasPrev() {
return this.state.current > 0;
}
_hasNext() {
return this.state.current < this.props.total;
}
_handleChange(p) {
this.setState({
current: p,
});
this.props.onChange(p);
return p;
}
onPrev() {
this._handleChange(this.state.current - 1);
}
onNext() {
this._handleChange(this.state.current + 1);
}
getIndexes(count) {
const arr = [];
for (let i = 0; i < count; i++) {
arr.push(i);
}
return arr;
}
render() {
const { prefixCls, className, mode, total, simple,
prevText, nextText } = this.props;
const current = this.state.current;
const numWrapCls = classNames({
className,
[`${prefixCls}-wrap`]: true,
});
let markup;
switch (mode) {
case 'button':
markup = (
<Flex>
<Flex.Item className={`${prefixCls}-wrap-btn`}>
<Button
inline
disabled={current <= 0}
onClick={this.onPrev}
>
{prevText}
</Button>
</Flex.Item>
{this.props.children ? (<Flex.Item>{this.props.children}</Flex.Item>) : (!simple &&
<Flex.Item className={numWrapCls}>
<span className="active">{current + 1}</span>/<span>{total}</span>
</Flex.Item>)
}
<Flex.Item className={`${prefixCls}-wrap-btn`}>
<Button
disabled={current >= total - 1}
inline
onClick={this.onNext}>{nextText}
</Button>
</Flex.Item>
</Flex>
);
break;
case 'number':
markup = (
<div className={numWrapCls}>
<span className="active">{current + 1}</span>/<span>{total}</span>
</div>
);
break;
case 'pointer':
const indexes = this.getIndexes(total);
markup = (
<div className={numWrapCls}>
{ indexes.map(function(index) {
const dotCls = classNames({
[`${prefixCls}-wrap-dot`]: true,
[`${prefixCls}-wrap-dot-active`]: index === current,
});
return (
<div className={dotCls} key={`dot-${index}`}>
<span></span>
</div>
);
})
}
</div>
);
break;
default:
markup = false;
break;
}
return (
<div className={prefixCls}>
{markup}
</div>
);
}
}