blob: 33db247aee93ef0bef400533065b0f83d9a4c3c2 [file] [log] [blame]
Einav Weiss Keidar1801b242018-08-13 16:19:46 +03001/*
2 * Copyright © 2016-2018 European Support Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Avi Zivb8e2faf2017-07-18 19:45:38 +030016import React from 'react';
talig8e9c0652017-12-20 14:30:43 +020017import PropTypes from 'prop-types';
Avi Zivb8e2faf2017-07-18 19:45:38 +030018import DatePicker from 'react-datepicker';
19import moment from 'moment';
20import SVGIcon from 'sdc-ui/lib/react/SVGIcon.js';
21
22class CustomInput extends React.Component {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020023 static propTypes = {
24 placeHolderText: PropTypes.string,
25 onChange: PropTypes.func,
26 onClick: PropTypes.func,
27 value: PropTypes.string
28 };
Avi Zivb8e2faf2017-07-18 19:45:38 +030029
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020030 render() {
31 const {
32 placeholderText,
33 onClick,
34 onClear,
35 inputRef,
36 value: date
37 } = this.props;
38 const text = date ? date : placeholderText;
39 const textStyle = date ? '' : 'placeholder';
40 return (
41 <div
42 onClick={onClick}
43 ref={inputRef}
44 className="datepicker-custom-input">
45 <div className={`datepicker-text ${textStyle}`}>{text}</div>
46 {date && (
47 <SVGIcon
48 onClick={e => {
49 e.stopPropagation();
50 onClear();
51 }}
52 name="close"
53 className="clear-input"
54 />
55 )}
56 <SVGIcon name="calendar" />
57 </div>
58 );
59 }
60}
Avi Zivb8e2faf2017-07-18 19:45:38 +030061
62const parseDate = (date, format) => {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020063 return typeof date === 'number' ? moment.unix(date) : moment(date, format);
Avi Zivb8e2faf2017-07-18 19:45:38 +030064};
65
66class Datepicker extends React.Component {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020067 static propTypes = {
68 date: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
69 format: PropTypes.string,
70 onChange: PropTypes.func,
71 selectsStart: PropTypes.bool,
72 selectsEnd: PropTypes.bool,
73 startDate: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
74 endDate: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
75 disabled: PropTypes.bool,
76 label: PropTypes.string,
77 isRequired: PropTypes.bool
78 };
79 render() {
80 let {
81 date,
82 format,
83 onChange,
84 selectsStart = false,
85 startDate = null,
86 endDate = null,
87 selectsEnd = false,
88 disabled = false,
89 inputRef
90 } = this.props;
91 const placeholderText = 'Enter a date';
92 const props = {
93 format,
94 onChange,
95 disabled,
96 selected: date ? parseDate(date, format) : date,
97 selectsStart,
98 selectsEnd,
99 placeholderText,
100 startDate: startDate ? parseDate(startDate, format) : startDate,
101 endDate: endDate ? parseDate(endDate, format) : endDate
102 };
Avi Zivb8e2faf2017-07-18 19:45:38 +0300103
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200104 return (
105 <div className="customized-date-picker">
106 <DatePicker
107 calendarClassName="customized-date-picker-calendar"
108 customInput={
109 <CustomInput
110 inputRef={inputRef}
111 onClear={() => onChange(undefined)}
112 placeholderText={placeholderText}
113 />
114 }
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300115 minDate={
116 selectsEnd && props.startDate
117 ? props.startDate
118 : undefined
119 }
120 maxDate={
121 selectsStart && props.endDate
122 ? props.endDate
123 : undefined
124 }
125 popperModifiers={{
126 preventOverflow: {
127 boundariesElement: 'scrollParent'
128 }
129 }}
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200130 {...props}
131 />
132 </div>
133 );
134 }
Avi Zivb8e2faf2017-07-18 19:45:38 +0300135}
136
137export default Datepicker;