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

refactor(form): scrollToFirstError supports passing in options #3918

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions components/form/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { defaultValidateMessages } from './utils/messages';
import { allPromiseFinish } from './utils/asyncUtil';
import { toArray } from './utils/typeUtil';
import isEqual from 'lodash-es/isEqual';
import scrollIntoView from 'scroll-into-view-if-needed';
import scrollIntoView, { Options } from 'scroll-into-view-if-needed';
import initDefaultProps from '../_util/props-util/initDefaultProps';
import { tuple, VueNode } from '../_util/type';
import { ColProps } from '../grid/Col';
Expand Down Expand Up @@ -56,7 +56,7 @@ export const formProps = {
validateMessages: PropTypes.object,
validateOnRuleChange: PropTypes.looseBool,
// 提交失败自动滚动到第一个错误字段
scrollToFirstError: PropTypes.looseBool,
scrollToFirstError: { type: [Boolean, Object] as PropType<boolean | Options> },
onSubmit: PropTypes.func,
onFinish: PropTypes.func,
onFinishFailed: PropTypes.func,
Expand Down Expand Up @@ -151,7 +151,11 @@ const Form = defineComponent({
const { scrollToFirstError } = this;
this.$emit('finishFailed', errorInfo);
if (scrollToFirstError && errorInfo.errorFields.length) {
this.scrollToField(errorInfo.errorFields[0].name);
let scrollToFieldOptions: Options = {};
if (typeof scrollToFirstError === 'object') {
scrollToFieldOptions = scrollToFirstError;
}
this.scrollToField(errorInfo.errorFields[0].name, scrollToFieldOptions);
}
},
validate(...args: any[]) {
Expand Down