import React, { useMemo } from 'react' import { run } from '@fexd/tools' import { DeleteOutlined } from '@ant-design/icons' import { useSetState, useMemoizedFn, useLatest } from 'ahooks' import { SetState } from 'ahooks/es/useSetState' import { useProps } from '../../utils' import useQueryFieldPlugin from '../queryField' import { ProTableBuiltInActionType, ProTableTableActionType, ProTableBuiltInBatchActionNames } from './types' import useConfigPlugin, { I18nText } from '../config' import Actions from './Actions' import Action from '../actions/Action' // 多选动作 export default function useBatchActions(): { batchActions: Record batchActionConfigs: ProTableTableActionType[] setBatchActions: SetState> renderBatchActions: () => JSX.Element } { const { batchActions: batchActionConfigs, onDelete, builtInActions } = useProps() const latestBatchActionConfigs = useLatest(batchActionConfigs) const queryField = useQueryFieldPlugin(({ selectedItems }) => [selectedItems]) const { t } = useConfigPlugin(() => []) // const { confirmPromise } = useModalPlugin(() => []) const { setSelectedItems, getSelectedItems } = queryField const [batchActions, setBatchActions] = useSetState>({ delete: (selectedItems) => ({ key: 'batch-delete', icon: , danger: true, content: , confirm: t('actions.multipleDeleteConfirm', { count: selectedItems.length }), async onClick() { const { success } = ((await run(onDelete, undefined, selectedItems)) as any) ?? {} if (success) { setSelectedItems([]) queryField.search() } }, }), ...(builtInActions?.batchActions ?? {}), } as Record) const latestBatchActions = useLatest(batchActions) const renderBatchActions = useMemoizedFn(() => ( latestBatchActionConfigs?.current as any} getBuiltInActions={() => latestBatchActions.current} actionParams={[getSelectedItems()]} renderAction={({ content, onClick, ...actionProps }: any = {}) => ( { const selectedItems = getSelectedItems() const { success } = ((await run(onClick, undefined, selectedItems)) as any) ?? {} if (success) { setSelectedItems([]) } }} > {content} )} /> )) return { batchActions, batchActionConfigs: useMemo( () => (batchActionConfigs ?? []) .filter(Boolean) .filter( (action) => (action as any)?.hidden !== true, ) as ProTableTableActionType[], [batchActionConfigs], ), setBatchActions, renderBatchActions, } }