Skip to content

Commit

Permalink
feat: support validate on form item
Browse files Browse the repository at this point in the history
  • Loading branch information
ccloli committed May 8, 2024
1 parent f64d178 commit c1054ea
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
13 changes: 13 additions & 0 deletions packages/helpers/src/types/prototype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ export type ComponentDndRulesType = IComponentDndRules;
*/
export type ComponentPrototypeType = IComponentPrototype;

/**
* 组件校验规则
*/
export type ComponentPropValidate = (
value: string,
field: any,
trigger: string,
) => string | Promise<string>;

/**
* 组件属性类型
*/
Expand Down Expand Up @@ -107,6 +116,10 @@ export interface IComponentProp<T = any> {
* 动态设置表单项是否展示
*/
getVisible?: (form: any) => boolean;
/**
* 表单值校验逻辑
*/
validate?: ComponentPropValidate;
/**
* 标记属性是否已废弃
*/
Expand Down
7 changes: 4 additions & 3 deletions packages/setting-form/src/form-item.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { toJS } from 'mobx';
import { observer } from 'mobx-react-lite';
import { clone, IComponentProp, useBoolean } from '@music163/tango-helpers';
import { clone, ComponentPropValidate, IComponentProp, useBoolean } from '@music163/tango-helpers';
import { isWrappedByExpressionContainer } from '@music163/tango-core';
import { ToggleButton, CodeOutlined } from '@music163/tango-ui';
import { InputProps } from 'antd';
Expand Down Expand Up @@ -58,7 +58,7 @@ export interface IFormItemCreateOptions {
/**
* 默认的表单值校验逻辑
*/
validate?: (value: string) => string | Promise<any>;
validate?: ComponentPropValidate;
}

const defaultGetSetterProps = () => ({});
Expand Down Expand Up @@ -86,6 +86,7 @@ export function createFormItem(options: IFormItemCreateOptions) {
extra,
footer,
noStyle,
validate,
}: FormItemProps) {
const { disableSwitchExpressionSetter, showItemSubtitle } = useFormVariable();
const model = useFormModel();
Expand All @@ -99,7 +100,7 @@ export function createFormItem(options: IFormItemCreateOptions) {
const setterName = isVariable ? 'expressionSetter' : setter;

field.setConfig({
validate: options.validate,
validate: validate || options.validate,
});

const baseComponentProps = clone(
Expand Down

0 comments on commit c1054ea

Please sign in to comment.