import React, { useState, useEffect, Component } from 'react'; // setter使用@alifd/next,和编辑器保持一致 import { Button, Dialog, Table, Pagination, Input, Form, Message } from '@alifd/next'; import './style.less'; interface LinkageDataItem { label: string; value: string; index?: number; } interface LinkageSetterProps { defaultValue: string; } const LinkageSetter = (props: any) => { console.log('🚀LinkageSetter props >>> :', props); // 使用对象管理多个规则的状态 const [visible, setVisible] = useState(false); const [defaultDataSource, setDefaultDataSource] = useState([]); const [loading, setLoading] = useState(false); const [currentData, setCurrentData] = useState([]); const [currentPage, setCurrentPage] = useState(1); const [total, setTotal] = useState(0); //测试地址1 /public/mock/rs-select.json //测试地址2 http://192.168.2.240:4523/m1/4813751-4468297-default/rs-select const [apiUrl, setApiUrl] = useState("http://192.168.2.240:4523/m1/4813751-4468297-default/rs-select"); const pageSize = 10; // 每页显示10条数据 // 从组件获取其他属性的值 const { value } = props; const { rs_id = '' } = value; const fetchData = async (url: string) => { setLoading(true); try { const response = await fetch(url); const responseData = await response.json(); const { total, data } = responseData; setDefaultDataSource( data.map((item: LinkageDataItem, index: number) => { item.index = index + 1; return item; }), ); // 初始化显示第一页数据 setCurrentData(data.slice(0, pageSize)); setCurrentPage(1); setTotal(total); // 更新父组件的数据 if (props.onChange) { Message.show({ type: 'success', content: '接口测试成功!', duration: 2000, }); // props.onChange({apiUrl}); props.onChange({ options: data, apiUrl}); // 更新父组件的数据 } } catch (error) { console.error('获取联动数据失败:', error); } finally { setLoading(false); } }; const onOpen = () => { setVisible(true); }; const handlePageChange = (page: number) => { setLoading(true); setCurrentPage(page); setTimeout(() => { // 计算当前页的数据范围 const startIndex = (page - 1) * pageSize; const endIndex = startIndex + pageSize; const paginatedData = defaultDataSource.slice(startIndex, endIndex); setCurrentData(paginatedData); setLoading(false); }, 200); }; const handleSearch = () => { if (apiUrl) { fetchData(apiUrl); } else { Message.warning('请输入接口URL'); setCurrentData([]); setTotal(0); setApiUrl(''); // 更新父组件的数据 if (props.onChange) { props.onChange({ options: [], apiUrl}); } } }; return (