|
1 | 1 | /* @flow strict */ |
2 | 2 | import * as React from 'react' |
| 3 | +import { useMemo } from 'react' |
3 | 4 | import ReactDom from 'react-dom/client' |
4 | 5 | import TimeAgo from '../../src/index' |
| 6 | +import { makeIntlFormatter } from '../../src/defaultFormatter' |
5 | 7 |
|
6 | 8 | const appElement = document.getElementById('app') |
7 | 9 |
|
8 | | -appElement && |
9 | | - ReactDom.createRoot(appElement).render( |
| 10 | +function Bare({ date }: { date: number }) { |
| 11 | + return ( |
| 12 | + <> |
| 13 | + You opened this page <TimeAgo date={date} /> |
| 14 | + </> |
| 15 | + ) |
| 16 | +} |
| 17 | + |
| 18 | +const intlFormatter = makeIntlFormatter({ |
| 19 | + locale: 'en', |
| 20 | + style: 'long', |
| 21 | + numeric: 'auto', |
| 22 | +}) |
| 23 | + |
| 24 | +function Intl({ date }: { date: number }) { |
| 25 | + const [locale, setLocale] = React.useState('en') |
| 26 | + const [style, setStyle] = React.useState('long') |
| 27 | + const [numeric, setNumeric] = React.useState('auto') |
| 28 | + |
| 29 | + const intlFormatter = useMemo( |
| 30 | + () => |
| 31 | + makeIntlFormatter({ |
| 32 | + locale, |
| 33 | + style, |
| 34 | + numeric, |
| 35 | + }), |
| 36 | + [locale, style, numeric], |
| 37 | + ) |
| 38 | + return ( |
10 | 39 | <div> |
11 | | - You opened this page <TimeAgo date={Date.now()} /> |
12 | | - </div>, |
| 40 | + <select value={locale} onChange={(e) => setLocale(e.target.value)}> |
| 41 | + <option value="en">en</option> |
| 42 | + <option value="fr">fr</option> |
| 43 | + <option value="es">es</option> |
| 44 | + <option value="de">de</option> |
| 45 | + <option value="it">it</option> |
| 46 | + <option value="ja">ja</option> |
| 47 | + <option value="ko">ko</option> |
| 48 | + <option value="zh">zh</option> |
| 49 | + </select> |
| 50 | + <select value={style} onChange={(e) => setStyle(e.target.value)}> |
| 51 | + <option value="long">long</option> |
| 52 | + <option value="short">short</option> |
| 53 | + <option value="narrow">narrow</option> |
| 54 | + </select> |
| 55 | + <select value={numeric} onChange={(e) => setNumeric(e.target.value)}> |
| 56 | + <option value="auto">auto</option> |
| 57 | + <option value="always">always</option> |
| 58 | + </select> |
| 59 | + <TimeAgo date={date} formatter={intlFormatter} /> |
| 60 | + </div> |
13 | 61 | ) |
| 62 | +} |
| 63 | + |
| 64 | +function App() { |
| 65 | + const [date, setDate] = React.useState(Date.now()) |
| 66 | + |
| 67 | + return ( |
| 68 | + <> |
| 69 | + <h1>Bare</h1> |
| 70 | + <Bare date={date} /> |
| 71 | + <h1>Intl</h1> |
| 72 | + You opened this page <Intl date={date} /> |
| 73 | + <hr /> |
| 74 | + <button onClick={() => setDate(Date.now())}>Reset Time</button> |
| 75 | + </> |
| 76 | + ) |
| 77 | +} |
| 78 | + |
| 79 | +appElement && ReactDom.createRoot(appElement).render(<App />) |
0 commit comments