X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FTimePicker.jsx;h=271515da4af3973ac5ed0486c3bbc1f1e8948a6d;hb=2fb27d673619b7bdd70178e617965a009fa72874;hp=d94ed940baffa48ff9868ad9ce659509639e79f6;hpb=16a18e356599ed2b9289314614739802ea5b5191;p=github%2Ffretlink%2Ftime-picker.git diff --git a/src/TimePicker.jsx b/src/TimePicker.jsx index d94ed94..271515d 100644 --- a/src/TimePicker.jsx +++ b/src/TimePicker.jsx @@ -1,4 +1,5 @@ -import React, { PropTypes } from 'react'; +import React, { Component } from 'react'; +import PropTypes from 'prop-types'; import Trigger from 'rc-trigger'; import Panel from './Panel'; import placements from './placements'; @@ -11,8 +12,8 @@ function refFn(field, component) { this[field] = component; } -const Picker = React.createClass({ - propTypes: { +export default class Picker extends Component { + static propTypes = { prefixCls: PropTypes.string, clearText: PropTypes.string, value: PropTypes.object, @@ -33,6 +34,7 @@ const Picker = React.createClass({ showSecond: PropTypes.bool, style: PropTypes.object, className: PropTypes.string, + popupClassName: PropTypes.string, disabledHours: PropTypes.func, disabledMinutes: PropTypes.func, disabledSeconds: PropTypes.func, @@ -40,47 +42,53 @@ const Picker = React.createClass({ onChange: PropTypes.func, onOpen: PropTypes.func, onClose: PropTypes.func, + onFocus: PropTypes.func, + onBlur: PropTypes.func, addon: PropTypes.func, name: PropTypes.string, autoComplete: PropTypes.string, use12Hours: PropTypes.bool, - }, - - getDefaultProps() { - return { - clearText: 'clear', - prefixCls: 'rc-time-picker', - defaultOpen: false, - style: {}, - className: '', - align: {}, - defaultOpenValue: moment(), - allowEmpty: true, - showHour: true, - showMinute: true, - showSecond: true, - disabledHours: noop, - disabledMinutes: noop, - disabledSeconds: noop, - hideDisabledOptions: false, - placement: 'bottomLeft', - onChange: noop, - onOpen: noop, - onClose: noop, - addon: noop, - use12Hours: false, - }; - }, + onKeyDown: PropTypes.func, + }; + + static defaultProps = { + clearText: 'clear', + prefixCls: 'rc-time-picker', + defaultOpen: false, + style: {}, + className: '', + popupClassName: '', + align: {}, + defaultOpenValue: moment(), + allowEmpty: true, + showHour: true, + showMinute: true, + showSecond: true, + disabledHours: noop, + disabledMinutes: noop, + disabledSeconds: noop, + hideDisabledOptions: false, + placement: 'bottomLeft', + onChange: noop, + onOpen: noop, + onClose: noop, + onFocus: noop, + onBlur: noop, + addon: noop, + use12Hours: false, + onKeyDown: noop, + }; - getInitialState() { + constructor(props) { + super(props); this.saveInputRef = refFn.bind(this, 'picker'); this.savePanelRef = refFn.bind(this, 'panelInstance'); - const { defaultOpen, defaultValue, open = defaultOpen, value = defaultValue } = this.props; - return { + const { defaultOpen, defaultValue, open = defaultOpen, value = defaultValue } = props; + this.state = { open, value, }; - }, + } componentWillReceiveProps(nextProps) { const { value, open } = nextProps; @@ -92,31 +100,31 @@ const Picker = React.createClass({ if (open !== undefined) { this.setState({ open }); } - }, + } - onPanelChange(value) { + onPanelChange = (value) => { this.setValue(value); - }, + } - onPanelClear() { + onPanelClear = () => { this.setValue(null); this.setOpen(false); - }, + } - onVisibleChange(open) { + onVisibleChange = (open) => { this.setOpen(open); - }, + } - onEsc() { + onEsc = () => { this.setOpen(false); this.focus(); - }, + } - onKeyDown(e) { + onKeyDown = (e) => { if (e.keyCode === 40) { this.setOpen(true); } - }, + } setValue(value) { if (!('value' in this.props)) { @@ -125,7 +133,7 @@ const Picker = React.createClass({ }); } this.props.onChange(value); - }, + } getFormat() { const { format, showHour, showMinute, showSecond, use12Hours } = this.props; @@ -148,14 +156,14 @@ const Picker = React.createClass({ showMinute ? 'mm' : '', showSecond ? 'ss' : '', ].filter(item => !!item).join(':'); - }, + } getPanelElement() { const { prefixCls, placeholder, disabledHours, disabledMinutes, disabledSeconds, hideDisabledOptions, allowEmpty, showHour, showMinute, showSecond, defaultOpenValue, clearText, - addon, use12Hours, + addon, use12Hours, onKeyDown, } = this.props; return ( ); - }, + } + + getPopupClassName() { + const { showHour, showMinute, showSecond, use12Hours, prefixCls } = this.props; + let popupClassName = this.props.popupClassName; + // Keep it for old compatibility + if ((!showHour || !showMinute || !showSecond) && !use12Hours) { + popupClassName += ` ${prefixCls}-panel-narrow`; + } + let selectColumnCount = 0; + if (showHour) { + selectColumnCount += 1; + } + if (showMinute) { + selectColumnCount += 1; + } + if (showSecond) { + selectColumnCount += 1; + } + if (use12Hours) { + selectColumnCount += 1; + } + popupClassName += ` ${prefixCls}-panel-column-${selectColumnCount}`; + return popupClassName; + } setOpen(open) { const { onOpen, onClose } = this.props; @@ -195,24 +228,20 @@ const Picker = React.createClass({ onClose({ open }); } } - }, + } focus() { this.picker.focus(); - }, + } render() { const { prefixCls, placeholder, placement, align, - disabled, transitionName, style, className, showHour, - showMinute, showSecond, getPopupContainer, name, autoComplete, - use12Hours, + disabled, transitionName, style, className, getPopupContainer, name, autoComplete, + onFocus, onBlur, } = this.props; const { open, value } = this.state; - let popupClassName; - if ((!showHour || !showMinute || !showSecond) && !use12Hours) { - popupClassName = `${prefixCls}-panel-narrow`; - } + const popupClassName = this.getPopupClassName(); return ( ); - }, -}); - -export default Picker; + } +}