2020-11-13 22:44:26 +01:00
|
|
|
import { Component, RefObject, createRef } from 'react';
|
2018-08-09 20:13:46 +02:00
|
|
|
import { isNil } from 'ramda';
|
2020-08-26 20:03:23 +02:00
|
|
|
import DatePicker, { ReactDatePickerProps } from 'react-datepicker';
|
2019-01-05 22:25:54 +01:00
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|
|
|
import { faCalendarAlt as calendarIcon } from '@fortawesome/free-regular-svg-icons';
|
2020-01-14 19:59:25 +01:00
|
|
|
import classNames from 'classnames';
|
2018-09-08 13:28:40 +02:00
|
|
|
import './DateInput.scss';
|
2018-07-31 21:24:34 +02:00
|
|
|
|
2020-08-26 20:03:23 +02:00
|
|
|
export interface DateInputProps extends ReactDatePickerProps {
|
|
|
|
ref?: RefObject<Component<ReactDatePickerProps> & { input: HTMLInputElement }>;
|
|
|
|
}
|
2018-07-31 21:24:34 +02:00
|
|
|
|
2020-08-26 20:03:23 +02:00
|
|
|
const DateInput = (props: DateInputProps) => {
|
2020-11-13 22:44:26 +01:00
|
|
|
const { className, isClearable, selected, ref = createRef() } = props;
|
2018-11-01 09:16:18 +01:00
|
|
|
const showCalendarIcon = !isClearable || isNil(selected);
|
2018-08-09 20:13:46 +02:00
|
|
|
|
2018-11-01 09:16:18 +01:00
|
|
|
return (
|
|
|
|
<div className="date-input-container">
|
|
|
|
<DatePicker
|
|
|
|
{...props}
|
2020-01-14 19:59:25 +01:00
|
|
|
className={classNames('date-input-container__input form-control', className)}
|
2018-11-01 09:16:18 +01:00
|
|
|
dateFormat="YYYY-MM-DD"
|
|
|
|
readOnly
|
|
|
|
ref={ref}
|
|
|
|
/>
|
|
|
|
{showCalendarIcon && (
|
|
|
|
<FontAwesomeIcon
|
|
|
|
icon={calendarIcon}
|
|
|
|
className="date-input-container__icon"
|
2020-08-26 20:03:23 +02:00
|
|
|
onClick={() => ref.current?.input.focus()}
|
2018-07-31 21:24:34 +02:00
|
|
|
/>
|
2018-11-01 09:16:18 +01:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default DateInput;
|