Created
October 16, 2015 11:09
-
-
Save radubrehar/d463d2a3450f1779ac41 to your computer and use it in GitHub Desktop.
react-date-picker input integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import assign from 'object-assign'; | |
import Input from 'react-filed'; | |
import join from 'classnames'; | |
import DatePicker from 'react-date-picker'; | |
import moment from 'moment'; | |
import ClassName from './index.local.styl'; | |
export default class DateInput extends React.Component { | |
constructor(props){ | |
super(props); | |
this.state = { | |
focused: false | |
}; | |
} | |
toDate(date){ | |
if (!date){ | |
return ''; | |
} | |
return moment(date, this.props.dateFormat); | |
} | |
toDateString(date){ | |
if (!date){ | |
return ''; | |
} | |
if (typeof date != 'string'){ | |
date = moment(date) | |
.format(this.props.displayDateFormat || this.props.dateFormat); | |
} | |
return date; | |
} | |
render(){ | |
const props = this.props; | |
const state = this.state; | |
this.date = this.toDate(props.date || props.value); | |
this.dateString = this.toDateString(this.date); | |
const className = join( | |
props.className, | |
ClassName.dateField, | |
state.focused? ClassName.focused: '' | |
); | |
const inputProps = assign({}, props.inputProps, { | |
onChange: props.onChange, | |
value: props.value, | |
placeholder: props.placeholder | |
}); | |
return <div {...props} className={className}> | |
<Input | |
readOnly | |
{...inputProps} | |
value={this.dateString} | |
ref="input" | |
onFocus={this.onFocus} | |
onBlur={this.onBlur} | |
onClick={this.onClick} | |
/> | |
{this.renderPicker()} | |
</div>; | |
} | |
renderPicker(){ | |
if (!this.state.focused || !this.state.pickerVisible){ | |
return null; | |
} | |
const props = this.props; | |
const datePickerProps = { | |
className: ClassName.picker, | |
date: this.date, | |
dateFormat: props.dateFormat, | |
onChange: this.onChange, | |
style: props.pickerStyle, | |
onMouseDown: this.onPickerMouseDown | |
}; | |
let result; | |
if (props.renderPicker){ | |
result = props.renderPicker(datePickerProps); | |
} | |
if (result === undefined){ | |
return <DatePicker {...datePickerProps}/>; | |
} | |
} | |
showPicker(){ | |
this.setState({ | |
pickerVisible: true | |
}); | |
} | |
togglePicker(){ | |
this.setState({ | |
pickerVisible: !this.state.pickerVisible | |
}); | |
} | |
onClick(){ | |
if (this.state.focused && !this.state.pickerVisible){ | |
this.togglePicker(); | |
} | |
} | |
onPickerMouseDown(event){ | |
event.preventDefault(); | |
} | |
onChange(dateString){ | |
this.setState({ | |
pickerVisible: false | |
}); | |
this.props.onChange(dateString); | |
} | |
focus(){ | |
React.findDOMNode(this.refs.input).focus(); | |
} | |
onBlur(event){ | |
this.setState({ | |
focused: false | |
}); | |
this.props.onBlur(event); | |
} | |
onFocus(event){ | |
this.setState({ | |
focused: true, | |
pickerVisible: true | |
}); | |
this.props.onFocus(event); | |
} | |
} | |
DateInput.propTypes = { | |
onFocus: PropTypes.func, | |
onBlur: PropTypes.func, | |
onChange: PropTypes.func, | |
dateFormat: PropTypes.string | |
}; | |
DateInput.defaultProps = { | |
dateFormat: 'YYYY-MM-DDTHH:mm:ss', | |
displayDateFormat: 'YYYY-MM-YY', | |
onChange: () => {}, | |
onBlur: () => {}, | |
onFocus: () => {} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment