import { Icon } from "@iconify/react/dist/iconify.js"; import { Box, IconButton, SvgIconProps, TableRow, useTheme } from "@mui/material"; import React from "react"; import { useDrag, useDrop } from "react-dnd"; import { CustomProps, CustomPropsType } from "../../../../api/explorer"; import { NoWrapCell } from "../../../Common/StyledComponents"; import { getPropsContent } from "../../../FileManager/Sidebar/CustomProps/CustomPropsItem"; import ArrowDown from "../../../Icons/ArrowDown"; import CheckboxChecked from "../../../Icons/CheckboxChecked"; import DataBarVerticalStar from "../../../Icons/DataBarVerticalStar"; import Dismiss from "../../../Icons/Dismiss"; import Edit from "../../../Icons/Edit"; import LinkOutlined from "../../../Icons/LinkOutlined"; import Numbers from "../../../Icons/Numbers"; import PersonOutlined from "../../../Icons/PersonOutlined"; import SlideText from "../../../Icons/SlideText"; import TaskListRegular from "../../../Icons/TaskListRegular"; import TextIndentIncrease from "../../../Icons/TextIndentIncrease"; const DND_TYPE = "storage-product-row"; // 拖拽item类型 type DragItem = { index: number }; export interface DraggableCustomPropsRowProps { customProps: CustomProps; index: number; moveRow: (from: number, to: number) => void; onEdit: (customProps: CustomProps) => void; onDelete: (id: string) => void; onMoveUp: () => void; onMoveDown: () => void; isFirst: boolean; isLast: boolean; t: any; style?: React.CSSProperties; } export interface FieldTypeProps { title: string; icon: (props: SvgIconProps) => JSX.Element; minTitle?: string; minDes?: string; maxTitle?: string; maxDes?: string; maxRequired?: boolean; showOptions?: boolean; } export const FieldTypes: Record = { [CustomPropsType.text]: { title: "customProps.text", icon: SlideText, minTitle: "customProps.minLength", minDes: "customProps.emptyLimit", maxTitle: "customProps.maxLength", maxDes: "customProps.emptyLimit", }, [CustomPropsType.number]: { title: "customProps.number", icon: Numbers, minTitle: "customProps.minValue", minDes: "customProps.emptyLimit", maxTitle: "customProps.maxValue", maxDes: "customProps.emptyLimit", }, [CustomPropsType.boolean]: { title: "customProps.boolean", icon: CheckboxChecked, }, [CustomPropsType.select]: { title: "customProps.select", icon: TextIndentIncrease, showOptions: true, }, [CustomPropsType.multi_select]: { title: "customProps.multiSelect", icon: TaskListRegular, showOptions: true, }, [CustomPropsType.user]: { title: "customProps.user", icon: PersonOutlined, }, [CustomPropsType.link]: { title: "customProps.link", icon: LinkOutlined, minTitle: "customProps.minLength", minDes: "customProps.emptyLimit", maxTitle: "customProps.maxLength", maxDes: "customProps.emptyLimit", }, [CustomPropsType.rating]: { title: "customProps.rating", icon: DataBarVerticalStar, maxRequired: true, maxTitle: "customProps.maxValue", }, }; const DraggableCustomPropsRow = React.memo( React.forwardRef( ( { customProps, index, moveRow, onEdit, onDelete, onMoveUp, onMoveDown, isFirst, isLast, t, style }, ref, ): JSX.Element => { const theme = useTheme(); const [, drop] = useDrop({ accept: DND_TYPE, hover(item, monitor) { if (!(ref && typeof ref !== "function" && ref.current)) return; const dragIndex = item.index; const hoverIndex = index; if (dragIndex === hoverIndex) return; const hoverBoundingRect = ref.current.getBoundingClientRect(); const hoverMiddleY = (hoverBoundingRect.bottom - hoverBoundingRect.top) / 2; const clientOffset = monitor.getClientOffset(); if (!clientOffset) return; const hoverClientY = clientOffset.y - hoverBoundingRect.top; if (dragIndex < hoverIndex && hoverClientY < hoverMiddleY) return; if (dragIndex > hoverIndex && hoverClientY > hoverMiddleY) return; moveRow(dragIndex, hoverIndex); item.index = hoverIndex; }, }); const [{ isDragging }, drag] = useDrag({ type: DND_TYPE, item: { index }, collect: (monitor) => ({ isDragging: monitor.isDragging(), }), }); // 兼容ref为function和对象 const setRowRef = (node: HTMLTableRowElement | null) => { if (typeof ref === "function") { ref(node); } else if (ref) { (ref as React.MutableRefObject).current = node; } drag(drop(node)); }; const fieldType = FieldTypes[customProps.type]; const TypeIcon = fieldType.icon; return ( {customProps.icon && ( )} {t(customProps.name, { ns: "application" })} {t(fieldType.title)} {getPropsContent( { props: customProps, id: customProps.id, value: customProps.default ?? "", }, () => {}, false, true, )} onEdit(customProps)}> onDelete(customProps.id)}> ); }, ), ); export default DraggableCustomPropsRow;