[CORL 581] toggleable timestamp (#2561)

* toggle between relative and absolute timestamps

* make one common timestamp component

* remove unused timestamp component from /admin
This commit is contained in:
Tessa Thornton
2019-09-17 17:11:52 -04:00
committed by GitHub
parent 24a27ca958
commit 867f0993cc
43 changed files with 1175 additions and 453 deletions
@@ -0,0 +1,2 @@
.root {
}
@@ -0,0 +1,33 @@
import { useCoralContext } from "coral-framework/lib/bootstrap";
import { PropTypesOf } from "coral-ui/types";
import React, { FunctionComponent, useMemo } from "react";
import { Typography } from "coral-ui/components";
interface Props {
date: string;
className?: string;
}
const AbsoluteTime: FunctionComponent<Props> = ({ date, className }) => {
const { locales } = useCoralContext();
const formatted = useMemo(() => {
const formatter = new Intl.DateTimeFormat(locales, {
year: "numeric",
month: "long",
day: "numeric",
hour: "numeric",
minute: "2-digit",
});
return formatter.format(new Date(date));
}, [locales, date]);
return (
<Typography className={className} variant="timestamp">
{formatted}
</Typography>
);
};
export default AbsoluteTime;
export type AbsoluteTimeProps = PropTypesOf<typeof AbsoluteTime>;
@@ -0,0 +1 @@
export { default, AbsoluteTimeProps } from "./AbsoluteTime";
@@ -0,0 +1,7 @@
.root {
cursor: default;
}
.text {
composes: timestamp from "coral-ui/shared/typography.css";
}
@@ -0,0 +1,15 @@
import React from "react";
import { createRenderer } from "react-test-renderer/shallow";
import { PropTypesOf } from "coral-framework/types";
import Timestamp from "./Timestamp";
it("renders correctly", () => {
const props: PropTypesOf<typeof Timestamp> = {
children: "1995-12-17T03:24:00.000Z",
};
const renderer = createRenderer();
renderer.render(<Timestamp {...props} />);
expect(renderer.getRenderOutput()).toMatchSnapshot();
});
@@ -0,0 +1,42 @@
import cn from "classnames";
import React, { FunctionComponent, useCallback, useState } from "react";
import { AbsoluteTime, BaseButton, RelativeTime } from "coral-ui/components";
import styles from "./Timestamp.css";
export interface TimestampProps {
className?: string;
children: string;
toggleAbsolute?: boolean;
}
const Timestamp: FunctionComponent<TimestampProps> = props => {
const [showAbsolute, setShowAbsolute] = useState(false);
const toggleShowAbsolute = useCallback(() => {
if (props.toggleAbsolute) {
setShowAbsolute(!showAbsolute);
}
}, [showAbsolute, setShowAbsolute]);
return (
<BaseButton className={styles.root} onClick={toggleShowAbsolute}>
{showAbsolute ? (
<AbsoluteTime
date={props.children}
className={cn(styles.text, props.className)}
/>
) : (
<RelativeTime
className={cn(styles.text, props.className)}
date={props.children}
/>
)}
</BaseButton>
);
};
Timestamp.defaultProps = {
toggleAbsolute: true,
};
export default Timestamp;
@@ -0,0 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`renders correctly 1`] = `
<ForwardRef(forwardRef)
className="Timestamp-root"
onClick={[Function]}
>
<ForwardRef(forwardRef)
className="Timestamp-text"
date="1995-12-17T03:24:00.000Z"
/>
</ForwardRef(forwardRef)>
`;
@@ -0,0 +1 @@
export { default } from "./Timestamp";
+2
View File
@@ -1,3 +1,4 @@
export { default as AbsoluteTime } from "./AbsoluteTime";
export { default as BaseButton } from "./BaseButton";
export { default as Button } from "./Button";
export { default as Backdrop } from "./Backdrop";
@@ -27,6 +28,7 @@ export { Tab, TabBar, TabContent, TabPane } from "./Tabs";
export { StepBar, Step } from "./Steps";
export { SelectField, Option, OptGroup } from "./SelectField";
export { default as TextLink } from "./TextLink";
export { default as Timestamp } from "./Timestamp";
export { default as CheckBox } from "./CheckBox";
export { default as RadioButton } from "./RadioButton";
export { default as Delay } from "./Delay";