X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FTimePicker.jsx;h=4d7d4d6c7416c2953711abd6a8b054273de45b7a;hb=73fb171157dd0eb56a92d57cf20ddaf937e9b607;hp=f0a3316c1b2d86d7264c06d044623e20c0b21e59;hpb=15bb18d90f02d146141cdde45a5a2b5a6a575202;p=github%2Ffretlink%2Ftime-picker.git diff --git a/src/TimePicker.jsx b/src/TimePicker.jsx index f0a3316..4d7d4d6 100644 --- a/src/TimePicker.jsx +++ b/src/TimePicker.jsx @@ -1,10 +1,8 @@ -import React, {PropTypes} from 'react'; -import ReactDOM from 'react-dom'; +import React, { PropTypes } from 'react'; import Trigger from 'rc-trigger'; -import {createChainedFunction} from 'rc-util'; -import Panel from './module/Panel'; -import placements from './util/placements'; -import CommonMixin from './mixin/CommonMixin'; +import Panel from './Panel'; +import placements from './placements'; +import moment from 'moment'; function noop() { } @@ -16,110 +14,169 @@ function refFn(field, component) { const Picker = React.createClass({ propTypes: { prefixCls: PropTypes.string, - locale: PropTypes.object, - children: PropTypes.func, - disabled: PropTypes.bool, + clearText: PropTypes.string, value: PropTypes.object, + defaultOpenValue: PropTypes.object, + disabled: PropTypes.bool, + allowEmpty: PropTypes.bool, + defaultValue: PropTypes.object, open: PropTypes.bool, + defaultOpen: PropTypes.bool, align: PropTypes.object, placement: PropTypes.any, transitionName: PropTypes.string, + getPopupContainer: PropTypes.func, placeholder: PropTypes.string, - formatter: PropTypes.object, - hourOptions: PropTypes.array, - minuteOptions: PropTypes.array, - secondOptions: PropTypes.array, + format: PropTypes.string, + showHour: PropTypes.bool, + showMinute: PropTypes.bool, + showSecond: PropTypes.bool, + style: PropTypes.object, + className: PropTypes.string, + disabledHours: PropTypes.func, + disabledMinutes: PropTypes.func, + disabledSeconds: PropTypes.func, + hideDisabledOptions: PropTypes.bool, onChange: PropTypes.func, onOpen: PropTypes.func, onClose: PropTypes.func, + addon: PropTypes.func, + name: PropTypes.string, + autoComplete: PropTypes.string, }, - mixins: [CommonMixin], - getDefaultProps() { return { - open: false, + 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, }; }, getInitialState() { + this.saveInputRef = refFn.bind(this, 'picker'); this.savePanelRef = refFn.bind(this, 'panelInstance'); - const { open, value } = this.props; - return { open, value }; + const { defaultOpen, defaultValue, open = defaultOpen, value = defaultValue } = this.props; + return { + open, + value, + }; }, componentWillReceiveProps(nextProps) { const { value, open } = nextProps; - if (value !== undefined) { - this.setState({value}); + if ('value' in nextProps) { + this.setState({ + value, + }); } if (open !== undefined) { - this.setState({open}); + this.setState({ open }); } }, onPanelChange(value) { - const props = this.props; - this.setState({ - value: value, - }); - props.onChange(value); + this.setValue(value); }, onPanelClear() { + this.setValue(null); this.setOpen(false); }, onVisibleChange(open) { - this.setOpen(open, () => { - if (open) { - ReactDOM.findDOMNode(this.refs.picker).blur(); - ReactDOM.findDOMNode(this.panelInstance).focus(); - } - }); + this.setOpen(open); }, - getPanel() { - const { prefixCls, value, locale, formatter, placeholder, hourOptions, minuteOptions, secondOptions } = this.props; + onEsc() { + this.setOpen(false); + this.focus(); + }, + + onKeyDown(e) { + if (e.keyCode === 40) { + this.setOpen(true); + } + }, + + setValue(value) { + if (!('value' in this.props)) { + this.setState({ + value, + }); + } + this.props.onChange(value); + }, + + getFormat() { + const { format, showHour, showMinute, showSecond } = this.props; + if (format) { + return format; + } + return [ + showHour ? 'HH' : '', + showMinute ? 'mm' : '', + showSecond ? 'ss' : '', + ].filter(item => !!item).join(':'); + }, + + getPanelElement() { + const { + prefixCls, placeholder, disabledHours, + disabledMinutes, disabledSeconds, hideDisabledOptions, + allowEmpty, showHour, showMinute, showSecond, defaultOpenValue, clearText, + addon, + } = this.props; return ( ); }, - getPanelElement() { - const panel = this.getPanel(); - const extraProps = { - ref: this.savePanelRef, - defaultValue: this.state.value || panel.props.defaultValue, - onChange: createChainedFunction(panel.props.onChange, this.onPanelChange), - onClear: createChainedFunction(panel.props.onClear, this.onPanelClear), - }; - - return React.cloneElement(panel, extraProps); - }, - setOpen(open, callback) { - const {onOpen, onClose} = this.props; + const { onOpen, onClose } = this.props; if (this.state.open !== open) { this.setState({ - open: open, + open, }, callback); const event = { - open: open, + open, }; if (open) { onOpen(event); @@ -129,26 +186,49 @@ const Picker = React.createClass({ } }, + focus() { + this.picker.focus(); + }, + render() { - const { prefixCls, placement, align, disabled, transitionName, children, formatter } = this.props; + const { + prefixCls, placeholder, placement, align, + disabled, transitionName, style, className, showHour, + showMinute, showSecond, getPopupContainer, name, autoComplete, + } = this.props; const { open, value } = this.state; - + let popupClassName; + if (!showHour || !showMinute || !showSecond) { + popupClassName = `${prefixCls}-panel-narrow`; + } return ( - - - + + + );