Skip to content

Commit 20aca8d

Browse files
authored
fix(Form): optimize ref bind for ProFormComponent (#555)
1 parent 1eeda9b commit 20aca8d

File tree

4 files changed

+9
-22
lines changed

4 files changed

+9
-22
lines changed

src/Form/FormComponent.ts

+3-20
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
import {
2-
computed,
3-
defineComponent,
4-
h,
5-
mergeProps,
6-
onMounted,
7-
reactive,
8-
ref,
9-
resolveComponent,
10-
} from 'vue'
1+
import { computed, defineComponent, h, mergeProps, resolveComponent } from 'vue'
112
import { isFunction, isObject, isString } from '../utils/index'
123
import { formComponentProps } from './props'
134
import type { Slot } from 'vue'
@@ -23,18 +14,16 @@ interface TargetEvent {
2314
export default defineComponent({
2415
name: 'ProFormComponent',
2516
props: formComponentProps,
26-
setup(props, { attrs, emit, expose }) {
17+
setup(props, { attrs, emit }) {
2718
const nativeComponents = ['input', 'textarea', 'select']
28-
const componentRef = ref<StringObject>({})
29-
const componentExpose = reactive<StringObject>({})
3019

3120
const type = computed(() => {
3221
return isString(props.is) && !nativeComponents.includes(props.is)
3322
? resolveComponent(props.is)
3423
: props.is!
3524
})
3625
const componentProps = computed(() => {
37-
const _props: StringObject = mergeProps({ ref: componentRef }, attrs)
26+
const _props: StringObject = mergeProps({ ref: props._ref }, attrs)
3827

3928
if (isString(props.is) && nativeComponents.includes(props.is)) {
4029
if (
@@ -73,12 +62,6 @@ export default defineComponent({
7362
}
7463
})
7564

76-
onMounted(() => {
77-
Object.assign(componentExpose, componentRef.value)
78-
})
79-
80-
expose(componentExpose)
81-
8265
return () => h(type.value, componentProps.value, children.value)
8366
},
8467
})

src/Form/FormItem.ts

+3
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ export default defineComponent({
9797
mergeProps(props.item.props || {}, {
9898
...modelProps.value,
9999
is: props.item.component,
100+
// Replace ref with _ref to avoid binding to ProFormComponent
101+
ref: undefined,
102+
_ref: props.item.props?.ref,
100103
}),
101104
),
102105
)

src/Form/index.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ describe('ProFormComponent', () => {
270270
test.concurrent('expose', async () => {
271271
const wrapper = await mount({
272272
template:
273-
'<pro-form-component ref="inputRef" v-model="form" is="el-input" />',
273+
'<pro-form-component :_ref="(el) => inputRef = el" v-model="form" is="el-input" />',
274274
setup() {
275275
const form = ref()
276276
const inputRef = ref()

src/Form/props.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
isString,
1111
isNumber,
1212
} from '../utils/index'
13-
import type { Component, PropType } from 'vue'
13+
import type { Component, PropType, VNodeRef } from 'vue'
1414
import type { CollapseModelValue, TabPaneName } from 'element-plus'
1515
import type {
1616
ColumnPropsSlots,
@@ -57,6 +57,7 @@ export const formComponentProps = {
5757
type: [String, Object] as PropType<string | Component>,
5858
require: true,
5959
},
60+
_ref: [String, Object, Function] as PropType<VNodeRef>,
6061
slots: [Function, Object, String] as PropType<ColumnPropsSlots>,
6162
}
6263

0 commit comments

Comments
 (0)