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

4.0 Form scrollToField: Pass options to form scrollToField #20774

Merged
merged 3 commits into from Jan 14, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions components/form/__tests__/index.test.js
Expand Up @@ -188,8 +188,16 @@ describe('Form', () => {
mount(<Demo />, { attachTo: document.body });

expect(scrollIntoView).not.toHaveBeenCalled();
callGetForm().scrollToField('test');
expect(scrollIntoView).toHaveBeenCalled();
const form = callGetForm();
form.scrollToField('test', {
block: 'start',
});

const inputNode = document.getElementById('scroll_test');
expect(scrollIntoView).toHaveBeenCalledWith(inputNode, {
block: 'start',
scrollMode: 'if-needed',
});
});
}

Expand Down
2 changes: 1 addition & 1 deletion components/form/index.en-US.md
Expand Up @@ -183,7 +183,7 @@ Provide linkage between forms. If a sub form with `name` prop update, it will au
| isFieldsTouched | Check if fields have been operated. Check if all fields is touched when `allTouched` is `true` | (nameList?: [NamePath](#NamePath)[], allTouched?: boolean) => boolean |
| isFieldValidating | Check fields if is in validating | (name: [NamePath](#NamePath)) => boolean |
| resetFields | Reset fields to `initialValues` | (fields?: [NamePath](#NamePath)[]) => void |
| scrollToField | Scroll to field position | (name: [NamePath](#NamePath)) => void |
| scrollToField | Scroll to field position | (name: [NamePath](#NamePath), options: [[ScrollOptions](https://github.com/stipsan/scroll-into-view-if-needed/blob/ece40bd9143f48caf4b99503425ecb16b0ad8249/src/types.ts#L10)]) => void |
| setFields | Set fields status | (fields: FieldData[]) => void |
| setFieldsValue | Set fields value | (values) => void |
| submit | Submit the form. It's same as click `submit` button | () => void |
Expand Down
2 changes: 1 addition & 1 deletion components/form/index.zh-CN.md
Expand Up @@ -184,7 +184,7 @@ Form 通过增量更新方式,只更新被修改的字段相关组件以达到
| isFieldsTouched | 检查一组字段是否被用户操作过,`allTouched` 为 `true` 时检查是否所有字段都被操作过 | (nameList?: [NamePath](#NamePath)[], allTouched?: boolean) => boolean |
| isFieldValidating | 检查一组字段是否正在校验 | (name: [NamePath](#NamePath)) => boolean |
| resetFields | 重置一组字段到 `initialValues` | (fields?: [NamePath](#NamePath)[]) => void |
| scrollToField | 滚动到对应字段位置 | (name: [NamePath](#NamePath)) => void |
| scrollToField | 滚动到对应字段位置 | (name: [NamePath](#NamePath), options: [[ScrollOptions](https://github.com/stipsan/scroll-into-view-if-needed/blob/ece40bd9143f48caf4b99503425ecb16b0ad8249/src/types.ts#L10)]) => void |
| setFields | 设置一组字段状态 | (fields: FieldData[]) => void |
| setFieldsValue | 设置表单的值 | (values) => void |
| submit | 提交表单,与点击 `submit` 按钮效果相同 | () => void |
Expand Down
1 change: 1 addition & 0 deletions components/form/interface.ts
@@ -1 +1,2 @@
export { Options as ScrollOptions } from 'scroll-into-view-if-needed';
export type FormLabelAlign = 'left' | 'right';
6 changes: 4 additions & 2 deletions components/form/util.ts
@@ -1,6 +1,7 @@
import * as React from 'react';
import { useForm as useRcForm, FormInstance as RcFormInstance } from 'rc-field-form';
import scrollIntoView from 'scroll-into-view-if-needed';
import { ScrollOptions } from './interface';

type InternalNamePath = (string | number)[];

Expand Down Expand Up @@ -65,7 +66,7 @@ export function getFieldId(namePath: InternalNamePath, formName?: string): strin
}

export interface FormInstance extends RcFormInstance {
scrollToField: (name: string | number | InternalNamePath) => void;
scrollToField: (name: string | number | InternalNamePath, options?: ScrollOptions) => void;
__INTERNAL__: {
name?: string;
};
Expand All @@ -75,7 +76,7 @@ export function useForm(form?: FormInstance): [FormInstance] {
const wrapForm: FormInstance = form || {
...useRcForm()[0],
__INTERNAL__: {},
scrollToField: name => {
scrollToField: (name: string, options: ScrollOptions = {}) => {
const namePath = toArray(name);
const fieldId = getFieldId(namePath, wrapForm.__INTERNAL__.name);
const node: HTMLElement | null = fieldId ? document.getElementById(fieldId) : null;
Expand All @@ -84,6 +85,7 @@ export function useForm(form?: FormInstance): [FormInstance] {
scrollIntoView(node, {
scrollMode: 'if-needed',
block: 'nearest',
...options,
});
}
},
Expand Down