Passed
Pull Request — main (#332)
by Alejandro
03:32
created

src/utils/DateInput.tsx   A

Complexity

Total Complexity 7
Complexity/F 0

Size

Lines of Code 57
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
wmc 7
eloc 48
mnd 7
bc 7
fnc 0
dl 0
loc 57
ccs 7
cts 8
cp 0.875
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import { useRef } from 'react';
2
import { isNil, dissoc } from 'ramda';
3
import DatePicker, { ReactDatePickerProps } from 'react-datepicker';
4
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
5
import { faCalendarAlt as calendarIcon } from '@fortawesome/free-regular-svg-icons';
6
import classNames from 'classnames';
7
import moment from 'moment';
8
import './DateInput.scss';
9
10
interface DatePropsInterface {
11
  endDate?: moment.Moment | null;
12
  maxDate?: moment.Moment | null;
13
  minDate?: moment.Moment | null;
14
  selected?: moment.Moment | null;
15
  startDate?: moment.Moment | null;
16
  onChange?: (date: moment.Moment | null) => void;
17
}
18
19
export type DateInputProps = DatePropsInterface & Omit<ReactDatePickerProps, keyof DatePropsInterface>;
20
21 8
const transformProps = (props: DateInputProps): ReactDatePickerProps => ({
22
  ...dissoc('ref', props),
23
  endDate: props.endDate?.toDate(),
24
  maxDate: props.maxDate?.toDate(),
25
  minDate: props.minDate?.toDate(),
26
  selected: props.selected?.toDate(),
27
  startDate: props.startDate?.toDate(),
28 2
  onChange: (date: Date | null) => props.onChange?.(date && moment(date)),
29
});
30
31 8
const DateInput = (props: DateInputProps) => {
32 4
  const { className, isClearable, selected } = props;
33 4
  const showCalendarIcon = !isClearable || isNil(selected);
34 4
  const ref = useRef<{ input: HTMLInputElement }>();
35
36 4
  return (
37
    <div className="date-input-container">
38
      <DatePicker
39
        {...transformProps(props)}
40
        dateFormat="yyyy-MM-dd"
41
        className={classNames('date-input-container__input form-control', className)}
42
        // @ts-expect-error
43
        ref={ref}
44
      />
45
      {showCalendarIcon && (
46
        <FontAwesomeIcon
47
          icon={calendarIcon}
48
          className="date-input-container__icon"
49
          onClick={() => ref.current?.input.focus()}
50
        />
51
      )}
52
    </div>
53
  );
54
};
55
56
export default DateInput;
57