react-toolbox/components/time_picker/TimePicker.js

137 lines
3.9 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';
2016-05-31 11:23:05 +03:00
import { TIME_PICKER } from '../identifiers.js';
import events from '../utils/events.js';
import time from '../utils/time.js';
import InjectDialog from '../dialog/Dialog.js';
import InjectInput from '../input/Input.js';
import timePickerDialogFactory from './TimePickerDialog.js';
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({
input: PropTypes.string
2016-05-31 11:23:05 +03:00
}),
value: PropTypes.object
};
2016-05-31 11:23:05 +03:00
static defaultProps = {
active: false,
2016-05-31 11:23:05 +03:00
className: '',
format: '24hr'
};
2016-05-31 11:23:05 +03:00
state = {
active: this.props.active
2016-05-31 11:23:05 +03:00
};
componentWillReceiveProps (nextProps) {
if (this.state.active !== nextProps.active) {
this.setState({ active: nextProps.active });
}
}
2016-05-31 11:23:05 +03:00
handleDismiss = () => {
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);
this.setState({active: true});
};
handleInputBlur = (event) => {
events.pauseEvent(event);
this.setState({active: false});
};
handleInputClick = (event) => {
2016-05-31 11:23:05 +03:00
events.pauseEvent(event);
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);
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);
this.setState({active: false});
};
2016-05-31 11:23:05 +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 (
<div data-react-toolbox='time-picker'>
<Input
{...others}
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
type='text'
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 };