Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PAT: add "never", "custom" options to expiry date (#2198)
* add DateTimePicker component * PAT expiry - custom, never * show "never" in PAT table * add alert, some styling
- Loading branch information
Showing
3 changed files
with
201 additions
and
31 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
frontend/src/component/common/DateTimePicker/DateTimePicker.tsx
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,65 @@ | ||
import { INPUT_ERROR_TEXT } from 'utils/testIds'; | ||
import { TextField, OutlinedTextFieldProps } from '@mui/material'; | ||
import { parseValidDate } from '../util'; | ||
import { format } from 'date-fns'; | ||
|
||
interface IDateTimePickerProps extends Omit<OutlinedTextFieldProps, 'variant'> { | ||
label: string; | ||
type?: 'date' | 'datetime'; | ||
error?: boolean; | ||
errorText?: string; | ||
min?: Date; | ||
max?: Date; | ||
value: Date; | ||
onChange: (e: any) => any; | ||
} | ||
|
||
export const formatDate = (value: string) => { | ||
const date = new Date(value); | ||
return format(date, 'yyyy-MM-dd'); | ||
}; | ||
|
||
export const formatDateTime = (value: string) => { | ||
const date = new Date(value); | ||
return format(date, 'yyyy-MM-dd') + 'T' + format(date, 'HH:mm'); | ||
}; | ||
|
||
export const DateTimePicker = ({ | ||
label, | ||
type = 'datetime', | ||
error, | ||
errorText, | ||
min, | ||
max, | ||
value, | ||
onChange, | ||
InputProps, | ||
...rest | ||
}: IDateTimePickerProps) => { | ||
const getDate = type === 'datetime' ? formatDateTime : formatDate; | ||
const inputType = type === 'datetime' ? 'datetime-local' : 'date'; | ||
|
||
return ( | ||
<TextField | ||
type={inputType} | ||
size="small" | ||
variant="outlined" | ||
label={label} | ||
error={error} | ||
helperText={errorText} | ||
value={getDate(value.toISOString())} | ||
onChange={e => { | ||
const parsedDate = parseValidDate(e.target.value); | ||
onChange(parsedDate ?? value); | ||
}} | ||
FormHelperTextProps={{ | ||
['data-testid']: INPUT_ERROR_TEXT, | ||
}} | ||
inputProps={{ | ||
min: min ? getDate(min.toISOString()) : min, | ||
max: max ? getDate(max.toISOString()) : max, | ||
}} | ||
{...rest} | ||
/> | ||
); | ||
}; |
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