react-toolbox/components/date_picker/DatePicker.js

132 lines
4.0 KiB
JavaScript
Raw Normal View History

import React, { Component, PropTypes } from 'react';
import classnames from 'classnames';
2016-05-21 21:52:48 +03:00
import { themr } from 'react-css-themr';
import { DATE_PICKER } from '../identifiers.js';
import events from '../utils/events.js';
import time from '../utils/time.js';
2015-09-07 02:59:11 +03:00
import InjectIconButton from '../button/IconButton.js';
import InjectInput from '../input/Input.js';
import InjectDialog from '../dialog/Dialog.js';
import calendarFactory from './Calendar.js';
import datePickerDialogFactory from './DatePickerDialog.js';
2015-09-07 02:59:11 +03:00
const factory = (Input, DatePickerDialog) => {
class DatePicker extends Component {
static propTypes = {
autoOk: PropTypes.bool,
className: PropTypes.string,
error: PropTypes.string,
icon: PropTypes.oneOfType([
PropTypes.string,
PropTypes.element
]),
inputClassName: PropTypes.string,
inputFormat: PropTypes.func,
label: PropTypes.string,
maxDate: PropTypes.object,
minDate: PropTypes.object,
2016-06-09 20:21:22 +03:00
name: PropTypes.string,
onChange: PropTypes.func,
2016-07-10 14:42:35 +03:00
onEscKeyDown: PropTypes.func,
onKeyPress: PropTypes.func,
2016-07-10 14:42:35 +03:00
onOverlayClick: PropTypes.func,
theme: PropTypes.shape({
input: PropTypes.string
}),
value: PropTypes.oneOfType([
PropTypes.instanceOf(Date),
PropTypes.string
]),
locale: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.object
]),
sundayFirstDayOfWeek: React.PropTypes.bool
};
static defaultProps = {
locale: 'en',
sundayFirstDayOfWeek: false
};
2015-09-07 02:59:11 +03:00
state = {
active: false
};
2015-11-10 16:27:00 +03:00
handleDismiss = () => {
this.setState({active: false});
};
handleInputMouseDown = (event) => {
events.pauseEvent(event);
this.setState({active: true});
};
2015-09-07 02:59:11 +03:00
handleInputKeyPress = (event) => {
if (event.charCode === 13) {
events.pauseEvent(event);
this.setState({active: true});
}
if (this.props.onKeyPress) this.props.onKeyPress(event);
};
handleSelect = (value, event) => {
if (this.props.onChange) this.props.onChange(value, event);
this.setState({active: false});
};
2015-11-16 11:56:05 +03:00
render () {
const { autoOk, inputClassName, inputFormat, maxDate, minDate, sundayFirstDayOfWeek,
onEscKeyDown, onOverlayClick, value, locale, ...others } = this.props;
2016-07-10 14:42:35 +03:00
const finalInputFormat = inputFormat || time.formatDate;
const date = Object.prototype.toString.call(value) === '[object Date]' ? value : undefined;
const formattedDate = date === undefined ? '' : finalInputFormat(value, locale);
return (
<div data-react-toolbox='date-picker'>
<Input
{...others}
className={classnames(this.props.theme.input, {[inputClassName]: inputClassName })}
error={this.props.error}
onMouseDown={this.handleInputMouseDown}
onKeyPress={this.handleInputKeyPress}
name={this.props.name}
label={this.props.label}
readOnly
type='text'
icon={this.props.icon}
value={formattedDate}
/>
<DatePickerDialog
active={this.state.active}
2016-07-10 14:42:35 +03:00
autoOk={autoOk}
className={this.props.className}
2016-07-10 14:42:35 +03:00
maxDate={maxDate}
minDate={minDate}
name={this.props.name}
onDismiss={this.handleDismiss}
2016-07-10 14:42:35 +03:00
onEscKeyDown={onEscKeyDown}
onOverlayClick={onOverlayClick}
onSelect={this.handleSelect}
theme={this.props.theme}
value={date}
locale={locale}
sundayFirstDayOfWeek={sundayFirstDayOfWeek}
/>
</div>
);
}
2015-09-07 02:59:11 +03:00
}
2015-10-22 02:31:17 +03:00
return DatePicker;
};
const Calendar = calendarFactory(InjectIconButton);
const DatePickerDialog = datePickerDialogFactory(InjectDialog, Calendar);
const DatePicker = factory(InjectInput, DatePickerDialog);
export default themr(DATE_PICKER)(DatePicker);
export { factory as datePickerFactory };
export { DatePicker };