-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added DateTime and FromNow components
- Loading branch information
1 parent
cd3f544
commit 364e20f
Showing
9 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { FunctionComponent, useEffect, useState } from "react"; | ||
import { DateTime as LuxonDateTime, LocaleOptions } from "luxon"; | ||
|
||
/** | ||
* Properties | ||
*/ | ||
export type DateTimeProps = { | ||
date: Date | string | undefined; | ||
format?: string; | ||
locale?: string; | ||
}; | ||
|
||
export const DateTime: FunctionComponent<DateTimeProps> = (props: DateTimeProps) => { | ||
const [formattedDate, setFormattedDate] = useState<string | null>(null); | ||
|
||
const format: string = props.format || "locale"; | ||
|
||
useEffect(() => { | ||
let luxonDT: LuxonDateTime | undefined = undefined; | ||
if (props.date && typeof props.date === "string") { | ||
luxonDT = LuxonDateTime.fromISO(props.date as string); | ||
} else if (props.date && typeof props.date === "object") { | ||
luxonDT = LuxonDateTime.fromJSDate(props.date as Date); | ||
} | ||
|
||
if (luxonDT) { | ||
const localeOptions: LocaleOptions = { | ||
locale: props.locale | ||
}; | ||
if (format === "fromNow") { | ||
setFormattedDate(luxonDT.toRelative()); | ||
} else if (format === "locale") { | ||
setFormattedDate(luxonDT.toLocaleString(LuxonDateTime.DATETIME_FULL, localeOptions)); | ||
} else { | ||
setFormattedDate(luxonDT.toFormat(format, localeOptions)); | ||
} | ||
} else { | ||
setFormattedDate(null); | ||
} | ||
}, [props.date]); | ||
|
||
return ( | ||
<span>{ formattedDate || "" }</span> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { FunctionComponent } from "react"; | ||
import { DateTime } from "./DateTime.tsx"; | ||
|
||
|
||
export type FromNowProps = { | ||
date: Date | string | undefined; | ||
}; | ||
|
||
|
||
export const FromNow: FunctionComponent<FromNowProps> = (props: FromNowProps) => { | ||
return ( | ||
<DateTime date={props.date} format="fromNow" /> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { FunctionComponent } from "react"; | ||
import { ChevronDownIcon, ChevronRightIcon } from "@patternfly/react-icons"; | ||
|
||
/** | ||
* Properties | ||
*/ | ||
export type ToggleIconProps = { | ||
expanded: boolean; | ||
onClick: () => void; | ||
}; | ||
|
||
export const ToggleIcon: FunctionComponent<ToggleIconProps> = ({ expanded, onClick }: ToggleIconProps) => { | ||
return expanded ? ( | ||
<ChevronDownIcon onClick={onClick} style={{ cursor: "pointer" }} /> | ||
) : ( | ||
<ChevronRightIcon onClick={onClick} style={{ cursor: "pointer" }} /> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
export * from "./DateTime"; | ||
export * from "./FromNow"; | ||
export * from "./If"; | ||
export * from "./IfNotEmpty"; | ||
export * from "./IfNotLoading"; | ||
export * from "./ListWithToolbar"; | ||
export * from "./ObjectDropdown"; | ||
export * from "./ObjectSelect"; | ||
export * from "./ToggleIcon"; | ||
export * from "./UrlUpload"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { FunctionComponent } from "react"; | ||
import { DateTime } from "../../lib/common"; | ||
import { Card, CardBody, CardTitle } from "@patternfly/react-core"; | ||
|
||
export const DateTimeDemo: FunctionComponent<any> = () => { | ||
return ( | ||
<div> | ||
<Card ouiaId="NowCard"> | ||
<CardTitle>Now</CardTitle> | ||
<CardBody> | ||
<div> | ||
<DateTime date={new Date()} /> | ||
</div> | ||
<div> | ||
<DateTime date={new Date()} format="locale" /> | ||
</div> | ||
<div> | ||
<DateTime date={new Date()} format="fromNow" /> | ||
</div> | ||
<div> | ||
<DateTime date={new Date()} format="yyyy-MM-dd 'at' h:mm a" /> | ||
</div> | ||
</CardBody> | ||
</Card> | ||
<div style={{ padding: "15px" }} /> | ||
<Card ouiaId="Jan17Card"> | ||
<CardTitle>Jan 17, 2012 @ 3:30 PM (UTC)</CardTitle> | ||
<CardBody> | ||
<div> | ||
<DateTime date="2012-01-17T15:30:00Z" /> | ||
</div> | ||
<div> | ||
<DateTime date="2012-01-17T15:30:00Z" format="locale" /> | ||
</div> | ||
<div> | ||
<DateTime date="2012-01-17T15:30:00Z" format="fromNow" /> | ||
</div> | ||
<div> | ||
<DateTime date="2012-01-17T15:30:00Z" format="yyyy-MM-dd 'at' h:mm a" /> | ||
</div> | ||
</CardBody> | ||
</Card> | ||
<div style={{ padding: "15px" }} /> | ||
<Card ouiaId="Feb10Card"> | ||
<CardTitle>Feb 10, 2023 @ 02:15 AM (UTC)</CardTitle> | ||
<CardBody> | ||
<div> | ||
<DateTime date="2023-02-10T02:15:00Z" /> | ||
</div> | ||
<div> | ||
<DateTime date="2023-02-10T02:15:00Z" format="locale" /> | ||
</div> | ||
<div> | ||
<DateTime date="2023-02-10T02:15:00Z" format="fromNow" /> | ||
</div> | ||
<div> | ||
<DateTime date="2023-02-10T02:15:00Z" format="yyyy-MM-dd 'at' h:mm a" /> | ||
</div> | ||
</CardBody> | ||
</Card> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { FunctionComponent, useState } from "react"; | ||
import { Card, CardBody } from "@patternfly/react-core"; | ||
import { ToggleIcon } from "../../lib/common"; | ||
|
||
export const ToggleIconDemo: FunctionComponent<any> = () => { | ||
const [isExpanded, setIsExpanded] = useState(false); | ||
|
||
return ( | ||
<div> | ||
<Card ouiaId="ToggleIconDemo"> | ||
<CardBody> | ||
<ToggleIcon expanded={isExpanded} onClick={() => setIsExpanded(!isExpanded)} /> | ||
</CardBody> | ||
</Card> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters