--- /dev/null
+/* 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(
+ <TimePicker
+ showSecond={showSecond}
+ defaultValue={now}
+ className="xxx"
+ onChange={onChange}
+ show12Hours
+ />,
+ document.getElementById('__react-content')
+);
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);
},
},
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 (
<Select
prefixCls={prefixCls}
- options={hourOptions.map(option => formatOption(option, disabledOptions))}
- selectedIndex={hourOptions.indexOf(hour)}
+ options={hourOptionsAdj.map(option => formatOption(option, disabledOptions))}
+ selectedIndex={hourOptionsAdj.indexOf(hourAdj)}
type="hour"
onSelect={this.onItemChange}
onMouseEnter={this.onEnterSelectPanel.bind(this, 'hour')}
);
},
+ getAMPMSelect() {
+ const { prefixCls, show12Hours, defaultOpenValue } = this.props;
+ if (!show12Hours) {
+ return null;
+ }
+ const value = this.props.value || defaultOpenValue;
+ const AMPMOptions = [{ value: 'AM' }, { value: 'PM' }];
+ const selected = (!value.hour() || value.hour() > 12) ? 1 : 0;
+
+ return (
+ <Select
+ prefixCls={prefixCls}
+ options={AMPMOptions}
+ selectedIndex={selected}
+ type="ampm"
+ onSelect={this.onItemChange}
+ onMouseEnter={this.onEnterSelectPanel.bind(this, 'ampm')}
+ />
+ );
+ },
+
render() {
const { prefixCls, defaultOpenValue } = this.props;
const value = this.props.value || defaultOpenValue;
{this.getHourSelect(value.hour())}
{this.getMinuteSelect(value.minute())}
{this.getSecondSelect(value.second())}
+ {this.getAMPMSelect(value.hour())}
</div>
);
},
showMinute: PropTypes.bool,
showSecond: PropTypes.bool,
onClear: PropTypes.func,
+ show12Hours: PropTypes.bool,
addon: PropTypes.func,
},
disabledMinutes: noop,
disabledSeconds: noop,
defaultOpenValue: moment(),
+ show12Hours: false,
addon: noop,
};
},
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,
disabledMinutes={disabledMinutes}
disabledSeconds={disabledSeconds}
onCurrentSelectPanelChange={this.onCurrentSelectPanelChange}
+ show12Hours={show12Hours}
/>
{addon(this)}
</div>
addon: PropTypes.func,
name: PropTypes.string,
autoComplete: PropTypes.string,
+ show12Hours: PropTypes.bool,
},
getDefaultProps() {
onOpen: noop,
onClose: noop,
addon: noop,
+ show12Hours: false,
};
},
prefixCls, placeholder, disabledHours,
disabledMinutes, disabledSeconds, hideDisabledOptions,
allowEmpty, showHour, showMinute, showSecond, defaultOpenValue, clearText,
- addon,
+ addon, show12Hours,
} = this.props;
return (
<Panel
disabledMinutes={disabledMinutes}
disabledSeconds={disabledSeconds}
hideDisabledOptions={hideDisabledOptions}
+ show12Hours={show12Hours}
addon={addon}
/>
);