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