DashboardItem.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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, useCallback } from 'react'
  21. import ControlComponent from '../../Control'
  22. import { Row, Col, Button } from 'antd'
  23. import { IRenderTreeItem, IMapControlOptions, IControl } from '../../types'
  24. import {
  25. DEFAULT_DASHBOARD_ITEM_CONTROL_GRID_WIDTH,
  26. ControlQueryMode
  27. } from '../../constants'
  28. import { getControlVisibility } from '../../util'
  29. import styles from './Layouts.less'
  30. interface IDashboardItemControlPanelLayoutProps {
  31. queryMode: ControlQueryMode
  32. renderTree: IRenderTreeItem[]
  33. formValues: object
  34. mapOptions: IMapControlOptions
  35. onChange: (control: IControl, val: any) => void
  36. onSearch: (changedValues?: object) => void
  37. onReset: () => void
  38. }
  39. const DashboardItemControlPanelLayout: FC<IDashboardItemControlPanelLayoutProps> = ({
  40. queryMode,
  41. renderTree,
  42. formValues,
  43. mapOptions,
  44. onChange,
  45. onSearch,
  46. onReset
  47. }) => {
  48. const renderControlComponents = useCallback(
  49. (controlRenderTreeItems: IRenderTreeItem[], parents?: IControl[]) => {
  50. let components = []
  51. controlRenderTreeItems.forEach((control) => {
  52. const { key, width, children, ...rest } = control
  53. if (getControlVisibility(renderTree, control, formValues)) {
  54. const controlValue = formValues && formValues[`${control.key}`]
  55. const controlGridProps = width
  56. ? {
  57. lg: width,
  58. md: width < 8 ? 12 : 24
  59. }
  60. : DEFAULT_DASHBOARD_ITEM_CONTROL_GRID_WIDTH
  61. components = components.concat(
  62. <Col key={key} {...controlGridProps}>
  63. <ControlComponent
  64. queryMode={queryMode}
  65. control={control}
  66. value={controlValue}
  67. size="small"
  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 manualSearch = useCallback(() => {
  92. onSearch()
  93. }, [onSearch])
  94. return (
  95. <div className={styles.dashboardItemControlPanel}>
  96. <div className={styles.controls}>
  97. <Row gutter={8}>{renderControlComponents(renderTree)}</Row>
  98. </div>
  99. {queryMode === ControlQueryMode.Manually && (
  100. <div className={styles.actions}>
  101. <Button
  102. type="primary"
  103. icon="search"
  104. size="small"
  105. onClick={manualSearch}
  106. >
  107. 查询
  108. </Button>
  109. <Button icon="reload" size="small" onClick={onReset}>
  110. 重置
  111. </Button>
  112. </div>
  113. )}
  114. </div>
  115. )
  116. }
  117. export default memo(DashboardItemControlPanelLayout)