Dashboard.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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, { FC, memo, useMemo, useCallback } from 'react'
  21. import classnames from 'classnames'
  22. import ControlComponent from '../../Control'
  23. import { Row, Col, Button } from 'antd'
  24. import { IRenderTreeItem, IMapControlOptions, IControl } from '../../types'
  25. import {
  26. DEFAULT_DASHBOARD_CONTROL_GRID_WIDTH,
  27. ControlQueryMode
  28. } from '../../constants'
  29. import { getControlVisibility } from '../../util'
  30. import styles from './Layouts.less'
  31. interface IDashboardControlPanelLayoutProps {
  32. queryMode: ControlQueryMode
  33. renderTree: IRenderTreeItem[]
  34. formValues: object
  35. mapOptions: IMapControlOptions
  36. onChange: (control: IControl, val: any) => void
  37. onSearch: (changedValues?: object) => void
  38. onReset: () => void
  39. }
  40. const DashboardControlPanelLayout: FC<IDashboardControlPanelLayoutProps> = ({
  41. queryMode,
  42. renderTree,
  43. formValues,
  44. mapOptions,
  45. onChange,
  46. onSearch,
  47. onReset
  48. }) => {
  49. const renderControlComponents = useCallback(
  50. (controlRenderTreeItems: IRenderTreeItem[], parents?: IControl[]) => {
  51. let components = []
  52. controlRenderTreeItems.forEach((control) => {
  53. const { key, width, children, ...rest } = control
  54. if (getControlVisibility(renderTree, control, formValues)) {
  55. const controlValue = formValues && formValues[`${control.key}`]
  56. const controlGridProps = width
  57. ? {
  58. lg: width,
  59. md: width < 8 ? 12 : 24
  60. }
  61. : DEFAULT_DASHBOARD_CONTROL_GRID_WIDTH
  62. components = components.concat(
  63. <Col key={key} {...controlGridProps}>
  64. <ControlComponent
  65. queryMode={queryMode}
  66. control={control}
  67. value={controlValue}
  68. currentOptions={mapOptions[key]}
  69. onChange={onChange}
  70. onSearch={onSearch}
  71. />
  72. </Col>
  73. )
  74. if (children) {
  75. const controlWithOutChildren = { key, width, ...rest }
  76. components = components.concat(
  77. renderControlComponents(
  78. children,
  79. parents
  80. ? parents.concat(controlWithOutChildren)
  81. : [controlWithOutChildren]
  82. )
  83. )
  84. }
  85. }
  86. })
  87. return components
  88. },
  89. [renderTree, formValues, mapOptions]
  90. )
  91. const panelClassNames = useMemo(
  92. () =>
  93. classnames({
  94. [styles.dashboardControlPanel]: true,
  95. [styles.empty]: !renderTree.filter((r) =>
  96. getControlVisibility(renderTree, r, formValues)
  97. ).length
  98. }),
  99. [renderTree]
  100. )
  101. const manualSearch = useCallback(() => {
  102. onSearch()
  103. }, [onSearch])
  104. return (
  105. <div className={panelClassNames}>
  106. <div className={styles.controls}>
  107. <Row gutter={8}>{renderControlComponents(renderTree)}</Row>
  108. </div>
  109. {queryMode === ControlQueryMode.Manually && (
  110. <div className={styles.actions}>
  111. <Button type="primary" icon="search" onClick={manualSearch}>
  112. 查询
  113. </Button>
  114. <Button icon="reload" onClick={onReset}>
  115. 重置
  116. </Button>
  117. </div>
  118. )}
  119. </div>
  120. )
  121. }
  122. export default memo(DashboardControlPanelLayout)