From 72419e2f826031a158173f46d723a672064e37cd Mon Sep 17 00:00:00 2001 From: king <18310653075@163.com> Date: 星期二, 31 八月 2021 22:42:51 +0800 Subject: [PATCH] 2021-08-31 --- src/menu/components/share/styleInput/index.jsx | 47 +++++++++++++++++++++++++++++++++++------------ 1 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/menu/components/share/styleInput/index.jsx b/src/menu/components/share/styleInput/index.jsx index 4233217..361265b 100644 --- a/src/menu/components/share/styleInput/index.jsx +++ b/src/menu/components/share/styleInput/index.jsx @@ -24,14 +24,8 @@ UNSAFE_componentWillMount () { const { value, options } = this.props - let val = '' - let unit = '' - - if (value !== undefined) { - val = value - } - - unit = options[0] + let val = value || '' + let unit = options[0] if (val) { if (val.indexOf('px') > -1) { @@ -56,6 +50,34 @@ shouldComponentUpdate (nextProps, nextState) { return !is(fromJS(this.state), fromJS(nextState)) + } + + UNSAFE_componentWillReceiveProps(nextProps) { + if (nextProps.value === '' && this.state.value !== '') { + this.setState({value: ''}) + } else if (nextProps.value && nextProps.value !== `${this.state.value}${this.state.unit}`) { + let val = nextProps.value + let unit = this.state.unit + + if (val) { + if (val.indexOf('px') > -1) { + unit = 'px' + } else if (val.indexOf('%') > -1) { + unit = '%' + } else if (val.indexOf('vw') > -1) { + unit = 'vw' + } else if (val.indexOf('vh') > -1) { + unit = 'vh' + } + } + + let _val = parseFloat(val) + + if (isNaN(_val)) { + _val = '' + } + this.setState({value: _val, unit}) + } } /** @@ -85,16 +107,17 @@ this.setState({ value: _val, + }, () => { + this.props.onChange(_val !== '' ? `${_val}${unit}` : '') }) - - this.props.onChange(_val ? `${_val}${unit}` : '') } changeUnit = (val) => { const { value } = this.state - this.setState({unit: val}) - this.props.onChange(value ? `${value}${val}` : '') + this.setState({unit: val}, () => { + this.props.onChange(value !== '' ? `${value}${val}` : '') + }) } render () { -- Gitblit v1.8.0