DefinitelyTyped/types/react-motion/react-motion-tests.tsx
Nathan Shively-Sanders 4fe7e4448c
Fix new errors from TS@next (3.9) (#42316)
1. Bogus trailing } and > are now detected in JSX.
2. Properties that are overwritten in JSX are now detected.

For react-motion, note that the fix correctly started to use a
previously unused attribute `style`, which exposes a type error in the
tests:

```
config.style: number | OpaqueConfig
```

but the test assigns it to

```
div.style: string | number
```

I squelched the error with a cast to `number`, but probably either the
test or the types are actually wrong.
2020-02-12 09:45:58 -08:00

96 lines
2.6 KiB
TypeScript

import * as React from 'react';
import { StaggeredMotion, Motion, spring, TransitionMotion, TransitionPlainStyle, TransitionStyle, Style, PlainStyle } from 'react-motion';
class Root extends React.Component {
render() {
return (
<Motion defaultStyle={{ x: 0 }} style = {{ x: spring(10) }}>
{(value: any) => <div>{ value.x } </div>}
</Motion>
);
}
}
class TransitionTest extends React.Component {
render() {
return (
<TransitionMotion defaultStyles={this.getDefaultStyles() }
styles={this.getStyles() }
>
{this.renderItems.bind(this)}
</TransitionMotion>
);
}
getDefaultStyles(): TransitionPlainStyle[] {
return [1, 2, 3].map(num => {
const style: PlainStyle = {
height: 0
};
return {
key: `${num}`,
data: num,
style: style
};
})
}
getStyles(): TransitionStyle[] {
return [1, 2, 3].map(num => {
const style: Style = {
height: spring(10)
};
return {
key: `${num}`,
data: num,
style: style
}
});
}
renderItems(interpolatedItems: TransitionStyle[]): JSX.Element {
return (
<div>
{interpolatedItems.map(config => {
return (
<div key={config.key} style={{ height: config.style['height'] as number }}>
{config.data}
</div>
);
}) }
</div>
)
}
}
class StaggeredTest extends React.Component {
render() {
return (
<StaggeredMotion defaultStyles={[{ h: 0 }, { h: 0 }, { h: 0 }]}
styles={this.getStyles.bind(this)}
>
</StaggeredMotion>
)
}
getStyles(prevInterpolatedStyles: PlainStyle[]): Style[] {
return prevInterpolatedStyles.map((prevStyle, index) => {
const style: Style = {};
style['h'] = (index === 0) ? spring(100) : spring(prevInterpolatedStyles[index - 1]['h']);
return style;
})
}
renderItems(interpolatedItems: PlainStyle[]): JSX.Element {
return (
<div>
{interpolatedItems.map((style, index) => {
return (
<div key={index} style={{ height: style['h'] }}/>
)
})}
</div>
)
}
}