const showSecond = false;
const str = showSecond ? 'HH:mm:ss' : 'HH:mm';
-const now = moment().hour(14).minute(30);
+const now = moment().hour(0).minute(0);
function onChange(value) {
console.log(value && value.format(str));
defaultValue={now}
className="xxx"
onChange={onChange}
- show12Hours
+ use12Hours
/>,
document.getElementById('__react-content')
);
if (type === 'hour') {
if (use12Hours) {
- if (value.hour() > 12 || !value.hour()) {
- value.hour(+itemValue + 12);
+ if (this.isAM()) {
+ value.hour(+itemValue % 12);
} else {
- value.hour(+itemValue);
+ value.hour((+itemValue % 12) + 12);
}
} else {
value.hour(+itemValue);
return null;
}
const disabledOptions = disabledHours();
- let hourAdj;
- if (use12Hours) {
- if (hour > 12) {
- hourAdj = hour - 12;
- } else {
- hourAdj = hour || 12;
- }
- } else {
- hourAdj = hour;
- }
-
let hourOptionsAdj;
+ let hourAdj;
if (use12Hours) {
- hourOptionsAdj = hourOptions.filter(h => h <= 12 && h > 0);
+ hourOptionsAdj = [12].concat(hourOptions.filter(h => h < 12 && h > 0));
+ hourAdj = (hour % 12) || 12;
} else {
hourOptionsAdj = hourOptions;
+ hourAdj = hour;
}
return (
},
getAMPMSelect() {
- const { prefixCls, use12Hours, defaultOpenValue } = this.props;
+ const { prefixCls, use12Hours } = this.props;
if (!use12Hours) {
return null;
}
- const value = this.props.value || defaultOpenValue;
const AMPMOptions = [{ value: 'AM' }, { value: 'PM' }];
- const selected = (!value.hour() || value.hour() > 12) ? 1 : 0;
+ const selected = this.isAM() ? 0 : 1;
return (
<Select
);
},
+ isAM() {
+ const { value } = this.props;
+ return value.hour() >= 0 && value.hour() < 12;
+ },
+
render() {
const { prefixCls, defaultOpenValue } = this.props;
const value = this.props.value || defaultOpenValue;
},
getFormat() {
- const { format, showHour, showMinute, showSecond } = this.props;
+ const { format, showHour, showMinute, showSecond, use12Hours } = this.props;
if (format) {
return format;
}
+
+ if (use12Hours) {
+ const fmtString = ([
+ showHour ? 'h' : '',
+ showMinute ? 'mm' : '',
+ showSecond ? 'ss' : '',
+ ].filter(item => !!item).join(':'));
+
+ return fmtString.concat(' a');
+ }
+
return [
showHour ? 'HH' : '',
showMinute ? 'mm' : '',
});
});
});
+
+
+ describe('select in 12 hours mode', () => {
+ it('renders correctly', (done) => {
+ const picker = renderPicker({
+ use12Hours: true,
+ defaultValue: moment().hour(14).minute(0).second(0),
+ showSecond: false,
+ format: undefined,
+ });
+ expect(picker.state.open).not.to.be.ok();
+ const input = TestUtils.scryRenderedDOMComponentsWithClass(picker,
+ 'rc-time-picker-input')[0];
+ let selector;
+ async.series([(next) => {
+ expect(picker.state.open).to.be(false);
+
+ Simulate.click(input);
+ setTimeout(next, 100);
+ }, (next) => {
+ expect(picker.state.open).to.be(true);
+ selector = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
+ 'rc-time-picker-panel-select');
+ expect((input).value).to.be('2:00 pm');
+
+ setTimeout(next, 100);
+ }, (next) => {
+ expect(selector.length).to.be(3);
+
+ next();
+ }], () => {
+ done();
+ });
+ });
+
+
+ it('renders 12am correctly', (done) => {
+ const picker = renderPicker({
+ use12Hours: true,
+ defaultValue: moment().hour(0).minute(0).second(0),
+ showSecond: false,
+ format: undefined,
+ });
+ expect(picker.state.open).not.to.be.ok();
+ const input = TestUtils.scryRenderedDOMComponentsWithClass(picker,
+ 'rc-time-picker-input')[0];
+ let selector;
+ async.series([(next) => {
+ expect(picker.state.open).to.be(false);
+
+ Simulate.click(input);
+ setTimeout(next, 100);
+ }, (next) => {
+ expect(picker.state.open).to.be(true);
+ selector = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
+ 'rc-time-picker-panel-select');
+ expect((input).value).to.be('12:00 am');
+
+ setTimeout(next, 100);
+ }, (next) => {
+ expect(selector.length).to.be(3);
+
+ next();
+ }], () => {
+ done();
+ });
+ });
+ });
});