-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(vue): change Field component type to functional (#2773)
* refactor(vue): change Field component type to functional * feat(vue): add vue3 branch ignore test
- Loading branch information
1 parent
064e13a
commit ffbaba2
Showing
19 changed files
with
733 additions
and
686 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,53 @@ | ||
import { provide, defineComponent, watch, computed } from 'vue-demi' | ||
import { useField, useForm } from '../hooks' | ||
import { useAttach } from '../hooks/useAttach' | ||
import { isVue2, h as _h } from 'vue-demi' | ||
import ReactiveField from './ReactiveField' | ||
import { FieldSymbol } from '../shared/context' | ||
import h from '../shared/h' | ||
import { getRawComponent } from '../utils/getRawComponent' | ||
|
||
import type { IArrayFieldProps, DefineComponent } from '../types' | ||
import { getFieldProps } from '../utils/getFieldProps' | ||
|
||
export default defineComponent({ | ||
name: 'ArrayField', | ||
props: [ | ||
'name', | ||
'basePath', | ||
'title', | ||
'description', | ||
'value', | ||
'initialValue', | ||
'required', | ||
'display', | ||
'pattern', | ||
'hidden', | ||
'visible', | ||
'editable', | ||
'disabled', | ||
'readOnly', | ||
'readPretty', | ||
'dataSource', | ||
'validateFirst', | ||
'validator', | ||
'decorator', | ||
'component', | ||
'reactions', | ||
'content', | ||
'data', | ||
], | ||
setup(props: IArrayFieldProps, { slots }) { | ||
const formRef = useForm() | ||
const parentRef = useField() | ||
let ArrayField: DefineComponent<IArrayFieldProps> | ||
|
||
const basePath = computed(() => | ||
props.basePath !== undefined ? props.basePath : parentRef?.value?.address | ||
) | ||
const createField = () => | ||
formRef.value.createArrayField({ | ||
...props, | ||
basePath: basePath.value, | ||
...getRawComponent(props), | ||
}) | ||
const [fieldRef, checker] = useAttach(createField()) | ||
watch( | ||
() => props, | ||
() => (fieldRef.value = checker(createField())), | ||
{ deep: true } | ||
) | ||
watch([formRef, parentRef], () => (fieldRef.value = checker(createField()))) | ||
|
||
provide(FieldSymbol, fieldRef) | ||
|
||
return () => { | ||
const field = fieldRef.value | ||
/* istanbul ignore else */ | ||
if (isVue2) { | ||
ArrayField = { | ||
functional: true, | ||
name: 'ArrayField', | ||
props: getFieldProps(), | ||
render(h, context) { | ||
const props = context.props as IArrayFieldProps | ||
const attrs = context.data.attrs | ||
const componentData = { | ||
...context.data, | ||
props: { | ||
field, | ||
fieldType: 'ArrayField', | ||
fieldProps: { | ||
...attrs, | ||
...props, | ||
...getRawComponent(props), | ||
}, | ||
}, | ||
} | ||
const children = { | ||
...slots, | ||
return _h(ReactiveField, componentData, context.children) | ||
}, | ||
} as unknown as DefineComponent<IArrayFieldProps> | ||
} else { | ||
ArrayField = { | ||
name: 'ArrayField', | ||
props: getFieldProps(), | ||
setup(props: IArrayFieldProps, context) { | ||
return () => { | ||
const componentData = { | ||
fieldType: 'ArrayField', | ||
fieldProps: { | ||
...props, | ||
...getRawComponent(props), | ||
}, | ||
} as any | ||
const slots = context.slots as any | ||
return _h(ReactiveField, componentData, slots) | ||
} | ||
if (slots.default) { | ||
children.default = () => | ||
slots.default({ | ||
field: field, | ||
form: field.form, | ||
}) | ||
} | ||
return h(ReactiveField, componentData, children) | ||
} | ||
}, | ||
}) as DefineComponent<IArrayFieldProps> | ||
}, | ||
} as unknown as DefineComponent<IArrayFieldProps> | ||
} | ||
|
||
export default ArrayField |
Oops, something went wrong.