From 0fb398c774d306bab90ab8b08c5f2a9be60beeef Mon Sep 17 00:00:00 2001 From: Vincent Siao Date: Mon, 8 Dec 2014 15:41:47 -0800 Subject: [PATCH] Add React.Children tests and change callback param return types from void to any --- react/react-tests.ts | 17 +++++++++++++++-- react/react.d.ts | 31 ++++++++++++++++++------------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/react/react-tests.ts b/react/react-tests.ts index c87137f8ef..ac7ca85fd0 100644 --- a/react/react-tests.ts +++ b/react/react-tests.ts @@ -119,12 +119,13 @@ myComponent.reset(); // Attributes // -------------------------------------------------------------------------- +var children = ["Hello world", [null], React.DOM.span(null)]; var divStyle = { // CSSProperties flex: "1 1 main-size", backgroundImage: "url('hello.png')" }; var htmlAttr = { - children: ["Hello world", [null], React.DOM.span(null)], + children: children, className: "test-attr", style: divStyle, onClick: (event: React.MouseEvent) => { @@ -140,7 +141,7 @@ React.DOM.span(htmlAttr); React.DOM.input(htmlAttr); // -// PropTypes +// React.PropTypes // -------------------------------------------------------------------------- var PropTypesSpecification: React.ComponentSpec = { @@ -180,6 +181,18 @@ var PropTypesSpecification: React.ComponentSpec = { } }; +// +// React.Children +// -------------------------------------------------------------------------- + +var childMap: { [key: string]: number } = + React.Children.map(children, (child) => { return 42; }); +React.Children.forEach(children, (child) => {}); +var nChildren: number = React.Children.count(children); +var onlyChild = React.Children.only([null, [[["Hallo"], true]], false, { + test: null +}]); + // // Example from http://facebook.github.io/react/ // -------------------------------------------------------------------------- diff --git a/react/react.d.ts b/react/react.d.ts index 849bd58f0c..65153341bb 100644 --- a/react/react.d.ts +++ b/react/react.d.ts @@ -26,7 +26,10 @@ declare module React { // type ReactText = string | number; // type Fragment = ReactNode[]; - // type ReactNode = ReactElement | Fragment | ReactText; + // type ReactNode = ReactElement | Fragment | ReactText | KeyMap; + // interface KeyMap { + // [key: string]: ReactNode; + // } // // React Components @@ -63,7 +66,7 @@ declare module React { createClass

(spec: ComponentSpec): ComponentClass

; createElement

(type: any/*ReactType*/, props: P, ...children: any/*ReactNode*/[]): ReactElement

; createFactory

(componentClass: ComponentClass

): ComponentFactory

; - render

(element: ReactElement

, container: Element, callback?: () => void): Component

; + render

(element: ReactElement

, container: Element, callback?: () => any): Component

; unmountComponentAtNode(container: Element): boolean; renderToString(element: ReactElement): string; renderToStaticMarkup(element: ReactElement): string; @@ -83,8 +86,8 @@ declare module React { isMounted(): boolean; props: P; - setProps(nextProps: P, callback?: () => void): void; - replaceProps(nextProps: P, callback?: () => void): void; + setProps(nextProps: P, callback?: () => any): void; + replaceProps(nextProps: P, callback?: () => any): void; } interface DOMComponent

extends Component

{ @@ -96,9 +99,9 @@ declare module React { interface CompositeComponent extends Component

, ComponentSpec { state: S; - setState(nextState: S, callback?: () => void): void; - replaceState(nextState: S, callback?: () => void): void; - forceUpdate(callback?: () => void): void; + setState(nextState: S, callback?: () => any): void; + replaceState(nextState: S, callback?: () => any): void; + forceUpdate(callback?: () => any): void; refs: { [key: string]: Component }; @@ -634,11 +637,13 @@ declare module React { // React.Children // ---------------------------------------------------------------------- + // type Child = ReactElement | ReactText; + interface ReactChildren { - map(children: any/*ReactNode*/, fn: (child: any/*ReactNode*/) => T): { [key:string]: T }; - forEach(children: any/*ReactNode*/, fn: (child: any/*ReactNode*/) => any): void; + map(children: any/*ReactNode*/, fn: (child: any/*Child*/) => T): { [key:string]: T }; + forEach(children: any/*ReactNode*/, fn: (child: any/*Child*/) => any): void; count(children: any/*ReactNode*/): number; - only(children: any/*ReactNode*/): any; + only(children: any/*ReactNode*/): any/*Child*/; } // @@ -865,9 +870,9 @@ declare module React { PureRenderMixin: PureRenderMixin; TransitionGroup: TransitionGroup; - batchedUpdates(callback: (a: A, b: B) => void, a: A, b: B): void; - batchedUpdates(callback: (a: A) => void, a: A): void; - batchedUpdates(callback: () => void): void; + batchedUpdates(callback: (a: A, b: B) => any, a: A, b: B): void; + batchedUpdates(callback: (a: A) => any, a: A): void; + batchedUpdates(callback: () => any): void; classSet(cx: { [key: string]: boolean }): string; cloneWithProps

(element: ReactElement

, props: P): ReactElement

;