Skip to content

Commit

Permalink
Port timepicker index to ES6
Browse files Browse the repository at this point in the history
  • Loading branch information
javivelasco committed Sep 5, 2015
1 parent 7aa068a commit 15d8f47
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 64 deletions.
64 changes: 0 additions & 64 deletions components/time_picker/index.cjsx

This file was deleted.

64 changes: 64 additions & 0 deletions components/time_picker/index.jsx
@@ -0,0 +1,64 @@
const React = window.React;

const Input = require('../input');
const TimeDialog = require('./dialog');
const utils = require('../utils/date-time');

module.exports = React.createClass({
displayName: 'TimePicker',

propTypes: {
format: React.PropTypes.oneOf(['24hr', 'ampm']),
value: React.PropTypes.object
},

getDefaultProps () {
return {
format: '24hr'
};
},

getInitialState () {
return {
value: this.props.value
};
},

onTimeSelected (time) {
this.refs.input.setValue(this.formatTime(time));
this.setState({value: time});
},

openTimeDialog () {
this.refs.dialog.show();
},

formatTime () {
if (this.state.value) {
return utils.formatTime(this.state.value, this.props.format);
}
},

getValue () {
return this.state.value;
},

render () {
return (
<div>
<Input
ref="input"
type="text"
disabled={true}
onClick={this.openTimeDialog}
placeholder="Pick up time"
value={this.formatTime()} />
<TimeDialog
ref="dialog"
initialTime={this.state.value}
format={this.props.format}
onTimeSelected={this.onTimeSelected} />
</div>
);
}
});
21 changes: 21 additions & 0 deletions components/utils/date-time.js
Expand Up @@ -151,6 +151,27 @@ module.exports = {

newDate.setHours(hours - (hours > 12 ? -12 : 12));
return newDate;
},

formatTime (date, format) {
let hours = date.getHours();
let mins = date.getMinutes().toString();

if (format === 'ampm') {
let isAM = hours < 12;
let additional = isAM ? ' am' : ' pm';

hours = hours % 12;
hours = (hours || 12).toString();
if (mins.length < 2) mins = '0' + mins;

return hours + (mins === '00' ? '' : ':' + mins) + additional;
}

hours = hours.toString();
if (hours.length < 2) hours = '0' + hours;
if (mins.length < 2) mins = '0' + mins;
return hours + ':' + mins;
}

};

0 comments on commit 15d8f47

Please sign in to comment.