/* eslint-disable react-hooks/rules-of-hooks */ /* eslint-disable react-hooks/exhaustive-deps */ /* eslint-disable @typescript-eslint/no-use-before-define */ import React, { useMemo } from 'react' import { Space, Alert, Table } from 'antd' import { run, isObject, flatten, isFunction } from '@fexd/tools' import { ProForm, ProFieldValueFieldType } from '@fexd/pro-form' import { Action, Hook } from '@fexd/pro-utils' import { useMemoizedFn } from 'ahooks' import { useProps } from '../../utils' import useQueryFieldPlugin from '../queryField' import useFieldParams from '../editField/useFieldParams' // import useActionsPlugin from '../actions' import { getAllFieldFromColumn } from '../valueType' // import useConfigPlugin, { I18nText } from '../config' import { ProTableEditFieldType } from '../../types' import useItem from './useItem' const flattenChildren: (arr: T) => T = (arr) => flatten(arr.map((item) => [item, flattenChildren(item?.children ?? [])])) as any export default function useSelectable() { const { dataSource: propDataSource, expandable, columns, expandableDescriptionConfig, expandableProFormRender, } = useProps() const queryField = useQueryFieldPlugin(({ dataSource }) => [dataSource]) const { dataSource: tableDataSource } = queryField const dataSource = propDataSource ?? tableDataSource const customeizedExpandColumn = useMemo(() => columns.includes(Table.EXPAND_COLUMN), [columns]) const expandViewFields = useMemo( () => flattenChildren(columns) .filter((column) => column?.expandView === true || !!column?.expandViewField) .map((column) => { const allField = getAllFieldFromColumn(column) return allField?.expandViewField ?? allField?.viewField }) .filter(Boolean), [columns], ) const expandedRowRender = useMemoizedFn((item: any = {}) => (
{() => ( ({ mode: 'view', viewType: 'expand', form: undefined }) as any, [])} > Math.random(), [item])} mode="view" fields={ (expandViewFields ?? []).map((field) => ({ ...field, ...(isFunction(field?.hook) ? { hook: ({ form }) => run(field?.hook, undefined, { form, item, mode: 'view', inTable: false }), } : {}), })) as ProFieldValueFieldType[] } initialValues={{ ...item }} render={ expandableProFormRender ?? (({ renderDescriptions }) => renderDescriptions(expandableDescriptionConfig)) } /> )}
)) const expandableConfig = useMemo( () => expandViewFields?.length > 0 && dataSource?.length > 0 ? { // fixed: 'right', // expandedRowKeys, // onExpand expandRowByClick: true, expandedRowRender, ...(isObject(expandable) ? expandable : {}), } : expandable, [expandable, dataSource?.length, customeizedExpandColumn, expandViewFields], ) return expandableConfig }