perf: [antd] default placeholder for input and select components (#4551)

* chore: demo of customizing form layout using tailwind

* perf: default placeholder for input and select components

* chore: update ts type

* chore: extract public methods
pull/48/MERGE
LinaBell 2024-10-03 13:04:19 +08:00 committed by GitHub
parent b3e196f001
commit aed31a5a4e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 54 additions and 53 deletions

View File

@ -4,7 +4,7 @@ import type {
VbenFormProps, VbenFormProps,
} from '@vben/common-ui'; } from '@vben/common-ui';
import { h } from 'vue'; import { type Component, h, type SetupContext } from 'vue';
import { setupVbenForm, useVbenForm as useForm, z } from '@vben/common-ui'; import { setupVbenForm, useVbenForm as useForm, z } from '@vben/common-ui';
import { $t } from '@vben/locales'; import { $t } from '@vben/locales';
@ -57,6 +57,16 @@ export type FormComponentType =
| 'Upload' | 'Upload'
| BaseFormComponentType; | BaseFormComponentType;
const withDefaultPlaceholder = <T extends Component>(
component: T,
type: 'input' | 'select',
) => {
return (props: any, { attrs, slots }: Omit<SetupContext, 'expose'>) => {
const placeholder = props?.placeholder || $t(`placeholder.${type}`);
return h(component, { ...props, attrs, placeholder }, slots);
};
};
// 初始化表单组件并注册到form组件内部 // 初始化表单组件并注册到form组件内部
setupVbenForm<FormComponentType>({ setupVbenForm<FormComponentType>({
components: { components: {
@ -73,20 +83,20 @@ setupVbenForm<FormComponentType>({
return h(Button, { ...props, attrs, type: 'primary' }, slots); return h(Button, { ...props, attrs, type: 'primary' }, slots);
}, },
Divider, Divider,
Input, Input: withDefaultPlaceholder(Input, 'input'),
InputNumber, InputNumber: withDefaultPlaceholder(InputNumber, 'input'),
InputPassword, InputPassword: withDefaultPlaceholder(InputPassword, 'input'),
Mentions, Mentions: withDefaultPlaceholder(Mentions, 'input'),
Radio, Radio,
RadioGroup, RadioGroup,
RangePicker, RangePicker,
Rate, Rate,
Select, Select: withDefaultPlaceholder(Select, 'select'),
Space, Space,
Switch, Switch,
Textarea, Textarea: withDefaultPlaceholder(Textarea, 'input'),
TimePicker, TimePicker,
TreeSelect, TreeSelect: withDefaultPlaceholder(TreeSelect, 'select'),
Upload, Upload,
}, },
config: { config: {

View File

@ -58,6 +58,10 @@
"required": "Please enter {0}", "required": "Please enter {0}",
"selectRequired": "Please select {0}" "selectRequired": "Please select {0}"
}, },
"placeholder": {
"input": "Please enter",
"select": "Please select"
},
"widgets": { "widgets": {
"document": "Document", "document": "Document",
"qa": "Q&A", "qa": "Q&A",

View File

@ -58,6 +58,10 @@
"required": "请输入{0}", "required": "请输入{0}",
"selectRequired": "请选择{0}" "selectRequired": "请选择{0}"
}, },
"placeholder": {
"input": "请输入",
"select": "请选择"
},
"widgets": { "widgets": {
"document": "文档", "document": "文档",
"qa": "问题 & 帮助", "qa": "问题 & 帮助",

View File

@ -4,7 +4,7 @@ import type {
VbenFormProps, VbenFormProps,
} from '@vben/common-ui'; } from '@vben/common-ui';
import { h } from 'vue'; import { type Component, h, type SetupContext } from 'vue';
import { setupVbenForm, useVbenForm as useForm, z } from '@vben/common-ui'; import { setupVbenForm, useVbenForm as useForm, z } from '@vben/common-ui';
import { $t } from '@vben/locales'; import { $t } from '@vben/locales';
@ -57,6 +57,16 @@ export type FormComponentType =
| 'Upload' | 'Upload'
| BaseFormComponentType; | BaseFormComponentType;
const withDefaultPlaceholder = <T extends Component>(
component: T,
type: 'input' | 'select',
) => {
return (props: any, { attrs, slots }: Omit<SetupContext, 'expose'>) => {
const placeholder = props?.placeholder || $t(`placeholder.${type}`);
return h(component, { ...props, attrs, placeholder }, slots);
};
};
// 初始化表单组件并注册到form组件内部 // 初始化表单组件并注册到form组件内部
setupVbenForm<FormComponentType>({ setupVbenForm<FormComponentType>({
components: { components: {
@ -73,20 +83,20 @@ setupVbenForm<FormComponentType>({
return h(Button, { ...props, attrs, type: 'primary' }, slots); return h(Button, { ...props, attrs, type: 'primary' }, slots);
}, },
Divider, Divider,
Input, Input: withDefaultPlaceholder(Input, 'input'),
InputNumber, InputNumber: withDefaultPlaceholder(InputNumber, 'input'),
InputPassword, InputPassword: withDefaultPlaceholder(InputPassword, 'input'),
Mentions, Mentions: withDefaultPlaceholder(Mentions, 'input'),
Radio, Radio,
RadioGroup, RadioGroup,
RangePicker, RangePicker,
Rate, Rate,
Select, Select: withDefaultPlaceholder(Select, 'select'),
Space, Space,
Switch, Switch,
Textarea, Textarea: withDefaultPlaceholder(Textarea, 'input'),
TimePicker, TimePicker,
TreeSelect, TreeSelect: withDefaultPlaceholder(TreeSelect, 'select'),
Upload, Upload,
}, },
config: { config: {

View File

@ -234,62 +234,41 @@ const [CustomLayoutForm] = useVbenForm({
layout: 'horizontal', layout: 'horizontal',
schema: [ schema: [
{ {
component: 'Input', component: 'Select',
componentProps: {
placeholder: '请输入',
},
fieldName: 'field1', fieldName: 'field1',
label: '字符串', label: '字符串',
}, },
{ {
component: 'Input', component: 'TreeSelect',
componentProps: {
placeholder: '请输入',
},
fieldName: 'field2', fieldName: 'field2',
label: '字符串', label: '字符串',
}, },
{ {
component: 'Input', component: 'Mentions',
componentProps: {
placeholder: '请输入',
},
fieldName: 'field3', fieldName: 'field3',
label: '字符串', label: '字符串',
}, },
{ {
component: 'Input', component: 'Input',
componentProps: {
placeholder: '请输入',
},
fieldName: 'field4', fieldName: 'field4',
label: '字符串', label: '字符串',
}, },
{ {
component: 'Input', component: 'InputNumber',
componentProps: {
placeholder: '请输入',
},
fieldName: 'field5', fieldName: 'field5',
// //
formItemClass: 'col-start-3', formItemClass: 'col-start-3',
label: '前面空了一列', label: '前面空了一列',
}, },
{ {
component: 'Input', component: 'Textarea',
componentProps: {
placeholder: '请输入',
},
fieldName: 'field6', fieldName: 'field6',
// // 线
formItemClass: 'col-span-3', formItemClass: 'col-span-3 items-baseline',
label: '占满三列', label: '占满三列',
}, },
{ {
component: 'Input', component: 'Input',
componentProps: {
placeholder: '请输入',
},
fieldName: 'field7', fieldName: 'field7',
// 2 // 2
formItemClass: 'col-span-2 col-start-2', formItemClass: 'col-span-2 col-start-2',
@ -297,26 +276,20 @@ const [CustomLayoutForm] = useVbenForm({
}, },
{ {
component: 'Input', component: 'Input',
componentProps: {
placeholder: '请输入',
},
fieldName: 'field8', fieldName: 'field8',
// //
formItemClass: 'col-start-2', formItemClass: 'col-start-2',
label: '左右留空', label: '左右留空',
}, },
{ {
component: 'Input', component: 'InputPassword',
componentProps: {
placeholder: '请输入',
},
fieldName: 'field9', fieldName: 'field9',
formItemClass: 'col-start-1', formItemClass: 'col-start-1',
label: '占满2列', label: '字符串',
}, },
], ],
// //
wrapperClass: 'lg:grid-cols-3', wrapperClass: 'grid-cols-3',
}); });
function onSubmit(values: Record<string, any>) { function onSubmit(values: Record<string, any>) {