VizSelectTree.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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, { useCallback } from 'react'
  21. import { Card, Tree } from 'antd'
  22. import { TreeProps, AntTreeNodeCheckedEvent, AntTreeNode } from 'antd/lib/tree'
  23. const { TreeNode } = Tree
  24. import { IPortal, IDisplayFormed } from 'containers/Viz/types'
  25. import { NodeKeyPrefix } from './constants'
  26. import { splitNodeKeyWithPrefix } from './util'
  27. interface IVizSelectTreeProps {
  28. title: string
  29. vizs: IPortal[] | IDisplayFormed[]
  30. vizIcon: JSX.Element
  31. vizKeyPrefix?: NodeKeyPrefix
  32. checkedKeys: string[]
  33. onLoadDetail: (
  34. id: number,
  35. prefix: NodeKeyPrefix
  36. ) => ReturnType<TreeProps['loadData']>
  37. onRenderVizItems: (id: number, prefix: NodeKeyPrefix) => JSX.Element[]
  38. onCheck: (
  39. id: number,
  40. prefix: NodeKeyPrefix,
  41. checked: boolean,
  42. dataRef: any
  43. ) => void
  44. }
  45. const VizSelectTree: React.FC<IVizSelectTreeProps> = (props) => {
  46. const {
  47. title,
  48. vizs,
  49. vizIcon,
  50. vizKeyPrefix,
  51. checkedKeys,
  52. onLoadDetail,
  53. onRenderVizItems,
  54. onCheck
  55. } = props
  56. const loadDetail = useCallback(
  57. (node: AntTreeNode) => {
  58. const nodeKey = node.props.eventKey
  59. const [nodePrefix, nodeId] = splitNodeKeyWithPrefix(nodeKey)
  60. return onLoadDetail(nodeId, nodePrefix)
  61. },
  62. [onLoadDetail]
  63. )
  64. const treeCheck = useCallback(
  65. (_, e: AntTreeNodeCheckedEvent) => {
  66. const { checked, node } = e
  67. const { eventKey, dataRef } = node.props
  68. const [nodePrefix, nodeId] = splitNodeKeyWithPrefix(eventKey)
  69. onCheck(nodeId, nodePrefix, checked, dataRef)
  70. },
  71. [onCheck]
  72. )
  73. return (
  74. <Card size="small" title={title}>
  75. <Tree
  76. checkable
  77. blockNode
  78. showIcon
  79. checkedKeys={checkedKeys}
  80. loadData={loadDetail}
  81. onCheck={treeCheck}
  82. >
  83. {Array.isArray(vizs) &&
  84. (vizs as Array<IPortal | IDisplayFormed>).map(({ id, name }) => (
  85. <TreeNode
  86. title={name}
  87. icon={vizIcon}
  88. key={`${vizKeyPrefix || ''}${id}`}
  89. isLeaf={false}
  90. >
  91. {onRenderVizItems(id, vizKeyPrefix)}
  92. </TreeNode>
  93. ))}
  94. </Tree>
  95. </Card>
  96. )
  97. }
  98. export default VizSelectTree