import { utils } from '@schema-render/core-react'
import type { SpaceProps } from 'antd'
import { Button, Space } from 'antd'
import type { FC } from 'react'
import { Fragment } from 'react'
import { ACTIONS } from '../constants'
import useFormRenderContext from '../hooks/useFormRenderContext'
import type { IRegisterActions, IRegisterActionsFnParams } from '../typings'
/**
* 内置操作:提交、重置
*/
const BUILTIN_ACTIONS: IRegisterActions = {
[ACTIONS.submit]: ({ loading, locale, disabled, submitText }) => (
),
[ACTIONS.reset]: ({ handleReset, loading, locale, disabled, resetText }) => (
),
}
interface IActionsProps extends SpaceProps {
/** 全局禁用状态 */
disabled?: boolean
}
/**
* 搜索操作
*/
const Actions: FC = ({ disabled, ...spaceProps }) => {
const {
actions,
actionsLoading,
registerActions,
handleReset,
handleSubmit,
layoutColumnGap,
locale,
submitText,
resetText,
} = useFormRenderContext()
const isShowActions = utils.isArray(actions) && actions.length > 0
if (!isShowActions) {
return null
}
const actionMap: IRegisterActions = {
...BUILTIN_ACTIONS,
...registerActions,
}
return (
{actions.map((action) => {
const params: IRegisterActionsFnParams = {
locale,
disabled,
loading: actionsLoading,
submitText,
resetText,
}
if (action === ACTIONS.submit) {
params.handleSubmit = handleSubmit
} else if (action === ACTIONS.reset) {
params.handleReset = handleReset
}
const fn = actionMap[action]
if (!fn) {
return null
}
return {fn?.(params)}
})}
)
}
export default Actions