2015-10-11 20:07:09 +03:00
|
|
|
import React from 'react';
|
2016-04-10 19:53:44 +03:00
|
|
|
import classnames from 'classnames';
|
2015-11-23 00:13:27 +03:00
|
|
|
import DatePickerDialog from './DatePickerDialog';
|
2015-10-22 02:31:17 +03:00
|
|
|
import events from '../utils/events';
|
2015-11-16 18:54:05 +03:00
|
|
|
import Input from '../input';
|
2015-10-22 02:31:17 +03:00
|
|
|
import style from './style';
|
2015-11-16 18:54:05 +03:00
|
|
|
import time from '../utils/time';
|
2015-09-07 02:59:11 +03:00
|
|
|
|
2015-10-22 02:31:17 +03:00
|
|
|
class DatePicker extends React.Component {
|
2015-10-20 08:40:51 +03:00
|
|
|
static propTypes = {
|
2016-03-23 17:20:29 +03:00
|
|
|
autoOk: React.PropTypes.bool,
|
2015-12-20 20:08:26 +03:00
|
|
|
className: React.PropTypes.string,
|
2015-12-20 19:51:18 +03:00
|
|
|
error: React.PropTypes.string,
|
2016-05-10 22:33:34 +03:00
|
|
|
icon: React.PropTypes.oneOfType([
|
|
|
|
React.PropTypes.string,
|
|
|
|
React.PropTypes.element
|
|
|
|
]),
|
2016-04-10 19:53:44 +03:00
|
|
|
inputClassName: React.PropTypes.string,
|
2016-03-09 17:07:16 +03:00
|
|
|
inputFormat: React.PropTypes.func,
|
2015-11-21 16:46:24 +03:00
|
|
|
label: React.PropTypes.string,
|
2015-11-16 11:56:05 +03:00
|
|
|
maxDate: React.PropTypes.object,
|
|
|
|
minDate: React.PropTypes.object,
|
2015-11-10 16:27:00 +03:00
|
|
|
onChange: React.PropTypes.func,
|
2016-04-22 06:34:48 +03:00
|
|
|
value: React.PropTypes.oneOfType([
|
|
|
|
React.PropTypes.instanceOf(Date),
|
|
|
|
React.PropTypes.string
|
|
|
|
])
|
2015-10-20 08:40:51 +03:00
|
|
|
};
|
2015-09-07 02:59:11 +03:00
|
|
|
|
2015-10-20 08:40:51 +03:00
|
|
|
state = {
|
2015-11-11 03:29:42 +03:00
|
|
|
active: false
|
2015-10-20 08:40:51 +03:00
|
|
|
};
|
2015-09-07 02:59:11 +03:00
|
|
|
|
2015-11-11 03:29:42 +03:00
|
|
|
handleDismiss = () => {
|
|
|
|
this.setState({active: false});
|
2015-11-10 16:27:00 +03:00
|
|
|
};
|
|
|
|
|
2015-11-11 03:29:42 +03:00
|
|
|
handleInputMouseDown = (event) => {
|
2015-11-07 05:36:51 +03:00
|
|
|
events.pauseEvent(event);
|
2015-11-11 03:29:42 +03:00
|
|
|
this.setState({active: true});
|
2015-11-07 05:36:51 +03:00
|
|
|
};
|
|
|
|
|
2015-11-25 13:40:14 +03:00
|
|
|
handleSelect = (value, event) => {
|
|
|
|
if (this.props.onChange) this.props.onChange(value, event);
|
2015-11-11 03:29:42 +03:00
|
|
|
this.setState({active: false});
|
|
|
|
};
|
2015-09-07 02:59:11 +03:00
|
|
|
|
|
|
|
render () {
|
2016-04-10 19:53:44 +03:00
|
|
|
const { inputClassName, value } = this.props;
|
2016-03-08 23:40:16 +03:00
|
|
|
const inputFormat = this.props.inputFormat || time.formatDate;
|
2016-04-22 06:34:48 +03:00
|
|
|
const date = Object.prototype.toString.call(value) === '[object Date]' ? value : undefined;
|
2016-04-28 03:12:59 +03:00
|
|
|
const formattedDate = date === undefined ? '' : inputFormat(value);
|
2015-11-16 11:56:05 +03:00
|
|
|
|
2015-09-07 02:59:11 +03:00
|
|
|
return (
|
2016-02-06 11:35:08 +03:00
|
|
|
<div data-react-toolbox='date-picker'>
|
2015-09-07 02:59:11 +03:00
|
|
|
<Input
|
2016-04-10 19:53:44 +03:00
|
|
|
className={classnames(style.input, {[inputClassName]: inputClassName })}
|
2015-12-20 19:51:18 +03:00
|
|
|
error={this.props.error}
|
2015-11-11 03:29:42 +03:00
|
|
|
onMouseDown={this.handleInputMouseDown}
|
2015-11-21 16:46:24 +03:00
|
|
|
label={this.props.label}
|
2015-11-22 21:20:05 +03:00
|
|
|
readOnly
|
2015-11-07 05:36:51 +03:00
|
|
|
type='text'
|
2016-04-28 16:15:40 +03:00
|
|
|
icon={this.props.icon}
|
2016-04-22 06:34:48 +03:00
|
|
|
value={formattedDate}
|
2015-10-11 20:07:09 +03:00
|
|
|
/>
|
2015-11-23 00:13:27 +03:00
|
|
|
<DatePickerDialog
|
2016-03-09 17:56:10 +03:00
|
|
|
autoOk={this.props.autoOk}
|
2015-11-11 03:29:42 +03:00
|
|
|
active={this.state.active}
|
2015-12-20 20:08:26 +03:00
|
|
|
className={this.props.className}
|
2015-11-16 11:56:05 +03:00
|
|
|
maxDate={this.props.maxDate}
|
|
|
|
minDate={this.props.minDate}
|
2015-11-11 03:29:42 +03:00
|
|
|
onDismiss={this.handleDismiss}
|
|
|
|
onSelect={this.handleSelect}
|
2016-04-22 06:34:48 +03:00
|
|
|
value={date}
|
2015-10-11 20:07:09 +03:00
|
|
|
/>
|
2015-09-07 02:59:11 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2015-10-21 13:25:07 +03:00
|
|
|
}
|
2015-10-22 02:31:17 +03:00
|
|
|
|
|
|
|
export default DatePicker;
|