From 9f9f39e4fb8073f6f31d545c746e4c3b7ad651da Mon Sep 17 00:00:00 2001 From: yiminghe Date: Thu, 19 Nov 2015 19:57:17 +0800 Subject: fix value --- src/TimePicker.jsx | 44 ++++++++++++++++++++++++++------------------ src/module/Panel.jsx | 25 +++++++++++++++++-------- 2 files changed, 43 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/TimePicker.jsx b/src/TimePicker.jsx index 98a1754..f0b580c 100644 --- a/src/TimePicker.jsx +++ b/src/TimePicker.jsx @@ -18,6 +18,7 @@ const Picker = React.createClass({ prefixCls: PropTypes.string, inputClassName: PropTypes.string, locale: PropTypes.object, + value: PropTypes.object, children: PropTypes.func, disabled: PropTypes.bool, defaultValue: PropTypes.object, @@ -50,17 +51,19 @@ const Picker = React.createClass({ getInitialState() { this.savePanelRef = refFn.bind(this, 'panelInstance'); - const { open, defaultValue } = this.props; + const { open, defaultValue, value } = this.props; return { open: open, - value: defaultValue, + value: value || defaultValue, }; }, componentWillReceiveProps(nextProps) { - const { defaultValue, open } = nextProps; - if (defaultValue !== undefined) { - this.setState({value: defaultValue}); + const { value, open } = nextProps; + if (value !== undefined) { + this.setState({ + value, + }); } if (open !== undefined) { this.setState({open}); @@ -68,16 +71,11 @@ const Picker = React.createClass({ }, onPanelChange(value) { - this.setState({ - value: value, - }); - this.props.onChange(value); + this.setValue(value); }, onPanelClear() { - this.setState({ - value: '', - }); + this.setValue(''); this.setOpen(false); }, @@ -90,6 +88,15 @@ const Picker = React.createClass({ }); }, + setValue(value) { + if (!('value' in this.props)) { + this.setState({ + value, + }); + } + this.props.onChange(value); + }, + getPanel() { const { prefixCls, defaultValue, locale, formatter, placeholder, hourOptions, minuteOptions, secondOptions } = this.props; return ( @@ -110,7 +117,7 @@ const Picker = React.createClass({ const panel = this.getPanel(); const extraProps = { ref: this.savePanelRef, - defaultValue: this.state.value || panel.props.defaultValue, + value: this.state.value, onChange: createChainedFunction(panel.props.onChange, this.onPanelChange), onClear: createChainedFunction(panel.props.onClear, this.onPanelClear), }; @@ -136,12 +143,12 @@ const Picker = React.createClass({ }, render() { - const { prefixCls, placeholder, placement, align, disabled, transitionName, children, formatter, inputClassName } = this.props; + const { prefixCls, placeholder, placement, align, disabled, transitionName, formatter, inputClassName } = this.props; const { open, value } = this.state; return ( - - - + + + ); diff --git a/src/module/Panel.jsx b/src/module/Panel.jsx index f041158..d5e1521 100644 --- a/src/module/Panel.jsx +++ b/src/module/Panel.jsx @@ -19,7 +19,7 @@ function generateOptions(length) { const Panel = React.createClass({ propTypes: { prefixCls: PropTypes.string, - defaultValue: PropTypes.object, + value: PropTypes.object, locale: PropTypes.object, placeholder: PropTypes.string, formatter: PropTypes.object, @@ -43,13 +43,13 @@ const Panel = React.createClass({ }, getInitialState() { - let defaultValue = this.props.defaultValue; - if (!defaultValue) { - defaultValue = new GregorianCalendar(zhCn); - defaultValue.setTime(Date.now()); + let value = this.props.value; + if (!value) { + value = new GregorianCalendar(zhCn); + value.setTime(Date.now()); } return { - value: defaultValue, + value, }; }, @@ -63,6 +63,15 @@ const Panel = React.createClass({ } }, + componentWillReceiveProps(nextProps) { + const value = nextProps.value; + if (value) { + this.setState({ + value, + }); + } + }, + onChange(newValue) { this.setState({ value: newValue }); this.props.onChange(newValue); @@ -76,8 +85,8 @@ const Panel = React.createClass({ showSecond: true, render() { - const { locale, prefixCls, defaultValue, placeholder, hourOptions, minuteOptions, secondOptions } = this.props; - const value = this.state.value || defaultValue; + const { locale, prefixCls, placeholder, hourOptions, minuteOptions, secondOptions } = this.props; + const value = this.state.value; const cls = classnames({ 'narrow': !this.showHour || !this.showSecond }); return ( -- cgit v1.2.3