Main.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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, useMemo } from 'react'
  21. import { Tag } from 'antd'
  22. import Toolbar from './Toolbar'
  23. import { IProjectItem, ItemToolbarProps } from './types'
  24. const styles = require('./item.less')
  25. const ProjectItem: React.FC<IProjectItem> & {
  26. Toolbar: React.FC<ItemToolbarProps>
  27. } = ({ tags, backgroundImg, title, description, onClick, style, children }) => {
  28. const handleClick: React.MouseEventHandler<HTMLDivElement> = useCallback(
  29. (e: React.MouseEvent<HTMLElement>) => {
  30. if (onClick) {
  31. onClick(e)
  32. }
  33. },
  34. [onClick]
  35. )
  36. const getBackgroundImg: React.CSSProperties = useMemo(() => {
  37. return {
  38. backgroundImage: backgroundImg
  39. }
  40. }, [backgroundImg])
  41. const getTags: React.ReactNode[] = useMemo(() => {
  42. return tags && tags.length > 0
  43. ? tags.map((tag) => {
  44. return (
  45. <Tag color={tag.color} key={`${tag.text}tagkey`}>
  46. {tag.text}
  47. </Tag>
  48. )
  49. })
  50. : []
  51. }, [tags])
  52. const body: React.ReactNode = useMemo(() => {
  53. return children ? <div className={styles.itemToolbar}>{children}</div> : []
  54. }, [children])
  55. return (
  56. <div className={styles.unit} style={style} onClick={handleClick}>
  57. <div className={styles.thumbnail} style={getBackgroundImg}>
  58. <header>
  59. <div className={styles.tags}>{getTags}</div>
  60. <h3 className={styles.title}>{title ? title : ''}</h3>
  61. <p className={styles.descs}>{description ? description : ''}</p>
  62. </header>
  63. </div>
  64. {body}
  65. </div>
  66. )
  67. }
  68. ProjectItem.Toolbar = Toolbar
  69. export default ProjectItem