TableCard.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import React, { useState } from 'react';
  2. import classNames from 'classnames';
  3. import { BadgeStatus } from '@/components';
  4. import type { StatusColorType } from '@/components/BadgeStatus';
  5. import { StatusColorEnum } from '@/components/BadgeStatus';
  6. import './index.less';
  7. export interface TableCardProps {
  8. className?: string;
  9. showStatus?: boolean;
  10. showTool?: boolean;
  11. showMask?: boolean;
  12. detail?: React.ReactNode;
  13. status?: string | number;
  14. statusText?: React.ReactNode;
  15. statusNames?: Record<string | number, StatusColorType>;
  16. children?: React.ReactNode;
  17. actions?: React.ReactNode[];
  18. contentClassName?: string;
  19. onClick?: React.MouseEventHandler<HTMLDivElement>;
  20. }
  21. function getAction(actions: React.ReactNode[]) {
  22. return actions
  23. .filter((item) => item)
  24. .map((item: any) => {
  25. return (
  26. <div
  27. className={classNames('card-button', {
  28. delete: item.key === 'delete',
  29. disabled: item.disabled,
  30. })}
  31. key={item.key}
  32. >
  33. {item}
  34. </div>
  35. );
  36. });
  37. }
  38. export default (props: TableCardProps) => {
  39. const [maskShow, setMaskShow] = useState(false);
  40. const handleStatusColor = (data: TableCardProps): StatusColorType | undefined => {
  41. if ('statusNames' in data && data.status !== undefined) {
  42. return data.statusNames![data.status];
  43. }
  44. return StatusColorEnum.default;
  45. };
  46. const statusNode =
  47. props.showStatus === false ? null : (
  48. <div className={classNames('card-state', handleStatusColor(props))}>
  49. <div className={'card-state-content'}>
  50. <BadgeStatus
  51. status={props.status !== undefined ? props.status : ''}
  52. text={props.statusText}
  53. statusNames={props.statusNames}
  54. />
  55. </div>
  56. </div>
  57. );
  58. const maskClassName = classNames('card-mask', { show: maskShow });
  59. const maskNode =
  60. props.showMask === false ? null : <div className={maskClassName}>{props.detail}</div>;
  61. const toolNode =
  62. props.showTool === false ? null : (
  63. <div className={'card-tools'}>
  64. {props.actions && props.actions.length ? getAction(props.actions) : null}
  65. </div>
  66. );
  67. return (
  68. <div
  69. className={classNames('iot-card', { hover: maskShow }, props.className)}
  70. onClick={(e) => {
  71. e.stopPropagation();
  72. props.onClick?.(e);
  73. }}
  74. >
  75. <div className={'card-warp'}>
  76. <div
  77. className={classNames('card-content', props.contentClassName)}
  78. onMouseEnter={() => {
  79. setMaskShow(true);
  80. }}
  81. onMouseLeave={() => {
  82. setMaskShow(false);
  83. }}
  84. >
  85. {props.children}
  86. {statusNode}
  87. {maskNode}
  88. </div>
  89. </div>
  90. {toolNode}
  91. </div>
  92. );
  93. };