shlink-web-client/src/utils/dates/DateInput.tsx
2022-10-23 10:49:35 +02:00

41 lines
1.4 KiB
TypeScript

import { useRef } from 'react';
import { isNil } from 'ramda';
import DatePicker, { ReactDatePickerProps } from 'react-datepicker';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faCalendarAlt as calendarIcon } from '@fortawesome/free-regular-svg-icons';
import classNames from 'classnames';
import { STANDARD_DATE_FORMAT } from '../helpers/date';
import './DateInput.scss';
export type DateInputProps = ReactDatePickerProps;
export const DateInput = (props: DateInputProps) => {
const { className, isClearable, selected, dateFormat } = props;
const showCalendarIcon = !isClearable || isNil(selected);
const ref = useRef<{ input: HTMLInputElement }>();
return (
<div className="date-input-container">
<DatePicker
{...props}
popperModifiers={[
{
name: 'arrow',
options: { padding: 24 }, // This prevents the arrow to be placed on the very edge, which looks ugly
},
]}
dateFormat={dateFormat ?? STANDARD_DATE_FORMAT}
className={classNames('date-input-container__input form-control', className)}
// @ts-expect-error The DatePicker type definition is wrong. It has a ref prop
ref={ref}
/>
{showCalendarIcon && (
<FontAwesomeIcon
icon={calendarIcon}
className="date-input-container__icon"
onClick={() => ref.current?.input.focus()}
/>
)}
</div>
);
};