]> git.immae.eu Git - github/fretlink/time-picker.git/commitdiff
add new options about disabled time
authorMG12 <wuzhao.mail@gmail.com>
Sat, 12 Dec 2015 12:34:00 +0000 (20:34 +0800)
committerMG12 <wuzhao.mail@gmail.com>
Sat, 12 Dec 2015 12:34:00 +0000 (20:34 +0800)
HISTORY.md
README.md
assets/index/Select.less
examples/pick-time.js
package.json
src/TimePicker.jsx
src/module/Combobox.jsx
src/module/Header.jsx
src/module/Panel.jsx
src/module/Select.jsx

index ebc3b9e3fe5adee65d65f88b77b1623263572295..48c386ad29bb4dc230d3a5f96f393a02435e3a82 100644 (file)
@@ -2,16 +2,22 @@
 
 ---
 
+1.0.0-alpha7 / 2015-12-12
+------------------
+
+`new` add options `disabledHours`, `disabledMinutes`, `disabledSeconds` and `hideDisabledOptions`.
+`remove` remove options `hourOptions`, `minuteOptions` and `secondOptions`.
+
 1.0.0-alpha2 / 2015-12-03
 ------------------
 
-`fix` IE8 compatible.
+`fixed` IE8 compatible.
 `new` add test users.
 
 0.7.1 / 2015-11-20
 ------------------
 
-`fix` change value to null when clear input content to remove the react warning.
+`fixed` change value to null when clear input content to remove the react warning.
 
 0.7.0 / 2015-11-20
 ------------------
@@ -21,7 +27,7 @@
 0.5.6 / 2015-11-19
 ------------------
 
-`fix` use another method to change time and fix the bug about value.getTime().
+`fixed` use another method to change time and fix the bug about value.getTime().
 
 0.5.4 / 2015-11-19
 ------------------
@@ -54,7 +60,7 @@
 0.3.3 / 2015-11-17
 ------------------
 
-`fix` fix some bugs.
+`fixed` fix some bugs.
 
 0.3.0 / 2015-11-17
 ------------------
index 60e5ab035cfb610e8dd581304700b3e5fa3439a1..98f913a14540dd0ed5c24a14ec96972ad7082eed 100644 (file)
--- a/README.md
+++ b/README.md
@@ -39,7 +39,6 @@ Usage
 
 ```
 import TimePicker from 'rc-time-picker';
-import React from 'react';
 import ReactDOM from 'react-dom';
 ReactDOM.render(<TimePicker />, container);
 ```
@@ -49,25 +48,26 @@ API
 
 ### TimePicker
 
-| Name           | Type                       | Default                                       | Description                                                                                |
-|----------------|----------------------------|-----------------------------------------------|--------------------------------------------------------------------------------------------|
-| prefixCls      | String                     |                                               | prefixCls of this component                                                                |
-| locale         | Object                     | import from 'rc-time-picker/lib/locale/en_US' |                                                                                            |
-| disabled       | Boolean                    | false                                         | whether picker is disabled                                                                 |
-| open           | Boolean                    | false                                         | current open state of picker. controlled prop                                              |
-| defaultValue   | GregorianCalendar          | null                                          | default initial value                                                                      |
-| value          | GregorianCalendar                 | null                                          | current value                                                                              |
-| gregorianCalendarLocale   | GregorianCalendar  locale object               | null                                          | if value and defaultValue not set, you should set this to your locale                                                                            |
-| placeholder    | String                     | ''                                            | time input's placeholder                                                                   |
-| showHour       | Boolean                    | whether show hour                             |                                                                                            |
-| showSecond     | Boolean                    | whether show second                           |                                                                                            |
-| formatter      | String|GregorianCalendarFormatter |                                        |                                                                                            |
-| hourOptions    | Array<Number>              | hour options                                  |                                                                                            |
-| minuteOptions  | Array<Number>              | minute options                                |                                                                                            |
-| secondOptions  | Array<Number>              | second options                                |                                                                                            |
-| onChange       | Function                   | null                                          | called when select a different value                                                       |
-| placement      | String                     | bottomLeft                                    | one of ['left','right','top','bottom', 'topLeft', 'topRight', 'bottomLeft', 'bottomRight'] |
-| transitionName | String                     | ''                                            |                                                                                            |
+| Name                    | Type                              | Default                                       | Description                                                                                |
+|-------------------------|-----------------------------------|-----------------------------------------------|--------------------------------------------------------------------------------------------|
+| prefixCls               | String                            |                                               | prefixCls of this component                                                                |
+| locale                  | Object                            | import from 'rc-time-picker/lib/locale/en_US' |                                                                                            |
+| disabled                | Boolean                           | false                                         | whether picker is disabled                                                                 |
+| open                    | Boolean                           | false                                         | current open state of picker. controlled prop                                              |
+| defaultValue            | GregorianCalendar                 | null                                          | default initial value                                                                      |
+| value                   | GregorianCalendar                 | null                                          | current value                                                                              |
+| gregorianCalendarLocale | GregorianCalendar  locale object  | null                                          | if value and defaultValue not set, you should set this to your locale                      |
+| placeholder             | String                            | ''                                            | time input's placeholder                                                                   |
+| showHour                | Boolean                           | whether show hour                             |                                                                                            |
+| showSecond              | Boolean                           | whether show second                           |                                                                                            |
+| formatter               | String|GregorianCalendarFormatter |                                               |                                                                                            |
+| disabledHours           | Array<Number>                     | disabled hour options                         |                                                                                            |
+| disabledMinutes         | Array<Number>                     | disabled minute options                       |                                                                                            |
+| disabledSeconds         | Array<Number>                     | disabled second options                       |                                                                                            |
+| hideDisabledOptions     | Boolean                           | whether hide disabled options                 |                                                                                            |
+| onChange                | Function                          | null                                          | called when select a different value                                                       |
+| placement               | String                            | bottomLeft                                    | one of ['left','right','top','bottom', 'topLeft', 'topRight', 'bottomLeft', 'bottomRight'] |
+| transitionName          | String                            | ''                                            |                                                                                            |
 
 License
 -------
index 3e94c11b58479649d56bb680b2257413a9ed8f8f..13b5b187418cb53ebf11e9bfd186c8ebd1389ad4 100644 (file)
     }
   }
 
-  &-option-selected {
+  li&-option-selected {
     background: #edfaff;
     color: #2db7f5;
   }
+
+  li&-option-disabled {
+    color: #bfbfbf;
+    &:hover {
+      background: transparent;
+      cursor: not-allowed;
+    }
+  }
 }
index dd5a2ce8629b3de6650440a60db8a6efece7eb42..074e72c57bd43681a9bacd072439495b77afea1e 100644 (file)
@@ -24,11 +24,27 @@ function onChange(value) {
   console.log(value && formatter.format(value));
 }
 
+const options = {
+  disabledHours() {
+    return [0, 2, 21];
+  },
+  disabledMinutes(h) {
+    return h === 22 ? [0, 3, 31] : [];
+  },
+  disabledSeconds(h, m) {
+    return [];
+  },
+};
+
 ReactDom.render(
   <TimePicker formatter={formatter} locale={TimePickerLocale}
               showSecond={showSecond}
               defaultValue={now}
               className="xxx"
-              onChange={onChange}/>,
+              onChange={onChange}
+              disabledHours={[0, 2, 21]}
+              disabledMinutes={[0, 2, 21]}
+              disabledSeconds={[]}
+              hideDisabledOptions={true} />,
   document.getElementById('__react-content')
 );
index fa91d868956bd3e3e0f0635f7cb4cf8e38420190..8bc0ae292c55bfa5453225379dcf35f459b4a3e9 100644 (file)
@@ -1,6 +1,6 @@
 {
   "name": "rc-time-picker",
-  "version": "1.0.0-alpha6",
+  "version": "1.0.0-alpha7",
   "description": "React TimePicker",
   "keywords": [
     "react",
index 3e331f160406d6a04eb8f82ef8451d45525fc543..fb855e29996228abd9aa5bf87b033ad000468050 100644 (file)
@@ -34,9 +34,10 @@ const Picker = React.createClass({
     style: PropTypes.object,
     className: PropTypes.string,
     showSecond: PropTypes.bool,
-    hourOptions: PropTypes.array,
-    minuteOptions: PropTypes.array,
-    secondOptions: PropTypes.array,
+    disabledHours: PropTypes.array,
+    disabledMinutes: PropTypes.array,
+    disabledSeconds: PropTypes.array,
+    hideDisabledOptions: PropTypes.bool,
     onChange: PropTypes.func,
     onOpen: PropTypes.func,
     onClose: PropTypes.func,
@@ -54,6 +55,10 @@ const Picker = React.createClass({
       allowEmpty: true,
       showHour: true,
       showSecond: true,
+      disabledHours: null,
+      disabledMinutes: null,
+      disabledSeconds: null,
+      hideDisabledOptions: false,
       placement: 'bottomLeft',
       onChange: noop,
       onOpen: noop,
@@ -145,7 +150,7 @@ const Picker = React.createClass({
   },
 
   getPanelElement() {
-    const { prefixCls, defaultValue, locale, placeholder, hourOptions, minuteOptions, secondOptions, allowEmpty, showHour, showSecond, gregorianCalendarLocale, value } = this.props;
+    const { prefixCls, defaultValue, locale, placeholder, disabledHours, disabledMinutes, disabledSeconds, hideDisabledOptions, allowEmpty, showHour, showSecond, gregorianCalendarLocale, value } = this.props;
     let calendarLocale;
     if (value) {
       calendarLocale = value.locale;
@@ -170,9 +175,10 @@ const Picker = React.createClass({
         allowEmpty={allowEmpty}
         formatter={this.getFormatter()}
         placeholder={placeholder}
-        hourOptions={hourOptions}
-        minuteOptions={minuteOptions}
-        secondOptions={secondOptions}
+        disabledHours={disabledHours}
+        disabledMinutes={disabledMinutes}
+        disabledSeconds={disabledSeconds}
+        hideDisabledOptions={hideDisabledOptions}
       />
     );
   },
index a017ec91ce4c73ad278a7267c2c04e840557119d..27a822686e1735cf6b08baff2191067cf6f2276d 100644 (file)
@@ -2,11 +2,21 @@ import React, {PropTypes} from 'react';
 import Select from './Select';
 import GregorianCalendar from 'gregorian-calendar';
 
-const formatOption = (option) => {
+const formatOption = (option, disabledOptions) => {
+  let value = `${option}`;
   if (option < 10) {
-    return `0${option}`;
+    value = `0${option}`;
   }
-  return `${option}`;
+
+  let disabled = false;
+  if (disabledOptions.indexOf(option) >= 0) {
+    disabled = true;
+  }
+
+  return {
+    value,
+    disabled,
+  };
 };
 
 const Combobox = React.createClass({
@@ -21,6 +31,9 @@ const Combobox = React.createClass({
     hourOptions: PropTypes.array,
     minuteOptions: PropTypes.array,
     secondOptions: PropTypes.array,
+    disabledHours: PropTypes.array,
+    disabledMinutes: PropTypes.array,
+    disabledSeconds: PropTypes.array,
     onCurrentSelectPanelChange: PropTypes.func,
   },
 
@@ -47,14 +60,14 @@ const Combobox = React.createClass({
   },
 
   getHourSelect(hour) {
-    const { prefixCls, hourOptions, showHour } = this.props;
+    const { prefixCls, hourOptions, disabledHours, showHour } = this.props;
     if (!showHour) {
       return null;
     }
     return (
       <Select
         prefixCls={prefixCls}
-        options={hourOptions.map(option => formatOption(option))}
+        options={hourOptions.map(option => formatOption(option, disabledHours))}
         selectedIndex={hourOptions.indexOf(hour)}
         type="hour"
         onSelect={this.onItemChange}
@@ -64,11 +77,11 @@ const Combobox = React.createClass({
   },
 
   getMinuteSelect(minute) {
-    const { prefixCls, minuteOptions } = this.props;
+    const { prefixCls, minuteOptions, disabledMinutes } = this.props;
     return (
       <Select
         prefixCls={prefixCls}
-        options={minuteOptions.map(option => formatOption(option))}
+        options={minuteOptions.map(option => formatOption(option, disabledMinutes))}
         selectedIndex={minuteOptions.indexOf(minute)}
         type="minute"
         onSelect={this.onItemChange}
@@ -78,14 +91,14 @@ const Combobox = React.createClass({
   },
 
   getSecondSelect(second) {
-    const { prefixCls, secondOptions, showSecond } = this.props;
+    const { prefixCls, secondOptions, disabledSeconds, showSecond } = this.props;
     if (!showSecond) {
       return null;
     }
     return (
       <Select
         prefixCls={prefixCls}
-        options={secondOptions.map(option => formatOption(option))}
+        options={secondOptions.map(option => formatOption(option, disabledSeconds))}
         selectedIndex={secondOptions.indexOf(second)}
         type="second"
         onSelect={this.onItemChange}
index ef88948f8e1cbaad5fe0dbec24659ee2ddd5e4e3..c2f8eefb6a092cb33b06f0c6f5674dbdbf353f4e 100644 (file)
@@ -13,6 +13,9 @@ const Header = React.createClass({
     hourOptions: PropTypes.array,
     minuteOptions: PropTypes.array,
     secondOptions: PropTypes.array,
+    disabledHours: PropTypes.array,
+    disabledMinutes: PropTypes.array,
+    disabledSeconds: PropTypes.array,
     onChange: PropTypes.func,
     onClear: PropTypes.func,
     onEsc: PropTypes.func,
@@ -54,7 +57,7 @@ const Header = React.createClass({
       str,
     });
     let value = null;
-    const {formatter, gregorianCalendarLocale, hourOptions, minuteOptions, secondOptions, onChange, allowEmpty} = this.props;
+    const {formatter, gregorianCalendarLocale, hourOptions, minuteOptions, secondOptions, disabledHours, disabledMinutes, disabledSeconds, onChange, allowEmpty} = this.props;
 
     if (str) {
       const originalValue = this.props.value;
@@ -71,6 +74,7 @@ const Header = React.createClass({
       }
 
       if (value) {
+        // if time value not allowed, response warning.
         if (
           hourOptions.indexOf(value.getHourOfDay()) < 0 ||
           minuteOptions.indexOf(value.getMinutes()) < 0 ||
@@ -82,6 +86,18 @@ const Header = React.createClass({
           return;
         }
 
+        // if time value is disabled, response warning.
+        if (
+          disabledHours.indexOf(value.getHourOfDay()) >= 0 ||
+          disabledMinutes.indexOf(value.getMinutes()) >= 0 ||
+          disabledSeconds.indexOf(value.getSeconds()) >= 0
+        ) {
+          this.setState({
+            invalid: true,
+          });
+          return;
+        }
+
         if (originalValue && value) {
           if (
             originalValue.getHourOfDay() !== value.getHourOfDay() ||
index 63823ee07e0a37d53662a772f754bbe58175b634..72934e5938a8e8641028ab6254904542bc0f067b 100644 (file)
@@ -6,10 +6,12 @@ import Combobox from './Combobox';
 function noop() {
 }
 
-function generateOptions(length) {
+function generateOptions(length, disabledOptions, hideDisabledOptions) {
   const arr = [];
-  for (let i = 0; i < length; i++) {
-    arr.push(i);
+  for (let value = 0; value < length; value++) {
+    if ((disabledOptions && disabledOptions.indexOf(value) < 0) || !hideDisabledOptions) {
+      arr.push(value);
+    }
   }
   return arr;
 }
@@ -22,9 +24,10 @@ const Panel = React.createClass({
     placeholder: PropTypes.string,
     gregorianCalendarLocale: PropTypes.object,
     formatter: PropTypes.object,
-    hourOptions: PropTypes.array,
-    minuteOptions: PropTypes.array,
-    secondOptions: PropTypes.array,
+    disabledHours: PropTypes.array,
+    disabledMinutes: PropTypes.array,
+    disabledSeconds: PropTypes.array,
+    hideDisabledOptions: PropTypes.bool,
     onChange: PropTypes.func,
     onEsc: PropTypes.func,
     allowEmpty: PropTypes.bool,
@@ -37,9 +40,10 @@ const Panel = React.createClass({
 
   getDefaultProps() {
     return {
-      hourOptions: generateOptions(24),
-      minuteOptions: generateOptions(60),
-      secondOptions: generateOptions(60),
+      disabledHours: null,
+      disabledMinutes: null,
+      disabledSeconds: null,
+      hideDisabledOptions: false,
       onChange: noop,
       onClear: noop,
     };
@@ -75,8 +79,12 @@ const Panel = React.createClass({
   },
 
   render() {
-    const { locale, prefixCls, placeholder, hourOptions, minuteOptions, secondOptions, allowEmpty, showHour, showSecond, formatter, gregorianCalendarLocale } = this.props;
+    const { locale, prefixCls, placeholder, disabledHours, disabledMinutes, disabledSeconds, hideDisabledOptions, allowEmpty, showHour, showSecond, formatter, gregorianCalendarLocale } = this.props;
     const value = this.state.value;
+    const hourOptions = generateOptions(24, disabledHours, hideDisabledOptions);
+    const minuteOptions = generateOptions(60, disabledMinutes, hideDisabledOptions);
+    const secondOptions = generateOptions(60, disabledSeconds, hideDisabledOptions);
+
     return (
       <div className={`${prefixCls}-inner`}>
         <Header
@@ -91,6 +99,9 @@ const Panel = React.createClass({
           hourOptions={hourOptions}
           minuteOptions={minuteOptions}
           secondOptions={secondOptions}
+          disabledHours={disabledHours}
+          disabledMinutes={disabledMinutes}
+          disabledSeconds={disabledSeconds}
           onChange={this.onChange}
           onClear={this.onClear}
           allowEmpty={allowEmpty}
@@ -106,6 +117,9 @@ const Panel = React.createClass({
           hourOptions={hourOptions}
           minuteOptions={minuteOptions}
           secondOptions={secondOptions}
+          disabledHours={disabledHours}
+          disabledMinutes={disabledMinutes}
+          disabledSeconds={disabledSeconds}
           onCurrentSelectPanelChange={this.onCurrentSelectPanelChange}
         />
       </div>
index 36596920f11179a9b9b22333f901fa579f0c410d..ab1078918795e12752bf5f98a35b8724ad061e21 100644 (file)
@@ -51,11 +51,15 @@ const Select = React.createClass({
   getOptions() {
     const { options, selectedIndex, prefixCls } = this.props;
     return options.map((item, index) => {
-      const selected = selectedIndex === index;
       const cls = classnames({
-        [`${prefixCls}-select-option-selected`]: selected,
+        [`${prefixCls}-select-option-selected`]: selectedIndex === index,
+        [`${prefixCls}-select-option-disabled`]: item.disabled,
       });
-      return <li className={cls} key={index} onClick={this.onSelect.bind(this, +item)}>{item}</li>;
+      let onclick = null;
+      if (!item.disabled) {
+        onclick = this.onSelect.bind(this, +item.value);
+      }
+      return <li className={cls} key={index} onClick={onclick} disabled={item.disabled}>{item.value}</li>;
     });
   },