Remove generic type from ComponentDecorator, fix #9331

This commit is contained in:
Wei Zhu 2018-02-22 15:52:35 +08:00
parent 2c95ea0e5e
commit dc439bd7c3
2 changed files with 29 additions and 4 deletions

View File

@ -117,10 +117,10 @@ export type Diff<T extends string, U extends string> =
({ [P in T]: P } & { [P in U]: never } & { [x: string]: never })[T];
export type Omit<T, K extends keyof T> = Pick<T, Diff<keyof T, K>>;
export interface ComponentDecorator<TOwnProps> {
export interface ComponentDecorator {
<P extends FormComponentProps>(
component: React.ComponentClass<P> | React.SFC<P>,
): React.ComponentClass<Omit<P, keyof FormComponentProps> & TOwnProps>;
): React.ComponentClass<Omit<P, keyof FormComponentProps>>;
}
export default class Form extends React.Component<FormProps, any> {
@ -149,7 +149,7 @@ export default class Form extends React.Component<FormProps, any> {
static createFormField = createFormField;
static create = function<TOwnProps>(options: FormCreateOption<TOwnProps> = {}): ComponentDecorator<TOwnProps> {
static create = function<TOwnProps>(options: FormCreateOption<TOwnProps> = {}): ComponentDecorator {
return createDOMForm({
fieldNameProp: 'id',
...options,

View File

@ -1,6 +1,6 @@
/* tslint:disable */
import * as React from 'react';
import Form, { FormComponentProps } from '../Form';
import Form, { FormComponentProps, FormCreateOption } from '../Form';
// test Form.create on component without own props
class WithoutOwnProps extends React.Component<any, any> {
@ -34,3 +34,28 @@ class WithOwnProps extends React.Component<WithOwnPropsProps, any> {
const WithOwnPropsForm = Form.create()(WithOwnProps);
<WithOwnPropsForm name="foo" />;
// test Form.create with options
interface WithCreateOptionsProps extends FormComponentProps {
username: string;
}
class WithCreateOptions extends React.Component<WithCreateOptionsProps, {}> {
render() {
return <div>foo</div>;
}
}
const mapPropsToFields = (props: WithCreateOptionsProps) => {
const { username } = props;
return {
username: Form.createFormField({ value: username })
};
};
const formOptions: FormCreateOption<WithCreateOptionsProps> = { mapPropsToFields };
const WithCreateOptionsForm = Form.create(formOptions)(WithCreateOptions);
<WithCreateOptionsForm username="foo" />