]> git.immae.eu Git - github/fretlink/time-picker.git/blobdiff - src/TimePicker.jsx
Merge pull request #60 from shoaibbhimani/master
[github/fretlink/time-picker.git] / src / TimePicker.jsx
index 2694cb0ae61c85f80f721a538a84348239c9880c..271515da4af3973ac5ed0486c3bbc1f1e8948a6d 100644 (file)
@@ -1,10 +1,9 @@
-import React, {PropTypes} from 'react';
+import React, { Component } from 'react';
+import PropTypes from 'prop-types';
 import Trigger from 'rc-trigger';
-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';
+import Panel from './Panel';
+import placements from './placements';
+import moment from 'moment';
 
 function noop() {
 }
@@ -13,11 +12,12 @@ function refFn(field, component) {
   this[field] = component;
 }
 
-const Picker = React.createClass({
-  propTypes: {
+export default class Picker extends Component {
+  static propTypes = {
     prefixCls: PropTypes.string,
-    locale: PropTypes.object,
+    clearText: PropTypes.string,
     value: PropTypes.object,
+    defaultOpenValue: PropTypes.object,
     disabled: PropTypes.bool,
     allowEmpty: PropTypes.bool,
     defaultValue: PropTypes.object,
@@ -27,82 +27,104 @@ const Picker = React.createClass({
     placement: PropTypes.any,
     transitionName: PropTypes.string,
     getPopupContainer: PropTypes.func,
-    gregorianCalendarLocale: PropTypes.object,
     placeholder: PropTypes.string,
-    formatter: PropTypes.any,
+    format: PropTypes.string,
     showHour: PropTypes.bool,
-    style: PropTypes.object,
+    showMinute: PropTypes.bool,
     showSecond: PropTypes.bool,
-    hourOptions: PropTypes.array,
-    minuteOptions: PropTypes.array,
-    secondOptions: PropTypes.array,
+    style: PropTypes.object,
+    className: PropTypes.string,
+    popupClassName: PropTypes.string,
+    disabledHours: PropTypes.func,
+    disabledMinutes: PropTypes.func,
+    disabledSeconds: PropTypes.func,
+    hideDisabledOptions: PropTypes.bool,
     onChange: PropTypes.func,
     onOpen: PropTypes.func,
     onClose: PropTypes.func,
-  },
-
-  mixins: [CommonMixin],
-
-  getDefaultProps() {
-    return {
-      defaultOpen: false,
-      style: {},
-      gregorianCalendarLocale: defaultGregorianCalendarLocale,
-      align: {},
-      allowEmpty: true,
-      showHour: true,
-      showSecond: true,
-      placement: 'bottomLeft',
-      onChange: noop,
-      onOpen: noop,
-      onClose: noop,
-    };
-  },
+    onFocus: PropTypes.func,
+    onBlur: PropTypes.func,
+    addon: PropTypes.func,
+    name: PropTypes.string,
+    autoComplete: PropTypes.string,
+    use12Hours: PropTypes.bool,
+    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;
-    if (value !== undefined) {
+    if ('value' in nextProps) {
       this.setState({
         value,
       });
     }
     if (open !== undefined) {
-      this.setState({open});
+      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.refs.picker.focus();
-  },
+    this.focus();
+  }
 
-  onKeyDown(e) {
+  onKeyDown = (e) => {
     if (e.keyCode === 40) {
       this.setOpen(true);
     }
-  },
+  }
 
   setValue(value) {
     if (!('value' in this.props)) {
@@ -111,94 +133,115 @@ const Picker = React.createClass({
       });
     }
     this.props.onChange(value);
-  },
-
-  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;
+  }
+
+  getFormat() {
+    const { format, showHour, showMinute, showSecond, use12Hours } = this.props;
+    if (format) {
+      return format;
     }
-    if (!this.normalFormatter) {
-      this.normalFormatter = getFormatter('HH:mm:ss', locale);
+
+    if (use12Hours) {
+      const fmtString = ([
+        showHour ? 'h' : '',
+        showMinute ? 'mm' : '',
+        showSecond ? 'ss' : '',
+      ].filter(item => !!item).join(':'));
+
+      return fmtString.concat(' a');
     }
-    return this.normalFormatter;
-  },
+
+    return [
+      showHour ? 'HH' : '',
+      showMinute ? 'mm' : '',
+      showSecond ? 'ss' : '',
+    ].filter(item => !!item).join(':');
+  }
 
   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;
-    }
+    const {
+      prefixCls, placeholder, disabledHours,
+      disabledMinutes, disabledSeconds, hideDisabledOptions,
+      allowEmpty, showHour, showMinute, showSecond, defaultOpenValue, clearText,
+      addon, use12Hours, onKeyDown,
+    } = this.props;
     return (
       <Panel
+        clearText={clearText}
         prefixCls={`${prefixCls}-panel`}
         ref={this.savePanelRef}
         value={this.state.value}
         onChange={this.onPanelChange}
-        gregorianCalendarLocale={calendarLocale}
         onClear={this.onPanelClear}
-        defaultValue={defaultValue}
+        defaultOpenValue={defaultOpenValue}
         showHour={showHour}
-        onEsc={this.onEsc}
+        showMinute={showMinute}
         showSecond={showSecond}
-        locale={locale}
+        onEsc={this.onEsc}
         allowEmpty={allowEmpty}
-        formatter={this.getFormatter()}
+        format={this.getFormat()}
         placeholder={placeholder}
-        hourOptions={hourOptions}
-        minuteOptions={minuteOptions}
-        secondOptions={secondOptions}
+        disabledHours={disabledHours}
+        disabledMinutes={disabledMinutes}
+        disabledSeconds={disabledSeconds}
+        hideDisabledOptions={hideDisabledOptions}
+        use12Hours={use12Hours}
+        addon={addon}
+        onKeyDown={onKeyDown}
       />
     );
-  },
+  }
+
+  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, callback) {
-    const {onOpen, onClose} = this.props;
+  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, showHour, showSecond, getPopupContainer } = this.props;
+    const {
+      prefixCls, placeholder, placement, align,
+      disabled, transitionName, style, className, getPopupContainer, name, autoComplete,
+      onFocus, onBlur,
+    } = this.props;
     const { open, value } = this.state;
-    let popupClassName;
-    if (!showHour || !showSecond) {
-      popupClassName = `${prefixCls}-panel-narrow}`;
-    }
+    const popupClassName = this.getPopupClassName();
     return (
       <Trigger
         prefixCls={`${prefixCls}-panel`}
@@ -214,17 +257,23 @@ const Picker = React.createClass({
         popupVisible={open}
         onPopupVisibleChange={this.onVisibleChange}
       >
-        <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} ${className}`} style={style}>
+          <input
+            className={`${prefixCls}-input`}
+            ref={this.saveInputRef}
+            type="text"
+            placeholder={placeholder}
+            name={name}
+            readOnly
+            onKeyDown={this.onKeyDown}
+            disabled={disabled} value={value && value.format(this.getFormat()) || ''}
+            autoComplete={autoComplete}
+            onFocus={onFocus}
+            onBlur={onBlur}
+          />
           <span className={`${prefixCls}-icon`}/>
         </span>
       </Trigger>
     );
-  },
-});
-
-export default Picker;
+  }
+}