Update for React 16 (#18463)

This commit is contained in:
Rich Seviora
2017-07-31 14:58:27 -07:00
committed by Sheetal Nandi
parent 45bcdda85e
commit 178dd15e5e
8 changed files with 4510 additions and 61 deletions
-60
View File
@@ -1,60 +0,0 @@
## Known Problems & Workarounds
### **The type of `cloneElement` is incorrect.**
`cloneElement(element, props)` should should accept a `props` object with a subset of the properties on `element.props`. React attributes such as `key` and `ref` should also be accepted in `props`, but should not exist on `element.props`. The "correct" way to model this, then, is with
```ts
declare function cloneElement<P extends Q, Q>(
element: ReactElement<P>,
props?: Q & Attributes,
...children: ReactNode[]): ReactElement<P>;
```
However, type inference for `Q` defaults to `{}` when [intersected with another type](https://github.com/Microsoft/TypeScript/pull/5738#issuecomment-181904905). And since any object is assignable to `{}`, we would lose the type safety of the `P extends Q` constraint. Therefore, the type of `props` is left as `Q`, which should work for most cases. If you need to call `cloneElement` with `key` or `ref`, you'll need a type cast:
```ts
interface ButtonProps {
label: string,
isDisabled?: boolean;
}
var element: React.CElement<ButtonProps, Button>;
React.cloneElement(element, { label: "label" });
// cloning with optional props requires a cast
React.cloneElement(element, <{ isDisabled?: boolean }>{ isDisabled: true });
// cloning with key or ref requires a cast
React.cloneElement(element, <React.ClassAttributes<Button>>{ ref: button => button.reset() });
React.cloneElement(element, <{ isDisabled?: boolean } & React.Attributes>{
key: "disabledButton",
isDisabled: true
});
```
### **`React.Component<P, S>` subclass members aren't contextually typed.**
This problem manifests itself in two ways. It should be fixed in [TypeScript 2.0](https://github.com/Microsoft/TypeScript/pull/6118).
- You might expect `componentDidUpdate(prevProps, prevState)` to have `prevProps` and `prevState` contextually typed as `P` and `S` respectively, but currently that is not the case. You must explicitly type-annotate both arguments.
- You may get a cryptic error message when either `React.createElement` or `React.createFactory` doesn't recognize the `type` argument that you passed in as a valid `React.Component` subclass, because you overrode one of the static or instance members with a type that's not compatible with the superclass property's type. For example, with the following code:
```ts
import * as React from "react";
class MyComponent extends React.Component<Props> {
static contextTypes = {
someValue: React.PropTypes.string
};
}
React.createFactory(MyComponent);
```
you might get this error:
```
error TS2345: Argument of type 'typeof MyComponent' is not assignable to parameter of type 'string | ComponentClass<Props> | StatelessComponent<Props>'
Type 'typeof ModernComponent' is not assignable to type 'StatelessComponent<Props>'.
Types of property 'contextTypes' are incompatible.
Type '{ someValue: Requireable<any>; }' is not assignable to type 'ValidationMap<any>'.
Index signature is missing in type '{ someValue: Requireable<any>; }'.
```
The work around is to add an explicit type annotation:
```ts
static contextTypes: React.ValidationMap<any> = ...
```
+48 -1
View File
@@ -1,4 +1,4 @@
// Type definitions for React 15.6
// Type definitions for React 16.0
// Project: http://facebook.github.io/react/
// Definitions by: Asana <https://asana.com>
// AssureSign <http://www.assuresign.com>
@@ -12,9 +12,41 @@
// Tanguy Krotoff <https://github.com/tkrotoff>
// Dovydas Navickas <https://github.com/DovydasNavickas>
// Stéphane Goetz <https://github.com/onigoetz>
// Rich Seviora <https://github.com/richseviora>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
/*
Known Problems & Workarounds
1. The type of cloneElement is incorrect.
cloneElement(element, props) should accept props object with a subset of properties on element.props.
React attributes, such as key and ref, should also be accepted in props, but should not exist on element.props.
The "correct" way to model this, then, is with:
declare function cloneElement<P extends Q, Q>(
element: ReactElement<P>,
props?: Q & Attributes,
...children: ReactNode[]): ReactElement<P>;
However, type inference for Q defaults to {} when intersected with another type.
(https://github.com/Microsoft/TypeScript/pull/5738#issuecomment-181904905)
And since any object is assignable to {}, we would lose the type safety of the P extends Q constraint.
Therefore, the type of props is left as Q, which should work for most cases.
If you need to call cloneElement with key or ref, you'll need a type cast:
interface ButtonProps {
label: string,
isDisabled?: boolean;
}
var element: React.CElement<ButtonProps, Button>;
React.cloneElement(element, { label: "label" });
// cloning with optional props requires a cast
React.cloneElement(element, <{ isDisabled?: boolean }>{ isDisabled: true });
// cloning with key or ref requires a cast
React.cloneElement(element, <React.ClassAttributes<Button>>{ ref: button => button.reset() });
React.cloneElement(element, <{ isDisabled?: boolean } & React.Attributes>{
key: "disabledButton",
isDisabled: true
});
*/
type NativeAnimationEvent = AnimationEvent;
type NativeClipboardEvent = ClipboardEvent;
type NativeCompositionEvent = CompositionEvent;
@@ -321,6 +353,11 @@ declare namespace React {
componentWillUpdate?(nextProps: Readonly<P>, nextState: Readonly<S>, nextContext: any): void;
componentDidUpdate?(prevProps: Readonly<P>, prevState: Readonly<S>, prevContext: any): void;
componentWillUnmount?(): void;
/**
* Catches exceptions generated in descendant components. Unhandled exceptions will cause
* the entire component tree to unmount.
*/
componentDidCatch?(error: Error, errorInfo: ErrorInfo): void;
}
interface Mixin<P, S> extends ComponentLifecycle<P, S> {
@@ -3354,6 +3391,16 @@ declare namespace React {
item(index: number): Touch;
identifiedTouch(identifier: number): Touch;
}
//
// Error Interfaces
// ----------------------------------------------------------------------
interface ErrorInfo {
/**
* Captures which component contained the exception, and it's ancestors.
*/
componentStack: string;
}
}
declare global {
+3587
View File
File diff suppressed because it is too large Load Diff
+45
View File
@@ -0,0 +1,45 @@
import * as React from 'react';
const initialStyle: React.CSSProperties = { fontWeight: 'initial' };
const initialStyleTest = <div style={initialStyle} />;
const backgroundAttachmentStyle: React.CSSProperties = { backgroundAttachment: 'fixed' };
const backgroundAttachmentStyleTest = <div style={backgroundAttachmentStyle} />;
const columnCountStyle: React.CSSProperties = { columnCount: 'auto' };
const columnCountStyleTest = <div style={columnCountStyle} />;
const fontSizeAdjustStyle: React.CSSProperties = { fontSizeAdjust: 'none' };
const fontSizeAdjustStyleTest = <div style={fontSizeAdjustStyle} />;
const fontStretchStyle: React.CSSProperties = { fontStretch: 'condensed' };
const fontStretchStyleTest = <div style={fontStretchStyle} />;
const fontStyleStyle: React.CSSProperties = { fontStyle: 'italic' };
const fontStyleStyleTest = <div style={fontStyleStyle} />;
const fontWeightStyle: React.CSSProperties = { fontWeight: 400 };
const fontWeightStyleTest = <div style={fontWeightStyle} />;
const justifyContentStyle: React.CSSProperties = { justifyContent: 'space-evenly' };
const justifyContentStyleTest = <div style={justifyContentStyle} />;
const boxShadowStyle: React.CSSProperties = { boxShadow: '2px 2px 2px 1px rgba(0, 0, 0, 0.2)' };
const boxShadowStyleTest = <div style={boxShadowStyle} />;
const overflowStyle: React.CSSProperties = { overflow: 'auto', overflowX: 'visible', overflowY: 'scroll' };
const overflowStyleTest = <div style={overflowStyle} />;
const positionStyle: React.CSSProperties = { position: 'relative' };
const positionStyleTest = <div style={positionStyle} />;
// SVG specific style attribute declarations
const fillOpacityStyle: React.CSSProperties = { fillOpacity: 0.3 };
const fillOpacityStyleTest = <svg style={fillOpacityStyle} />;
const strokeOpacityStyle: React.CSSProperties = { strokeOpacity: 0.3 };
const strokeOpacityStyleTest = <svg style={strokeOpacityStyle} />;
const strokeWidthStyle: React.CSSProperties = { strokeWidth: '10px' };
const strokeWidthStyleTest = <svg style={strokeWidthStyle} />;
+733
View File
@@ -0,0 +1,733 @@
import * as React from "react";
import * as ReactDOM from "react-dom";
import * as ReactDOMServer from "react-dom/server";
import createFragment = require("react-addons-create-fragment");
import * as CSSTransitionGroup from "react-addons-css-transition-group";
import * as LinkedStateMixin from "react-addons-linked-state-mixin";
import * as Perf from "react-addons-perf";
import * as PureRenderMixin from "react-addons-pure-render-mixin";
import * as shallowCompare from "react-addons-shallow-compare";
import * as TestUtils from "react-addons-test-utils";
import * as TransitionGroup from "react-addons-transition-group";
import update = require("react-addons-update");
interface Props extends React.Attributes {
hello: string;
world?: string;
foo: number;
}
interface State {
inputValue?: string;
seconds?: number;
}
interface Context {
someValue?: string;
}
interface ChildContext {
someOtherValue: string;
}
interface MyComponent extends React.Component<Props, State> {
reset(): void;
}
const props: Props & React.ClassAttributes<{}> = {
key: 42,
ref: "myComponent42",
hello: "world",
foo: 42
};
const container: Element = document.createElement("div");
//
// Top-Level API
// --------------------------------------------------------------------------
const ClassicComponent: React.ClassicComponentClass<Props> =
React.createClass<Props, State>({
displayName: "ClassicComponent",
getDefaultProps() {
return {
hello: "hello",
world: "peace",
foo: 0,
};
},
getInitialState() {
return {
inputValue: this.context.someValue,
seconds: this.props.foo
};
},
reset() {
this.replaceState(this.getInitialState());
},
render() {
return React.DOM.div(null,
React.DOM.input({
ref: input => this._input = input,
value: this.state.inputValue
}));
}
});
class ModernComponent extends React.Component<Props, State>
implements MyComponent, React.ChildContextProvider<ChildContext> {
static propTypes: React.ValidationMap<Props> = {
foo: React.PropTypes.number
};
static contextTypes: React.ValidationMap<Context> = {
someValue: React.PropTypes.string
};
static childContextTypes: React.ValidationMap<ChildContext> = {
someOtherValue: React.PropTypes.string
};
context: Context;
getChildContext() {
return {
someOtherValue: "foo"
};
}
state = {
inputValue: this.context.someValue,
seconds: this.props.foo
};
reset() {
this._myComponent.reset();
this.setState({
inputValue: this.context.someValue,
seconds: this.props.foo
});
}
private _myComponent: MyComponent;
private _input: HTMLInputElement | null;
render() {
return React.DOM.div(null,
React.DOM.input({
ref: input => this._input = input,
value: this.state.inputValue
}),
React.DOM.input({
onChange: event => console.log(event.target)
}));
}
shouldComponentUpdate(nextProps: Props, nextState: State, nextContext: any): boolean {
return shallowCompare(this, nextProps, nextState);
}
}
class ModernComponentNoState extends React.Component<Props> { }
class ModernComponentNoPropsAndState extends React.Component { }
interface SCProps {
foo?: number;
}
function StatelessComponent(props: SCProps) {
return props.foo ? React.DOM.div(null, props.foo) : null;
}
// tslint:disable-next-line:no-namespace
namespace StatelessComponent {
export const displayName = "StatelessComponent";
export const defaultProps = { foo: 42 };
}
const StatelessComponent2: React.SFC<SCProps> =
// props is contextually typed
props => React.DOM.div(null, props.foo);
StatelessComponent2.displayName = "StatelessComponent2";
StatelessComponent2.defaultProps = {
foo: 42
};
const StatelessComponent3: React.SFC<SCProps> =
// allows usage of props.children
// allows null return
props => props.foo ? React.DOM.div(null, props.foo, props.children) : null;
// React.createFactory
const factory: React.CFactory<Props, ModernComponent> =
React.createFactory(ModernComponent);
const factoryElement: React.CElement<Props, ModernComponent> =
factory(props);
const statelessFactory: React.SFCFactory<SCProps> =
React.createFactory(StatelessComponent);
const statelessFactoryElement: React.SFCElement<SCProps> =
statelessFactory(props);
const classicFactory: React.ClassicFactory<Props> =
React.createFactory(ClassicComponent);
const classicFactoryElement: React.ClassicElement<Props> =
classicFactory(props);
const domFactory: React.DOMFactory<React.DOMAttributes<{}>, Element> =
React.createFactory("div");
const domFactoryElement: React.DOMElement<React.DOMAttributes<{}>, Element> =
domFactory();
// React.createElement
const element: React.CElement<Props, ModernComponent> = React.createElement(ModernComponent, props);
const elementNoState: React.CElement<Props, ModernComponentNoState> = React.createElement(ModernComponentNoState, props);
const statelessElement: React.SFCElement<SCProps> = React.createElement(StatelessComponent, props);
const classicElement: React.ClassicElement<Props> = React.createElement(ClassicComponent, props);
const domElement: React.DOMElement<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement> = React.createElement("div");
const htmlElement = React.createElement("input", { type: "text" });
const svgElement = React.createElement("svg", { accentHeight: 12 });
let customProps: React.HTMLProps<HTMLElement> = props;
let customDomElement: string = "my-element";
const nonLiteralElement = React.createElement(customDomElement, customProps);
// React.cloneElement
const clonedElement: React.CElement<Props, ModernComponent> = React.cloneElement(element, { foo: 43 });
React.cloneElement(element, {});
React.cloneElement(element, {}, null);
const clonedElement2: React.CElement<Props, ModernComponent> =
// known problem: cloning with key or ref requires cast
React.cloneElement(element, {
ref: c => c && c.reset()
} as React.ClassAttributes<ModernComponent>);
const clonedElement3: React.CElement<Props, ModernComponent> =
React.cloneElement(element, {
key: "8eac7",
foo: 55
} as { foo: number } & React.Attributes);
const clonedStatelessElement: React.SFCElement<SCProps> =
// known problem: cloning with optional props don't work properly
// workaround: cast to actual props type
React.cloneElement(statelessElement, { foo: 44 } as SCProps);
const clonedClassicElement: React.ClassicElement<Props> =
React.cloneElement(classicElement, props);
// Clone base DOMElement
const clonedDOMElement: React.DOMElement<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement> =
React.cloneElement(domElement, {
className: "clonedDOMElement"
});
// Clone ReactHTMLElement
const clonedHtmlElement: React.ReactHTMLElement<HTMLDivElement> =
React.cloneElement(htmlElement, {
className: "clonedHTMLElement"
});
// Clone ReactSVGElement
const clonedSvgElement: React.ReactSVGElement =
React.cloneElement(svgElement, {
className: "clonedVGElement"
});
// React.render
const component: ModernComponent = ReactDOM.render(element, container);
const componentNullContainer: ModernComponent = ReactDOM.render(element, null);
const componentElementOrNull: ModernComponent = ReactDOM.render(element, document.getElementById("anelement"));
const componentNoState: ModernComponentNoState = ReactDOM.render(elementNoState, container);
const componentNoStateElementOrNull: ModernComponentNoState = ReactDOM.render(elementNoState, document.getElementById("anelement"));
const classicComponent: React.ClassicComponent<Props> = ReactDOM.render(classicElement, container);
const domComponent: Element = ReactDOM.render(domElement, container);
// Other Top-Level API
const unmounted: boolean = ReactDOM.unmountComponentAtNode(container);
const str: string = ReactDOMServer.renderToString(element);
const markup: string = ReactDOMServer.renderToStaticMarkup(element);
const notValid: boolean = React.isValidElement(props); // false
const isValid = React.isValidElement(element); // true
let domNode: Element = ReactDOM.findDOMNode(component);
domNode = ReactDOM.findDOMNode(domNode);
//
// React Elements
// --------------------------------------------------------------------------
const type: React.ComponentClass<Props> = element.type;
const elementProps: Props = element.props;
const key = element.key;
//
// React Components
// --------------------------------------------------------------------------
const displayName: string | undefined = ClassicComponent.displayName;
const defaultProps: Props = ClassicComponent.getDefaultProps ? ClassicComponent.getDefaultProps() : {} as Props;
const propTypes: React.ValidationMap<Props> | undefined = ClassicComponent.propTypes;
//
// Component API
// --------------------------------------------------------------------------
// modern
const componentState: State = component.state;
component.setState({ inputValue: "!!!" });
component.forceUpdate();
// classic
const isMounted: boolean = classicComponent.isMounted();
classicComponent.replaceState({ inputValue: "???", seconds: 60 });
const myComponent = component as MyComponent;
myComponent.reset();
//
// Refs
// --------------------------------------------------------------------------
// tslint:disable:no-empty-interface
interface RCProps { }
class RefComponent extends React.Component<RCProps> {
static create = React.createFactory(RefComponent);
refMethod() {
}
}
let componentRef: RefComponent | null = new RefComponent();
RefComponent.create({ ref: "componentRef" });
// type of c should be inferred
RefComponent.create({ ref: c => componentRef = c });
componentRef.refMethod();
let domNodeRef: Element | null;
React.DOM.div({ ref: "domRef" });
// type of node should be inferred
React.DOM.div({ ref: node => domNodeRef = node });
let inputNodeRef: HTMLInputElement | null;
React.DOM.input({ ref: node => inputNodeRef = node as HTMLInputElement });
//
// Attributes
// --------------------------------------------------------------------------
const children: any[] = ["Hello world", [null], React.DOM.span(null)];
const divStyle: React.CSSProperties = { // CSSProperties
flex: "1 1 main-size",
backgroundImage: "url('hello.png')"
};
const htmlAttr: React.HTMLProps<HTMLElement> = {
key: 36,
ref: "htmlComponent",
children,
className: "test-attr",
style: divStyle,
slot: "HTMLComponent",
onClick: (event: React.MouseEvent<{}>) => {
event.preventDefault();
event.stopPropagation();
},
onClickCapture: (event: React.MouseEvent<{}>) => {
event.preventDefault();
event.stopPropagation();
},
onAnimationStart: event => {
console.log(event.currentTarget.className);
},
dangerouslySetInnerHTML: {
__html: "<strong>STRONG</strong>"
}
};
React.DOM.div(htmlAttr);
React.DOM.span(htmlAttr);
React.DOM.input(htmlAttr);
React.DOM.svg({
viewBox: "0 0 48 48",
xmlns: "http://www.w3.org/2000/svg"
},
React.DOM.rect({
className: 'foobar',
id: 'foo',
color: 'black',
x: 22,
y: 10,
width: 4,
height: 28,
strokeDasharray: '30%',
strokeDashoffset: '20%'
}),
React.DOM.rect({
x: 10,
y: 22,
width: 28,
height: 4,
strokeDasharray: 30,
strokeDashoffset: 20
}),
React.DOM.path({
d: "M0,0V3H3V0ZM1,1V2H2V1Z",
fill: "#999999",
fillRule: "evenodd"
})
);
//
// React.PropTypes
// --------------------------------------------------------------------------
const PropTypesSpecification: React.ComponentSpec<any, any> = {
propTypes: {
optionalArray: React.PropTypes.array,
optionalBool: React.PropTypes.bool,
optionalFunc: React.PropTypes.func,
optionalNumber: React.PropTypes.number,
optionalObject: React.PropTypes.object,
optionalString: React.PropTypes.string,
optionalNode: React.PropTypes.node,
optionalElement: React.PropTypes.element,
optionalMessage: React.PropTypes.instanceOf(Date),
optionalEnum: React.PropTypes.oneOf(["News", "Photos"]),
optionalUnion: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.number,
React.PropTypes.instanceOf(Date)
]),
optionalArrayOf: React.PropTypes.arrayOf(React.PropTypes.number),
optionalObjectOf: React.PropTypes.objectOf(React.PropTypes.number),
optionalObjectWithShape: React.PropTypes.shape({
color: React.PropTypes.string,
fontSize: React.PropTypes.number
}),
requiredFunc: React.PropTypes.func.isRequired,
requiredAny: React.PropTypes.any.isRequired,
customProp(props: any, propName: string, componentName: string): Error | null {
if (!/matchme/.test(props[propName])) {
return new Error("Validation failed!");
}
return null;
},
// https://facebook.github.io/react/warnings/dont-call-proptypes.html#fixing-the-false-positive-in-third-party-proptypes
percentage: (object: any, key: string, componentName: string, ...rest: any[]): Error | null => {
const error = React.PropTypes.number(object, key, componentName, ...rest);
if (error) {
return error;
}
if (object[key] < 0 || object[key] > 100) {
return new Error(`prop ${key} must be between 0 and 100`);
}
return null;
}
},
render: (): React.ReactElement<any> | null => {
return null;
}
};
//
// ContextTypes
// --------------------------------------------------------------------------
const ContextTypesSpecification: React.ComponentSpec<any, any> = {
contextTypes: {
optionalArray: React.PropTypes.array,
optionalBool: React.PropTypes.bool,
optionalFunc: React.PropTypes.func,
optionalNumber: React.PropTypes.number,
optionalObject: React.PropTypes.object,
optionalString: React.PropTypes.string,
optionalNode: React.PropTypes.node,
optionalElement: React.PropTypes.element,
optionalMessage: React.PropTypes.instanceOf(Date),
optionalEnum: React.PropTypes.oneOf(["News", "Photos"]),
optionalUnion: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.number,
React.PropTypes.instanceOf(Date)
]),
optionalArrayOf: React.PropTypes.arrayOf(React.PropTypes.number),
optionalObjectOf: React.PropTypes.objectOf(React.PropTypes.number),
optionalObjectWithShape: React.PropTypes.shape({
color: React.PropTypes.string,
fontSize: React.PropTypes.number
}),
requiredFunc: React.PropTypes.func.isRequired,
requiredAny: React.PropTypes.any.isRequired,
customProp(props: any, propName: string, componentName: string): Error | null {
if (!/matchme/.test(props[propName])) {
return new Error("Validation failed!");
}
return null;
}
},
render: (): null => {
return null;
}
};
//
// React.Children
// --------------------------------------------------------------------------
const mappedChildrenArray: number[] =
React.Children.map<number>(children, (child) => 42);
React.Children.forEach(children, (child) => { });
const nChildren: number = React.Children.count(children);
let onlyChild: React.ReactElement<any> = React.Children.only(React.DOM.div()); // ok
onlyChild = React.Children.only([null, [[["Hallo"], true]], false]); // error
const childrenToArray: React.ReactChild[] = React.Children.toArray(children);
//
// Example from http://facebook.github.io/react/
// --------------------------------------------------------------------------
interface TimerState {
secondsElapsed: number;
}
class Timer extends React.Component<{}, TimerState> {
state = {
secondsElapsed: 0
};
private _interval: number;
tick() {
this.setState((prevState, props) => ({
secondsElapsed: prevState.secondsElapsed + 1
}));
}
componentDidMount() {
this._interval = setInterval(() => this.tick(), 1000);
}
componentWillUnmount() {
clearInterval(this._interval);
}
render() {
return React.DOM.div(
null,
"Seconds Elapsed: ",
this.state.secondsElapsed
);
}
}
ReactDOM.render(React.createElement(Timer), container);
//
// createFragment addon
// --------------------------------------------------------------------------
createFragment({
a: React.DOM.div(),
b: ["a", false, React.createElement("span")]
});
//
// CSSTransitionGroup addon
// --------------------------------------------------------------------------
React.createFactory(CSSTransitionGroup)({
component: React.createClass({
render: (): null => null
}),
childFactory: (c) => c,
transitionName: "transition",
transitionAppear: false,
transitionEnter: true,
transitionLeave: true,
id: "some-id",
className: "some-class"
});
React.createFactory(CSSTransitionGroup)({
transitionName: {
enter: "enter",
enterActive: "enterActive",
leave: "leave",
leaveActive: "leaveActive",
appear: "appear",
appearActive: "appearActive"
}
});
//
// LinkedStateMixin addon
// --------------------------------------------------------------------------
React.createClass({
mixins: [LinkedStateMixin],
getInitialState() {
return {
isChecked: false,
message: "hello!"
};
},
render() {
return React.DOM.div(null,
React.DOM.input({
type: "checkbox",
checkedLink: this.linkState("isChecked")
}),
React.DOM.input({
type: "text",
valueLink: this.linkState("message")
})
);
}
});
//
// Perf addon
// --------------------------------------------------------------------------
Perf.start();
Perf.stop();
const measurements = Perf.getLastMeasurements();
Perf.printInclusive(measurements);
Perf.printExclusive(measurements);
Perf.printWasted(measurements);
Perf.printOperations(measurements);
Perf.printInclusive();
Perf.printExclusive();
Perf.printWasted();
Perf.printOperations();
console.log(Perf.getExclusive());
console.log(Perf.getInclusive());
console.log(Perf.getWasted());
console.log(Perf.getOperations());
console.log(Perf.getExclusive(measurements));
console.log(Perf.getInclusive(measurements));
console.log(Perf.getWasted(measurements));
console.log(Perf.getOperations(measurements));
// Renamed to printOperations(). Please use it instead.
Perf.printDOM(measurements);
Perf.printDOM();
//
// PureRenderMixin addon
// --------------------------------------------------------------------------
React.createClass({
mixins: [PureRenderMixin],
render() { return React.DOM.div(null); }
});
//
// TestUtils addon
// --------------------------------------------------------------------------
const inst: ModernComponent = TestUtils.renderIntoDocument<ModernComponent>(element);
const node: Element = TestUtils.renderIntoDocument(React.DOM.div());
TestUtils.Simulate.click(node);
TestUtils.Simulate.change(node);
TestUtils.Simulate.keyDown(node, { key: "Enter", cancelable: false });
const renderer: TestUtils.ShallowRenderer = TestUtils.createRenderer();
renderer.render(React.createElement(Timer));
const output: React.ReactElement<React.Props<Timer>> =
renderer.getRenderOutput();
const foundComponent: ModernComponent = TestUtils.findRenderedComponentWithType(
inst, ModernComponent);
const foundComponents: ModernComponent[] = TestUtils.scryRenderedComponentsWithType(
inst, ModernComponent);
// ReactTestUtils custom type guards
const emptyElement1: React.ReactElement<{}> = React.createElement(ModernComponent);
if (TestUtils.isElementOfType(emptyElement1, StatelessComponent)) {
emptyElement1.props.foo;
}
const emptyElement2: React.ReactElement<{}> = React.createElement(StatelessComponent);
if (TestUtils.isElementOfType(emptyElement2, StatelessComponent)) {
emptyElement2.props.foo;
}
if (TestUtils.isDOMComponent(container)) {
container.getAttribute("className");
} else if (TestUtils.isCompositeComponent(new ModernComponent())) {
new ModernComponent().props;
}
//
// TransitionGroup addon
// --------------------------------------------------------------------------
React.createFactory(TransitionGroup)({ component: "div" });
//
// update addon
// --------------------------------------------------------------------------
{
// These are copied from https://facebook.github.io/react/docs/update.html
let initialArray = [1, 2, 3];
let newArray = update(initialArray, { $push: [4] }); // => [1, 2, 3, 4]
let collection = [1, 2, { a: [12, 17, 15] }];
let newCollection = update(collection, { 2: { a: { $splice: [[1, 1, 13, 14]] } } });
// => [1, 2, {a: [12, 13, 14, 15]}]
let obj = { a: 5, b: 3 };
let newObj = update(obj, {
b: {
$apply: (x) => x * 2
}
});
// => {a: 5, b: 6}
let newObj2 = update(obj, { b: { $set: obj.b * 2 } });
let objShallow = { a: 5, b: 3 };
let newObjShallow = update(obj, { $merge: { b: 6, c: 7 } }); // => {a: 5, b: 6, c: 7}
}
//
// The SyntheticEvent.target.value should be accessible for onChange
// --------------------------------------------------------------------------
class SyntheticEventTargetValue extends React.Component<{}, { value: string }> {
constructor(props: {}) {
super(props);
this.state = { value: 'a' };
}
render() {
return React.DOM.textarea({
value: this.state.value,
onChange: e => this.setState({ value: e.target.value })
});
}
}
React.DOM.input({
onChange: event => {
// `event.target` is guaranteed to be HTMLInputElement
event.target.value;
}
});
// A ChangeEvent is a valid FormEvent (maintain compatibility with existing
// event handlers)
type InputChangeEvent = React.ChangeEvent<HTMLInputElement>;
type InputFormEvent = React.FormEvent<HTMLInputElement>;
const changeEvent: InputChangeEvent = undefined as any;
const formEvent: InputFormEvent = changeEvent;
// defaultProps should be optional of props
{
interface ComponentProps {
prop1: string;
prop2: string;
prop3?: string;
}
class ComponentWithDefaultProps extends React.Component<ComponentProps> {
static defaultProps = {
prop3: "default value",
};
}
const VariableWithAClass: React.ComponentClass<ComponentProps> = ComponentWithDefaultProps;
}
// complex React.DOMElement type
declare var x: React.DOMElement<{
className: string;
style: {
height: string;
overflowY: "auto";
transition: string;
};
}, Element>;
+65
View File
@@ -0,0 +1,65 @@
import * as React from "react";
interface SCProps {
foo?: number;
}
const StatelessComponent: React.SFC<SCProps> = ({ foo }: SCProps) => {
return <div>{foo}</div>;
};
StatelessComponent.displayName = "StatelessComponent3";
StatelessComponent.defaultProps = {
foo: 42
};
<StatelessComponent />;
const StatelessComponent2: React.SFC<SCProps> = ({ foo, children }) => {
return <div>{foo}{children}</div>;
};
StatelessComponent2.displayName = "StatelessComponent4";
StatelessComponent2.defaultProps = {
foo: 42
};
<StatelessComponent2>24</StatelessComponent2>;
// svg sanity check
<svg viewBox="0 0 1000 1000">
<g>
<text x="200" y="300" strokeWidth="5" stroke="black" alignmentBaseline="middle">
Hello, world!
</text>
<div slot="Some Div"> Hello again! </div>
</g>
</svg>;
// React-specific Attributes
<div
defaultChecked
defaultValue="some value"
contentEditable
suppressContentEditableWarning
>
<b>foo</b>
</div>;
interface Props {
hello: string;
}
interface State {
foobar: string;
}
class ComponentWithPropsAndState extends React.Component<Props, State> {
}
<ComponentWithPropsAndState hello="TypeScript" />;
class ComponentWithoutState extends React.Component<Props> {
}
<ComponentWithoutState hello="TypeScript" />;
class ComponentWithoutPropsAndState extends React.Component {
}
<ComponentWithoutPropsAndState />;
const StatelessComponentWithoutProps: React.SFC = (props) => {
return <div />;
};
<StatelessComponentWithoutProps />;
+31
View File
@@ -0,0 +1,31 @@
{
"files": [
"index.d.ts",
"test/index.ts",
"test/tsx.tsx",
"test/cssProperties.tsx"
],
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"dom"
],
"paths": {
"react": [
"react/v15"
]
},
"noImplicitAny": true,
"noImplicitThis": false,
"strictNullChecks": true,
"baseUrl": "../../",
"typeRoots": [
"../../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true,
"jsx": "preserve"
}
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }