From 95699887ac75de5dff6bd25278289e31e4745482 Mon Sep 17 00:00:00 2001 From: Antony Shaleynikov Date: Wed, 1 Mar 2017 23:58:06 +0300 Subject: [PATCH] Added 12hours display support --- examples/12hours.html | 0 examples/12hours.js | 30 +++++++++++++++++ src/Combobox.jsx | 78 +++++++++++++++++++++++++++++++++++++++---- src/Panel.jsx | 5 ++- src/Select.jsx | 2 +- src/TimePicker.jsx | 5 ++- 6 files changed, 110 insertions(+), 10 deletions(-) create mode 100644 examples/12hours.html create mode 100644 examples/12hours.js diff --git a/examples/12hours.html b/examples/12hours.html new file mode 100644 index 0000000..e69de29 diff --git a/examples/12hours.js b/examples/12hours.js new file mode 100644 index 0000000..18fb626 --- /dev/null +++ b/examples/12hours.js @@ -0,0 +1,30 @@ +/* eslint no-console:0 */ + +import 'rc-time-picker/assets/index.less'; + +import React from 'react'; +import ReactDom from 'react-dom'; + +import moment from 'moment'; + +import TimePicker from 'rc-time-picker'; + +const showSecond = false; +const str = showSecond ? 'HH:mm:ss' : 'HH:mm'; + +const now = moment().hour(14).minute(30); + +function onChange(value) { + console.log(value && value.format(str)); +} + +ReactDom.render( + , + document.getElementById('__react-content') +); diff --git a/src/Combobox.jsx b/src/Combobox.jsx index 013617c..e31b7fd 100644 --- a/src/Combobox.jsx +++ b/src/Combobox.jsx @@ -35,17 +35,42 @@ const Combobox = React.createClass({ disabledMinutes: PropTypes.func, disabledSeconds: PropTypes.func, onCurrentSelectPanelChange: PropTypes.func, + show12Hours: PropTypes.bool, }, onItemChange(type, itemValue) { - const { onChange, defaultOpenValue } = this.props; + const { onChange, defaultOpenValue, show12Hours } = this.props; const value = (this.props.value || defaultOpenValue).clone(); + if (type === 'hour') { - value.hour(itemValue); + if (show12Hours) { + if (value.hour() > 12 || !value.hour()) { + value.hour(+itemValue + 12); + } else { + value.hour(+itemValue); + } + } else { + value.hour(+itemValue); + } } else if (type === 'minute') { - value.minute(itemValue); + value.minute(+itemValue); + } else if (type === 'ampm') { + if (show12Hours) { + if (itemValue === 'PM' && value.hour() <= 12) { + value.hour(value.hour() + 12); + } + + if (itemValue === 'AM') { + if (!value.hour()) { + value.hour(12); + } else + if (value.hour() > 12) { + value.hour(value.hour() - 12); + } + } + } } else { - value.second(itemValue); + value.second(+itemValue); } onChange(value); }, @@ -55,17 +80,34 @@ const Combobox = React.createClass({ }, getHourSelect(hour) { - const { prefixCls, hourOptions, disabledHours, showHour } = this.props; + const { prefixCls, hourOptions, disabledHours, showHour, show12Hours } = this.props; if (!showHour) { return null; } const disabledOptions = disabledHours(); + let hourAdj; + if (show12Hours) { + if (hour > 12) { + hourAdj = hour - 12; + } else { + hourAdj = hour || 12; + } + } else { + hourAdj = hour; + } + + let hourOptionsAdj; + if (show12Hours) { + hourOptionsAdj = hourOptions.filter(h => h <= 12 && h > 0); + } else { + hourOptionsAdj = hourOptions; + } return ( + ); + }, + render() { const { prefixCls, defaultOpenValue } = this.props; const value = this.props.value || defaultOpenValue; @@ -121,6 +184,7 @@ const Combobox = React.createClass({ {this.getHourSelect(value.hour())} {this.getMinuteSelect(value.minute())} {this.getSecondSelect(value.second())} + {this.getAMPMSelect(value.hour())} ); }, diff --git a/src/Panel.jsx b/src/Panel.jsx index fddea1c..d02bd01 100644 --- a/src/Panel.jsx +++ b/src/Panel.jsx @@ -37,6 +37,7 @@ const Panel = React.createClass({ showMinute: PropTypes.bool, showSecond: PropTypes.bool, onClear: PropTypes.func, + show12Hours: PropTypes.bool, addon: PropTypes.func, }, @@ -49,6 +50,7 @@ const Panel = React.createClass({ disabledMinutes: noop, disabledSeconds: noop, defaultOpenValue: moment(), + show12Hours: false, addon: noop, }; }, @@ -90,7 +92,7 @@ const Panel = React.createClass({ const { prefixCls, className, placeholder, disabledHours, disabledMinutes, disabledSeconds, hideDisabledOptions, allowEmpty, showHour, showMinute, showSecond, - format, defaultOpenValue, clearText, onEsc, addon, + format, defaultOpenValue, clearText, onEsc, addon, show12Hours, } = this.props; const { value, currentSelectPanel, @@ -140,6 +142,7 @@ const Panel = React.createClass({ disabledMinutes={disabledMinutes} disabledSeconds={disabledSeconds} onCurrentSelectPanelChange={this.onCurrentSelectPanelChange} + show12Hours={show12Hours} /> {addon(this)} diff --git a/src/Select.jsx b/src/Select.jsx index 238a776..5733b1a 100644 --- a/src/Select.jsx +++ b/src/Select.jsx @@ -58,7 +58,7 @@ const Select = React.createClass({ }); let onclick = null; if (!item.disabled) { - onclick = this.onSelect.bind(this, +item.value); + onclick = this.onSelect.bind(this, item.value); } return (
  • ); -- 2.41.0