react-toolbox/components/date_picker/DatePicker.js

173 lines
5.4 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';
2017-01-26 20:05:32 +03:00
import { DATE_PICKER } from '../identifiers';
import events from '../utils/events';
import time from '../utils/time';
2015-09-07 02:59:11 +03:00
2017-01-26 20:05:32 +03:00
import InjectIconButton from '../button/IconButton';
import InjectInput from '../input/Input';
import InjectDialog from '../dialog/Dialog';
import calendarFactory from './Calendar';
import datePickerDialogFactory from './DatePickerDialog';
2015-09-07 02:59:11 +03:00
const factory = (Input, DatePickerDialog) => {
class DatePicker extends Component {
static propTypes = {
active: PropTypes.bool,
autoOk: PropTypes.bool,
cancelLabel: PropTypes.string,
className: PropTypes.string,
2017-01-26 20:05:32 +03:00
disabledDates: React.PropTypes.arrayOf(PropTypes.instanceOf(Date)),
enabledDates: React.PropTypes.arrayOf(PropTypes.instanceOf(Date)),
error: PropTypes.string,
icon: PropTypes.oneOfType([
PropTypes.string,
2017-01-26 20:05:32 +03:00
PropTypes.element,
]),
inputClassName: PropTypes.string,
inputFormat: PropTypes.func,
label: PropTypes.string,
locale: React.PropTypes.oneOfType([
React.PropTypes.string,
2017-01-26 20:05:32 +03:00
React.PropTypes.object,
]),
2017-01-26 20:05:32 +03:00
maxDate: PropTypes.instanceOf(Date),
minDate: PropTypes.instanceOf(Date),
2016-06-09 20:21:22 +03:00
name: PropTypes.string,
okLabel: PropTypes.string,
onChange: PropTypes.func,
2016-10-06 21:29:36 +03:00
onClick: PropTypes.func,
onDismiss: 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,
2016-08-04 21:48:52 +03:00
readonly: PropTypes.bool,
sundayFirstDayOfWeek: React.PropTypes.bool,
theme: PropTypes.shape({
2017-01-24 13:19:25 +03:00
container: PropTypes.string,
2017-01-26 20:05:32 +03:00
input: PropTypes.string,
}),
value: PropTypes.oneOfType([
PropTypes.instanceOf(Date),
2017-01-26 20:05:32 +03:00
PropTypes.string,
]),
};
2015-09-07 02:59:11 +03:00
static defaultProps = {
active: false,
locale: 'en',
2017-01-26 20:05:32 +03:00
sundayFirstDayOfWeek: false,
};
2015-09-07 02:59:11 +03:00
state = {
2017-01-26 20:05:32 +03:00
active: this.props.active,
};
2015-11-10 16:27:00 +03:00
2017-01-26 20:05:32 +03:00
componentWillReceiveProps(nextProps) {
2016-11-05 14:04:59 +03:00
if (nextProps.active !== this.props.active && this.state.active !== nextProps.active) {
this.setState({ active: nextProps.active });
}
}
handleDismiss = () => {
2017-01-26 20:05:32 +03:00
this.setState({ active: false });
2016-10-22 17:56:18 +03:00
if (this.props.onDismiss) {
this.props.onDismiss();
}
};
2016-08-04 21:48:52 +03:00
handleInputFocus = (event) => {
events.pauseEvent(event);
2017-01-26 20:05:32 +03:00
this.setState({ active: true });
2016-08-04 21:48:52 +03:00
};
handleInputBlur = (event) => {
events.pauseEvent(event);
2017-01-26 20:05:32 +03:00
this.setState({ active: false });
2016-08-04 21:48:52 +03:00
};
handleInputClick = (event) => {
events.pauseEvent(event);
2017-01-26 20:05:32 +03:00
this.setState({ active: true });
if (this.props.onClick) this.props.onClick(event);
};
2015-09-07 02:59:11 +03:00
handleInputKeyPress = (event) => {
if (event.charCode === 13) {
events.pauseEvent(event);
2017-01-26 20:05:32 +03:00
this.setState({ active: true });
}
if (this.props.onKeyPress) this.props.onKeyPress(event);
};
handleSelect = (value, event) => {
if (this.props.onChange) this.props.onChange(value, event);
2017-01-26 20:05:32 +03:00
this.setState({ active: false });
};
2015-11-16 11:56:05 +03:00
2017-01-26 20:05:32 +03:00
render() {
2016-10-22 17:56:18 +03:00
const { active, onDismiss,// eslint-disable-line
2016-11-22 18:38:03 +03:00
autoOk, cancelLabel, enabledDates, disabledDates, inputClassName, inputFormat,
locale, maxDate, minDate, okLabel, onEscKeyDown, onOverlayClick, readonly,
sundayFirstDayOfWeek, value, ...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 (
2017-01-26 20:05:32 +03:00
<div data-react-toolbox="date-picker" className={this.props.theme.container}>
<Input
{...others}
2017-01-26 20:05:32 +03:00
className={classnames(this.props.theme.input, { [inputClassName]: inputClassName })}
2016-08-04 21:48:52 +03:00
disabled={readonly}
error={this.props.error}
2016-08-04 21:48:52 +03:00
icon={this.props.icon}
label={this.props.label}
2016-08-04 21:48:52 +03:00
name={this.props.name}
onFocus={this.handleInputFocus}
onKeyPress={this.handleInputKeyPress}
onClick={this.handleInputClick}
readOnly
2017-01-26 20:05:32 +03:00
type="text"
value={formattedDate}
/>
<DatePickerDialog
active={this.state.active}
2016-07-10 14:42:35 +03:00
autoOk={autoOk}
cancelLabel={cancelLabel}
className={this.props.className}
disabledDates={disabledDates}
enabledDates={enabledDates}
2016-08-07 14:43:07 +03:00
locale={locale}
2016-07-10 14:42:35 +03:00
maxDate={maxDate}
minDate={minDate}
name={this.props.name}
2016-10-22 17:56:18 +03:00
onDismiss={this.handleDismiss}
okLabel={okLabel}
onEscKeyDown={onEscKeyDown || this.handleDismiss}
onOverlayClick={onOverlayClick || this.handleDismiss}
onSelect={this.handleSelect}
2016-08-07 14:43:07 +03:00
sundayFirstDayOfWeek={sundayFirstDayOfWeek}
theme={this.props.theme}
value={date}
/>
</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);
2016-07-21 17:47:03 +03:00
export {
2017-01-26 20:05:32 +03:00
DatePickerDialog,
factory as datePickerFactory,
2016-07-21 17:47:03 +03:00
};
export { DatePicker };