2023-02-18 13:11:01 +03:00
|
|
|
import { faCalendarAlt as calendarIcon } from '@fortawesome/free-regular-svg-icons';
|
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|
|
|
import classNames from 'classnames';
|
2021-06-24 21:13:06 +03:00
|
|
|
import { isNil } from 'ramda';
|
2023-02-18 13:11:01 +03:00
|
|
|
import { useRef } from 'react';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { ReactDatePickerProps } from 'react-datepicker';
|
|
|
|
import DatePicker from 'react-datepicker';
|
2022-10-23 11:49:35 +03:00
|
|
|
import { STANDARD_DATE_FORMAT } from '../helpers/date';
|
2018-09-08 14:28:40 +03:00
|
|
|
import './DateInput.scss';
|
2018-07-31 22:24:34 +03:00
|
|
|
|
2021-06-24 21:13:06 +03:00
|
|
|
export type DateInputProps = ReactDatePickerProps;
|
2020-11-14 14:10:42 +03:00
|
|
|
|
2022-05-28 12:16:59 +03:00
|
|
|
export const DateInput = (props: DateInputProps) => {
|
2022-10-18 23:02:09 +03:00
|
|
|
const { className, isClearable, selected, dateFormat } = props;
|
2018-11-01 11:16:18 +03:00
|
|
|
const showCalendarIcon = !isClearable || isNil(selected);
|
2020-11-14 14:10:42 +03:00
|
|
|
const ref = useRef<{ input: HTMLInputElement }>();
|
2018-08-09 21:13:46 +03:00
|
|
|
|
2018-11-01 11:16:18 +03:00
|
|
|
return (
|
2023-03-14 10:50:53 +03:00
|
|
|
<div className="icon-input-container">
|
2018-11-01 11:16:18 +03:00
|
|
|
<DatePicker
|
2021-06-24 21:13:06 +03:00
|
|
|
{...props}
|
2022-10-08 11:18:08 +03:00
|
|
|
popperModifiers={[
|
|
|
|
{
|
|
|
|
name: 'arrow',
|
|
|
|
options: { padding: 24 }, // This prevents the arrow to be placed on the very edge, which looks ugly
|
|
|
|
},
|
|
|
|
]}
|
2022-10-23 11:43:01 +03:00
|
|
|
dateFormat={dateFormat ?? STANDARD_DATE_FORMAT}
|
2023-03-14 10:50:53 +03:00
|
|
|
className={classNames('icon-input-container__input form-control', className)}
|
2021-02-28 14:56:56 +03:00
|
|
|
// @ts-expect-error The DatePicker type definition is wrong. It has a ref prop
|
2018-11-01 11:16:18 +03:00
|
|
|
ref={ref}
|
|
|
|
/>
|
|
|
|
{showCalendarIcon && (
|
|
|
|
<FontAwesomeIcon
|
|
|
|
icon={calendarIcon}
|
2023-03-14 10:50:53 +03:00
|
|
|
className="icon-input-container__icon"
|
2020-08-26 21:03:23 +03:00
|
|
|
onClick={() => ref.current?.input.focus()}
|
2018-07-31 22:24:34 +03:00
|
|
|
/>
|
2018-11-01 11:16:18 +03:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|