@schema-render/form-render-react
Version:
Out-of-the-box form rendering library based on Core and Antd.
72 lines (71 loc) • 2.84 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import { utils } from "@schema-render/core-react";
import { Button, Space } from "antd";
import { Fragment } from "react";
import { ACTIONS } from "../constants";
import useFormRenderContext from "../hooks/useFormRenderContext";
/**
* 内置操作:提交、重置
*/ const BUILTIN_ACTIONS = {
[ACTIONS.submit]: ({ loading, locale, disabled, submitText })=>{
var _locale_FormRender;
return /*#__PURE__*/ _jsx(Button, {
type: "primary",
htmlType: "submit",
// 自身在 loading 时不使用禁用态
disabled: loading.submit ? false : disabled || loading.reset,
loading: loading.submit,
children: submitText || (locale === null || locale === void 0 ? void 0 : (_locale_FormRender = locale.FormRender) === null || _locale_FormRender === void 0 ? void 0 : _locale_FormRender.submit)
});
},
[ACTIONS.reset]: ({ handleReset, loading, locale, disabled, resetText })=>{
var _locale_FormRender;
return /*#__PURE__*/ _jsx(Button, {
htmlType: "button",
// 自身在 loading 时不使用禁用态
disabled: loading.reset ? false : disabled || loading.submit,
loading: loading.reset,
onClick: handleReset,
children: resetText || (locale === null || locale === void 0 ? void 0 : (_locale_FormRender = locale.FormRender) === null || _locale_FormRender === void 0 ? void 0 : _locale_FormRender.reset)
});
}
};
/**
* 搜索操作
*/ const Actions = ({ 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 = {
...BUILTIN_ACTIONS,
...registerActions
};
return /*#__PURE__*/ _jsx(Space, {
size: layoutColumnGap,
...spaceProps,
children: actions.map((action)=>{
const params = {
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 /*#__PURE__*/ _jsx(Fragment, {
children: fn === null || fn === void 0 ? void 0 : fn(params)
}, action);
})
});
};
export default Actions;