diff options
author | Eugene Sokolov <eug-vs@keemail.me> | 2020-10-10 12:14:04 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-10 12:14:04 +0300 |
commit | e7fb5387af7d3397df49b913795b956fc375e39d (patch) | |
tree | c10166f9d4a132855c4d4ff26295760fd654c12c /src/components/DateString/DateString.tsx | |
parent | 40ac5922118015aae943872717730d7068976b1a (diff) | |
parent | 5ee4f42b577449a58469fd3c7892455d097a6c79 (diff) | |
download | which-ui-e7fb5387af7d3397df49b913795b956fc375e39d.tar.gz |
Merge pull request #106 from which-ecosystem/feat/date-format
Imrove date formats
Diffstat (limited to 'src/components/DateString/DateString.tsx')
-rw-r--r-- | src/components/DateString/DateString.tsx | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/components/DateString/DateString.tsx b/src/components/DateString/DateString.tsx new file mode 100644 index 0000000..7c824cd --- /dev/null +++ b/src/components/DateString/DateString.tsx @@ -0,0 +1,41 @@ +import React, { useState, useMemo, useCallback } from 'react'; +import { makeStyles } from '@material-ui/core/styles'; +import compactDateString from './compactDateString'; + +interface PropTypes { + value: Date | string; +} + +const useStyles = makeStyles({ + root: { + display: 'block', + cursor: 'pointer' + } +}); + +const DATE_FORMAT = { + month: 'long', + day: 'numeric', + year: 'numeric', + hour: '2-digit', + minute: '2-digit' +}; + +const DateString: React.FC<PropTypes> = ({ value }) => { + const [isCompact, setIsCompact] = useState<boolean>(true); + const classes = useStyles(); + + const toggleScompact = useCallback(() => setIsCompact(v => !v), [setIsCompact]); + + const date = useMemo(() => new Date(value), [value]); + const formatted = useMemo(() => date.toLocaleString('default', DATE_FORMAT), [date]); + const compact = useMemo(() => compactDateString(date), [date]); + + return ( + <div role="presentation" className={classes.root} onClick={toggleScompact}> + {isCompact ? compact : formatted} + </div> + ); +}; + +export default DateString; |