forked from ant-design/ant-design-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
132 lines (122 loc) · 3.16 KB
/
index.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
/* tslint:disable:no-switch-case-fall-through */
import * as React from 'react';
import { View, Text } from 'react-native';
import Flex from '../flex';
import Button from '../button';
import PaginationProps from './PaginationPropTypes';
import styles from './style/index';
export default class Pagination extends React.Component<PaginationProps, any> {
static defaultProps = {
mode: 'button',
current: 0,
simple: false,
prevText: 'Prev',
nextText: 'Next',
onChange: () => {},
};
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 { mode, style, simple, total,
prevText, nextText } = this.props;
const current = this.state.current;
let markup;
switch (mode) {
case 'button':
markup = (
<Flex>
<Flex.Item>
<Button
inline
disabled={current <= 0}
onClick={this.onPrev}
>
{prevText}
</Button>
</Flex.Item>
{!simple ?
<Flex.Item>
<View style={[styles.numberStyle]}>
<Text style={[styles.activeTextStyle]}>{current + 1}</Text>
<Text style={[styles.totalStyle]}>/{total}</Text>
</View>
</Flex.Item> : <Flex.Item />
}
<Flex.Item>
<Button
disabled={current >= total - 1}
inline
onClick={this.onNext}
>
{nextText}
</Button>
</Flex.Item>
</Flex>
);
break;
case 'number':
markup = (
<View style={[styles.numberStyle]}>
<Text style={[styles.activeTextStyle]}>{current + 1}</Text>
<Text style={[styles.totalStyle]}>/{total}</Text>
</View>
);
break;
case 'pointer':
const indexes = this.getIndexes(total);
const pointer = indexes.map((index) => {
const activeStyle = index === current ? styles.pointActiveStyle : null;
return (
<View style={[styles.pointStyle, styles.spaceStyle, activeStyle]} key={`point-${index}`}></View>
);
});
markup = (<View style={[styles.indicatorStyle]}>{pointer}</View>);
break;
default:
markup = false;
break;
}
return (
<View style={[style]}>
{markup}
</View>
);
}
}