/
index.jsx
168 lines (155 loc) · 3.59 KB
/
index.jsx
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import React from 'react';
import styled, { css } from 'styled-components';
import PropTypes from 'prop-types';
import ArrowRightIcon from '@material-ui/icons/KeyboardArrowRight';
import ArrowLeftIcon from '@material-ui/icons/KeyboardArrowLeft';
import { usePagination } from 'hooks/usePagination';
import { useColor } from 'hooks/useColor';
const buttonStyle = css`
width: 32px;
height: 32px;
border-radius: 100%;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
transition: background 0.2s ease-in-out;
&:hover {
color: #222;
background: #0000000a;
transition: background 0.2s ease-in-out;
}
`;
const disabledButtonStyle = css`
color: #00000042;
background: none;
cursor: default;
&:hover {
color: #00000042;
background: none;
}
`;
const StyledPagination = styled.div`
display: flex;
align-items: center;
& > *:not(:first-child) {
margin-left: 12px;
}
`;
const ArrowButton = styled.div`
${buttonStyle}
${(props) => (props.$isDisabled ? disabledButtonStyle : null)}
`;
const currentItemStyle = css`
background: ${(props) => props.$color};
color: #FFF;
&:hover {
background: ${(props) => props.$color};
color: #FFF;
transition: background 0.2s ease-in-out;
}
`;
const StyledItem = styled.div`
${buttonStyle}
${(props) => (props.$isCurrent ? currentItemStyle : null)}
`;
/**
* `Pagination` 是一個分頁元件,當頁面中一次要載入過多的資料時,載入及渲染將會花費更多的時間,
* 因此,考慮分批載入資料的時候,需要分頁元件來幫助我們在不同頁面之間切換。
*/
const Pagination = ({
themeColor,
withEllipsis,
page,
pageSize,
total,
onChange,
}) => {
const { makeColor } = useColor();
const color = makeColor({ themeColor });
const {
items,
totalPage,
handleClickNext,
handleClickPrev,
} = usePagination({
page,
pageSize,
total,
withEllipsis,
onChange,
});
return (
<StyledPagination>
<ArrowButton
role="presentation"
onClick={page === 1 ? null : handleClickPrev}
$isDisabled={page === 1}
>
<ArrowLeftIcon />
</ArrowButton>
{
items.map((item) => {
if (item.type === 'page') {
return (
<StyledItem
key={item.page}
$isCurrent={item.isCurrent}
$color={color}
onClick={item.onClick}
>
<span>{item.page}</span>
</StyledItem>
);
}
return (
<div key={item.page}>
...
</div>
);
})
}
<ArrowButton
role="presentation"
onClick={page === totalPage ? null : handleClickNext}
$isDisabled={page === totalPage}
>
<ArrowRightIcon />
</ArrowButton>
</StyledPagination>
);
};
Pagination.propTypes = {
/**
* 主題配色,primary、secondary 或是自己傳入色票
*/
themeColor: PropTypes.oneOfType([PropTypes.oneOf(['primary', 'secondary']), PropTypes.string]),
/**
* 當前頁數
*/
page: PropTypes.number,
/**
* 每一頁資料筆數
*/
pageSize: PropTypes.number,
/**
* 數據總數
*/
total: PropTypes.number.isRequired,
/**
* 頁數過多是否省略
*/
withEllipsis: PropTypes.bool,
/**
* 頁碼以及 pageSize 改變時的 callback
*/
onChange: PropTypes.func,
};
Pagination.defaultProps = {
themeColor: 'primary',
page: 1,
pageSize: 20,
withEllipsis: false,
onChange: () => {},
};
export default Pagination;