import React, { createElement, useState, useEffect, Component, Children } from 'react'; import { Divider, Select, Button, Form, Table, Box, CascaderSelect } from '@alifd/next'; import { project } from '@alilc/lowcode-engine'; import Item from './components/Item'; import './index.scss'; import { Base64 } from 'js-base64'; import { listOtherReport, getCurrentColumnMetadata, getDateSetTree, previewData, treeCateAndTable } from '../../../src/api/form'; import HcDialog from '../../components/HcDialog'; const Option = Select.Option; const FormItem = Form.Item; const formItemLayout = { labelCol: { fixedSpan: 4, }, labelTextAlign:'left', }; interface OptionSetterProps { onChange: (any) => void; value: any; type: 'radio' | 'checkbox'; } const list = [ { label: '自定义', value: '自定义', key: 0 }, { label: '关联表单', value: '关联表单', key: 1 }, { label: '数据模型', value: '数据模型', key: 2 }, // { label: '关联人员', value: '关联人员', key: 3 }, { label: '数据集', value: '数据集', key: 4 }, ]; const defaultOptions = [ { label: '选项一', value: '选项一', link: [] }, { label: '选项二', value: '选项二', link: [] }, { label: '选项三', value: '选项三', link: [] }, ]; const queryString = window.location.search; const urlParams = new URLSearchParams(queryString); const OptionSetter: React.FC = (props) => { const { onChange, value } = props; const [type, setType] = useState(value?.type || '自定义'); const [options, setOptions] = useState(value?.options || defaultOptions); const [reportId, setReportId] = useState(value?.reportId || null); const [columnName, setColumnName] = useState(value?.columnName || null); // 其他表单 const [listR, setListR] = useState([]); // report列表 const [listU, setListU] = useState([]); // unit列表 // 请求listu const queryListU = (e) => { setReportId(e); setColumnName(null); if (e) { queryColumnList(e); } }; // 配置项获取类型 const handleChange = (type) => { setType(type); }; // 某一项修改 const optionsEdit = (obj, index) => { setOptions( options.map((e, i) => { if (i === index) return obj; return e; }), ); }; // 数据模型部分 const [listM, setListM] = useState([]); // 数据模型列表 const [labellist, setLabelList] = useState([]); // 数据模型label const [modelId, setModelId] = useState(value?.modelId); //数据模型id const [labelId, setLabelId] = useState(value?.labelId); //数据模型labelId // const [valueId, setValueId] = useState(value?.valueId); //数据模型valueId // 选中了数据模型 const onModelChange = (id) => { setModelId(id); previewData(id).then((res) => { setLabelList(res?.data?.data?.fields || []); }); }; // 修改label const onLabelChange = (id) => { setLabelId(id); }; // 修改value // const onValueChange = (id) => { // setValueId(id); // }; // 数据集 const [datesetId,setDatesetId] = useState(null); const [datesetTree, setDatesetTree] = useState([]); useEffect(() => { if (type === '数据模型' && !listM.length) { getDateSetTree({ appId: '0', busiFlag: 'dataset' }).then((res) => { const temp = res.data; function parse(list) { list.forEach((item) => { item.label = item.name; item.value = item.id; item.isLeaf = item.leaf; if (item?.children?.length) { parse(item.children); } }); } parse(temp); setListM(temp); }); if (value?.modelId) { previewData(value?.modelId).then((res) => { setLabelList(res?.data?.data?.fields || []); }); } } // 只请求一次 if (type === '关联表单' && !listR.length) { const id = Number(Base64.decode(urlParams.get('form')).replace('abc567', '')); const params = { reportId: id, type: 5, }; listOtherReport(params).then((res) => { if (res.code === 200) { setListR(res.data); } }); if (reportId) { queryColumnList(reportId); } } if (type === '数据集' && !datesetTree.length) { const params = { busiFlag: 'dataset', appId: Number(urlParams.get('applicationId')), }; treeCateAndTable(params).then(res=>{ const temp = res.data; function formatDate(list){ list.forEach(item=>{ item.label = item.name item.value = item.id if(item.children){ formatDate(item.children) } }) } formatDate(temp) setDatesetTree(temp) }) } }, [type]); // 请求列数据 const queryColumnList = (id) => { getCurrentColumnMetadata(id).then((res) => { if (res.code === 200) { setListU(res.data.filter((e) => !e.defaultFlag)); } }); }; // 数据同步 useEffect(() => { onChange({ type, options, reportId, columnName, modelId, labelId, // valueId, datesetId }); }, [ type, options, reportId, columnName, modelId, labelId, // valueId, datesetId, ]); // 添加自定义options const pushOptions = () => { setOptions([ ...options, { label: '新选项', value: '新选项', link: [], }, ]); }; // 删除options const delOptions = (index) => { setOptions(options.filter((_, i) => i !== index)); }; // 关联应用 const [isModalOpen, setIsModalOpen] = useState(false); const [links, setLinks] = useState([]); //可关联列表 const linkOptions = () => { const complist = project .exportSchema() .componentsTree[0]?.children[0]?.children.filter((e) => e.id !== props?.selected?._id) .map((e) => ({ label: e.props.title, value: e.id, })) || []; setLinks(complist); setIsModalOpen(true); }; const handleOk = () => { setIsModalOpen(false); }; // table表格关联列渲染 const render = (value, index, record) => { const linkChange = (val) => { const temp = JSON.parse(JSON.stringify(options)); temp[index].link = val; setOptions(temp); }; return ( {list.map((e) => ( ))} {type === '自定义' ? (
{options.map((e, i) => ( optionsEdit(e, i)} del={(_) => delOptions(i)} /> ))}
) : null} {type === '数据模型' ? (
labels[labels.length - 1]} /> {/* */}
) : null} {type === '关联表单' ? (
) : null} {type === '数据集' ? (
labels[labels.length - 1]} />
) : null} setIsModalOpen(false)} onClose={(_) => setIsModalOpen(false)} >
); }; export default class extends Component { render() { return ; } }