Skip to content

Commit

Permalink
fix(Form): optimize ref bind for ProFormComponent (#555)
Browse files Browse the repository at this point in the history
  • Loading branch information
tolking authored Dec 20, 2024
1 parent 1eeda9b commit 20aca8d
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 22 deletions.
23 changes: 3 additions & 20 deletions src/Form/FormComponent.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
import {
computed,
defineComponent,
h,
mergeProps,
onMounted,
reactive,
ref,
resolveComponent,
} from 'vue'
import { computed, defineComponent, h, mergeProps, resolveComponent } from 'vue'
import { isFunction, isObject, isString } from '../utils/index'
import { formComponentProps } from './props'
import type { Slot } from 'vue'
Expand All @@ -23,18 +14,16 @@ interface TargetEvent {
export default defineComponent({
name: 'ProFormComponent',
props: formComponentProps,
setup(props, { attrs, emit, expose }) {
setup(props, { attrs, emit }) {
const nativeComponents = ['input', 'textarea', 'select']
const componentRef = ref<StringObject>({})
const componentExpose = reactive<StringObject>({})

const type = computed(() => {
return isString(props.is) && !nativeComponents.includes(props.is)
? resolveComponent(props.is)
: props.is!
})
const componentProps = computed(() => {
const _props: StringObject = mergeProps({ ref: componentRef }, attrs)
const _props: StringObject = mergeProps({ ref: props._ref }, attrs)

if (isString(props.is) && nativeComponents.includes(props.is)) {
if (
Expand Down Expand Up @@ -73,12 +62,6 @@ export default defineComponent({
}
})

onMounted(() => {
Object.assign(componentExpose, componentRef.value)
})

expose(componentExpose)

return () => h(type.value, componentProps.value, children.value)
},
})
3 changes: 3 additions & 0 deletions src/Form/FormItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ export default defineComponent({
mergeProps(props.item.props || {}, {
...modelProps.value,
is: props.item.component,
// Replace ref with _ref to avoid binding to ProFormComponent
ref: undefined,
_ref: props.item.props?.ref,
}),
),
)
Expand Down
2 changes: 1 addition & 1 deletion src/Form/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ describe('ProFormComponent', () => {
test.concurrent('expose', async () => {
const wrapper = await mount({
template:
'<pro-form-component ref="inputRef" v-model="form" is="el-input" />',
'<pro-form-component :_ref="(el) => inputRef = el" v-model="form" is="el-input" />',
setup() {
const form = ref()
const inputRef = ref()
Expand Down
3 changes: 2 additions & 1 deletion src/Form/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
isString,
isNumber,
} from '../utils/index'
import type { Component, PropType } from 'vue'
import type { Component, PropType, VNodeRef } from 'vue'
import type { CollapseModelValue, TabPaneName } from 'element-plus'
import type {
ColumnPropsSlots,
Expand Down Expand Up @@ -57,6 +57,7 @@ export const formComponentProps = {
type: [String, Object] as PropType<string | Component>,
require: true,
},
_ref: [String, Object, Function] as PropType<VNodeRef>,
slots: [Function, Object, String] as PropType<ColumnPropsSlots>,
}

Expand Down

0 comments on commit 20aca8d

Please sign in to comment.