import React, { useState, useEffect, Component } from 'react'; import { Select, Dialog, Balloon, Input, Button } from '@alifd/next'; import { findPageParent } from '../../_utils/utils'; import './index.less'; interface RsConfigCopySetterProps { value?: string; onChange?: (value: string) => undefined; conponentName?: string; selected?: any; field?: any; } interface ListItemProps { tele_id?: string; tele_name?: string; itemProps?: any; } interface OptionItemProps { label?: string; value?: string; tele_name?: string; itemProps?: any; } const TELE_COPY_PROPS = [ 'tele_name', 'iconPosition', 'isMultiple', 'isBackfillToMutilRecord', 'isPagination', 'pageSize', 'isSupportInput', 'supportInputValue', 'tele_query_cols', 'url', 'tele_queryParams', 'telescopeCustomParams', 'telescopeColumns', 'showTree', 'tree_autoExpandParent', 'tree_defaultSelectRoot', 'treeWidth', 'tree_hasSearch', 'tree_placeholder', 'treeTitleIndex', 'treeKeyIndex', 'treeChildrenIndex', 'tree_extraQueryParams', 'treeUrl', ]; const RsConfigCopySetter: React.FC = (props) => { const { onChange = () => undefined, value, selected, conponentName, field } = props; const [setterValue, setSetterValue] = useState(); const [optionsList, setOptionsList] = useState([]); const [isInArraySetter, setIsInArraySetter] = useState(false); const getListItem = (obj: ListItemProps | undefined) => { if (!obj || !obj.tele_id) return; const { tele_id = '', tele_name = '' } = obj; return { label: `${tele_id || ''}${tele_name ? `(${tele_name})` : ''}`, value: tele_id, tele_name, itemProps: obj, }; }; const findAllTeleItems = (children: any, componentName: string | undefined): any[] => { if (!children.length || !Array.isArray(children)) return []; let responseList: any[] = []; children.forEach((child) => { if (child.componentName === componentName) { const listItem = getListItem(child?.props); listItem && responseList.push(listItem); } else if (child.componentName === 'RsQueryFilter') { const { items = [] } = child.props; items.forEach((item: any) => { if (item.componentName === componentName) { const listItem = getListItem(item); listItem && responseList.push(listItem); } }); } else if ( ['RsEditTable', 'RsListTable', 'RsSubForm', 'RsLinkedSubFormTable'].includes( child.componentName, ) ) { const { columns = [] } = child.props; columns.forEach((item: any) => { if (item.dataType === 'telescope') { const listItem = getListItem(item); listItem && responseList.push(listItem); } }); } // 递归检查子组件 if (child.children && Array.isArray(child.children)) { responseList = responseList.concat(findAllTeleItems(child.children, componentName)); } }); return responseList; }; const getOptions = () => { const pageSchema = findPageParent(selected); let list = []; if (pageSchema?.schema?.children) { list = findAllTeleItems(pageSchema?.schema?.children, conponentName); } setOptionsList([...list]); }; const handleChange = (val: any) => { Dialog.show({ title: '提示', content: `此操作会覆盖现有望远镜配置,确定要选择该项(望远镜编码:${val || ''})吗?`, onOk: () => { setSetterValue(val); onChange?.(val); const selectProps = optionsList.filter((item: OptionItemProps) => item.value === val); if (selectProps && selectProps.length && selectProps[0].itemProps) { const newProps = selectProps[0].itemProps; TELE_COPY_PROPS.forEach((item) => { if (isInArraySetter) { field?.parent?.setPropValue(item, newProps[item]); } else { selected.setPropValue(item, newProps[item]); } }); } }, onCancel: () => {}, }); }; useEffect(() => { setSetterValue(value || null); getOptions(); const { componentName = '' } = selected; if (['RsEditTable', 'RsListTable', 'RsSubForm', 'RsQueryFilter'].includes(componentName)) { setIsInArraySetter(true); } // eslint-disable-next-line react-hooks/exhaustive-deps }, []); const coptBtn = ( ); return (
{ setSetterValue(val); onChange?.(val); }} value={setterValue} />