aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--src/Combobox.jsx10
-rw-r--r--src/Panel.jsx27
-rw-r--r--tests/Select.spec.jsx120
3 files changed, 146 insertions, 11 deletions
diff --git a/src/Combobox.jsx b/src/Combobox.jsx
index 1eed4d2..ae53356 100644
--- a/src/Combobox.jsx
+++ b/src/Combobox.jsx
@@ -37,6 +37,7 @@ class Combobox extends Component {
37 disabledSeconds: PropTypes.func, 37 disabledSeconds: PropTypes.func,
38 onCurrentSelectPanelChange: PropTypes.func, 38 onCurrentSelectPanelChange: PropTypes.func,
39 use12Hours: PropTypes.bool, 39 use12Hours: PropTypes.bool,
40 isAM: PropTypes.bool,
40 }; 41 };
41 42
42 onItemChange = (type, itemValue) => { 43 onItemChange = (type, itemValue) => {
@@ -45,7 +46,7 @@ class Combobox extends Component {
45 46
46 if (type === 'hour') { 47 if (type === 'hour') {
47 if (use12Hours) { 48 if (use12Hours) {
48 if (this.isAM()) { 49 if (this.props.isAM) {
49 value.hour(+itemValue % 12); 50 value.hour(+itemValue % 12);
50 } else { 51 } else {
51 value.hour((+itemValue % 12) + 12); 52 value.hour((+itemValue % 12) + 12);
@@ -156,7 +157,7 @@ class Combobox extends Component {
156 .map(c => format.match(/\sA/) ? c.toUpperCase() : c) 157 .map(c => format.match(/\sA/) ? c.toUpperCase() : c)
157 .map(c => ({ value: c })); 158 .map(c => ({ value: c }));
158 159
159 const selected = this.isAM() ? 0 : 1; 160 const selected = this.props.isAM ? 0 : 1;
160 161
161 return ( 162 return (
162 <Select 163 <Select
@@ -170,11 +171,6 @@ class Combobox extends Component {
170 ); 171 );
171 } 172 }
172 173
173 isAM() {
174 const value = (this.props.value || this.props.defaultOpenValue);
175 return value.hour() >= 0 && value.hour() < 12;
176 }
177
178 render() { 174 render() {
179 const { prefixCls, defaultOpenValue } = this.props; 175 const { prefixCls, defaultOpenValue } = this.props;
180 const value = this.props.value || defaultOpenValue; 176 const value = this.props.value || defaultOpenValue;
diff --git a/src/Panel.jsx b/src/Panel.jsx
index 1adb19a..648944d 100644
--- a/src/Panel.jsx
+++ b/src/Panel.jsx
@@ -93,9 +93,27 @@ class Panel extends Component {
93 this.props.onEsc(); 93 this.props.onEsc();
94 } 94 }
95 95
96 disabledHours = () => {
97 const { use12Hours, disabledHours } = this.props;
98 let disabledOptions = disabledHours();
99 if (use12Hours && Array.isArray(disabledOptions)) {
100 if (this.isAM()) {
101 disabledOptions = disabledOptions.filter(h => h < 12).map(h => (h === 0 ? 12 : h));
102 } else {
103 disabledOptions = disabledOptions.map(h => (h === 12 ? 12 : h - 12));
104 }
105 }
106 return disabledOptions;
107 }
108
109 isAM() {
110 const value = (this.state.value || this.props.defaultOpenValue);
111 return value.hour() >= 0 && value.hour() < 12;
112 }
113
96 render() { 114 render() {
97 const { 115 const {
98 prefixCls, className, placeholder, disabledHours, disabledMinutes, 116 prefixCls, className, placeholder, disabledMinutes,
99 disabledSeconds, hideDisabledOptions, allowEmpty, showHour, showMinute, showSecond, 117 disabledSeconds, hideDisabledOptions, allowEmpty, showHour, showMinute, showSecond,
100 format, defaultOpenValue, clearText, onEsc, addon, use12Hours, onClear, 118 format, defaultOpenValue, clearText, onEsc, addon, use12Hours, onClear,
101 focusOnOpen, onKeyDown, hourStep, minuteStep, secondStep, inputReadOnly, 119 focusOnOpen, onKeyDown, hourStep, minuteStep, secondStep, inputReadOnly,
@@ -103,7 +121,7 @@ class Panel extends Component {
103 const { 121 const {
104 value, currentSelectPanel, 122 value, currentSelectPanel,
105 } = this.state; 123 } = this.state;
106 const disabledHourOptions = disabledHours(); 124 const disabledHourOptions = this.disabledHours();
107 const disabledMinuteOptions = disabledMinutes(value ? value.hour() : null); 125 const disabledMinuteOptions = disabledMinutes(value ? value.hour() : null);
108 const disabledSecondOptions = disabledSeconds(value ? value.hour() : null, 126 const disabledSecondOptions = disabledSeconds(value ? value.hour() : null,
109 value ? value.minute() : null); 127 value ? value.minute() : null);
@@ -131,7 +149,7 @@ class Panel extends Component {
131 hourOptions={hourOptions} 149 hourOptions={hourOptions}
132 minuteOptions={minuteOptions} 150 minuteOptions={minuteOptions}
133 secondOptions={secondOptions} 151 secondOptions={secondOptions}
134 disabledHours={disabledHours} 152 disabledHours={this.disabledHours}
135 disabledMinutes={disabledMinutes} 153 disabledMinutes={disabledMinutes}
136 disabledSeconds={disabledSeconds} 154 disabledSeconds={disabledSeconds}
137 onChange={this.onChange} 155 onChange={this.onChange}
@@ -153,11 +171,12 @@ class Panel extends Component {
153 hourOptions={hourOptions} 171 hourOptions={hourOptions}
154 minuteOptions={minuteOptions} 172 minuteOptions={minuteOptions}
155 secondOptions={secondOptions} 173 secondOptions={secondOptions}
156 disabledHours={disabledHours} 174 disabledHours={this.disabledHours}
157 disabledMinutes={disabledMinutes} 175 disabledMinutes={disabledMinutes}
158 disabledSeconds={disabledSeconds} 176 disabledSeconds={disabledSeconds}
159 onCurrentSelectPanelChange={this.onCurrentSelectPanelChange} 177 onCurrentSelectPanelChange={this.onCurrentSelectPanelChange}
160 use12Hours={use12Hours} 178 use12Hours={use12Hours}
179 isAM={this.isAM()}
161 /> 180 />
162 {addon(this)} 181 {addon(this)}
163 </div> 182 </div>
diff --git a/tests/Select.spec.jsx b/tests/Select.spec.jsx
index 2a15e7c..f414c18 100644
--- a/tests/Select.spec.jsx
+++ b/tests/Select.spec.jsx
@@ -552,5 +552,125 @@ describe('Select', () => {
552 done(); 552 done();
553 }); 553 });
554 }); 554 });
555
556 it('disabled correctly', done => {
557 let change;
558 const picker = renderPicker({
559 use12Hours: true,
560 format: undefined,
561 onChange(v) {
562 change = v;
563 },
564 disabledHours() {
565 return [0, 2, 6, 18, 12];
566 },
567 defaultValue: moment()
568 .hour(0)
569 .minute(0)
570 .second(0),
571 showSecond: false,
572 });
573 expect(picker.state.open).not.to.be.ok();
574 const input = TestUtils.scryRenderedDOMComponentsWithClass(picker, 'rc-time-picker-input')[0];
575 let header;
576 async.series(
577 [
578 next => {
579 expect(picker.state.open).to.be(false);
580
581 Simulate.click(input);
582 setTimeout(next, 100);
583 },
584 next => {
585 expect(picker.state.open).to.be(true);
586 header = TestUtils.scryRenderedDOMComponentsWithClass(
587 picker.panelInstance,
588 'rc-time-picker-panel-input',
589 )[0];
590 expect(header).to.be.ok();
591 expect(header.value).to.be('12:00 am');
592 expect(input.value).to.be('12:00 am');
593
594 const selector = TestUtils.scryRenderedDOMComponentsWithClass(
595 picker.panelInstance,
596 'rc-time-picker-panel-select',
597 )[0];
598 const option = selector.getElementsByTagName('li')[2];
599 Simulate.click(option);
600 setTimeout(next, 100);
601 },
602 next => {
603 expect(change).not.to.be.ok();
604 expect(header.value).to.be('12:00 am');
605 expect(input.value).to.be('12:00 am');
606 expect(picker.state.open).to.be.ok();
607
608 const selector = TestUtils.scryRenderedDOMComponentsWithClass(
609 picker.panelInstance,
610 'rc-time-picker-panel-select',
611 )[0];
612 const option = selector.getElementsByTagName('li')[5];
613 Simulate.click(option);
614 setTimeout(next, 100);
615 },
616 next => {
617 expect(change).to.be.ok();
618 expect(change.hour()).to.be(5);
619 expect(header.value).to.be('5:00 am');
620 expect(input.value).to.be('5:00 am');
621 expect(picker.state.open).to.be.ok();
622
623 const selector = TestUtils.scryRenderedDOMComponentsWithClass(
624 picker.panelInstance,
625 'rc-time-picker-panel-select',
626 )[2];
627 Simulate.click(selector.getElementsByTagName('li')[1]);
628
629 setTimeout(next, 200);
630 change = null;
631 },
632 next => {
633 expect(change).not.to.be.ok();
634 expect(header.value).to.be('5:00 pm');
635 expect(input.value).to.be('5:00 pm');
636 expect(picker.state.open).to.be.ok();
637
638 const selector = TestUtils.scryRenderedDOMComponentsWithClass(
639 picker.panelInstance,
640 'rc-time-picker-panel-select',
641 )[0];
642 const option = selector.getElementsByTagName('li')[0];
643 Simulate.click(option);
644 setTimeout(next, 100);
645 },
646 next => {
647 expect(change).not.to.be.ok();
648 expect(header.value).to.be('5:00 pm');
649 expect(input.value).to.be('5:00 pm');
650 expect(picker.state.open).to.be.ok();
651
652 const selector = TestUtils.scryRenderedDOMComponentsWithClass(
653 picker.panelInstance,
654 'rc-time-picker-panel-select',
655 )[0];
656 const option = selector.getElementsByTagName('li')[5];
657 Simulate.click(option);
658 setTimeout(next, 100);
659 },
660 next => {
661 expect(change).to.be.ok();
662 expect(change.hour()).to.be(17);
663 expect(header.value).to.be('5:00 pm');
664 expect(input.value).to.be('5:00 pm');
665 expect(picker.state.open).to.be.ok();
666
667 next();
668 },
669 ],
670 () => {
671 done();
672 },
673 );
674 });
555 }); 675 });
556}); 676});