-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing several issues with the new Datepicker and making behavior clo…
…ser to the old one (#2625) * different approach to parsing and saving from dateinput * improve formatting display * make locales work with pattern-format * fixes for #2637 * fix scrolling logic for the case where there are no scroll bars
- Loading branch information
1 parent
399708e
commit f49a523
Showing
10 changed files
with
290 additions
and
200 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
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,81 +1,69 @@ | ||
import React, { forwardRef, useEffect, useState } from 'react'; | ||
import type { FocusEventHandler, RefObject } from 'react'; | ||
import React, { useEffect, useState } from 'react'; | ||
import { PatternFormat } from 'react-number-format'; | ||
|
||
import { Button, Textfield } from '@digdir/designsystemet-react'; | ||
import { CalendarIcon } from '@navikt/aksel-icons'; | ||
import { format, isMatch, isValid } from 'date-fns'; | ||
import { Textfield } from '@digdir/designsystemet-react'; | ||
import { format, isValid } from 'date-fns'; | ||
|
||
import { useLanguage } from 'src/features/language/useLanguage'; | ||
import styles from 'src/layout/Datepicker/Calendar.module.css'; | ||
import { DatepickerSaveFormatNoTimestamp, DatepickerSaveFormatTimestamp } from 'src/utils/dateHelpers'; | ||
import { getSaveFormattedDateString, strictParseFormat, strictParseISO } from 'src/utils/dateHelpers'; | ||
import { getFormatPattern } from 'src/utils/formatDateLocale'; | ||
|
||
export interface DatePickerInputProps { | ||
id: string; | ||
datepickerFormat: string; | ||
timeStamp: boolean; | ||
value?: string; | ||
formatString?: string; | ||
onBlur?: FocusEventHandler<HTMLInputElement>; | ||
onClick?: () => void; | ||
isDialogOpen?: boolean; | ||
onValueChange?: (value: string) => void; | ||
readOnly?: boolean; | ||
} | ||
|
||
export const DatePickerInput = forwardRef( | ||
( | ||
{ id, value, formatString, onBlur, isDialogOpen, readOnly, onClick }: DatePickerInputProps, | ||
ref: RefObject<HTMLButtonElement>, | ||
) => { | ||
const [input, setInput] = useState(value ?? ''); | ||
export function DatePickerInput({ | ||
id, | ||
value, | ||
datepickerFormat, | ||
timeStamp, | ||
onValueChange, | ||
readOnly, | ||
}: DatePickerInputProps) { | ||
const formatPattern = getFormatPattern(datepickerFormat); | ||
const dateValue = strictParseISO(value); | ||
const formattedDateValue = dateValue ? format(dateValue, datepickerFormat) : value; | ||
const [inputValue, setInputValue] = useState(formattedDateValue ?? ''); | ||
|
||
const { langAsString } = useLanguage(); | ||
useEffect(() => { | ||
setInputValue(formattedDateValue ?? ''); | ||
}, [formattedDateValue]); | ||
|
||
useEffect(() => { | ||
if (value) { | ||
if (formatString && isMatch(value, formatString)) { | ||
setInput(isValid(new Date(value)) ? format(value, formatString) : value); | ||
} else if (isMatch(value, DatepickerSaveFormatNoTimestamp)) { | ||
setInput(isValid(new Date(value)) ? format(value, formatString ?? 'dd.MM.yyyy') : value); | ||
} else if (isMatch(value, DatepickerSaveFormatTimestamp)) { | ||
setInput(isValid(new Date(value)) ? format(value, formatString ?? 'dd.MM.yyyy') : value); | ||
} | ||
} | ||
}, [value, formatString]); | ||
const saveValue = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
const stringValue = e.target.value; | ||
const date = strictParseFormat(stringValue, datepickerFormat); | ||
const valueToSave = getSaveFormattedDateString(date, timeStamp) ?? stringValue; | ||
onValueChange && onValueChange(valueToSave); | ||
}; | ||
|
||
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
setInput(e.target.value); | ||
}; | ||
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
const stringValue = e.target.value; | ||
setInputValue(stringValue); | ||
// If the date is valid, save immediately | ||
if (stringValue.length == 0 || isValid(strictParseFormat(stringValue, datepickerFormat))) { | ||
saveValue(e); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className={styles.calendarInputWrapper}> | ||
<Textfield | ||
className={styles.calendarInput} | ||
type='text' | ||
id={id} | ||
value={input} | ||
placeholder={formatString} | ||
onChange={handleInputChange} | ||
onBlur={onBlur} | ||
readOnly={readOnly} | ||
aria-readonly={readOnly} | ||
/> | ||
<Button | ||
id={`${id}-button`} | ||
variant='tertiary' | ||
icon={true} | ||
aria-controls='dialog' | ||
aria-haspopup='dialog' | ||
onClick={onClick} | ||
aria-expanded={isDialogOpen} | ||
aria-label={langAsString('date_picker.aria_label_icon')} | ||
ref={ref} | ||
disabled={readOnly} | ||
color='first' | ||
size='small' | ||
> | ||
<CalendarIcon title={langAsString('date_picker.aria_label_icon')} /> | ||
</Button> | ||
</div> | ||
); | ||
}, | ||
); | ||
|
||
DatePickerInput.displayName = 'DatePickerInput'; | ||
return ( | ||
<PatternFormat | ||
format={formatPattern} | ||
customInput={Textfield} | ||
mask='_' | ||
className={styles.calendarInput} | ||
type='text' | ||
id={id} | ||
value={inputValue} | ||
placeholder={datepickerFormat.toUpperCase()} | ||
onChange={handleChange} | ||
onBlur={saveValue} | ||
readOnly={readOnly} | ||
aria-readonly={readOnly} | ||
/> | ||
); | ||
} |
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.