import React, { useState, useEffect, Component } from 'react'; import { Select } from '@alifd/next'; import { IPublicModelSettingField } from '@alilc/lowcode-types'; import { useGetFields } from '@/hooks/use-get-fields'; import './style.less'; interface SelectSetterProps { onChange: (value: string) => undefined; value?: any; mode?: 'single' | 'multiple' | 'tag'; defaultValue?: any; options: any[] | Function; showSearch?: boolean; hasClear?: boolean; placeholder?: string; selected: any; // 用于获取设计器实例 } const formateOptions = (options: any[] | Function) => { if (typeof options === 'function') { return options(); } return options.map((item: any) => { if (item.children) { const children = item.children.map((child: any) => { return { label: child.title || child.label || '-', value: child.value, disabled: child.disabled || false, }; }); return { label: item.title || item.label || '-', children, }; } else { return { label: item.title || item.label || '-', value: item.value, disabled: item.disabled || false, }; } }); }; const RelateSetter = (props: SelectSetterProps) => { const { options = props.options.length > 0 ? props.options : [], onChange = () => undefined, mode, value, showSearch, hasClear, placeholder = '请选择', selected, } = props; const [formList, setFormList] = useState([]); // 存储所有表单列表 const [fieldList, setFieldList] = useState([]); // 存储所有表单的字段列表 const [selectedForm, setSelectedForm] = useState(''); // 当前选中的表单 const [selectedField, setSelectedField] = useState(''); // 当前选中的字段 const { getForms, getFieldListByFormId } = useGetFields({ selected }); // useEffect(() => { // const { selected } = props; // const findPageParent = (component: any): any => { // if (!component) return null; // // 如果当前组件是 Page,直接返回 // if (component.componentName === 'Page') { // return component; // } // // 递归查找 parent // if (component.parent) { // return findPageParent(component.parent); // } // return null; // }; // const pageParent = findPageParent(selected); // if (pageParent && pageParent.schema && pageParent.schema.children) { // // 查找所有表单组件 // const findForms = (children: any[]): any[] => { // let forms: any[] = []; // if (!children.length || !Array.isArray(children)) return forms; // children.forEach((child) => { // // 检查是否有 props 属性和 formId // if (child.props && child.props.formId) { // forms.push({ // label: `${child.props.formName || child.componentName}(${child.props.formId})`, // value: child.props.formId, // componentName: child.componentName, // childrenComponents: child.children.map((item: any) => { // item.formId = child.props.formId; // return item; // }), // 保存子组件,方便后续查找字段 // }); // } // // 递归检查子组件 // if (child.children && Array.isArray(child.children)) { // forms = forms.concat(findForms(child.children)); // } // }); // return forms; // }; // // 查找特定表单下的所有字段 // const findFields = (children: any[], formId: string): any[] => { // let fields: any[] = []; // if (!children.length || !Array.isArray(children)) return fields; // children.forEach((child) => { // // 检查组件是否属于目标表单 // if (child.props && child.formId === formId && child.props.label) { // console.log("🚀 >>> index.tsx >>> children.forEach >>> child.props:", child.props); // // 添加有rs_id的字段 // if (child.props.rs_id) { // fields.push({ // label: child.props.label, // value: child.props.rs_id, // componentName: child.componentName, // disabled: !!child.props.relateOptions // }); // } // } // // 递归检查子组件 // if (child.children && Array.isArray(child.children)) { // fields = fields.concat(findFields(child.children, formId)); // } // }); // console.log("🚀 >>> index.tsx >>> useEffect >>> fields:", fields); // return fields; // }; // const forms = findForms(pageParent.schema.children); // if (forms.length > 0) { // setFormList(forms); // } // // 当选择表单时,更新字段选项 // if (selectedForm) { // const currentForm = forms.find((form) => form.value === selectedForm).childrenComponents; // const fields = findFields(currentForm, selectedForm); // setFieldList(fields); // } // } // }, [selected, selectedForm]); useEffect(() => { console.log('🚀 ~ useEffect ~ forms:', selected); const forms = getForms(); if (forms.length > 0) { setFormList(forms); } // 当选择表单时,更新字段选项 if (selectedForm) { let fields = getFieldListByFormId(selectedForm); fields = fields.map((t) => { return { label: `${t.name}(${t.fieldId})`, value: t.fieldId, componentName: t.componentName, disabled: !!t.relateOptions, }; }); setFieldList(fields); } }, [selected]); useEffect(() => { // 当选择表单时,更新字段选项 if (selectedForm) { let fields = getFieldListByFormId(selectedForm); fields = fields.map((t) => { return { label: `${t.name}(${t.fieldId})`, value: t.fieldId, componentName: t.componentName, disabled: !!t.relateOptions, }; }); setFieldList(fields); } }, [selectedForm]); return ( <> 0 ? fieldList : formateOptions(fieldList)} mode={mode} onChange={(val: any) => { onChange && onChange(val); setSelectedField(val); // 更新当前字段 }} style={{ width: '100%', marginTop: '16px' }} showSearch={showSearch} hasClear={hasClear} placeholder={'请选取字段'} notFoundContent="暂无可用字段" /> )} ); }; RelateSetter.displayName = 'RelateSetter'; RelateSetter.defaultProps = { placeholder: '请选择', options: [{ label: '-', value: '' }], defaultValue: null as any, onChange: () => undefined as any, }; export default class extends Component { render() { return ; } }