From 59daf53c63d8485f3207e88bd5dcbf3b2ed9531b Mon Sep 17 00:00:00 2001 From: Alec Hill Date: Tue, 24 Oct 2017 13:41:09 +0000 Subject: [PATCH] Updated react-responsive types to v3.0 - also addresses issue https://github.com/DefinitelyTyped/DefinitelyTyped/issues/20656 --- types/react-responsive/index.d.ts | 143 ++++++++++-------- .../react-responsive-tests.tsx | 101 ++++++++++++- types/react-responsive/tsconfig.json | 7 +- types/react-responsive/tslint.json | 3 + types/react-responsive/v1/index.d.ts | 65 ++++++++ .../v1/react-responsive-tests.tsx | 42 +++++ types/react-responsive/v1/tsconfig.json | 33 ++++ 7 files changed, 328 insertions(+), 66 deletions(-) create mode 100644 types/react-responsive/tslint.json create mode 100644 types/react-responsive/v1/index.d.ts create mode 100644 types/react-responsive/v1/react-responsive-tests.tsx create mode 100644 types/react-responsive/v1/tsconfig.json diff --git a/types/react-responsive/index.d.ts b/types/react-responsive/index.d.ts index 2b9a37839f..dde02d8dae 100644 --- a/types/react-responsive/index.d.ts +++ b/types/react-responsive/index.d.ts @@ -1,65 +1,90 @@ -// Type definitions for react-responsive 1.1.3 +// Type definitions for react-responsive 3.0 // Project: https://github.com/contra/react-responsive // Definitions by: Alexey Svetliakov +// Alec Hill +// Javier Gonzalez // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.3 +// TypeScript Version: 2.4 -declare module "react-responsive" { - import * as React from "react"; - - namespace MediaQuery { - export interface MediaQueryProps { - query?: string; - // matchers - orientation?: "portrait" | "landscape"; - scan?: "progressive" | "interlace"; - aspectRatio?: string; - deviceAspectRatio?: string; - height?: number | string; - deviceHeight?: number | string; - width?: number | string; - deviceWidth?: number | string; - color?: boolean; - colorIndex?: boolean; - monochrome?: boolean; - resolution?: number | string; - // types - minAspectRatio?: string; - maxAspectRatio?: string; - minDeviceAspectRatio?: string; - maxDeviceAspectRatio?: string; - minHeight?: number | string; - maxHeight?: number | string; - minDeviceHeight?: number | string; - maxDeviceHeight?: number | string; - minDeviceWidth?: number | string; - maxDeviceWidth?: number | string; - minWidth?: number | string; - maxWidth?: number | string; - minColor?: number; - maxColor?: number; - minColorIndex?: number; - maxColorIndex?: number; - minMonochrome?: number; - maxMonochrome?: number; - minResolution?: number | string; - maxResolution?: number | string; - // types - all?: boolean; - grid?: boolean; - aural?: boolean; - braille?: boolean; - handheld?: boolean; - print?: boolean; - projection?: boolean; - screen?: boolean; - tty?: boolean; - tv?: boolean; - embossed?: boolean; - } - } - - class MediaQuery extends React.Component { } - export = MediaQuery; +import * as React from "react"; +export interface MediaQueryTypes { + all?: boolean; + grid?: boolean; + aural?: boolean; + braille?: boolean; + handheld?: boolean; + print?: boolean; + projection?: boolean; + screen?: boolean; + tty?: boolean; + tv?: boolean; + embossed?: boolean; } + +export type MediaQueryType = keyof MediaQueryTypes; + +export interface MediaQueryMatchers { + aspectRatio?: string; + deviceAspectRatio?: string; + height?: number | string; + deviceHeight?: number | string; + width?: number | string; + deviceWidth?: number | string; + color?: boolean; + colorIndex?: boolean; + monochrome?: boolean; + resolution?: number | string; + orientation?: 'portrait' | 'landscape'; + scan?: 'progressive' | 'interlace'; + type?: MediaQueryType; +} + +export interface MediaQueryFeatures extends MediaQueryMatchers { + minAspectRatio?: string; + maxAspectRatio?: string; + + minDeviceAspectRatio?: string; + maxDeviceAspectRatio?: string; + + minHeight?: number | string; + maxHeight?: number | string; + + minDeviceHeight?: number | string; + maxDeviceHeight?: number | string; + + minWidth?: number | string; + maxWidth?: number | string; + + minDeviceWidth?: number | string; + maxDeviceWidth?: number | string; + + minColor?: number; + maxColor?: number; + + minColorIndex?: number; + maxColorIndex?: number; + + minMonochrome?: number; + maxMonochrome?: number; + + minResolution?: number | string; + maxResolution?: number | string; +} + +export interface MediaQueryAllQueryable extends MediaQueryFeatures, MediaQueryTypes {} + +export interface MediaQueryProps extends MediaQueryAllQueryable { + component?: string | React.SFC | React.ClassType | React.ComponentClass; + query?: string; + style?: React.CSSProperties; + className?: string; + children?: React.ReactNode | ((matches: boolean) => React.ReactNode); + values?: Partial; + onBeforeChange?: (matches: boolean) => void; + onChange?: (matches: boolean) => void; +} + +declare class MediaQuery extends React.Component { } +export function toQuery(matchers: Partial): string; +export default MediaQuery; diff --git a/types/react-responsive/react-responsive-tests.tsx b/types/react-responsive/react-responsive-tests.tsx index 28f8f39c30..3445a8d1cf 100644 --- a/types/react-responsive/react-responsive-tests.tsx +++ b/types/react-responsive/react-responsive-tests.tsx @@ -1,7 +1,9 @@ import * as React from "react"; -import * as MediaQuery from "react-responsive"; +import MediaQuery, { + toQuery +} from "react-responsive"; -class Test extends React.Component { +class QueryTests extends React.Component { render() { return (
@@ -37,6 +39,99 @@ class Test extends React.Component {
- ) + ); } } + +const ChildrenPropTest: React.SFC = ({ children }) => ; + +class PropsTests extends React.Component { + render() { + return ( +
+ +
  • Item 1
  • +
  • Item 2
  • +
    + +
  • Item 1
  • +
  • Item 2
  • +
    + +
  • Item 1
  • +
  • Item 2
  • +
    + +
    Wrapped
    +
    Content
    +
    + + uppercase me! + + + Values supplied for SSR + +
    + ); + } +} + +class FunctionChildrenTest extends React.Component { + render() { + return ( + + {(matches: boolean) => { + if (matches) { + return
    Media query matches!
    ; + } else { + return
    Media query does not match!
    ; + } + }} +
    + ); + } +} + +class CallbackTest extends React.Component { + onBeforeChange = (matches: boolean): void => { + if (matches) { + // do something + } + } + + onChange = (matches: boolean): void => { + if (matches) { + // do something else + } + } + + render() { + return ( + + Media query matches! + + ); + } +} + +const queryStrTest: string = toQuery({ + type: 'print', + screen: true, + minHeight: 666 +}); diff --git a/types/react-responsive/tsconfig.json b/types/react-responsive/tsconfig.json index 61a49072b7..294f0c8cde 100644 --- a/types/react-responsive/tsconfig.json +++ b/types/react-responsive/tsconfig.json @@ -2,12 +2,11 @@ "compilerOptions": { "module": "commonjs", "lib": [ - "es6", - "dom" + "es6" ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "strictFunctionTypes": true, "baseUrl": "../", "typeRoots": [ @@ -22,4 +21,4 @@ "index.d.ts", "react-responsive-tests.tsx" ] -} \ No newline at end of file +} diff --git a/types/react-responsive/tslint.json b/types/react-responsive/tslint.json new file mode 100644 index 0000000000..f93cf8562a --- /dev/null +++ b/types/react-responsive/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +} diff --git a/types/react-responsive/v1/index.d.ts b/types/react-responsive/v1/index.d.ts new file mode 100644 index 0000000000..2b9a37839f --- /dev/null +++ b/types/react-responsive/v1/index.d.ts @@ -0,0 +1,65 @@ +// Type definitions for react-responsive 1.1.3 +// Project: https://github.com/contra/react-responsive +// Definitions by: Alexey Svetliakov +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +declare module "react-responsive" { + import * as React from "react"; + + namespace MediaQuery { + export interface MediaQueryProps { + query?: string; + // matchers + orientation?: "portrait" | "landscape"; + scan?: "progressive" | "interlace"; + aspectRatio?: string; + deviceAspectRatio?: string; + height?: number | string; + deviceHeight?: number | string; + width?: number | string; + deviceWidth?: number | string; + color?: boolean; + colorIndex?: boolean; + monochrome?: boolean; + resolution?: number | string; + // types + minAspectRatio?: string; + maxAspectRatio?: string; + minDeviceAspectRatio?: string; + maxDeviceAspectRatio?: string; + minHeight?: number | string; + maxHeight?: number | string; + minDeviceHeight?: number | string; + maxDeviceHeight?: number | string; + minDeviceWidth?: number | string; + maxDeviceWidth?: number | string; + minWidth?: number | string; + maxWidth?: number | string; + minColor?: number; + maxColor?: number; + minColorIndex?: number; + maxColorIndex?: number; + minMonochrome?: number; + maxMonochrome?: number; + minResolution?: number | string; + maxResolution?: number | string; + // types + all?: boolean; + grid?: boolean; + aural?: boolean; + braille?: boolean; + handheld?: boolean; + print?: boolean; + projection?: boolean; + screen?: boolean; + tty?: boolean; + tv?: boolean; + embossed?: boolean; + } + } + + class MediaQuery extends React.Component { } + export = MediaQuery; + +} diff --git a/types/react-responsive/v1/react-responsive-tests.tsx b/types/react-responsive/v1/react-responsive-tests.tsx new file mode 100644 index 0000000000..28f8f39c30 --- /dev/null +++ b/types/react-responsive/v1/react-responsive-tests.tsx @@ -0,0 +1,42 @@ +import * as React from "react"; +import * as MediaQuery from "react-responsive"; + +class Test extends React.Component { + render() { + return ( +
    +
    Device Test!
    + +
    You are a desktop or laptop
    + +
    You also have a huge screen
    +
    + +
    You are sized like a tablet or mobile phone though
    +
    +
    + +
    You are a tablet or mobile phone
    +
    + +
    You are portrait
    +
    + +
    You are landscape
    +
    + +
    You are retina
    +
    + +
    You are a desktop or laptop
    + +
    You also have a huge screen
    +
    + +
    You are sized like a tablet or mobile phone though
    +
    +
    +
    + ) + } +} diff --git a/types/react-responsive/v1/tsconfig.json b/types/react-responsive/v1/tsconfig.json new file mode 100644 index 0000000000..774fd47a00 --- /dev/null +++ b/types/react-responsive/v1/tsconfig.json @@ -0,0 +1,33 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": false, + "strictFunctionTypes": true, + "baseUrl": "../../", + "typeRoots": [ + "../../" + ], + "types": [], + "paths": { + "react-responsive": [ + "react-responsive/v1" + ], + "react-responsive/*": [ + "react-responsive/v1/*" + ] + }, + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "jsx": "react" + }, + "files": [ + "index.d.ts", + "react-responsive-tests.tsx" + ] +}