Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 1 | import React from 'react'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 2 | import PropTypes from 'prop-types'; |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 3 | import DatePicker from 'react-datepicker'; |
| 4 | import moment from 'moment'; |
| 5 | import SVGIcon from 'sdc-ui/lib/react/SVGIcon.js'; |
| 6 | |
| 7 | class CustomInput extends React.Component { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 8 | static propTypes = { |
| 9 | placeHolderText: PropTypes.string, |
| 10 | onChange: PropTypes.func, |
| 11 | onClick: PropTypes.func, |
| 12 | value: PropTypes.string |
| 13 | }; |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 14 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 15 | 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 Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 46 | |
| 47 | const parseDate = (date, format) => { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 48 | return typeof date === 'number' ? moment.unix(date) : moment(date, format); |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | class Datepicker extends React.Component { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 52 | 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 Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 88 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 89 | 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 Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | export default Datepicker; |