DefinitelyTyped/types/react-timeago/react-timeago-tests.tsx
Mike Martin b87418f945 Genericize ReactTimeago by ComponentType (#36123)
* Genericize ReactTimeago by ComponentType

This allows properties from `component` to be added to `TimeAgo` without throwing an error.
e.g.
```
<TimeAgo<typeof Text>
  component={Text}
  style={[styles.lastActiveText, { fontSize: activeBubbleSize * 0.588 }]}
  date={lastActiveAt}
/>
```
Type inferencing not working yet, so must specify generic in order to add properties from `component`

* Removing RN dep

* Stylefix and updating TS to 2.9
2019-06-15 23:30:58 -07:00

50 lines
1.2 KiB
TypeScript

import ReactTimeago, { Unit, Suffix } from "react-timeago";
import * as React from "react";
const ReactTimeagoRequiredOptions: JSX.Element = (
<ReactTimeago date={new Date()} />
);
const customFormatter = (
value: number,
unit: Unit,
suffix: Suffix,
epochMiliseconds: number
) => {
return epochMiliseconds > 300000
? `${value}${unit[0]} ${suffix}`
: "a really long time ago";
};
const ReactTimeagoAllOptions: JSX.Element = (
<ReactTimeago
date={new Date()}
live
minPeriod={0}
maxPeriod={100}
component={() => <div />}
title="Timeago"
formatter={customFormatter}
/>
);
// inspired by react-native
class Text extends React.Component<{
style?: Array<{}>;
numberOfLines?: number;
ellipsizeMode?: "head" | "middle" | "tail" | "clip";
allowFontScaling?: boolean;
}> {}
const ReactTimeagoCustomComponent: JSX.Element = (
<ReactTimeago<typeof Text>
date={new Date()}
component={Text}
// props passed down to Text
style={[{ textAlign: "center" }, { fontSize: 24 }]}
numberOfLines={2}
ellipsizeMode="middle"
allowFontScaling
/>
);