]> git.immae.eu Git - github/fretlink/time-picker.git/blobdiff - src/TimePicker.jsx
Readme file was updated to reflect show12Hours option
[github/fretlink/time-picker.git] / src / TimePicker.jsx
index da0c57bbd589906b3787cda74a3aab376109a301..f73d2b14cb60777b87b93aa4b2e8f93df38ca8e1 100644 (file)
@@ -29,9 +29,10 @@ const Picker = React.createClass({
     placeholder: PropTypes.string,
     format: PropTypes.string,
     showHour: PropTypes.bool,
+    showMinute: PropTypes.bool,
+    showSecond: PropTypes.bool,
     style: PropTypes.object,
     className: PropTypes.string,
-    showSecond: PropTypes.bool,
     disabledHours: PropTypes.func,
     disabledMinutes: PropTypes.func,
     disabledSeconds: PropTypes.func,
@@ -39,7 +40,10 @@ const Picker = React.createClass({
     onChange: PropTypes.func,
     onOpen: PropTypes.func,
     onClose: PropTypes.func,
+    addon: PropTypes.func,
+    name: PropTypes.string,
     autoComplete: PropTypes.string,
+    show12Hours: PropTypes.bool,
   },
 
   getDefaultProps() {
@@ -53,6 +57,7 @@ const Picker = React.createClass({
       defaultOpenValue: moment(),
       allowEmpty: true,
       showHour: true,
+      showMinute: true,
       showSecond: true,
       disabledHours: noop,
       disabledMinutes: noop,
@@ -62,11 +67,13 @@ const Picker = React.createClass({
       onChange: noop,
       onOpen: noop,
       onClose: noop,
-      autoComplete: 'yes',
+      addon: noop,
+      show12Hours: false,
     };
   },
 
   getInitialState() {
+    this.saveInputRef = refFn.bind(this, 'picker');
     this.savePanelRef = refFn.bind(this, 'panelInstance');
     const { defaultOpen, defaultValue, open = defaultOpen, value = defaultValue } = this.props;
     return {
@@ -102,7 +109,7 @@ const Picker = React.createClass({
 
   onEsc() {
     this.setOpen(false);
-    this.refs.picker.focus();
+    this.focus();
   },
 
   onKeyDown(e) {
@@ -121,24 +128,23 @@ const Picker = React.createClass({
   },
 
   getFormat() {
-    const format = this.props.format;
+    const { format, showHour, showMinute, showSecond } = this.props;
     if (format) {
       return format;
     }
-    if (!this.props.showSecond) {
-      return 'HH:mm';
-    }
-    if (!this.props.showHour) {
-      return 'mm:ss';
-    }
-    return 'HH:mm:ss';
+    return [
+      showHour ? 'HH' : '',
+      showMinute ? 'mm' : '',
+      showSecond ? 'ss' : '',
+    ].filter(item => !!item).join(':');
   },
 
   getPanelElement() {
     const {
       prefixCls, placeholder, disabledHours,
       disabledMinutes, disabledSeconds, hideDisabledOptions,
-      allowEmpty, showHour, showSecond, defaultOpenValue, clearText,
+      allowEmpty, showHour, showMinute, showSecond, defaultOpenValue, clearText,
+      addon, show12Hours,
     } = this.props;
     return (
       <Panel
@@ -150,8 +156,9 @@ const Picker = React.createClass({
         onClear={this.onPanelClear}
         defaultOpenValue={defaultOpenValue}
         showHour={showHour}
-        onEsc={this.onEsc}
+        showMinute={showMinute}
         showSecond={showSecond}
+        onEsc={this.onEsc}
         allowEmpty={allowEmpty}
         format={this.getFormat()}
         placeholder={placeholder}
@@ -159,36 +166,39 @@ const Picker = React.createClass({
         disabledMinutes={disabledMinutes}
         disabledSeconds={disabledSeconds}
         hideDisabledOptions={hideDisabledOptions}
+        show12Hours={show12Hours}
+        addon={addon}
       />
     );
   },
 
-  setOpen(open, callback) {
+  setOpen(open) {
     const { onOpen, onClose } = this.props;
     if (this.state.open !== open) {
-      this.setState({
-        open,
-      }, callback);
-      const event = {
-        open,
-      };
+      if (!('open' in this.props)) {
+        this.setState({ open });
+      }
       if (open) {
-        onOpen(event);
+        onOpen({ open });
       } else {
-        onClose(event);
+        onClose({ open });
       }
     }
   },
 
+  focus() {
+    this.picker.focus();
+  },
+
   render() {
     const {
       prefixCls, placeholder, placement, align,
       disabled, transitionName, style, className, showHour,
-      showSecond, getPopupContainer, autoComplete,
+      showMinute, showSecond, getPopupContainer, name, autoComplete,
     } = this.props;
     const { open, value } = this.state;
     let popupClassName;
-    if (!showHour || !showSecond) {
+    if (!showHour || !showMinute || !showSecond) {
       popupClassName = `${prefixCls}-panel-narrow`;
     }
     return (
@@ -209,7 +219,10 @@ const Picker = React.createClass({
         <span className={`${prefixCls} ${className}`} style={style}>
           <input
             className={`${prefixCls}-input`}
-            ref="picker" type="text" placeholder={placeholder}
+            ref={this.saveInputRef}
+            type="text"
+            placeholder={placeholder}
+            name={name}
             readOnly
             onKeyDown={this.onKeyDown}
             disabled={disabled} value={value && value.format(this.getFormat()) || ''}