import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { is, fromJS } from 'immutable'
|
import { Form, Select } from 'antd'
|
|
import asyncComponent from '@/utils/asyncComponent'
|
import './index.scss'
|
|
const ColorSketch = asyncComponent(() => import('@/mob/colorsketch'))
|
const SourceComponent = asyncComponent(() => import('@/menu/components/share/sourcecomponent'))
|
const { Option } = Select
|
|
class MobController extends Component {
|
static propTpyes = {
|
config: PropTypes.any,
|
updateConfig: PropTypes.func,
|
}
|
|
state = {
|
backgroundColor: '',
|
backgroundImage: '',
|
backgroundSize: '',
|
backgroundRepeat: '',
|
}
|
|
UNSAFE_componentWillMount () {
|
const { config } = this.props
|
|
let bgImg = config.style.backgroundImage || ''
|
|
if (bgImg && /^url/.test(bgImg)) {
|
bgImg = bgImg.replace('url(', '')
|
bgImg = bgImg.replace(')', '')
|
}
|
|
this.setState({
|
backgroundColor: config.style.backgroundColor,
|
backgroundImage: bgImg,
|
backgroundSize: config.style.backgroundSize || '100%',
|
backgroundRepeat: config.style.backgroundRepeat || 'repeat',
|
})
|
}
|
|
shouldComponentUpdate (nextProps, nextState) {
|
return !is(fromJS(this.state), fromJS(nextState))
|
}
|
|
/**
|
* @description 修改背景颜色 ,颜色控件
|
*/
|
changeBackgroundColor = (val) => {
|
let config = fromJS(this.props.config).toJS()
|
|
this.setState({
|
backgroundColor: val
|
})
|
|
config.style.backgroundColor = val
|
this.props.updateConfig(config)
|
}
|
|
imgChange = (val) => {
|
this.setState({
|
backgroundImage: val
|
})
|
|
let config = fromJS(this.props.config).toJS()
|
|
if (val) {
|
config.style.backgroundImage = `url(${val})`
|
} else {
|
delete config.style.backgroundImage
|
}
|
this.props.updateConfig(config)
|
}
|
|
backgroundSizeChange = (val) => {
|
this.setState({
|
backgroundSize: val
|
})
|
|
let config = fromJS(this.props.config).toJS()
|
config.style.backgroundSize = val
|
|
this.props.updateConfig(config)
|
}
|
|
backgroundRepeatChange = (val) => {
|
this.setState({
|
backgroundRepeat: val
|
})
|
|
let config = fromJS(this.props.config).toJS()
|
config.style.backgroundRepeat = val
|
|
this.props.updateConfig(config)
|
}
|
|
render () {
|
const { backgroundColor, backgroundImage, backgroundSize, backgroundRepeat } = this.state
|
const formItemLayout = {
|
labelCol: {
|
xs: { span: 24 },
|
sm: { span: 4 }
|
},
|
wrapperCol: {
|
xs: { span: 24 },
|
sm: { span: 20 }
|
}
|
}
|
|
return (
|
<div className="menu-background-controller">
|
<Form {...formItemLayout}>
|
<Form.Item className="color-control" colon={false} label="颜色">
|
<ColorSketch value={backgroundColor} onChange={this.changeBackgroundColor} />
|
</Form.Item>
|
<Form.Item colon={false} label="图片">
|
<SourceComponent value={backgroundImage} type="" placement="right" onChange={this.imgChange}/>
|
</Form.Item>
|
<Form.Item colon={false} label="比例">
|
<Select defaultValue={backgroundSize} onChange={this.backgroundSizeChange}>
|
<Option value="100%">100%</Option>
|
<Option value="100% 100%">100% 100%</Option>
|
<Option value="auto 100%">auto 100%</Option>
|
<Option value="contain">contain</Option>
|
<Option value="cover">cover</Option>
|
</Select>
|
</Form.Item>
|
<Form.Item colon={false} label="重复">
|
<Select defaultValue={backgroundRepeat} onChange={this.backgroundRepeatChange}>
|
<Option value="repeat">repeat</Option>
|
<Option value="no-repeat">no-repeat</Option>
|
<Option value="repeat-x">repeat-x</Option>
|
<Option value="repeat-y">repeat-y</Option>
|
</Select>
|
</Form.Item>
|
</Form>
|
</div>
|
)
|
}
|
}
|
|
export default MobController
|