mirror of
https://github.com/element-plus/element-plus.git
synced 2024-12-21 02:50:11 +08:00
feat(components): form add scrollToField method (#3110)
* feat(form): add scrollToField function * feat(components): complete scrollToField function Co-authored-by: linjie <linjie@zhuanzhuan.com>
This commit is contained in:
parent
629d98edf8
commit
8f67fb7645
@ -1,5 +1,6 @@
|
|||||||
<template>
|
<template>
|
||||||
<div
|
<div
|
||||||
|
ref="formItemRef"
|
||||||
class="el-form-item"
|
class="el-form-item"
|
||||||
:class="formItemClass"
|
:class="formItemClass"
|
||||||
>
|
>
|
||||||
@ -116,6 +117,8 @@ export default defineComponent({
|
|||||||
|
|
||||||
const computedLabelWidth = ref('')
|
const computedLabelWidth = ref('')
|
||||||
|
|
||||||
|
const formItemRef = ref<HTMLDivElement>()
|
||||||
|
|
||||||
const vm = getCurrentInstance()
|
const vm = getCurrentInstance()
|
||||||
const isNested = computed(() => {
|
const isNested = computed(() => {
|
||||||
let parent = vm.parent
|
let parent = vm.parent
|
||||||
@ -200,7 +203,7 @@ export default defineComponent({
|
|||||||
return required
|
return required
|
||||||
})
|
})
|
||||||
const elFormItemSize = computed(() => props.size || elForm.size)
|
const elFormItemSize = computed(() => props.size || elForm.size)
|
||||||
const sizeClass = computed(() => {
|
const sizeClass = computed<ComponentSize>(() => {
|
||||||
return elFormItemSize.value || $ELEMENT.size
|
return elFormItemSize.value || $ELEMENT.size
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -327,12 +330,13 @@ export default defineComponent({
|
|||||||
...toRefs(props),
|
...toRefs(props),
|
||||||
size: sizeClass,
|
size: sizeClass,
|
||||||
validateState,
|
validateState,
|
||||||
|
$el: formItemRef,
|
||||||
|
formItemMitt,
|
||||||
removeValidateEvents,
|
removeValidateEvents,
|
||||||
addValidateEvents,
|
addValidateEvents,
|
||||||
resetField,
|
resetField,
|
||||||
clearValidate,
|
clearValidate,
|
||||||
validate,
|
validate,
|
||||||
formItemMitt,
|
|
||||||
updateComputedLabelWidth,
|
updateComputedLabelWidth,
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -370,6 +374,7 @@ export default defineComponent({
|
|||||||
})
|
})
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
formItemRef,
|
||||||
formItemClass,
|
formItemClass,
|
||||||
shouldShowError,
|
shouldShowError,
|
||||||
elForm,
|
elForm,
|
||||||
|
@ -93,6 +93,7 @@ export default defineComponent({
|
|||||||
type: Boolean,
|
type: Boolean,
|
||||||
default: false,
|
default: false,
|
||||||
},
|
},
|
||||||
|
scrollToError: Boolean,
|
||||||
},
|
},
|
||||||
emits: ['validate'],
|
emits: ['validate'],
|
||||||
setup(props, { emit }) {
|
setup(props, { emit }) {
|
||||||
@ -177,10 +178,12 @@ export default defineComponent({
|
|||||||
let valid = true
|
let valid = true
|
||||||
let count = 0
|
let count = 0
|
||||||
let invalidFields = {}
|
let invalidFields = {}
|
||||||
|
let firstInvalidFields
|
||||||
for (const field of fields) {
|
for (const field of fields) {
|
||||||
field.validate('', (message, field) => {
|
field.validate('', (message, field) => {
|
||||||
if (message) {
|
if (message) {
|
||||||
valid = false
|
valid = false
|
||||||
|
firstInvalidFields || (firstInvalidFields = field)
|
||||||
}
|
}
|
||||||
invalidFields = { ...invalidFields, ...field }
|
invalidFields = { ...invalidFields, ...field }
|
||||||
if (++count === fields.length) {
|
if (++count === fields.length) {
|
||||||
@ -188,6 +191,9 @@ export default defineComponent({
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
if (!valid && props.scrollToError) {
|
||||||
|
scrollToField(Object.keys(firstInvalidFields)[0])
|
||||||
|
}
|
||||||
return promise
|
return promise
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -204,6 +210,14 @@ export default defineComponent({
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const scrollToField = (prop: string) => {
|
||||||
|
fields.forEach(item => {
|
||||||
|
if (item.prop === prop) {
|
||||||
|
item.$el.scrollIntoView()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
const elForm = reactive({
|
const elForm = reactive({
|
||||||
formMitt,
|
formMitt,
|
||||||
...toRefs(props),
|
...toRefs(props),
|
||||||
@ -221,6 +235,7 @@ export default defineComponent({
|
|||||||
resetFields,
|
resetFields,
|
||||||
clearValidate,
|
clearValidate,
|
||||||
validateField,
|
validateField,
|
||||||
|
scrollToField,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
@ -34,6 +34,7 @@ export interface ElFormItemContext {
|
|||||||
formItemMitt: Emitter
|
formItemMitt: Emitter
|
||||||
size?: ComponentSize
|
size?: ComponentSize
|
||||||
validateState: string
|
validateState: string
|
||||||
|
$el: HTMLDivElement
|
||||||
validate(trigger: string, callback?: ValidateFieldCallback): void
|
validate(trigger: string, callback?: ValidateFieldCallback): void
|
||||||
updateComputedLabelWidth(width: number): void
|
updateComputedLabelWidth(width: number): void
|
||||||
addValidateEvents(): void
|
addValidateEvents(): void
|
||||||
|
Loading…
Reference in New Issue
Block a user