-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
705db6c
commit 3c60ebb
Showing
7 changed files
with
379 additions
and
105 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,143 @@ | ||
import { | ||
FillLevelContext, | ||
Flex, | ||
FormField, | ||
SegmentedInput, | ||
SegmentedInputHandle, | ||
SemanticColorKey, | ||
} from '@pluralsh/design-system' | ||
import { Body2P } from 'components/utils/typography/Text' | ||
|
||
import { ComponentPropsWithRef, useEffect, useRef, useState } from 'react' | ||
import styled, { useTheme } from 'styled-components' | ||
import { DateParam, formatDateTime, isValidDateTime } from 'utils/datetime' | ||
|
||
const EMPTY_DATE_STR = '//' | ||
const EMPTY_TIME_STR = '::' | ||
|
||
export function DateTimeFormInput({ | ||
initialDate, | ||
setDate, | ||
...props | ||
}: { | ||
initialDate?: DateParam | ||
setDate?: (date?: DateParam) => void | ||
} & Omit<ComponentPropsWithRef<typeof FormField>, 'caption'>) { | ||
const { colors } = useTheme() | ||
const initDateStr = formatDateTime(initialDate, 'M/D/YYYY') || EMPTY_DATE_STR | ||
const initTimeStr = formatDateTime(initialDate, 'H:m:s') || EMPTY_TIME_STR | ||
const [initDateM, initDateD, initDateY] = initDateStr.split('/') | ||
const [initTimeH, initTimeM, initTimeS] = initTimeStr.split(':') | ||
|
||
const [dateStr, setDateStr] = useState('') | ||
const [timeStr, setTimeStr] = useState('') | ||
const isSetToNow = dateStr === EMPTY_DATE_STR && timeStr === EMPTY_TIME_STR | ||
|
||
const dateValid = isValidDateTime(dateStr, 'M/D/YYYY', true) | ||
const dateChanged = dateStr !== initDateStr | ||
const dateError = !isSetToNow && !dateValid && dateChanged | ||
const timeValid = isValidDateTime(timeStr, 'H:m:s') | ||
const timeChanged = timeStr !== initTimeStr | ||
const timeError = !isSetToNow && !timeValid && timeChanged | ||
|
||
const dateInputRef = useRef<SegmentedInputHandle>(null) | ||
const timeInputRef = useRef<SegmentedInputHandle>(null) | ||
|
||
// does an imperative clear, and manually sets state accordingly | ||
// necessary because SegmentedInput component is not fully controlled | ||
const clearDateTime = () => { | ||
dateInputRef.current?.clear() | ||
timeInputRef.current?.clear() | ||
setDateStr(EMPTY_DATE_STR) | ||
setTimeStr(EMPTY_TIME_STR) | ||
setDate?.(undefined) | ||
} | ||
|
||
// keep parent in sync | ||
useEffect(() => { | ||
if (isSetToNow) return | ||
if (dateValid && timeValid && (dateChanged || timeChanged)) | ||
setDate?.(`${dateStr} ${timeStr}`) | ||
else setDate?.(initialDate) | ||
}, [ | ||
dateStr, | ||
setDate, | ||
timeStr, | ||
initialDate, | ||
dateValid, | ||
dateChanged, | ||
timeValid, | ||
timeChanged, | ||
]) | ||
|
||
return ( | ||
<FillLevelContext value={0}> | ||
<FormField | ||
hint="All logs displayed will be before this date/time" | ||
caption={ | ||
<CaptionWrapperSC> | ||
<CaptionTextBtnSC | ||
$disabled={isSetToNow} | ||
onClick={isSetToNow ? undefined : clearDateTime} | ||
> | ||
Set to now | ||
</CaptionTextBtnSC> | ||
<CaptionTextBtnSC $color="text-primary-accent"> | ||
Enter timestamp | ||
</CaptionTextBtnSC> | ||
</CaptionWrapperSC> | ||
} | ||
{...props} | ||
> | ||
<Flex | ||
gap="xsmall" | ||
direction="column" | ||
> | ||
<SegmentedInput | ||
ref={dateInputRef} | ||
{...(isSetToNow && { value: 'Today' })} | ||
style={{ borderColor: dateError && colors['border-danger'] }} | ||
prefix="Date" | ||
endIcon={<Body2P $color="text-xlight">MM/DD/YYYY</Body2P>} | ||
onChange={setDateStr} | ||
separator="/" | ||
segments={[ | ||
{ length: 2, max: 12, name: 'MM', initialVal: initDateM }, | ||
{ length: 2, min: 1, max: 31, name: 'DD', initialVal: initDateD }, | ||
{ length: 4, max: 9999, name: 'YYYY', initialVal: initDateY }, | ||
]} | ||
/> | ||
<SegmentedInput | ||
ref={timeInputRef} | ||
{...(isSetToNow && { value: 'Now' })} | ||
style={{ borderColor: timeError && colors['border-danger'] }} | ||
prefix="Time" | ||
endIcon={<Body2P $color="text-xlight">UTC</Body2P>} | ||
onChange={setTimeStr} | ||
separator=":" | ||
segments={[ | ||
{ length: 2, max: 23, name: 'HH', initialVal: initTimeH }, | ||
{ length: 2, max: 59, name: 'MM', initialVal: initTimeM }, | ||
{ length: 2, max: 59, name: 'SS', initialVal: initTimeS }, | ||
]} | ||
/> | ||
</Flex> | ||
</FormField> | ||
</FillLevelContext> | ||
) | ||
} | ||
|
||
const CaptionWrapperSC = styled.span(({ theme }) => ({ | ||
display: 'flex', | ||
gap: theme.spacing.medium, | ||
})) | ||
|
||
const CaptionTextBtnSC = styled.span<{ | ||
$color?: SemanticColorKey | ||
$disabled?: boolean | ||
}>(({ theme, $color = 'text-xlight', $disabled = false }) => ({ | ||
color: theme.colors[$color], | ||
cursor: $disabled ? 'default' : 'pointer', | ||
opacity: $disabled ? 0.4 : 1, | ||
'&:hover': { textDecoration: $disabled ? 'none' : 'underline' }, | ||
})) |
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
Oops, something went wrong.