import * as React from 'react'; import classnames from 'classnames'; import jsbarcode from 'jsbarcode'; import { expandObj } from 'src/types'; import OPButton from '../op-button'; import OPTag from '../op-tag'; import event from '../event'; import './index.scss'; type fitEnum = 'contain-in' | 'contain-out' | 'width' | 'height'; type backgroundColorEnum = 'primary' | 'pm' | 'protection'; type iconTypeMap = 'check-circle' | 'warning' | 'cancel'; interface CardProps { /** 自定义样式类 */ className?: string; /** 卡片最大高 */ maxHeight?: boolean | string; /** 卡片标题 */ title?: string | React.ReactNode; /** 卡片内容 */ content?: string | Array | React.ReactNode; /** 卡片背景颜色 */ backgroundColor?: backgroundColorEnum | string; /** 图片列表 */ imgList?: Array; /** 条码 */ barcode?: BarcodeProps; /** 操作配置 */ action?: ActionProps; /** 卡片状态 */ status?: string; /** 卡片tag */ tag?: expandObj; /** 提示-类型 */ infoType?: iconTypeMap; /** 提示-标题 */ infoTitle?: string; /** 提示-标签 */ infoTags?: Array; /** 提示-内容 */ infoMessage?: string; /** 标题自定义内联样式 */ titleStyle?: React.CSSProperties; /** 内容自定义内联样式 */ contentStyle?: React.CSSProperties; } interface ImageProps { url: string; fit?: fitEnum; } interface BarcodeProps { id: string; value: string; [propsName: string]: string | number | boolean; } interface ActionProps { name: string; hotkey?: string; onAction?: () => void; } const Card: React.FC = ({ className = '', maxHeight = '', title = '', content = '', backgroundColor = '', imgList = [], action = {}, barcode = {}, status = '', tag = {}, infoType = 'warning', infoTitle = '', infoTags = [], infoMessage = '', titleStyle = {}, contentStyle = {}, }) => { const defaultBarcodeId = `barcode-${Math.random().toString().slice(-10)}`; const { name: actionName, hotkey, onAction } = action as ActionProps; const { id: barcodeId = defaultBarcodeId, value: barcodeValue, ...barcodeRest } = barcode as BarcodeProps; // 卡片状态 enable const enable = React.useMemo(() => { return title || content || imgList?.length || barcodeValue; }, [title, content, imgList, barcodeValue]); const cardStyle = React.useMemo(() => { const newCardStyle = { backgroundColor: '', maxHeight: '', }; // 卡片背景色 if (backgroundColor) { let bgColor = ''; switch (backgroundColor) { case 'primary': // 重点信息 - 黄色 bgColor = '#FFD119'; break; case 'pm': // 包材信息 - 淡蓝色 bgColor = '#D7EAFC'; break; case 'protection': // 防护信息 - 淡绿色 bgColor = '#DAF2DE'; break; default: bgColor = backgroundColor; break; } newCardStyle.backgroundColor = bgColor; } // 卡片最大高 if (typeof maxHeight === 'string' && maxHeight) { newCardStyle.maxHeight = maxHeight; } return newCardStyle; }, [backgroundColor, maxHeight]); React.useEffect(() => { // 条形码初始化 if (barcodeValue) { document.getElementById(barcodeId) && jsbarcode(`#${barcodeId}`, barcodeValue, { ...barcodeRest, }); } }, [barcodeId, barcodeValue, barcodeRest]); React.useEffect(() => { hotkey && onAction && event.bind(hotkey, onAction); return () => { hotkey && onAction && event.unbind(hotkey, onAction); }; }, [hotkey, onAction]); return enable ? (
{title ? (
{title} {tag?.children ? {tag.children} : ''}
) : null} {content ? (
{Array.isArray(content) ? content.map((item) =>
{item}
) : content}
) : null} {imgList?.length ? (
{imgList.map((item) => { const { url = '', fit = 'contain-out' } = item; return (
); })}
) : null} {barcodeValue ? (
) : null} {infoTitle && (
{infoTitle}
{infoTags.map((infoTag) => (
{infoTag}
))}
{infoMessage}
)} {actionName ? ( {actionName} ) : null}
) : null; }; export default Card;