react-toolbox/components/date_picker/DatePicker.js

82 lines
2.3 KiB
JavaScript
Raw Normal View History

import React from 'react';
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';
import Input from '../input';
2015-10-22 02:31:17 +03:00
import style from './style';
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 {
static propTypes = {
2016-03-23 17:20:29 +03:00
autoOk: React.PropTypes.bool,
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
]),
inputClassName: React.PropTypes.string,
2016-03-09 17:07:16 +03:00
inputFormat: React.PropTypes.func,
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,
value: React.PropTypes.oneOfType([
React.PropTypes.instanceOf(Date),
React.PropTypes.string
])
};
2015-09-07 02:59:11 +03:00
state = {
2015-11-11 03:29:42 +03:00
active: false
};
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) => {
events.pauseEvent(event);
2015-11-11 03:29:42 +03:00
this.setState({active: true});
};
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 () {
const { inputClassName, value } = this.props;
const inputFormat = this.props.inputFormat || time.formatDate;
const date = Object.prototype.toString.call(value) === '[object Date]' ? value : undefined;
const formattedDate = date === undefined ? '' : inputFormat(value);
2015-11-16 11:56:05 +03:00
2015-09-07 02:59:11 +03:00
return (
<div data-react-toolbox='date-picker'>
2015-09-07 02:59:11 +03:00
<Input
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}
label={this.props.label}
readOnly
type='text'
2016-04-28 16:15:40 +03:00
icon={this.props.icon}
value={formattedDate}
/>
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}
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}
value={date}
/>
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;