UNPKG

material-ui

Version:

Material Design UI components built with React

174 lines (140 loc) 4 kB
import React from 'react'; import StylePropable from '../mixins/style-propable'; import WindowListenable from '../mixins/window-listenable'; import TimePickerDialog from './time-picker-dialog'; import TextField from '../text-field'; import ThemeManager from '../styles/theme-manager'; import DefaultRawTheme from '../styles/raw-themes/light-raw-theme'; let emptyTime = new Date(); emptyTime.setHours(0); emptyTime.setMinutes(0); emptyTime.setSeconds(0); emptyTime.setMilliseconds(0); const TimePicker = React.createClass({ propTypes: { autoOk: React.PropTypes.bool, defaultTime: React.PropTypes.object, format: React.PropTypes.oneOf(['ampm', '24hr']), onChange: React.PropTypes.func, onDismiss: React.PropTypes.func, onFocus: React.PropTypes.func, onShow: React.PropTypes.func, onTouchTap: React.PropTypes.func, pedantic: React.PropTypes.bool, /** * Override the inline-styles of the root element. */ style: React.PropTypes.object, textFieldStyle: React.PropTypes.object, }, contextTypes: { muiTheme: React.PropTypes.object, }, mixins: [StylePropable, WindowListenable], getDefaultProps() { return { defaultTime: null, format: 'ampm', pedantic: false, autoOk: false, style: {}, }; }, getInitialState() { return { time: this.props.defaultTime || emptyTime, dialogTime: new Date(), muiTheme: this.context.muiTheme ? this.context.muiTheme : ThemeManager.getMuiTheme(DefaultRawTheme), }; }, windowListeners: { 'keyup': '_handleWindowKeyUp', }, formatTime(date) { let hours = date.getHours(); let mins = date.getMinutes().toString(); if (this.props.format === 'ampm') { let isAM = hours < 12; hours = hours % 12; let additional = isAM ? ' am' : ' pm'; hours = (hours || 12).toString(); if (mins.length < 2 ) mins = '0' + mins; if (this.props.pedantic) { // Treat midday/midnight specially http://www.nist.gov/pml/div688/times.cfm if (hours === '12' && mins === '00') { return additional === ' pm' ? '12 noon' : '12 midnight'; } } 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; }, getTime() { return this.state.time; }, setTime(time) { this.setState({time: time ? time : emptyTime}); }, /** * Alias for `openDialog()` for an api consistent with TextField. */ focus() { this.openDialog(); }, openDialog() { this.setState({ dialogTime: this.getTime(), }); this.refs.dialogWindow.show(); }, _handleDialogAccept(t) { this.setTime(t); if (this.props.onChange) this.props.onChange(null, t); }, _handleInputFocus(e) { e.target.blur(); if (this.props.onFocus) this.props.onFocus(e); }, _handleInputTouchTap(e) { e.preventDefault(); this.openDialog(); if (this.props.onTouchTap) this.props.onTouchTap(e); }, render() { const { autoOk, format, onFocus, onTouchTap, onShow, onDismiss, style, textFieldStyle, ...other, } = this.props; const {time} = this.state; return ( <div style={this.prepareStyles(style)}> <TextField {...other} style={textFieldStyle} ref="input" value={time === emptyTime ? null : this.formatTime(time)} onFocus={this._handleInputFocus} onTouchTap={this._handleInputTouchTap} /> <TimePickerDialog ref="dialogWindow" initialTime={this.state.dialogTime} onAccept={this._handleDialogAccept} onShow={onShow} onDismiss={onDismiss} format={format} autoOk={autoOk} /> </div> ); }, }); export default TimePicker;