react-toolbox/components/time_picker/TimePicker.js

138 lines
4.1 KiB
JavaScript
Raw Normal View History

2016-05-31 11:23:05 +03:00
import React, { Component, PropTypes } from 'react';
import classnames from 'classnames';
2016-05-22 14:25:56 +03:00
import { themr } from 'react-css-themr';
2017-01-26 20:05:32 +03:00
import { TIME_PICKER } from '../identifiers';
import events from '../utils/events';
import time from '../utils/time';
import InjectDialog from '../dialog/Dialog';
import InjectInput from '../input/Input';
import timePickerDialogFactory from './TimePickerDialog';
2016-05-31 11:23:05 +03:00
const factory = (TimePickerDialog, Input) => {
class TimePicker extends Component {
static propTypes = {
active: PropTypes.bool,
cancelLabel: PropTypes.string,
2016-05-31 11:23:05 +03:00
className: PropTypes.string,
error: PropTypes.string,
format: PropTypes.oneOf(['24hr', 'ampm']),
inputClassName: PropTypes.string,
label: PropTypes.string,
2016-06-09 20:21:22 +03:00
name: PropTypes.string,
okLabel: PropTypes.string,
2016-05-31 11:23:05 +03:00
onChange: PropTypes.func,
2016-10-06 21:29:36 +03:00
onClick: PropTypes.func,
2016-10-22 17:56:18 +03:00
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,
2016-05-31 11:23:05 +03:00
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,
2016-05-31 11:23:05 +03:00
}),
2017-01-26 20:05:32 +03:00
value: PropTypes.instanceOf(Date),
2016-05-31 11:23:05 +03:00
};
2016-05-31 11:23:05 +03:00
static defaultProps = {
active: false,
2016-05-31 11:23:05 +03:00
className: '',
2017-01-26 20:05:32 +03:00
format: '24hr',
2016-05-31 11:23:05 +03:00
};
2016-05-31 11:23:05 +03:00
state = {
2017-01-26 20:05:32 +03:00
active: this.props.active,
2016-05-31 11:23:05 +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 });
}
}
2016-05-31 11:23:05 +03:00
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-05-31 11:23:05 +03:00
};
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) => {
2016-05-31 11:23:05 +03:00
events.pauseEvent(event);
2017-01-26 20:05:32 +03:00
this.setState({ active: true });
if (this.props.onClick) this.props.onClick(event);
2016-05-31 11:23:05 +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);
};
2016-05-31 11:23:05 +03:00
handleSelect = (value, event) => {
if (this.props.onChange) this.props.onChange(value, event);
2017-01-26 20:05:32 +03:00
this.setState({ active: false });
2016-05-31 11:23:05 +03:00
};
2017-01-26 20:05:32 +03:00
render() {
const {
2016-10-22 17:56:18 +03:00
active, onDismiss, // eslint-disable-line
cancelLabel, format, inputClassName, okLabel, onEscKeyDown, onOverlayClick,
readonly, value, ...others
} = this.props;
2016-05-31 11:23:05 +03:00
const formattedTime = value ? time.formatTime(value, format) : '';
return (
2017-01-26 20:05:32 +03:00
<div data-react-toolbox="time-picker" className={this.props.theme.container}>
2016-05-31 11:23:05 +03:00
<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}
2016-05-31 11:23:05 +03:00
error={this.props.error}
label={this.props.label}
2016-08-04 21:48:52 +03:00
name={this.props.name}
onKeyPress={this.handleInputKeyPress}
onClick={this.handleInputClick}
2016-05-31 11:23:05 +03:00
readOnly
2017-01-26 20:05:32 +03:00
type="text"
2016-05-31 11:23:05 +03:00
value={formattedTime}
/>
<TimePickerDialog
active={this.state.active}
cancelLabel={cancelLabel}
2016-05-31 11:23:05 +03:00
className={this.props.className}
format={format}
2016-07-10 14:42:35 +03:00
name={this.props.name}
okLabel={okLabel}
2016-05-31 11:23:05 +03:00
onDismiss={this.handleDismiss}
2016-07-10 14:42:35 +03:00
onEscKeyDown={onEscKeyDown}
onOverlayClick={onOverlayClick}
2016-05-31 11:23:05 +03:00
onSelect={this.handleSelect}
theme={this.props.theme}
value={this.props.value}
/>
</div>
);
}
}
2016-05-31 11:23:05 +03:00
return TimePicker;
};
const TimePickerDialog = timePickerDialogFactory(InjectDialog);
const TimePicker = factory(TimePickerDialog, InjectInput);
export default themr(TIME_PICKER)(TimePicker);
export { factory as timePickerFactory };
export { TimePicker };