Skip to content

Commit

Permalink
feat: support submitText and resetText
Browse files Browse the repository at this point in the history
  • Loading branch information
Barrior committed Oct 31, 2024
1 parent 9f83f61 commit 9c4933e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 20 deletions.
16 changes: 9 additions & 7 deletions examples/form-render-react/300-actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -450,13 +450,15 @@ export default Demo

`IRegisterActionsFnParams` 参数详解如下表格

| **参数** | **描述** | **类型** |
| ---------------- | ------------------------------------ | -------------------------------------- |
| **handleSubmit** | 当操作名称为 `submit` 时,存在该事件 | `() => void` |
| **handleReset** | 当操作名称为 `reset` 时,存在该事件 | `() => void` |
| **loading** | 提交与重置事件的 `loading` 状态 | `{ submit: boolean; reset: boolean; }` |
| **disabled** | 全局禁用状态 | `boolean` |
| **locale** | 当前语言数据 | `object` |
| **参数** | **描述** | **类型** |
| ---------------- | -------------------------------------- | -------------------------------------- |
| **handleSubmit** | 当操作名称为 `submit` 时,存在该事件 | `() => void` |
| **handleReset** | 当操作名称为 `reset` 时,存在该事件 | `() => void` |
| **loading** | 提交与重置事件的 `loading` 状态 | `{ submit: boolean; reset: boolean; }` |
| **disabled** | 全局禁用状态 | `boolean` |
| **locale** | 当前语言数据 | `object` |
| **submitText** | 提交按钮文案,优先级大于 locale 语言包 | `string` |
| **resetText** | 重置按钮文案,优先级大于 locale 语言包 | `string` |

## 其他相关 API 详解

Expand Down
4 changes: 3 additions & 1 deletion packages/form-render-react/src/FormRender.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ const FormRender = (formRenderProps: IFormRenderProps, ref?: Ref<IFormRenderRef>
'layoutRowGap',
'actions',
'registerActions',
'readonlyPlaceholder'
'readonlyPlaceholder',
'submitText',
'resetText'
),
locale,
actionsLoading,
Expand Down
12 changes: 8 additions & 4 deletions packages/form-render-react/src/components/Actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@ import type { IRegisterActions, IRegisterActionsFnParams } from '../typings'
* 内置操作:提交、重置
*/
const BUILTIN_ACTIONS: IRegisterActions = {
[ACTIONS.submit]: ({ loading, locale, disabled }) => (
[ACTIONS.submit]: ({ loading, locale, disabled, submitText }) => (
<Button
type="primary"
htmlType="submit"
// 自身在 loading 时不使用禁用态
disabled={loading.submit ? false : disabled || loading.reset}
loading={loading.submit}
>
{locale?.FormRender?.submit}
{submitText || locale?.FormRender?.submit}
</Button>
),
[ACTIONS.reset]: ({ handleReset, loading, locale, disabled }) => (
[ACTIONS.reset]: ({ handleReset, loading, locale, disabled, resetText }) => (
<Button
htmlType="button"
// 自身在 loading 时不使用禁用态
disabled={loading.reset ? false : disabled || loading.submit}
loading={loading.reset}
onClick={handleReset}
>
{locale?.FormRender?.reset}
{resetText || locale?.FormRender?.reset}
</Button>
),
}
Expand All @@ -53,6 +53,8 @@ const Actions: FC<IActionsProps> = ({ disabled, ...spaceProps }) => {
handleSubmit,
layoutColumnGap,
locale,
submitText,
resetText,
} = useFormRenderContext()

const isShowActions = utils.isArray(actions) && actions.length > 0
Expand All @@ -73,6 +75,8 @@ const Actions: FC<IActionsProps> = ({ disabled, ...spaceProps }) => {
locale,
disabled,
loading: actionsLoading,
submitText,
resetText,
}

if (action === ACTIONS.submit) {
Expand Down
16 changes: 15 additions & 1 deletion packages/form-render-react/src/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ export interface IRegisterActionsFnParams<T extends IObjectAny = ILocale> {
loading: IActionsLoading
/** 全局禁用状态,@schema-render/core-react global-status */
disabled?: boolean
/* 语言 */
/* 语言包 */
locale: T
/** 提交按钮文案 */
submitText?: string
/** 重置按钮文案 */
resetText?: string
}

/**
Expand Down Expand Up @@ -150,6 +154,14 @@ export type IFormRenderProps = IPartPartial<
* 注册操作行为,如果是 submit、reset 将覆盖原有样式
*/
registerActions?: IRegisterActions<ILocale & IObjectAny>
/**
* 提交按钮文案,优先级大于 locale 语言包
*/
submitText?: string
/**
* 重置按钮文案,优先级大于 locale 语言包
*/
resetText?: string
/**
* 提交事件
* @param value 表单数据
Expand Down Expand Up @@ -200,6 +212,8 @@ export type IFormRenderContext = Pick<
| 'layoutColumnGap'
| 'layoutRowGap'
| 'readonlyPlaceholder'
| 'submitText'
| 'resetText'
> & {
locale: ILocale
actionsLoading: IActionsLoading
Expand Down
23 changes: 16 additions & 7 deletions packages/search-react/src/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,31 @@ import { createActionsResetSchema } from './utils/actions'
* 覆盖 submit、reset 操作,添加图标样式
*/
const overrideDefaultActions: IRegisterActions = {
submit: ({ loading, locale }) => {
submit: ({ loading, locale, submitText, disabled }) => {
return (
<Button
loading={loading.submit}
htmlType="submit"
type="primary"
htmlType="submit"
icon={<SearchOutlined />}
loading={loading.submit}
// 自身在 loading 时不使用禁用态
disabled={loading.submit ? false : disabled || loading.reset}
>
{locale.FormRender.submit}
{submitText || locale.FormRender.submit}
</Button>
)
},
reset: ({ loading, locale, handleReset }) => {
reset: ({ loading, locale, handleReset, resetText, disabled }) => {
return (
<Button loading={loading.reset} onClick={handleReset} icon={<DeleteOutlined />}>
{locale.FormRender.reset}
<Button
htmlType="button"
icon={<DeleteOutlined />}
loading={loading.reset}
// 自身在 loading 时不使用禁用态
disabled={loading.reset ? false : disabled || loading.submit}
onClick={handleReset}
>
{resetText || locale.FormRender.reset}
</Button>
)
},
Expand Down

0 comments on commit 9c4933e

Please sign in to comment.