Skip to content

Commit 1e74c77

Browse files
committed
fix(packages/vue): 修复类型声明报错
1 parent 8cecdaa commit 1e74c77

5 files changed

Lines changed: 55 additions & 49 deletions

File tree

‎.changeset/six-buckets-heal.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@silver-formily/vue': patch
3+
---
4+
5+
修复类型声明错误

‎packages/vue/src/components/RecursionField.ts‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import type { GeneralField } from '@formily/core'
22
import type { Slots, VNode } from 'vue'
33
import type { IRecursionFieldProps, SchemaExpressionScope } from '../types'
4+
import type { RecursionFieldProps } from '../utils/recursionFieldProps'
5+
import type { NamedSlotMap } from '../utils/slotMap'
46
import { Schema } from '@formily/json-schema'
57
import { isFn, isValid, lazyMerge } from '@formily/shared'
68
import { computed, defineComponent, Fragment, h, inject, markRaw, provide, shallowRef, watch } from 'vue'
@@ -15,13 +17,13 @@ import VoidField from './VoidField'
1517

1618
type PropertyRenderFn = (field?: GeneralField) => VNode
1719
interface ScopedSlotPayload { field?: GeneralField }
18-
type SlotRender = (payload?: ScopedSlotPayload) => VNode[] | undefined
19-
type SlotMap = Record<string, SlotRender>
20+
type SlotRender = (payload?: ScopedSlotPayload) => VNode[]
21+
type SlotMap = NamedSlotMap<SlotRender>
2022

2123
function resolveEmptySlot(slots: SlotMap) {
2224
const slotKeys = Object.keys(slots)
2325
if (!slotKeys.length)
24-
return undefined
26+
return null
2527
const children = slotKeys.reduce<VNode[]>((acc, key) => {
2628
const slot = slots[key]
2729
if (!slot)
@@ -34,15 +36,15 @@ function resolveEmptySlot(slots: SlotMap) {
3436
return acc
3537
}, [])
3638
if (!children.length)
37-
return undefined
39+
return null
3840
return h(Fragment, null, children)
3941
}
4042

4143
const RecursionField = defineComponent({
4244
name: 'RecursionField',
4345
inheritAttrs: false,
4446
props: recursionFieldProps,
45-
setup(props: IRecursionFieldProps) {
47+
setup(props: RecursionFieldProps) {
4648
const parentRef = useField()
4749
const optionsRef = inject(SchemaOptionsSymbol)
4850
const scopeRef = inject(SchemaExpressionScopeSymbol)
@@ -52,7 +54,7 @@ const RecursionField = defineComponent({
5254
}
5355
const createSchema = (schemaProp: IRecursionFieldProps['schema']) =>
5456
markRaw(Schema.isSchemaInstance(schemaProp) ? schemaProp : new Schema(schemaProp))
55-
const fieldSchemaRef = computed(() => createSchema(props.schema))
57+
const fieldSchemaRef = computed<Schema>(() => createSchema(props.schema ?? {}))
5658

5759
const getPropsFromSchema = (schema: Schema) =>
5860
schema?.toFieldProps?.({
@@ -173,9 +175,6 @@ const RecursionField = defineComponent({
173175
})
174176
}
175177

176-
if (!fieldSchemaRef.value)
177-
return
178-
179178
return render()
180179
}
181180
},

‎packages/vue/src/components/SchemaField.ts‎

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import type { ISchema, SchemaTypes } from '@formily/json-schema'
2+
import type { VNode } from 'vue'
23
import type {
3-
ISchemaFieldProps,
44
ISchemaFieldVueFactoryOptions,
55
SchemaExpressionScope,
66
SchemaVueComponents,
77
} from '../types'
8-
import type { MarkupSchemaProps } from '../utils/schemaFieldProps'
98
import { Schema } from '@formily/json-schema'
109
import { lazyMerge } from '@formily/shared'
1110
import { computed, defineComponent, Fragment, h, inject, provide, shallowRef, watch } from 'vue'
@@ -14,6 +13,9 @@ import { resolveSchemaProps } from '../utils/resolveSchemaProps'
1413
import { markupSchemaProps, schemaFieldProps } from '../utils/schemaFieldProps'
1514
import RecursionField from './RecursionField'
1615

16+
type SchemaFieldProps = import('../utils/schemaFieldProps').SchemaFieldProps
17+
type MarkupSchemaProps = import('../utils/schemaFieldProps').MarkupSchemaProps
18+
1719
const env = {
1820
nonameId: 0,
1921
}
@@ -29,8 +31,8 @@ export function createSchemaField<Components extends SchemaVueComponents = Schem
2931
name: 'SchemaField',
3032
inheritAttrs: false,
3133
props: schemaFieldProps,
32-
setup(props: ISchemaFieldProps, { slots }) {
33-
const schemaRef = computed(() =>
34+
setup(props: SchemaFieldProps, { slots }) {
35+
const schemaRef = computed<Schema>(() =>
3436
Schema.isSchemaInstance(props.schema)
3537
? props.schema
3638
: new Schema({
@@ -43,7 +45,7 @@ export function createSchemaField<Components extends SchemaVueComponents = Schem
4345
lazyMerge({} as SchemaExpressionScope, options.scope ?? {}, props.scope ?? {}),
4446
)
4547

46-
const optionsRef = computed(() => ({
48+
const optionsRef = computed<ISchemaFieldVueFactoryOptions>(() => ({
4749
...options,
4850
components: {
4951
...options.components,
@@ -58,12 +60,7 @@ export function createSchemaField<Components extends SchemaVueComponents = Schem
5860
return () => {
5961
env.nonameId = 0
6062

61-
const slotContent = slots.default?.()
62-
const normalizedSlots = Array.isArray(slotContent)
63-
? slotContent
64-
: slotContent
65-
? [slotContent]
66-
: []
63+
const normalizedSlots = slots.default?.() ?? []
6764

6865
const recursionNode = h(RecursionField, {
6966
...props,
@@ -83,37 +80,40 @@ export function createSchemaField<Components extends SchemaVueComponents = Schem
8380
},
8481
setup(props: MarkupSchemaProps, { slots }) {
8582
const parentRef = inject(SchemaMarkupSymbol, null)
86-
if (!parentRef || !parentRef.value)
87-
return () => null
88-
89-
const name = props.name || getRandomName()
90-
const appendArraySchema = (schema: ISchema) => {
91-
if (parentRef.value.items) {
92-
return parentRef.value.addProperty(name, schema)
93-
}
94-
else {
95-
return parentRef.value.setItems(resolveSchemaProps(props))
96-
}
97-
}
98-
99-
const schemaRef = shallowRef()
83+
let render: () => VNode | null = () => null
10084

101-
watch(
102-
parentRef,
103-
() => {
104-
if (parentRef.value.type === 'object' || parentRef.value.type === 'void') {
105-
schemaRef.value = parentRef.value.addProperty(name, resolveSchemaProps(props))
85+
if (parentRef?.value) {
86+
const name = props.name || getRandomName()
87+
const appendArraySchema = (schema: ISchema) => {
88+
if (parentRef.value.items) {
89+
return parentRef.value.addProperty(name, schema)
10690
}
107-
else if (parentRef.value.type === 'array') {
108-
const schema = appendArraySchema(resolveSchemaProps(props))
109-
schemaRef.value = Array.isArray(schema) ? schema[0] : schema
91+
else {
92+
return parentRef.value.setItems(resolveSchemaProps(props))
11093
}
111-
},
112-
{ immediate: true, flush: 'sync' },
113-
)
114-
provide(SchemaMarkupSymbol, schemaRef)
94+
}
95+
96+
const schemaRef = shallowRef<Schema>(parentRef.value)
97+
98+
watch(
99+
parentRef,
100+
() => {
101+
if (parentRef.value.type === 'object' || parentRef.value.type === 'void') {
102+
schemaRef.value = parentRef.value.addProperty(name, resolveSchemaProps(props))
103+
}
104+
else if (parentRef.value.type === 'array') {
105+
const schema = appendArraySchema(resolveSchemaProps(props))
106+
schemaRef.value = Array.isArray(schema) ? schema[0] : schema
107+
}
108+
},
109+
{ immediate: true, flush: 'sync' },
110+
)
111+
provide(SchemaMarkupSymbol, schemaRef)
112+
113+
render = () => h(Fragment, null, slots.default?.() ?? [])
114+
}
115115

116-
return () => h(Fragment, null, slots.default?.() ?? [])
116+
return render
117117
},
118118
})
119119

‎packages/vue/src/utils/reactiveFieldHelpers.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { GeneralField } from '@formily/core'
22
import type { Component, Slot, Slots, VNode } from 'vue'
3+
import type { NamedSlotMap } from './slotMap'
34
import { createTextVNode, Fragment, h } from 'vue'
45

56
interface VueLikeOptions {
@@ -9,7 +10,7 @@ interface VueLikeOptions {
910
}
1011

1112
type SlotContent = string | Component | null | undefined
12-
type SlotContentMap = Record<string, SlotContent>
13+
type SlotContentMap = NamedSlotMap<SlotContent>
1314
type FieldContent = SlotContent | SlotContentMap | null | undefined
1415

1516
type EventHandler = (...args: unknown[]) => unknown
@@ -140,7 +141,7 @@ export function mergeSlots(field: GeneralField, slots: Slots, content: FieldCont
140141
return slots[slotName]?.({ field, form: field.form, ...scopedProps }) ?? []
141142
}
142143

143-
const patchedSlots: Record<string, Slot> = {}
144+
const patchedSlots: NamedSlotMap<Slot> = {}
144145
slotNames.forEach((name) => {
145146
patchedSlots[name] = patchSlot(name)
146147
})

‎packages/vue/src/utils/slotMap.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type NamedSlotMap<T> = Record<string, T>

0 commit comments

Comments
 (0)