2016-05-31 11:23:05 +03:00
|
|
|
import React, { Component, PropTypes } from 'react';
|
2016-04-10 19:53:44 +03:00
|
|
|
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';
|
2015-11-22 23:41:28 +03:00
|
|
|
|
2016-05-31 11:23:05 +03:00
|
|
|
const factory = (TimePickerDialog, Input) => {
|
|
|
|
class TimePicker extends Component {
|
|
|
|
static propTypes = {
|
|
|
|
className: PropTypes.string,
|
|
|
|
error: PropTypes.string,
|
|
|
|
format: PropTypes.oneOf(['24hr', 'ampm']),
|
|
|
|
inputClassName: PropTypes.string,
|
2016-06-06 20:37:51 +03:00
|
|
|
name: PropTypes.string,
|
2016-05-31 11:23:05 +03:00
|
|
|
label: PropTypes.string,
|
|
|
|
onChange: PropTypes.func,
|
|
|
|
theme: PropTypes.shape({
|
2016-06-04 00:44:33 +03:00
|
|
|
input: PropTypes.string
|
2016-05-31 11:23:05 +03:00
|
|
|
}),
|
|
|
|
value: PropTypes.object
|
|
|
|
};
|
2015-11-22 23:41:28 +03:00
|
|
|
|
2016-05-31 11:23:05 +03:00
|
|
|
static defaultProps = {
|
|
|
|
className: '',
|
|
|
|
format: '24hr'
|
|
|
|
};
|
2015-11-22 23:41:28 +03:00
|
|
|
|
2016-05-31 11:23:05 +03:00
|
|
|
state = {
|
|
|
|
active: false
|
|
|
|
};
|
2015-11-22 23:41:28 +03:00
|
|
|
|
2016-05-31 11:23:05 +03:00
|
|
|
handleDismiss = () => {
|
|
|
|
this.setState({active: false});
|
|
|
|
};
|
2015-11-22 23:41:28 +03:00
|
|
|
|
2016-05-31 11:23:05 +03:00
|
|
|
handleInputMouseDown = (event) => {
|
|
|
|
events.pauseEvent(event);
|
|
|
|
this.setState({active: true});
|
|
|
|
};
|
2015-11-22 23:41:28 +03:00
|
|
|
|
2016-05-31 11:23:05 +03:00
|
|
|
handleSelect = (value, event) => {
|
|
|
|
if (this.props.onChange) this.props.onChange(value, event);
|
|
|
|
this.setState({active: false});
|
|
|
|
};
|
2015-11-22 23:41:28 +03:00
|
|
|
|
2016-05-31 11:23:05 +03:00
|
|
|
render () {
|
|
|
|
const { value, format, inputClassName, theme } = this.props;
|
|
|
|
const formattedTime = value ? time.formatTime(value, format) : '';
|
|
|
|
return (
|
|
|
|
<div data-react-toolbox='time-picker'>
|
|
|
|
<Input
|
|
|
|
className={classnames(theme.input, {[inputClassName]: inputClassName })}
|
|
|
|
error={this.props.error}
|
2016-06-06 20:37:51 +03:00
|
|
|
name={this.props.name}
|
2016-05-31 11:23:05 +03:00
|
|
|
label={this.props.label}
|
|
|
|
onMouseDown={this.handleInputMouseDown}
|
|
|
|
readOnly
|
|
|
|
type='text'
|
|
|
|
value={formattedTime}
|
|
|
|
/>
|
|
|
|
<TimePickerDialog
|
|
|
|
active={this.state.active}
|
|
|
|
className={this.props.className}
|
2016-06-06 20:37:51 +03:00
|
|
|
name={this.props.name}
|
2016-05-31 11:23:05 +03:00
|
|
|
format={format}
|
|
|
|
onDismiss={this.handleDismiss}
|
|
|
|
onSelect={this.handleSelect}
|
|
|
|
theme={this.props.theme}
|
|
|
|
value={this.props.value}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2015-11-22 23:41:28 +03:00
|
|
|
}
|
|
|
|
|
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 };
|