Skip to content

Commit

Permalink
feat: Form support scrollToField (ant-design#17457)
Browse files Browse the repository at this point in the history
* support scroll & label htmlFor

* update snapshot

* add test case

* doc update

* add decleare

* adjust logic of label id

* clean ts error
  • Loading branch information
zombieJ authored Jul 4, 2019
1 parent 186f973 commit b3a76c1
Show file tree
Hide file tree
Showing 15 changed files with 242 additions and 8 deletions.
14 changes: 11 additions & 3 deletions components/form/Form.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import * as React from 'react';
import omit from 'omit.js';
import classNames from 'classnames';
import FieldForm, { FormInstance, useForm, List } from 'rc-field-form';
import FieldForm, { List } from 'rc-field-form';
import { FormProps as RcFormProps } from 'rc-field-form/lib/Form';
import { ColProps } from '../grid/col';
import { ConfigContext, ConfigConsumerProps } from '../config-provider';
import { FormContext } from './context';
import { FormLabelAlign } from './interface';
import { useForm, FormInstance } from './util';

export type FormLayout = 'horizontal' | 'inline' | 'vertical';

interface FormProps extends RcFormProps {
interface FormProps extends Omit<RcFormProps, 'form'> {
prefixCls?: string;
hideRequiredMark?: boolean;
colon?: boolean;
Expand All @@ -19,12 +20,14 @@ interface FormProps extends RcFormProps {
labelAlign?: FormLabelAlign;
labelCol?: ColProps;
wrapperCol?: ColProps;
form?: FormInstance;
}

const InternalForm: React.FC<FormProps> = (props, ref) => {
const { getPrefixCls }: ConfigConsumerProps = React.useContext(ConfigContext);

const {
form,
colon,
name,
labelAlign,
Expand Down Expand Up @@ -57,6 +60,11 @@ const InternalForm: React.FC<FormProps> = (props, ref) => {
'colon',
]);

const [wrapForm] = useForm(form);
wrapForm.__INTERNAL__.name = name;

React.useImperativeHandle(ref, () => wrapForm);

return (
<FormContext.Provider
value={{
Expand All @@ -68,7 +76,7 @@ const InternalForm: React.FC<FormProps> = (props, ref) => {
colon,
}}
>
<FieldForm id={name} {...formProps} ref={ref} className={formClassName} />
<FieldForm id={name} {...formProps} form={wrapForm} className={formClassName} />
</FormContext.Provider>
);
};
Expand Down
13 changes: 9 additions & 4 deletions components/form/FormItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import warning from '../_util/warning';
import FormItemLabel, { FormItemLabelProps } from './FormItemLabel';
import FormItemInput, { FormItemInputProps } from './FormItemInput';
import { FormContext, FormItemContext } from './context';
import { toArray } from './util';
import { toArray, getFieldId } from './util';

const ValidateStatuses = tuple('success', 'warning', 'error', 'validating', '');
export type ValidateStatus = (typeof ValidateStatuses)[number];
Expand Down Expand Up @@ -154,10 +154,10 @@ const FormItem: React.FC<FormItemProps> = (props: FormItemProps) => {
: !!(rules && rules.some(rule => typeof rule === 'object' && rule.required));

// ======================= Children =======================
const mergedId = mergedName.join('_');
const fieldId = getFieldId(mergedName, formName);
const mergedControl: typeof control = {
...control,
id: formName ? `${formName}_${mergedId}` : mergedId,
id: fieldId,
};

let childNode;
Expand Down Expand Up @@ -195,7 +195,12 @@ const FormItem: React.FC<FormItemProps> = (props: FormItemProps) => {
return (
<Row type="flex" className={classNames(itemClassName)} style={style} key="row">
{/* Label */}
<FormItemLabel {...props} required={isRequired} prefixCls={prefixCls} />
<FormItemLabel
htmlFor={fieldId}
{...props}
required={isRequired}
prefixCls={prefixCls}
/>
{/* Input Group */}
<FormItemInput
{...props}
Expand Down
2 changes: 1 addition & 1 deletion components/form/FormItemLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { FormContext, FormContextProps } from './context';

export interface FormItemLabelProps {
colon?: boolean;
htmlFor: string;
htmlFor?: string;
label?: React.ReactNode;
labelAlign?: FormLabelAlign;
labelCol?: ColProps;
Expand Down
Loading

0 comments on commit b3a76c1

Please sign in to comment.