From 4acbf95c7d2693d73b0c8c021ad892b6529d4c06 Mon Sep 17 00:00:00 2001 From: MG12 Date: Tue, 17 Nov 2015 22:16:15 +0800 Subject: remove TimePanel and merge it to TimePicker --- src/module/Panel.jsx | 129 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 src/module/Panel.jsx (limited to 'src/module') diff --git a/src/module/Panel.jsx b/src/module/Panel.jsx new file mode 100644 index 0000000..4220da8 --- /dev/null +++ b/src/module/Panel.jsx @@ -0,0 +1,129 @@ +import React, {PropTypes} from 'react'; +import classnames from 'classnames'; +import GregorianCalendar from 'gregorian-calendar'; +import zhCn from 'gregorian-calendar/lib/locale/zh_CN'; + +import CommonMixin from '../mixin/CommonMixin'; +import Header from './Header'; +import Combobox from './Combobox'; + +function noop() { +} + +function generateOptions(length) { + return Array.apply(null, {length: length}).map((item, index) => { + return index; + }); +} + +const Panel = React.createClass({ + propTypes: { + prefixCls: PropTypes.string, + defaultValue: PropTypes.object, + locale: PropTypes.object, + placeholder: PropTypes.string, + formatter: PropTypes.object, + hourOptions: PropTypes.array, + minuteOptions: PropTypes.array, + secondOptions: PropTypes.array, + onChange: PropTypes.func, + onClear: PropTypes.func, + }, + + mixins: [CommonMixin], + + getDefaultProps() { + return { + hourOptions: generateOptions(24), + minuteOptions: generateOptions(60), + secondOptions: generateOptions(60), + onChange: noop, + onClear: noop, + }; + }, + + getInitialState() { + let defaultValue = this.props.defaultValue; + if (!defaultValue) { + defaultValue = new GregorianCalendar(zhCn); + defaultValue.setTime(Date.now()); + } + return { + value: defaultValue, + }; + }, + + componentWillMount() { + const formatter = this.props.formatter; + const pattern = formatter.originalPattern; + if (pattern === 'HH:mm') { + this.showSecond = false; + } else if (pattern === 'mm:ss') { + this.showHour = false; + } + }, + + onChange(newValue) { + this.setState({ value: newValue }); + this.props.onChange(newValue); + }, + + onClear() { + this.props.onClear(); + }, + + getPlaceholder(placeholder) { + if (placeholder) { + return placeholder; + } + + const { locale } = this.props; + if (!this.showHour) { + return locale.placeholdermmss; + } else if (!this.showSecond) { + return locale.placeholderHHmm; + } + return locale.placeholderHHmmss; + }, + + showHour: true, + showSecond: true, + + render() { + const { locale, prefixCls, defaultValue, placeholder, hourOptions, minuteOptions, secondOptions } = this.props; + const value = this.state.value || defaultValue; + const cls = classnames({ 'narrow': !this.showHour || !this.showSecond }); + + return ( +
+
+ +
+ ); + }, +}); + +export default Panel; -- cgit v1.2.3