From 8133e8cf3b37b2be764616493ade7c7a7678fce1 Mon Sep 17 00:00:00 2001
From: yiminghe <yiminghe@gmail.com>
Date: Fri, 27 Nov 2015 16:15:05 +0800
Subject: add gregorianCalendarLocale prop

---
 src/TimePicker.jsx | 134 ++++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 96 insertions(+), 38 deletions(-)

(limited to 'src/TimePicker.jsx')

diff --git a/src/TimePicker.jsx b/src/TimePicker.jsx
index e82b46f..2694cb0 100644
--- a/src/TimePicker.jsx
+++ b/src/TimePicker.jsx
@@ -1,10 +1,10 @@
 import React, {PropTypes} from 'react';
-import ReactDOM from 'react-dom';
 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 {getFormatter} from './util/index';
+import defaultGregorianCalendarLocale from 'gregorian-calendar/lib/locale/en_US';
 
 function noop() {
 }
@@ -16,18 +16,23 @@ function refFn(field, component) {
 const Picker = React.createClass({
   propTypes: {
     prefixCls: PropTypes.string,
-    inputClassName: PropTypes.string,
     locale: PropTypes.object,
     value: PropTypes.object,
-    children: PropTypes.func,
     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,
+    gregorianCalendarLocale: PropTypes.object,
     placeholder: PropTypes.string,
-    formatter: PropTypes.object,
+    formatter: PropTypes.any,
+    showHour: PropTypes.bool,
+    style: PropTypes.object,
+    showSecond: PropTypes.bool,
     hourOptions: PropTypes.array,
     minuteOptions: PropTypes.array,
     secondOptions: PropTypes.array,
@@ -40,8 +45,13 @@ const Picker = React.createClass({
 
   getDefaultProps() {
     return {
-      open: false,
+      defaultOpen: false,
+      style: {},
+      gregorianCalendarLocale: defaultGregorianCalendarLocale,
       align: {},
+      allowEmpty: true,
+      showHour: true,
+      showSecond: true,
       placement: 'bottomLeft',
       onChange: noop,
       onOpen: noop,
@@ -51,10 +61,10 @@ const Picker = React.createClass({
 
   getInitialState() {
     this.savePanelRef = refFn.bind(this, 'panelInstance');
-    const { open, defaultValue, value } = this.props;
+    const { defaultOpen, defaultValue, open = defaultOpen, value = defaultValue } = this.props;
     return {
-      open: open,
-      value: value || defaultValue,
+      open,
+      value,
     };
   },
 
@@ -80,12 +90,18 @@ const Picker = React.createClass({
   },
 
   onVisibleChange(open) {
-    this.setOpen(open, () => {
-      if (open) {
-        ReactDOM.findDOMNode(this.refs.picker).blur();
-        ReactDOM.findDOMNode(this.panelInstance).focus();
-      }
-    });
+    this.setOpen(open);
+  },
+
+  onEsc() {
+    this.setOpen(false);
+    this.refs.picker.focus();
+  },
+
+  onKeyDown(e) {
+    if (e.keyCode === 40) {
+      this.setOpen(true);
+    }
   },
 
   setValue(value) {
@@ -97,14 +113,60 @@ const Picker = React.createClass({
     this.props.onChange(value);
   },
 
-  getPanel() {
-    const { prefixCls, defaultValue, locale, formatter, placeholder, hourOptions, minuteOptions, secondOptions } = this.props;
+  getFormatter() {
+    const formatter = this.props.formatter;
+    const locale = this.props.locale;
+    if (formatter) {
+      if (formatter === this.lastFormatter) {
+        return this.normalFormatter;
+      }
+      this.normalFormatter = getFormatter(formatter, locale);
+      this.lastFormatter = formatter;
+      return this.normalFormatter;
+    }
+    if (!this.props.showSecond) {
+      if (!this.notShowSecondFormatter) {
+        this.notShowSecondFormatter = getFormatter('HH:mm', locale);
+      }
+      return this.notShowSecondFormatter;
+    }
+    if (!this.props.showHour) {
+      if (!this.notShowHourFormatter) {
+        this.notShowHourFormatter = getFormatter('mm:ss', locale);
+      }
+      return this.notShowHourFormatter;
+    }
+    if (!this.normalFormatter) {
+      this.normalFormatter = getFormatter('HH:mm:ss', locale);
+    }
+    return this.normalFormatter;
+  },
+
+  getPanelElement() {
+    const { prefixCls, defaultValue, locale, placeholder, hourOptions, minuteOptions, secondOptions, allowEmpty, showHour, showSecond, gregorianCalendarLocale, value } = this.props;
+    let calendarLocale;
+    if (value) {
+      calendarLocale = value.locale;
+    } else if (defaultValue) {
+      calendarLocale = defaultValue.locale;
+    } else {
+      calendarLocale = gregorianCalendarLocale;
+    }
     return (
       <Panel
-        prefixCls={prefixCls}
+        prefixCls={`${prefixCls}-panel`}
+        ref={this.savePanelRef}
+        value={this.state.value}
+        onChange={this.onPanelChange}
+        gregorianCalendarLocale={calendarLocale}
+        onClear={this.onPanelClear}
         defaultValue={defaultValue}
+        showHour={showHour}
+        onEsc={this.onEsc}
+        showSecond={showSecond}
         locale={locale}
-        formatter={formatter}
+        allowEmpty={allowEmpty}
+        formatter={this.getFormatter()}
         placeholder={placeholder}
         hourOptions={hourOptions}
         minuteOptions={minuteOptions}
@@ -113,26 +175,14 @@ const Picker = React.createClass({
     );
   },
 
-  getPanelElement() {
-    const panel = this.getPanel();
-    const extraProps = {
-      ref: this.savePanelRef,
-      value: this.state.value,
-      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;
     if (this.state.open !== open) {
       this.setState({
-        open: open,
+        open,
       }, callback);
       const event = {
-        open: open,
+        open,
       };
       if (open) {
         onOpen(event);
@@ -143,25 +193,33 @@ const Picker = React.createClass({
   },
 
   render() {
-    const { prefixCls, placeholder, placement, align, disabled, transitionName, formatter, inputClassName } = this.props;
+    const { prefixCls, placeholder, placement, align, disabled, transitionName, style, showHour, showSecond, getPopupContainer } = this.props;
     const { open, value } = this.state;
-
+    let popupClassName;
+    if (!showHour || !showSecond) {
+      popupClassName = `${prefixCls}-panel-narrow}`;
+    }
     return (
       <Trigger
         prefixCls={`${prefixCls}-panel`}
+        popupClassName={popupClassName}
         popup={this.getPanelElement()}
         popupAlign={align}
         builtinPlacements={placements}
         popupPlacement={placement}
         action={disabled ? [] : ['click']}
         destroyPopupOnHide
+        getPopupContainer={getPopupContainer}
         popupTransitionName={transitionName}
         popupVisible={open}
         onPopupVisibleChange={this.onVisibleChange}
       >
-        <span className={`${prefixCls}`}>
-          <input className={inputClassName} ref="picker" type="text" placeholder={placeholder} readOnly
-                 disabled={disabled} value={value && formatter.format(value)}/>
+        <span className={`${prefixCls}`} style={style}>
+          <input className={`${prefixCls}-input`}
+                 ref="picker" type="text" placeholder={placeholder}
+                 readOnly
+                 onKeyDown={this.onKeyDown}
+                 disabled={disabled} value={value && this.getFormatter().format(value)}/>
           <span className={`${prefixCls}-icon`}/>
         </span>
       </Trigger>
-- 
cgit v1.2.3

