tablePagination.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 { useState, useCallback, useEffect } from 'react'
  21. import { PaginationConfig } from 'antd/lib/table'
  22. const defaultSimpleWidth = 768
  23. const basePagination: PaginationConfig = {
  24. defaultPageSize: 20,
  25. showSizeChanger: true
  26. }
  27. function useTablePagination(
  28. simpleWidth: number,
  29. baseConfig?: PaginationConfig
  30. ) {
  31. const [screenWidth, setScreenWidth] = useState(
  32. document.documentElement.clientWidth
  33. )
  34. const updateScreenWidth = useCallback(() => {
  35. setScreenWidth(document.documentElement.clientWidth)
  36. }, [])
  37. useEffect(() => {
  38. window.addEventListener('resize', updateScreenWidth, false)
  39. return function cleanup() {
  40. window.removeEventListener('resize', updateScreenWidth, false)
  41. }
  42. }, [])
  43. const simple =
  44. screenWidth <= (simpleWidth <= 0 ? defaultSimpleWidth : simpleWidth)
  45. const pagination: PaginationConfig = {
  46. ...basePagination,
  47. ...baseConfig,
  48. simple
  49. }
  50. return pagination
  51. }
  52. export default useTablePagination