From beb66996a4d85cf146e835d2afce19bc28518488 Mon Sep 17 00:00:00 2001 From: Jason Unger Date: Mon, 14 May 2018 17:41:17 -0400 Subject: [PATCH 1/4] Create definitions for react-radio-group --- types/react-radio-group/index.d.ts | 22 ++++++++++++++++ .../react-radio-group-tests.tsx | 24 ++++++++++++++++++ types/react-radio-group/tsconfig.json | 25 +++++++++++++++++++ types/react-radio-group/tslint.json | 1 + 4 files changed, 72 insertions(+) create mode 100644 types/react-radio-group/index.d.ts create mode 100644 types/react-radio-group/react-radio-group-tests.tsx create mode 100644 types/react-radio-group/tsconfig.json create mode 100644 types/react-radio-group/tslint.json diff --git a/types/react-radio-group/index.d.ts b/types/react-radio-group/index.d.ts new file mode 100644 index 0000000000..9628561b61 --- /dev/null +++ b/types/react-radio-group/index.d.ts @@ -0,0 +1,22 @@ +// Type definitions for react-radio-group 3.0 +// Project: https://github.com/chenglou/react-radio-group +// Definitions by: Jason Unger +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +import * as React from 'react'; + +export type Value = React.InputHTMLAttributes['value']; + +export type RadioProps = React.InputHTMLAttributes; + +export const Radio: React.ComponentClass; + +export interface RadioGroupProps { + name?: string; + selectedValue?: Value; + onChange?: (value: Value) => void; + Component?: string | React.ReactElement>; +} + +export const RadioGroup: React.ComponentClass; diff --git a/types/react-radio-group/react-radio-group-tests.tsx b/types/react-radio-group/react-radio-group-tests.tsx new file mode 100644 index 0000000000..65cd53fab3 --- /dev/null +++ b/types/react-radio-group/react-radio-group-tests.tsx @@ -0,0 +1,24 @@ +import * as React from 'react'; +import { Radio, RadioGroup, RadioGroupProps, Value } from "react-radio-group"; + +class ReactRadioGroup extends React.Component { + constructor(props: RadioGroupProps) { + super(props); + } + + handleChange(value: Value) { + console.log(value); + } + + render() { + return ( +
+ + + + + +
+ ); + } +} diff --git a/types/react-radio-group/tsconfig.json b/types/react-radio-group/tsconfig.json new file mode 100644 index 0000000000..615f55eee3 --- /dev/null +++ b/types/react-radio-group/tsconfig.json @@ -0,0 +1,25 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "jsx": "react" + }, + "files": [ + "index.d.ts", + "react-radio-group-tests.tsx" + ] +} diff --git a/types/react-radio-group/tslint.json b/types/react-radio-group/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/react-radio-group/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From bcdd5baf8bcecc5e2775fd638e423faf876eaaf5 Mon Sep 17 00:00:00 2001 From: Jason Unger Date: Mon, 14 May 2018 18:04:34 -0400 Subject: [PATCH 2/4] Use namespaces --- types/react-radio-group/index.d.ts | 24 ++++++++++--------- .../react-radio-group-tests.tsx | 10 +++----- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/types/react-radio-group/index.d.ts b/types/react-radio-group/index.d.ts index 9628561b61..3dfa872527 100644 --- a/types/react-radio-group/index.d.ts +++ b/types/react-radio-group/index.d.ts @@ -6,17 +6,19 @@ import * as React from 'react'; -export type Value = React.InputHTMLAttributes['value']; +type Value = React.InputHTMLAttributes['value']; -export type RadioProps = React.InputHTMLAttributes; - -export const Radio: React.ComponentClass; - -export interface RadioGroupProps { - name?: string; - selectedValue?: Value; - onChange?: (value: Value) => void; - Component?: string | React.ReactElement>; +export namespace Radio { + export type RadioProps = React.InputHTMLAttributes; } +export const Radio: React.ComponentClass; -export const RadioGroup: React.ComponentClass; +export namespace RadioGroup { + export interface RadioGroupProps { + name?: string; + selectedValue?: Value; + onChange?: (value: Value) => void; + Component?: string | React.ReactElement>; + } +} +export const RadioGroup: React.ComponentClass; diff --git a/types/react-radio-group/react-radio-group-tests.tsx b/types/react-radio-group/react-radio-group-tests.tsx index 65cd53fab3..76e093059b 100644 --- a/types/react-radio-group/react-radio-group-tests.tsx +++ b/types/react-radio-group/react-radio-group-tests.tsx @@ -1,12 +1,8 @@ import * as React from 'react'; -import { Radio, RadioGroup, RadioGroupProps, Value } from "react-radio-group"; +import { Radio, RadioGroup } from "react-radio-group"; -class ReactRadioGroup extends React.Component { - constructor(props: RadioGroupProps) { - super(props); - } - - handleChange(value: Value) { +class ReactRadioGroup extends React.Component { + handleChange: RadioGroup.RadioGroupProps['onChange'] = value => { console.log(value); } From d7407e11715519331531b202f25599c114a8c8f7 Mon Sep 17 00:00:00 2001 From: Jason Unger Date: Tue, 15 May 2018 09:50:10 -0400 Subject: [PATCH 3/4] Clean up type declarations --- types/react-radio-group/index.d.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/types/react-radio-group/index.d.ts b/types/react-radio-group/index.d.ts index 3dfa872527..5c3e6092ab 100644 --- a/types/react-radio-group/index.d.ts +++ b/types/react-radio-group/index.d.ts @@ -2,22 +2,20 @@ // Project: https://github.com/chenglou/react-radio-group // Definitions by: Jason Unger // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.3 +// TypeScript Version: 2.6 import * as React from 'react'; -type Value = React.InputHTMLAttributes['value']; - export namespace Radio { - export type RadioProps = React.InputHTMLAttributes; + type RadioProps = React.InputHTMLAttributes; } export const Radio: React.ComponentClass; export namespace RadioGroup { - export interface RadioGroupProps { + interface RadioGroupProps { name?: string; - selectedValue?: Value; - onChange?: (value: Value) => void; + selectedValue?: React.InputHTMLAttributes['value']; + onChange?: (value: React.InputHTMLAttributes['value']) => void; Component?: string | React.ReactElement>; } } From 63211a477b67d9ffbcf62613dc2ab7a41e14130a Mon Sep 17 00:00:00 2001 From: Jason Unger Date: Tue, 15 May 2018 09:51:32 -0400 Subject: [PATCH 4/4] Update test to set state on change --- types/react-radio-group/react-radio-group-tests.tsx | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/types/react-radio-group/react-radio-group-tests.tsx b/types/react-radio-group/react-radio-group-tests.tsx index 76e093059b..d34d7d05ff 100644 --- a/types/react-radio-group/react-radio-group-tests.tsx +++ b/types/react-radio-group/react-radio-group-tests.tsx @@ -1,15 +1,20 @@ import * as React from 'react'; import { Radio, RadioGroup } from "react-radio-group"; -class ReactRadioGroup extends React.Component { - handleChange: RadioGroup.RadioGroupProps['onChange'] = value => { - console.log(value); +class ReactRadioGroup extends React.Component['value'] }> { + state = { + selectedValue: 2, + }; + + handleChange: RadioGroup.RadioGroupProps['onChange'] = selectedValue => { + console.log(selectedValue); + this.setState({ selectedValue }); } render() { return (
- +