Skip to content

Commit

Permalink
Merge pull request #46 from roshan-labs/feat-form-custom-item
Browse files Browse the repository at this point in the history
feat: pro-form 支持自定义表单项组件
  • Loading branch information
gxmari007 authored Aug 9, 2023
2 parents 735010d + ae11c2e commit 1d1b2b5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/five-swans-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@roshan-ui/ui': minor
---

ProForm 支持自定义表单项组件
15 changes: 15 additions & 0 deletions packages/ui/src/components/pro-form/pro-form.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,21 @@ Upload.args = {
}
Upload.storyName = '上传'

export const SlotOption = Template.bind({})
SlotOption.args = {
options: [
{
prop: 'custom',
type: 'custom',
slots: {
default: () => h(ElButton, null, () => '自定义表单项'),
},
},
] as ProFormOption[],
labelWidth: 90,
}
SlotOption.storyName = '自定义插槽表单项'

export const CustomAction = Template.bind({})
CustomAction.args = {
...Default.args,
Expand Down
15 changes: 14 additions & 1 deletion packages/ui/src/components/pro-form/pro-form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@
<el-row v-if="isLayout">
<el-col v-for="item in options" v-show="!item.hide" :key="item.prop" v-bind="item">
<el-form-item v-bind="item">
<!-- slots -->
<template v-for="(slot, name) in item.slots" :key="name" #[name]="slotProps">
<slot v-if="typeof slot === 'string'" :name="slot" v-bind="slotProps" />
<component :is="slot(slotProps)" v-if="typeof slot === 'function'" />
</template>
<!-- Other form item -->
<component
:is="getComponent(item.type)"
v-if="item.type !== 'custom' && !item.slots"
v-bind="item.component"
:model-value="fields[item.prop]"
@update:model-value="updateFieldValue($event, item.prop)"
Expand All @@ -32,11 +39,17 @@
</el-form-item>
</el-col>
</el-row>
<!-- Default -->
<!-- No layout -->
<template v-else>
<el-form-item v-for="item in options" v-show="!item.hide" :key="item.prop" v-bind="item">
<!-- slots -->
<template v-for="(slot, name) in item.slots" :key="name" #[name]="slotProps">
<slot v-if="typeof slot === 'string'" :name="slot" v-bind="slotProps" />
<component :is="slot(slotProps)" v-if="typeof slot === 'function'" />
</template>
<component
:is="getComponent(item.type)"
v-if="item.type !== 'custom' && !item.slots"
v-bind="item.component"
:model-value="fields[item.prop]"
@update:model-value="updateFieldValue($event, item.prop)"
Expand Down
11 changes: 10 additions & 1 deletion packages/ui/src/components/pro-form/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Ref } from 'vue'
import type { Ref, VNode } from 'vue'
import type {
FormProps,
FormEmits,
Expand Down Expand Up @@ -299,6 +299,14 @@ interface FormSelectV2Option extends FormBaseOption {
component?: FormSelectProps
}

/**
* 自定义表单项
*/
interface FormCustomOption extends FormBaseOption {
type: 'custom'
component?: VNode
}

export type ProFormOption =
| FormAutocompleteOption
| FormCascaderOption
Expand All @@ -318,6 +326,7 @@ export type ProFormOption =
| FormUploadOption
| FormSelectOption
| FormSelectV2Option
| FormCustomOption

export interface ProFormAction extends LayoutColProps {
submit?: boolean
Expand Down

0 comments on commit 1d1b2b5

Please sign in to comment.