index.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import React, { PureComponent } from 'react'
  21. import classnames from 'classnames'
  22. import { ButtonSize } from 'antd/lib/button'
  23. import { Button, Select, Input, Icon } from 'antd'
  24. const Option = Select.Option
  25. const styles = require('./PaginationWithoutTotal.less')
  26. interface IPaginationWithoutTotalProps {
  27. dataLength: number
  28. loading?: boolean
  29. size?: ButtonSize
  30. defaultPageSize?: number
  31. showSizeChanger?: boolean
  32. showQuickJumper?: boolean | {
  33. goButton?: React.ReactNode
  34. }
  35. pageSizeOptions?: string[]
  36. className?: string
  37. onChange?: (current: number, pageSize: number) => void
  38. }
  39. interface IPaginationWithoutTotalStates {
  40. current: number
  41. pageSize: number
  42. jumpTo: string
  43. }
  44. export class PaginationWithoutTotal extends PureComponent<IPaginationWithoutTotalProps, IPaginationWithoutTotalStates> {
  45. constructor (props) {
  46. super(props)
  47. this.state = {
  48. current: 1,
  49. pageSize: 0,
  50. jumpTo: ''
  51. }
  52. }
  53. private static defaultProps = {
  54. loading: false,
  55. size: 'default',
  56. defaultPageSize: 10,
  57. showSizeChanger: true,
  58. showQuickJumper: true,
  59. pageSizeOptions: ['10', '20', '30', '40']
  60. }
  61. public componentWillMount () {
  62. this.setState({
  63. pageSize: this.props.defaultPageSize
  64. })
  65. }
  66. public componentWillReceiveProps (nextProps) {
  67. if (nextProps.defaultPageSize !== this.props.defaultPageSize) {
  68. this.setState({
  69. pageSize: nextProps.defaultPageSize
  70. })
  71. }
  72. }
  73. private changeJumpToValue = (e) => {
  74. this.setState({
  75. jumpTo: e.target.value
  76. })
  77. }
  78. private change = (action: string) => (val) => {
  79. let { current, pageSize, jumpTo } = this.state
  80. switch (action) {
  81. case 'prev':
  82. current -= 1
  83. break
  84. case 'next':
  85. current += 1
  86. break
  87. case 'jump':
  88. if (!Number(jumpTo) || Number(jumpTo) === current) {
  89. this.setState({
  90. jumpTo: ''
  91. })
  92. return
  93. } else {
  94. current = Number(jumpTo)
  95. jumpTo = ''
  96. break
  97. }
  98. default:
  99. pageSize = Number(val)
  100. current = 1
  101. break
  102. }
  103. this.props.onChange(current, pageSize)
  104. this.setState({
  105. current,
  106. pageSize,
  107. jumpTo
  108. })
  109. }
  110. public render () {
  111. const { loading, size, dataLength, showSizeChanger, showQuickJumper, pageSizeOptions, className } = this.props
  112. const { current, pageSize, jumpTo } = this.state
  113. const paginationClass = classnames({
  114. [styles.pagination]: true,
  115. [className]: true
  116. })
  117. return (
  118. <div className={paginationClass}>
  119. <Button
  120. loading={loading}
  121. size={size}
  122. disabled={current === 1}
  123. onClick={this.change('prev')}
  124. >
  125. <Icon type="left" />上一页
  126. </Button>
  127. <Button
  128. loading={loading}
  129. size={size}
  130. disabled={dataLength < pageSize}
  131. onClick={this.change('next')}
  132. >
  133. 下一页<Icon type="right" />
  134. </Button>
  135. {showSizeChanger && (
  136. <Select
  137. value={`${pageSize}`}
  138. size={size}
  139. className={styles.sizer}
  140. onChange={this.change('')}
  141. >
  142. {
  143. pageSizeOptions.map((p) => (
  144. <Option key={p} value={p}>{`${p} / Page`}</Option>
  145. ))
  146. }
  147. </Select>
  148. )}
  149. {showQuickJumper && (
  150. <div className={styles.quickJumper}>
  151. 跳至
  152. <Input
  153. value={jumpTo}
  154. size={size}
  155. onChange={this.changeJumpToValue}
  156. onPressEnter={this.change('jump')}
  157. />
  158. </div>
  159. )}
  160. </div>
  161. )
  162. }
  163. }
  164. export default PaginationWithoutTotal