Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(form): scroll-to-first-error error in nested form items #2707

Merged
merged 1 commit into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions packages/web-vue/components/form/form-item.vue
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ import FormItemMessage from './form-item-message.vue';
import { getPrefixCls } from '../_utils/global-config';
import { getValueByPath, setValueByPath } from '../_utils/get-value-by-path';
import { Data } from '../_utils/types';
import { getFinalValidateMessage, getFinalValidateStatus } from './utils';
import {
getFinalValidateMessage,
getFinalValidateStatus,
getFormElementId,
} from './utils';
import { useI18n } from '../locale';

export default defineComponent({
Expand Down Expand Up @@ -353,8 +357,8 @@ export default defineComponent({
const colProps = {
...(props.wrapperColProps ?? formCtx.wrapperColProps),
};
if (mergedId.value) {
colProps.id = mergedId.value;
if (field.value) {
colProps.id = getFormElementId(formCtx.id, field.value);
}
if (props.labelColFlex || formCtx.autoLabelWidth) {
colProps.flex = 'auto';
Expand All @@ -368,12 +372,6 @@ export default defineComponent({
const mergedWrapperStyle = computed(
() => props.wrapperColStyle ?? formCtx.wrapperColStyle
);
const mergedId = computed(() => {
if (formCtx.id) {
return `${formCtx.id}_${field.value}`;
}
return field.value;
});

// 记录初始值,用于重置表单
const initialValue = getValueByPath(formCtx.model, props.field);
Expand Down
6 changes: 4 additions & 2 deletions packages/web-vue/components/form/form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { Size } from '../_utils/constant';
import { isArray, isFunction, isBoolean } from '../_utils/is';
import { FieldData, FieldRule, ValidatedError } from './interface';
import { useSize } from '../_hooks/use-size';
import { getFormElementId } from './utils';

const FORM_LAYOUTS = ['horizontal', 'vertical', 'inline'] as const;
type FormLayout = typeof FORM_LAYOUTS[number];
Expand Down Expand Up @@ -242,8 +243,9 @@ export default defineComponent({

const scrollToField = (field: string, options?: ScrollIntoViewOptions) => {
const node = formRef.value || document.body;
const fieldId = props.id ? `${props.id}_${field}` : field;
const fieldNode = node?.querySelector(`#${fieldId}`);
const fieldNode = node.querySelector(
`#${getFormElementId(props.id, field as string)}`
);

if (fieldNode) {
scrollIntoView(fieldNode as HTMLDivElement, {
Expand Down
5 changes: 5 additions & 0 deletions packages/web-vue/components/form/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,8 @@ export const getFinalValidateMessage = (
}
return messages;
};

export const getFormElementId = (prefix: string | undefined, field: string) => {
const id = (field as string).replace(/[[.]/g, '_').replace(/\]/g, '');
return prefix ? `${prefix}-${id}` : `${id}`;
};
Loading