Skip to content

Commit

Permalink
fix: form type (ant-design#7245)
Browse files Browse the repository at this point in the history
  • Loading branch information
yesmeck authored and benjycui committed Aug 18, 2017
1 parent 723da5e commit 145ed77
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
components/**/*.js
components/**/*.jsx
components/*/__tests__/type.tsx
!.eslintrc.js
!components/*/__tests__/*
!components/*/__tests__/**/*.js
!components/*/demo/*
1 change: 1 addition & 0 deletions .jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module.exports = {
'!components/*/style/index.tsx',
'!components/style/index.tsx',
'!components/*/locale/index.tsx',
'!components/*/__tests__/**/type.tsx',
],
transformIgnorePatterns,
snapshotSerializers: [
Expand Down
9 changes: 7 additions & 2 deletions components/form/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,14 @@ export interface FormComponentProps {
form: WrappedFormUtils;
}

// https://github.com/DefinitelyTyped/DefinitelyTyped/issues/9951
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> {
(component: React.ComponentClass<FormComponentProps & TOwnProps>): React.ComponentClass<TOwnProps>;
<P extends FormComponentProps>(
component: React.ComponentClass<P>,
): React.ComponentClass<Omit<P, keyof FormComponentProps> & TOwnProps>;
}

export default class Form extends React.Component<FormProps, any> {
Expand Down
36 changes: 36 additions & 0 deletions components/form/__tests__/type.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* tslint:disable */
import React from 'react';
import Form, { FormComponentProps } from '../Form';

// test Form.create on component without own props
class WithoutOwnProps extends React.Component<any, any> {
state = {
foo: 'bar',
};
render() {
return <div>foo</div>;
}
}

const WithoutOwnPropsForm = Form.create()(WithoutOwnProps);

<WithoutOwnPropsForm />;

// test Form.create on component with own props
interface WithOwnPropsProps extends FormComponentProps {
name: string;
}

class WithOwnProps extends React.Component<WithOwnPropsProps, any> {
state = {
foo: 'bar',
};

render() {
return <div>foo</div>;
}
}

const WithOwnPropsForm = Form.create()(WithOwnProps);

<WithOwnPropsForm name="foo" />;

0 comments on commit 145ed77

Please sign in to comment.