blob: 25e7e7e02d0ad20c358cf0bd1736c1a08d924974 [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 {
8
9 static propTypes = {
talig8e9c0652017-12-20 14:30:43 +020010 placeHolderText: PropTypes.string,
11 onChange: PropTypes.func,
12 onClick: PropTypes.func,
13 value: PropTypes.string
Avi Zivb8e2faf2017-07-18 19:45:38 +030014 };
15
16 render() {
17 const {placeholderText, onClick, onClear, inputRef, value: date} = this.props;
18 const text = date ? date : placeholderText;
19 const textStyle = date ? '' : 'placeholder';
20 return (
Avi Ziv61070c92017-07-26 17:37:57 +030021 <div onClick={onClick} ref={inputRef} className='datepicker-custom-input'>
22 <div className={`datepicker-text ${textStyle}`}>{text}</div>
23 {date && <SVGIcon onClick={e => {e.stopPropagation(); onClear();}} name='close' className='clear-input'/>}
24 <SVGIcon name='calendar'/>
Avi Zivb8e2faf2017-07-18 19:45:38 +030025 </div>
26 );
27 }
28};
29
30const parseDate = (date, format) => {
31 return typeof date === 'number' ? moment.unix(date) : moment(date, format);
32};
33
34class Datepicker extends React.Component {
35 static propTypes = {
talig8e9c0652017-12-20 14:30:43 +020036 date: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
37 format: PropTypes.string,
38 onChange: PropTypes.func,
39 selectsStart: PropTypes.bool,
40 selectsEnd: PropTypes.bool,
41 startDate: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
42 endDate: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
43 disabled: PropTypes.bool,
44 label: PropTypes.string,
45 isRequired: PropTypes.bool
Avi Zivb8e2faf2017-07-18 19:45:38 +030046 }
47 render() {
48 let {date, format, onChange, selectsStart = false, startDate = null, endDate = null, selectsEnd = false,
49 disabled = false, inputRef} = this.props;
50 const placeholderText = 'Enter a date';
51 const props = {
52 format,
53 onChange,
54 disabled,
55 selected: date ? parseDate(date, format) : date,
56 selectsStart,
57 selectsEnd,
58 placeholderText,
59 startDate: startDate ? parseDate(startDate, format) : startDate,
60 endDate: endDate ? parseDate(endDate, format) : endDate
61 };
62
63 return (
64 <div className='customized-date-picker'>
65 <DatePicker
66 calendarClassName='customized-date-picker-calendar'
67 customInput={<CustomInput inputRef={inputRef} onClear={() => onChange(undefined)} placeholderText={placeholderText}/>}
68 minDate={selectsEnd && props.startDate}
69 maxDate={selectsStart && props.endDate}
70 {...props}/>
71 </div>
72 );
73 }
74}
75
76export default Datepicker;