import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { is, fromJS } from 'immutable'
|
import { Menu, Popover, Input } from 'antd'
|
|
import './index.scss'
|
|
class StyleInput extends Component {
|
static propTpyes = {
|
defaultValue: PropTypes.any,
|
options: PropTypes.any,
|
value: PropTypes.any,
|
onChange: PropTypes.func,
|
}
|
|
state = {
|
value: '',
|
parseVal: '',
|
width: '',
|
options: null
|
}
|
|
UNSAFE_componentWillMount () {
|
const { defaultValue, value, options } = this.props
|
let val = ''
|
let _options = ['px']
|
|
if (value !== undefined) {
|
val = value
|
} else if (defaultValue !== undefined) {
|
val = defaultValue
|
}
|
|
if (options && options.length > 0) {
|
_options = options
|
}
|
|
let _val = parseInt(val)
|
|
if (isNaN(_val)) {
|
_val = ''
|
}
|
|
this.setState({value: val, options: _options, parseVal: _val})
|
}
|
|
shouldComponentUpdate (nextProps, nextState) {
|
return !is(fromJS(this.state), fromJS(nextState))
|
}
|
|
UNSAFE_componentWillReceiveProps(nextProps) {
|
if (nextProps.value !== undefined && nextProps.value !== this.state.value) {
|
this.setState({ value: nextProps.value })
|
}
|
}
|
|
componentDidMount () {
|
this.setState({width: (this.input.offsetWidth - 10) + 'px'})
|
}
|
|
/**
|
* @description 组件销毁,清除state更新,清除快捷键设置
|
*/
|
componentWillUnmount () {
|
this.setState = () => {
|
return
|
}
|
}
|
|
changeValue = (e) => {
|
let val = e.target.value
|
let _val = parseInt(val)
|
|
if (isNaN(_val)) {
|
_val = ''
|
}
|
|
this.setState({
|
value: val,
|
parseVal: _val
|
})
|
|
if (!val && this.props.onChange) {
|
this.props.onChange('0px')
|
}
|
}
|
|
submitValue = (val) => {
|
this.setState({
|
value: val
|
})
|
|
if (this.props.onChange) {
|
this.props.onChange(val)
|
}
|
}
|
|
render () {
|
const { value, options, parseVal, width } = this.state
|
|
return (
|
<Popover placement="bottom" overlayClassName="style-input-popover" content={
|
parseVal !== '' ?
|
<Menu>
|
{options.map(option => (
|
<Menu.Item key={option} style={{width: width}} onClick={() => this.submitValue(`${parseVal}${option}`)}>{parseVal} {option}</Menu.Item>
|
))}
|
</Menu> : null
|
} trigger="click">
|
<div ref={dom => { this.input = dom }} style={{lineHeight: '32px'}}>
|
<Input value={value} onChange={this.changeValue}/>
|
</div>
|
</Popover>
|
)
|
}
|
}
|
|
export default StyleInput
|