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 { |
| 8 | |
| 9 | static propTypes = { |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame^] | 10 | placeHolderText: PropTypes.string, |
| 11 | onChange: PropTypes.func, |
| 12 | onClick: PropTypes.func, |
| 13 | value: PropTypes.string |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 14 | }; |
| 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 Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 21 | <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 Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 25 | </div> |
| 26 | ); |
| 27 | } |
| 28 | }; |
| 29 | |
| 30 | const parseDate = (date, format) => { |
| 31 | return typeof date === 'number' ? moment.unix(date) : moment(date, format); |
| 32 | }; |
| 33 | |
| 34 | class Datepicker extends React.Component { |
| 35 | static propTypes = { |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame^] | 36 | 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 Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 46 | } |
| 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 | |
| 76 | export default Datepicker; |