UNPKG

@schema-render/search-react

Version:

Conditional search component based on FormRender.

72 lines (71 loc) 2.59 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { DeleteOutlined, SearchOutlined } from "@ant-design/icons"; import FormRender from "@schema-render/form-render-react"; import { Button } from "antd"; import { forwardRef, useImperativeHandle, useMemo, useRef } from "react"; import { DEFAULT_SEARCH_PROPS } from "./constants"; import useCollapse from "./hooks/useCollapse"; import zh_CN from "./locale/zh_CN"; import { createActionsResetSchema } from "./utils/actions"; /** * 覆盖 submit、reset 操作,添加图标样式 */ const overrideDefaultActions = { submit: ({ loading, locale, submitText, disabled })=>{ return /*#__PURE__*/ _jsx(Button, { type: "primary", htmlType: "submit", icon: /*#__PURE__*/ _jsx(SearchOutlined, {}), loading: loading.submit, // 自身在 loading 时不使用禁用态 disabled: loading.submit ? false : disabled || loading.reset, children: submitText || locale.FormRender.submit }); }, reset: ({ loading, locale, handleReset, resetText, disabled })=>{ return /*#__PURE__*/ _jsx(Button, { htmlType: "button", icon: /*#__PURE__*/ _jsx(DeleteOutlined, {}), loading: loading.reset, // 自身在 loading 时不使用禁用态 disabled: loading.reset ? false : disabled || loading.submit, onClick: handleReset, children: resetText || locale.FormRender.reset }); } }; const Search = (searchProps, ref)=>{ const props = { ...DEFAULT_SEARCH_PROPS, ...searchProps }; const formRenderRef = useRef(null); // 国际化处理 const locale = useMemo(()=>({ ...zh_CN, ...props.locale }), [ props.locale ]); // 「收起/展开」功能处理 const { schema, registerActions } = useCollapse(props, formRenderRef); // Actions 样式处理 const actionsRestSchema = useMemo(()=>createActionsResetSchema(props.prefixCls), [ props.prefixCls ]); // 暴露 Open API useImperativeHandle(ref, ()=>formRenderRef.current); return /*#__PURE__*/ _jsx(FormRender, { layout: "autoFill", ...props, ref: formRenderRef, locale: locale, schema: schema, registerActions: { ...overrideDefaultActions, ...registerActions }, actionsRestSchema: actionsRestSchema, actionsRenderMode: "formItem" }); }; export default /*#__PURE__*/ forwardRef(Search);