import React, { useState, useRef, useEffect } from 'react'; import { StyleSheet, ScrollView, TouchableOpacity } from 'react-native'; import { Input, BottomSheet, ListItem } from '@rneui/themed'; import { Task, RobotAction } from '../types/task'; import { LOCATIONS, ROBOT_ACTIONS, PAYLOADS } from '../data/mockData'; interface TaskFormProps { task: Task; onTaskChange: (updatedTask: Task) => void; onLocationBayChange?: (task: Task) => void; // 库位变化时的回调 } const TaskForm: React.FC = ({ task, onTaskChange, onLocationBayChange, }) => { const [isVisible, setIsVisible] = useState(false); const [currentField, setCurrentField] = useState(''); const [currentItems, setCurrentItems] = useState< { label: string; value: string }[] >([]); // 创建库位输入框的ref const locationBayInputRef = useRef(null); // 用于防抖的ref const debounceTimerRef = useRef(null); // 保存上一次的库位值,用于检测变化 const previousLocationBayRef = useRef( task.parameters.locationBay || '', ); // 组件挂载时自动聚焦到库位输入框 useEffect(() => { const timer = setTimeout(() => { if (locationBayInputRef.current) { locationBayInputRef.current.focus(); } }, 100); return () => clearTimeout(timer); }, []); // 监听库位值变化,自动触发运行任务 useEffect(() => { const currentLocationBay = task.parameters.locationBay || ''; const previousLocationBay = previousLocationBayRef.current; // 只有当库位值真正发生变化且不为空时才触发 if ( currentLocationBay !== previousLocationBay && currentLocationBay.trim() !== '' ) { // 清除之前的防抖定时器 if (debounceTimerRef.current) { clearTimeout(debounceTimerRef.current); } // 设置新的防抖定时器,500ms后触发 debounceTimerRef.current = setTimeout(() => { if (onLocationBayChange) { onLocationBayChange(task); } }, 500); } // 更新上一次的库位值 previousLocationBayRef.current = currentLocationBay; // 清理函数 return () => { if (debounceTimerRef.current) { clearTimeout(debounceTimerRef.current); } }; }, [task.parameters.locationBay, task, onLocationBayChange]); const handleParamChange = (field: string, value: string | RobotAction) => { const updatedTask = { ...task, parameters: { ...task.parameters, [field]: value, }, }; onTaskChange(updatedTask); }; const openBottomSheet = ( field: string, items: { label: string; value: string }[], ) => { setCurrentField(field); setCurrentItems(items); setIsVisible(true); }; const renderDropdown = ( name: string, label: string, value: string, items: { label: string; value: string }[], ) => ( openBottomSheet(name, items)}> ); return ( onTaskChange({ ...task, name: text })} /> {renderDropdown( 'startLocation', '起点', task.parameters.startLocation, LOCATIONS, )} {renderDropdown( 'endLocation', '终点', task.parameters.endLocation, LOCATIONS, )} handleParamChange('waypoint', text)} /> {renderDropdown( 'robotAction', '机器人动作', task.parameters.robotAction, ROBOT_ACTIONS, )} {renderDropdown('payload', '载荷', task.parameters.payload, PAYLOADS)} {/* 库位字段改为普通输入框 */} handleParamChange('locationBay', text)} showSoftInputOnFocus={false} autoFocus={false} placeholder="请输入库位" /> setIsVisible(false)} > {currentItems.map((item, index) => ( { handleParamChange(currentField, item.value); setIsVisible(false); }} > {item.label} ))} ); }; const styles = StyleSheet.create({ container: { padding: 16, }, }); export default TaskForm;