]> git.immae.eu Git - github/fretlink/time-picker.git/commitdiff
Added 12hours display support
authorAntony Shaleynikov <shaleynikov@gmail.com>
Wed, 1 Mar 2017 20:58:06 +0000 (23:58 +0300)
committerAntony Shaleynikov <shaleynikov@gmail.com>
Wed, 1 Mar 2017 20:58:06 +0000 (23:58 +0300)
examples/12hours.html [new file with mode: 0644]
examples/12hours.js [new file with mode: 0644]
src/Combobox.jsx
src/Panel.jsx
src/Select.jsx
src/TimePicker.jsx

diff --git a/examples/12hours.html b/examples/12hours.html
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/examples/12hours.js b/examples/12hours.js
new file mode 100644 (file)
index 0000000..18fb626
--- /dev/null
@@ -0,0 +1,30 @@
+/* eslint no-console:0 */
+
+import 'rc-time-picker/assets/index.less';
+
+import React from 'react';
+import ReactDom from 'react-dom';
+
+import moment from 'moment';
+
+import TimePicker from 'rc-time-picker';
+
+const showSecond = false;
+const str = showSecond ? 'HH:mm:ss' : 'HH:mm';
+
+const now = moment().hour(14).minute(30);
+
+function onChange(value) {
+  console.log(value && value.format(str));
+}
+
+ReactDom.render(
+  <TimePicker
+    showSecond={showSecond}
+    defaultValue={now}
+    className="xxx"
+    onChange={onChange}
+    show12Hours
+  />,
+  document.getElementById('__react-content')
+);
index 013617cbe75af9fb3c12432030f5057f2f9bbd10..e31b7fdac95201008653478b831d1725dc7127a1 100644 (file)
@@ -35,17 +35,42 @@ const Combobox = React.createClass({
     disabledMinutes: PropTypes.func,
     disabledSeconds: PropTypes.func,
     onCurrentSelectPanelChange: PropTypes.func,
+    show12Hours: PropTypes.bool,
   },
 
   onItemChange(type, itemValue) {
-    const { onChange, defaultOpenValue } = this.props;
+    const { onChange, defaultOpenValue, show12Hours } = this.props;
     const value = (this.props.value || defaultOpenValue).clone();
+
     if (type === 'hour') {
-      value.hour(itemValue);
+      if (show12Hours) {
+        if (value.hour() > 12 || !value.hour()) {
+          value.hour(+itemValue + 12);
+        } else {
+          value.hour(+itemValue);
+        }
+      } else {
+        value.hour(+itemValue);
+      }
     } else if (type === 'minute') {
-      value.minute(itemValue);
+      value.minute(+itemValue);
+    } else if (type === 'ampm') {
+      if (show12Hours) {
+        if (itemValue === 'PM' && value.hour() <= 12) {
+          value.hour(value.hour() + 12);
+        }
+
+        if (itemValue === 'AM') {
+          if (!value.hour()) {
+            value.hour(12);
+          } else
+          if (value.hour() > 12) {
+            value.hour(value.hour() - 12);
+          }
+        }
+      }
     } else {
-      value.second(itemValue);
+      value.second(+itemValue);
     }
     onChange(value);
   },
@@ -55,17 +80,34 @@ const Combobox = React.createClass({
   },
 
   getHourSelect(hour) {
-    const { prefixCls, hourOptions, disabledHours, showHour } = this.props;
+    const { prefixCls, hourOptions, disabledHours, showHour, show12Hours } = this.props;
     if (!showHour) {
       return null;
     }
     const disabledOptions = disabledHours();
+    let hourAdj;
+    if (show12Hours) {
+      if (hour > 12) {
+        hourAdj = hour - 12;
+      } else {
+        hourAdj = hour || 12;
+      }
+    } else {
+      hourAdj = hour;
+    }
+
+    let hourOptionsAdj;
+    if (show12Hours) {
+      hourOptionsAdj = hourOptions.filter(h => h <= 12 && h > 0);
+    } else {
+      hourOptionsAdj = hourOptions;
+    }
 
     return (
       <Select
         prefixCls={prefixCls}
-        options={hourOptions.map(option => formatOption(option, disabledOptions))}
-        selectedIndex={hourOptions.indexOf(hour)}
+        options={hourOptionsAdj.map(option => formatOption(option, disabledOptions))}
+        selectedIndex={hourOptionsAdj.indexOf(hourAdj)}
         type="hour"
         onSelect={this.onItemChange}
         onMouseEnter={this.onEnterSelectPanel.bind(this, 'hour')}
@@ -113,6 +155,27 @@ const Combobox = React.createClass({
     );
   },
 
+  getAMPMSelect() {
+    const { prefixCls, show12Hours, defaultOpenValue } = this.props;
+    if (!show12Hours) {
+      return null;
+    }
+    const value = this.props.value || defaultOpenValue;
+    const AMPMOptions = [{ value: 'AM' }, { value: 'PM' }];
+    const selected = (!value.hour() || value.hour() > 12) ? 1 : 0;
+
+    return (
+      <Select
+        prefixCls={prefixCls}
+        options={AMPMOptions}
+        selectedIndex={selected}
+        type="ampm"
+        onSelect={this.onItemChange}
+        onMouseEnter={this.onEnterSelectPanel.bind(this, 'ampm')}
+      />
+    );
+  },
+
   render() {
     const { prefixCls, defaultOpenValue } = this.props;
     const value = this.props.value || defaultOpenValue;
@@ -121,6 +184,7 @@ const Combobox = React.createClass({
         {this.getHourSelect(value.hour())}
         {this.getMinuteSelect(value.minute())}
         {this.getSecondSelect(value.second())}
+        {this.getAMPMSelect(value.hour())}
       </div>
     );
   },
index fddea1c09ddbc8fa591bd0847c7db1e7f23450a0..d02bd01c461ae9cdbf796520f70b84abb461c375 100644 (file)
@@ -37,6 +37,7 @@ const Panel = React.createClass({
     showMinute: PropTypes.bool,
     showSecond: PropTypes.bool,
     onClear: PropTypes.func,
+    show12Hours: PropTypes.bool,
     addon: PropTypes.func,
   },
 
@@ -49,6 +50,7 @@ const Panel = React.createClass({
       disabledMinutes: noop,
       disabledSeconds: noop,
       defaultOpenValue: moment(),
+      show12Hours: false,
       addon: noop,
     };
   },
@@ -90,7 +92,7 @@ const Panel = React.createClass({
     const {
       prefixCls, className, placeholder, disabledHours, disabledMinutes,
       disabledSeconds, hideDisabledOptions, allowEmpty, showHour, showMinute, showSecond,
-      format, defaultOpenValue, clearText, onEsc, addon,
+      format, defaultOpenValue, clearText, onEsc, addon, show12Hours,
     } = this.props;
     const {
       value, currentSelectPanel,
@@ -140,6 +142,7 @@ const Panel = React.createClass({
           disabledMinutes={disabledMinutes}
           disabledSeconds={disabledSeconds}
           onCurrentSelectPanelChange={this.onCurrentSelectPanelChange}
+          show12Hours={show12Hours}
         />
         {addon(this)}
       </div>
index 238a776811cc913eb21feb389c17b40b91f3d6a5..5733b1a235b9348f94c5f7fac1b3f9c4109fba71 100644 (file)
@@ -58,7 +58,7 @@ const Select = React.createClass({
       });
       let onclick = null;
       if (!item.disabled) {
-        onclick = this.onSelect.bind(this, +item.value);
+        onclick = this.onSelect.bind(this, item.value);
       }
       return (<li
         className={cls}
index e9d6d15330a11fa26d6d8ca17bba0892648fae66..f73d2b14cb60777b87b93aa4b2e8f93df38ca8e1 100644 (file)
@@ -43,6 +43,7 @@ const Picker = React.createClass({
     addon: PropTypes.func,
     name: PropTypes.string,
     autoComplete: PropTypes.string,
+    show12Hours: PropTypes.bool,
   },
 
   getDefaultProps() {
@@ -67,6 +68,7 @@ const Picker = React.createClass({
       onOpen: noop,
       onClose: noop,
       addon: noop,
+      show12Hours: false,
     };
   },
 
@@ -142,7 +144,7 @@ const Picker = React.createClass({
       prefixCls, placeholder, disabledHours,
       disabledMinutes, disabledSeconds, hideDisabledOptions,
       allowEmpty, showHour, showMinute, showSecond, defaultOpenValue, clearText,
-      addon,
+      addon, show12Hours,
     } = this.props;
     return (
       <Panel
@@ -164,6 +166,7 @@ const Picker = React.createClass({
         disabledMinutes={disabledMinutes}
         disabledSeconds={disabledSeconds}
         hideDisabledOptions={hideDisabledOptions}
+        show12Hours={show12Hours}
         addon={addon}
       />
     );