import React, { useState } from "react"; import { SearchOutlined } from "@ant-design/icons"; import { Input, Button } from "antd"; import { DeleteOutlined } from "@ant-design/icons"; import { DeviceSelectionProps } from "./data"; import SelectionPopup from "./selectionPopup"; import style from "./index.module.scss"; const DeviceSelection: React.FC = ({ selectedOptions, onSelect, placeholder = "选择设备", className = "", mode = "input", open, onOpenChange, selectedListMaxHeight = 300, // 默认300 showInput = true, showSelectedList = true, readonly = false, }) => { // 弹窗控制 const [popupVisible, setPopupVisible] = useState(false); const isDialog = mode === "dialog"; const realVisible = isDialog ? !!open : popupVisible; const setRealVisible = (v: boolean) => { if (isDialog && onOpenChange) onOpenChange(v); if (!isDialog) setPopupVisible(v); }; // 打开弹窗 const openPopup = () => { if (readonly) return; setRealVisible(true); }; // 获取显示文本 const getDisplayText = () => { if (selectedOptions.length === 0) return ""; return `已选择 ${selectedOptions.length} 个设备`; }; // 删除已选设备 const handleRemoveDevice = (id: number) => { if (readonly) return; onSelect(selectedOptions.filter(v => v.id !== id)); }; return ( <> {/* mode=input 显示输入框,mode=dialog不显示 */} {mode === "input" && showInput && (
} allowClear={!readonly} size="large" readOnly={readonly} disabled={readonly} style={ readonly ? { background: "#f5f5f5", cursor: "not-allowed" } : {} } />
)} {/* 已选设备列表窗口 */} {mode === "input" && showSelectedList && selectedOptions.length > 0 && (
{selectedOptions.map(device => (
【 {device.name}】 - {device.wechatId}
{!readonly && (
))}
)} {/* 弹窗 */} setRealVisible(false)} selectedOptions={selectedOptions} onSelect={onSelect} /> ); }; export default DeviceSelection;