Skip to content

Commit

Permalink
feat: form colon support (#5156)
Browse files Browse the repository at this point in the history
  • Loading branch information
mynetfan authored Dec 16, 2024
1 parent 38805a0 commit 593916d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
29 changes: 19 additions & 10 deletions docs/src/components/common-ui/vben-form.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,14 +313,14 @@ useVbenForm 返回的第二个参数,是一个对象,包含了一些表单
| resetButtonOptions | 重置按钮组件参数 | `ActionButtonOptions` | - |
| submitButtonOptions | 提交按钮组件参数 | `ActionButtonOptions` | - |
| showDefaultActions | 是否显示默认操作按钮 | `boolean` | `true` |
| collapsed | 是否折叠,在`是否展开,在showCollapseButton=true`时生效 | `boolean` | `false` |
| collapsed | 是否折叠,在`showCollapseButton``true`时生效 | `boolean` | `false` |
| collapseTriggerResize | 折叠时,触发`resize`事件 | `boolean` | `false` |
| collapsedRows | 折叠时保持的行数 | `number` | `1` |
| fieldMappingTime | 用于将表单内时间区域的应设成 2 个字段 | `[string, [string, string], string?][]` | - |
| fieldMappingTime | 用于将表单内时间区域组件的数组值映射成 2 个字段 | `[string, [string, string], string?][]` | - |
| commonConfig | 表单项的通用配置,每个配置都会传递到每个表单项,表单项可覆盖 | `FormCommonConfig` | - |
| schema | 表单项的每一项配置 | `FormSchema` | - |
| schema | 表单项的每一项配置 | `FormSchema[]` | - |
| submitOnEnter | 按下回车健时提交表单 | `boolean` | false |
| submitOnChange | 字段值改变时提交表单 | `boolean` | false |
| submitOnChange | 字段值改变时提交表单(内部防抖,这个属性一般用于表格的搜索表单) | `boolean` | false |

### TS 类型说明

Expand Down Expand Up @@ -361,6 +361,10 @@ export interface FormCommonConfig {
* 所有表单项的控件样式
*/
controlClass?: string;
/**
* 在表单项的Label后显示一个冒号
*/
colon?: boolean;
/**
* 所有表单项的禁用状态
* @default false
Expand Down Expand Up @@ -420,7 +424,7 @@ export interface FormSchema<
dependencies?: FormItemDependencies;
/** 描述 */
description?: string;
/** 字段名 */
/** 字段名,也作为自定义插槽的名称 */
fieldName: string;
/** 帮助信息 */
help?: string;
Expand All @@ -443,7 +447,7 @@ export interface FormSchema<

```ts
dependencies: {
// 只有当 name 字段的值变化时,才会触发联动
// 触发字段。只有这些字段值变动时,联动才会触发
triggerFields: ['name'],
// 动态判断当前字段是否需要显示,不显示则直接销毁
if(values,formApi){},
Expand All @@ -464,11 +468,11 @@ dependencies: {

### 表单校验

表单联动需要通过 schema 内的 `rules` 属性进行配置。
表单校验需要通过 schema 内的 `rules` 属性进行配置。

rules的值可以是一个字符串,也可以是一个zod的schema。
rules的值可以是字符串(预定义的校验规则名称),也可以是一个zod的schema。

#### 字符串
#### 预定义的校验规则

```ts
// 表示字段必填,默认会根据适配器的required进行国际化
Expand All @@ -494,11 +498,16 @@ import { z } from '#/adapter/form';
rules: z.string().min(1, { message: '请输入字符串' });
}

// 可选,并且携带默认值
// 可选(可以是undefined),并且携带默认值。注意zod的optional不包括空字符串''
{
rules: z.string().default('默认值').optional(),
}

// 可以是空字符串、undefined或者一个邮箱地址
{
rules: z.union(z.string().email().optional(), z.literal(""))
}

// 复杂校验
{
z.string().min(1, { message: "请输入" })
Expand Down
6 changes: 5 additions & 1 deletion packages/@core/ui-kit/form-ui/src/form-render/form-field.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { isEventObjectLike } from './helper';
interface Props extends FormSchema {}
const {
colon,
commonComponentProps,
component,
componentProps,
Expand Down Expand Up @@ -300,7 +301,10 @@ function autofocus() {
:required="shouldRequired && !hideRequiredMark"
:style="labelStyle"
>
{{ label }}
<template v-if="label">
<span>{{ label }}</span>
<span v-if="colon" class="ml-[2px]">:</span>
</template>
</FormLabel>
<div :class="cn('relative flex w-full items-center', wrapperClass)">
<FormControl :class="cn(controlClass)">
Expand Down
2 changes: 2 additions & 0 deletions packages/@core/ui-kit/form-ui/src/form-render/form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ const computedSchema = computed(
formFieldProps: Record<string, any>;
} & Omit<FormSchema, 'formFieldProps'>)[] => {
const {
colon = false,
componentProps = {},
controlClass = '',
disabled,
Expand All @@ -110,6 +111,7 @@ const computedSchema = computed(
: false;
return {
colon,
disabled,
disabledOnChangeListener,
disabledOnInputListener,
Expand Down
4 changes: 4 additions & 0 deletions packages/@core/ui-kit/form-ui/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ type ComponentProps =
| MaybeComponentProps;

export interface FormCommonConfig {
/**
* 在Label后显示一个冒号
*/
colon?: boolean;
/**
* 所有表单项的props
*/
Expand Down
3 changes: 3 additions & 0 deletions playground/src/views/examples/form/basic.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const activeTab = ref('basic');
const [BaseForm, baseFormApi] = useVbenForm({
// 所有表单项共用,可单独在表单内覆盖
commonConfig: {
// 在label后显示一个冒号
colon: true,
// 所有表单项
componentProps: {
class: 'w-full',
Expand All @@ -40,6 +42,7 @@ const [BaseForm, baseFormApi] = useVbenForm({
fieldName: 'username',
// 界面显示的label
label: '字符串',
rules: 'required',
},
{
// 组件需要在 #/adapter.ts内注册,并加上类型
Expand Down

0 comments on commit 593916d

Please sign in to comment.