|
| 1 | +import { PanelLabel, FormatDate, Card, CardContent } from "@eqtylab/equality"; |
| 2 | +import { useMemo } from "react"; |
| 3 | + |
| 4 | +const MINUTE = 60 * 1000; |
| 5 | +const HOUR = 60 * MINUTE; |
| 6 | +const DAY = 24 * HOUR; |
| 7 | +const WEEK = 7 * DAY; |
| 8 | + |
| 9 | +export function FormatDateRelativeDemo() { |
| 10 | + // Compute offsets from "now" once on mount so the examples read naturally. |
| 11 | + const samples = useMemo(() => { |
| 12 | + const now = new Date(); |
| 13 | + const time = now.getTime(); |
| 14 | + return [ |
| 15 | + { label: "Seconds ago", date: new Date(time - 10 * 1000) }, |
| 16 | + { label: "Minutes ago", date: new Date(time - 5 * MINUTE) }, |
| 17 | + { label: "Hours ago", date: new Date(time - 3 * HOUR) }, |
| 18 | + { label: "Days ago", date: new Date(time - 2 * DAY) }, |
| 19 | + { label: "Weeks ago", date: new Date(time - 2 * WEEK) }, |
| 20 | + { label: "In the future", date: new Date(time + 4 * DAY) }, |
| 21 | + ]; |
| 22 | + }, []); |
| 23 | + |
| 24 | + return ( |
| 25 | + <div className="my-4"> |
| 26 | + <Card> |
| 27 | + <CardContent> |
| 28 | + {samples.map((sample) => ( |
| 29 | + <div key={sample.label} className="flex items-center gap-3"> |
| 30 | + <PanelLabel label={sample.label} className="w-28" /> |
| 31 | + <FormatDate date={sample.date} displayAs="relative" /> |
| 32 | + </div> |
| 33 | + ))} |
| 34 | + </CardContent> |
| 35 | + </Card> |
| 36 | + </div> |
| 37 | + ); |
| 38 | +} |
| 39 | + |
| 40 | +export function FormatDateTimeZoneDemo() { |
| 41 | + const date = useMemo(() => new Date("2026-06-09T18:42:03Z"), []); |
| 42 | + |
| 43 | + return ( |
| 44 | + <div className="my-4"> |
| 45 | + <Card> |
| 46 | + <CardContent className="divide-border divide-y divide-solid"> |
| 47 | + <div className="flex items-center gap-3 py-1"> |
| 48 | + <PanelLabel label="UTC (default)" className="w-28" /> |
| 49 | + <FormatDate date={date} displayAs="absolute" /> |
| 50 | + </div> |
| 51 | + <div className="flex items-center gap-3 py-1"> |
| 52 | + <PanelLabel label="New York" className="w-28" /> |
| 53 | + <FormatDate |
| 54 | + date={date} |
| 55 | + displayAs="absolute" |
| 56 | + timeZone="America/New_York" |
| 57 | + /> |
| 58 | + </div> |
| 59 | + <div className="flex items-center gap-3 py-1"> |
| 60 | + <PanelLabel label="Tokyo" className="w-28" /> |
| 61 | + <FormatDate |
| 62 | + date={date} |
| 63 | + displayAs="absolute" |
| 64 | + timeZone="Asia/Tokyo" |
| 65 | + /> |
| 66 | + </div> |
| 67 | + <div className="flex items-center gap-3 py-1"> |
| 68 | + <PanelLabel label="Auckland" className="w-28" /> |
| 69 | + <FormatDate |
| 70 | + date={date} |
| 71 | + displayAs="absolute" |
| 72 | + timeZone="Pacific/Auckland" |
| 73 | + /> |
| 74 | + </div> |
| 75 | + <div className="flex items-center gap-3 py-1"> |
| 76 | + <PanelLabel label="Toronto" className="w-28" /> |
| 77 | + <FormatDate |
| 78 | + date={date} |
| 79 | + displayAs="absolute" |
| 80 | + timeZone="America/Toronto" |
| 81 | + /> |
| 82 | + </div> |
| 83 | + <div className="flex items-center gap-3 py-1"> |
| 84 | + <PanelLabel label="Los Angeles" className="w-28" /> |
| 85 | + <FormatDate |
| 86 | + date={date} |
| 87 | + displayAs="absolute" |
| 88 | + timeZone="America/Los_Angeles" |
| 89 | + /> |
| 90 | + </div> |
| 91 | + <div className="flex items-center gap-3 py-1"> |
| 92 | + <PanelLabel label="Buenos Aires" className="w-28" /> |
| 93 | + <FormatDate |
| 94 | + date={date} |
| 95 | + displayAs="absolute" |
| 96 | + timeZone="America/Buenos_Aires" |
| 97 | + /> |
| 98 | + </div> |
| 99 | + <div className="flex items-center gap-3 py-1"> |
| 100 | + <PanelLabel label="London" className="w-28" /> |
| 101 | + <FormatDate |
| 102 | + date={date} |
| 103 | + displayAs="absolute" |
| 104 | + timeZone="Europe/London" |
| 105 | + /> |
| 106 | + </div> |
| 107 | + </CardContent> |
| 108 | + </Card> |
| 109 | + </div> |
| 110 | + ); |
| 111 | +} |
| 112 | + |
| 113 | +export function FormatDateOptionsDemo() { |
| 114 | + const date = useMemo(() => new Date("2026-06-09T18:42:03Z"), []); |
| 115 | + |
| 116 | + // Each entry overrides the default Intl.DateTimeFormat options. |
| 117 | + const samples: { |
| 118 | + label: string; |
| 119 | + options: Intl.DateTimeFormatOptions; |
| 120 | + }[] = [ |
| 121 | + { |
| 122 | + label: "Date only", |
| 123 | + options: { year: "numeric", month: "long", day: "numeric" }, |
| 124 | + }, |
| 125 | + { |
| 126 | + label: "Time only", |
| 127 | + options: { hour: "2-digit", minute: "2-digit" }, |
| 128 | + }, |
| 129 | + { |
| 130 | + label: "With weekday", |
| 131 | + options: { |
| 132 | + weekday: "long", |
| 133 | + year: "numeric", |
| 134 | + month: "long", |
| 135 | + day: "numeric", |
| 136 | + }, |
| 137 | + }, |
| 138 | + { |
| 139 | + label: "Short numeric", |
| 140 | + options: { dateStyle: "short", timeStyle: "short" }, |
| 141 | + }, |
| 142 | + ]; |
| 143 | + |
| 144 | + return ( |
| 145 | + <div className="my-4"> |
| 146 | + <Card> |
| 147 | + <CardContent className="divide-border divide-y divide-solid"> |
| 148 | + {samples.map((sample) => ( |
| 149 | + <div key={sample.label} className="flex items-center gap-3 py-1"> |
| 150 | + <PanelLabel label={sample.label} className="w-28" /> |
| 151 | + <FormatDate |
| 152 | + date={date} |
| 153 | + displayAs="absolute" |
| 154 | + absoluteOptions={sample.options} |
| 155 | + /> |
| 156 | + </div> |
| 157 | + ))} |
| 158 | + </CardContent> |
| 159 | + </Card> |
| 160 | + </div> |
| 161 | + ); |
| 162 | +} |
| 163 | + |
| 164 | +export function FormatDateIsoDemo() { |
| 165 | + const date = useMemo(() => new Date("2026-06-09T18:42:03Z"), []); |
| 166 | + |
| 167 | + // en-CA orders numeric dates as yyyy-mm-dd |
| 168 | + // en-US would render dd/mm/yyyy |
| 169 | + const options: Intl.DateTimeFormatOptions = { |
| 170 | + year: "numeric", |
| 171 | + month: "2-digit", |
| 172 | + day: "2-digit", |
| 173 | + }; |
| 174 | + |
| 175 | + return ( |
| 176 | + <div className="my-4"> |
| 177 | + <Card> |
| 178 | + <CardContent className="divide-border divide-y divide-solid"> |
| 179 | + <div className="flex items-center gap-3 py-1"> |
| 180 | + <PanelLabel label="en-CA" className="w-28" /> |
| 181 | + <FormatDate |
| 182 | + date={date} |
| 183 | + displayAs="absolute" |
| 184 | + locale="en-CA" |
| 185 | + absoluteOptions={options} |
| 186 | + /> |
| 187 | + </div> |
| 188 | + <div className="flex items-center gap-3 py-1"> |
| 189 | + <PanelLabel label="en-US" className="w-28" /> |
| 190 | + <FormatDate |
| 191 | + date={date} |
| 192 | + displayAs="absolute" |
| 193 | + locale="en-US" |
| 194 | + absoluteOptions={options} |
| 195 | + /> |
| 196 | + </div> |
| 197 | + </CardContent> |
| 198 | + </Card> |
| 199 | + </div> |
| 200 | + ); |
| 201 | +} |
0 commit comments