blob: b4bc8be9ec499198e66660f91bce7373ad478f58 [file] [log] [blame]
Avi Zivb8e2faf2017-07-18 19:45:38 +03001import React from 'react';
talig8e9c0652017-12-20 14:30:43 +02002import PropTypes from 'prop-types';
Avi Zivb8e2faf2017-07-18 19:45:38 +03003import DatePicker from 'react-datepicker';
4import moment from 'moment';
5import SVGIcon from 'sdc-ui/lib/react/SVGIcon.js';
6
7class CustomInput extends React.Component {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +02008 static propTypes = {
9 placeHolderText: PropTypes.string,
10 onChange: PropTypes.func,
11 onClick: PropTypes.func,
12 value: PropTypes.string
13 };
Avi Zivb8e2faf2017-07-18 19:45:38 +030014
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020015 render() {
16 const {
17 placeholderText,
18 onClick,
19 onClear,
20 inputRef,
21 value: date
22 } = this.props;
23 const text = date ? date : placeholderText;
24 const textStyle = date ? '' : 'placeholder';
25 return (
26 <div
27 onClick={onClick}
28 ref={inputRef}
29 className="datepicker-custom-input">
30 <div className={`datepicker-text ${textStyle}`}>{text}</div>
31 {date && (
32 <SVGIcon
33 onClick={e => {
34 e.stopPropagation();
35 onClear();
36 }}
37 name="close"
38 className="clear-input"
39 />
40 )}
41 <SVGIcon name="calendar" />
42 </div>
43 );
44 }
45}
Avi Zivb8e2faf2017-07-18 19:45:38 +030046
47const parseDate = (date, format) => {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020048 return typeof date === 'number' ? moment.unix(date) : moment(date, format);
Avi Zivb8e2faf2017-07-18 19:45:38 +030049};
50
51class Datepicker extends React.Component {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020052 static propTypes = {
53 date: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
54 format: PropTypes.string,
55 onChange: PropTypes.func,
56 selectsStart: PropTypes.bool,
57 selectsEnd: PropTypes.bool,
58 startDate: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
59 endDate: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
60 disabled: PropTypes.bool,
61 label: PropTypes.string,
62 isRequired: PropTypes.bool
63 };
64 render() {
65 let {
66 date,
67 format,
68 onChange,
69 selectsStart = false,
70 startDate = null,
71 endDate = null,
72 selectsEnd = false,
73 disabled = false,
74 inputRef
75 } = this.props;
76 const placeholderText = 'Enter a date';
77 const props = {
78 format,
79 onChange,
80 disabled,
81 selected: date ? parseDate(date, format) : date,
82 selectsStart,
83 selectsEnd,
84 placeholderText,
85 startDate: startDate ? parseDate(startDate, format) : startDate,
86 endDate: endDate ? parseDate(endDate, format) : endDate
87 };
Avi Zivb8e2faf2017-07-18 19:45:38 +030088
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020089 return (
90 <div className="customized-date-picker">
91 <DatePicker
92 calendarClassName="customized-date-picker-calendar"
93 customInput={
94 <CustomInput
95 inputRef={inputRef}
96 onClear={() => onChange(undefined)}
97 placeholderText={placeholderText}
98 />
99 }
100 minDate={selectsEnd && props.startDate}
101 maxDate={selectsStart && props.endDate}
102 {...props}
103 />
104 </div>
105 );
106 }
Avi Zivb8e2faf2017-07-18 19:45:38 +0300107}
108
109export default Datepicker;