From dd2f6abda00cea99ec0a24e3f162fabeba7ac176 Mon Sep 17 00:00:00 2001 From: Antony Shaleynikov Date: Thu, 2 Mar 2017 15:42:05 +0300 Subject: [PATCH] Updated 12 hours example, added default format for 12 hours mode, updated tests --- examples/12hours.js | 4 +-- src/Combobox.jsx | 32 +++++++++----------- src/TimePicker.jsx | 13 ++++++++- tests/Select.spec.jsx | 68 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 21 deletions(-) diff --git a/examples/12hours.js b/examples/12hours.js index 18fb626..44e514a 100644 --- a/examples/12hours.js +++ b/examples/12hours.js @@ -12,7 +12,7 @@ import TimePicker from 'rc-time-picker'; 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)); @@ -24,7 +24,7 @@ ReactDom.render( defaultValue={now} className="xxx" onChange={onChange} - show12Hours + use12Hours />, document.getElementById('__react-content') ); diff --git a/src/Combobox.jsx b/src/Combobox.jsx index 339764c..d2c934c 100644 --- a/src/Combobox.jsx +++ b/src/Combobox.jsx @@ -44,10 +44,10 @@ const Combobox = React.createClass({ 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); @@ -85,22 +85,14 @@ const Combobox = React.createClass({ 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 ( @@ -156,13 +148,12 @@ const Combobox = React.createClass({ }, 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 (