import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { fromJS } from 'immutable'
|
import { Modal, Form, Row, Col, Select, Radio, Tooltip, Input, InputNumber, Tabs, Button } from 'antd'
|
import { QuestionCircleOutlined, EditOutlined } from '@ant-design/icons'
|
|
import Utils from '@/utils/utils.js'
|
import { getBaseForm, getOptionForm, getRadioOptionForm } from './formconfig'
|
import asyncComponent from '@/utils/asyncComponent'
|
import ColorSketch from '@/mob/colorsketch'
|
import './index.scss'
|
|
const { TabPane } = Tabs
|
|
const EditTable = asyncComponent(() => import('@/templates/zshare/editTable'))
|
const NormalForm = asyncComponent(() => import('@/menu/components/share/normalform'))
|
|
class LineChartDrawerForm extends Component {
|
static propTpyes = {
|
plot: PropTypes.object,
|
config: PropTypes.object,
|
plotchange: PropTypes.func
|
}
|
|
state = {
|
visible: false,
|
plot: null,
|
formlist: null,
|
baseFormlist: null,
|
view: 'normal',
|
colorColumns: [
|
{
|
title: '指标',
|
dataIndex: 'tick',
|
inputType: this.props.config.subtype === 'ratioboard' ? 'text' : 'number',
|
editable: true,
|
width: '40%'
|
},
|
{
|
title: '颜色',
|
dataIndex: 'color',
|
inputType: 'color',
|
editable: true,
|
width: '40%',
|
render: (text, record) => {
|
return (<div style={{width: '80px', height: '23px', background: text}}></div>)
|
}
|
},
|
]
|
}
|
|
showDrawer = () => {
|
const { config } = this.props
|
|
this.setState({
|
visible: true,
|
view: 'normal',
|
plot: fromJS(config.plot).toJS(),
|
baseFormlist: getBaseForm(config.plot),
|
formlist: config.subtype === 'ratioboard' ? getRadioOptionForm(config.plot, config.columns) : getOptionForm(config.plot, config.columns)
|
})
|
}
|
|
getFields() {
|
const { formlist } = this.state
|
const { getFieldDecorator } = this.props.form
|
const fields = []
|
|
if (!formlist) {
|
return fields
|
}
|
|
formlist.forEach((item, index) => {
|
if (item.hidden || item.forbid) return
|
|
if (item.type === 'text') {
|
fields.push(
|
<Col span={12} key={index}>
|
<Form.Item label={item.tooltip ?
|
<Tooltip placement="topLeft" title={item.tooltip}>
|
<QuestionCircleOutlined className="mk-form-tip" />
|
{item.label}
|
</Tooltip> : item.label
|
}>
|
{getFieldDecorator(item.key, {
|
initialValue: item.initVal,
|
rules: [
|
{
|
required: !!item.required,
|
message: '请输入' + item.label + '!'
|
}
|
]
|
})(<Input placeholder="" autoComplete="off" disabled={item.readonly} onPressEnter={this.onSubmit}/>)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'number') {
|
fields.push(
|
<Col span={12} key={index}>
|
<Form.Item label={item.tooltip ?
|
<Tooltip placement="topLeft" title={item.tooltip}>
|
<QuestionCircleOutlined className="mk-form-tip" />
|
{item.label}
|
</Tooltip> : item.label
|
}>
|
{getFieldDecorator(item.key, {
|
initialValue: item.initVal,
|
rules: [
|
{
|
required: !!item.required,
|
message: '请输入' + item.label + '!'
|
}
|
]
|
})(<InputNumber min={item.min} max={item.max} precision={item.decimal} onPressEnter={this.onSubmit}/>)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'select') { // 下拉
|
fields.push(
|
<Col span={12} key={index}>
|
<Form.Item label={item.tooltip ?
|
<Tooltip placement="topLeft" title={item.tooltip}>
|
<QuestionCircleOutlined className="mk-form-tip" />
|
{item.label}
|
</Tooltip> : item.label
|
}>
|
{getFieldDecorator(item.key, {
|
initialValue: item.initVal,
|
rules: [
|
{
|
required: !!item.required,
|
message: '请选择' + item.label + '!'
|
}
|
]
|
})(
|
<Select mode={item.multi ? 'multiple' : ''}>
|
{item.options.map((option, index) =>
|
<Select.Option key={index} value={option.field}>
|
{option.label}
|
</Select.Option>
|
)}
|
</Select>
|
)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'radio') {
|
fields.push(
|
<Col span={12} key={index}>
|
<Form.Item label={item.tooltip ?
|
<Tooltip placement="topLeft" title={item.tooltip}>
|
<QuestionCircleOutlined className="mk-form-tip" />
|
{item.label}
|
</Tooltip> : item.label
|
}>
|
{getFieldDecorator(item.key, {
|
initialValue: item.initVal,
|
rules: [
|
{
|
required: !!item.required,
|
message: '请选择' + item.label + '!'
|
}
|
]
|
})(
|
<Radio.Group disabled={item.readonly}>
|
{item.options.map(option => {
|
return (
|
<Radio key={option.value} value={option.value}>{option.text}</Radio>
|
)
|
})}
|
</Radio.Group>
|
)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'color') {
|
fields.push(
|
<Col span={12} key={index} className="color-col">
|
<Form.Item label={item.tooltip ?
|
<Tooltip placement="topLeft" title={item.tooltip}>
|
<QuestionCircleOutlined className="mk-form-tip" />
|
{item.label}
|
</Tooltip> : item.label
|
}>
|
{getFieldDecorator(item.key, {
|
initialValue: item.initVal
|
})(
|
<ColorSketch />
|
)}
|
</Form.Item>
|
</Col>
|
)
|
}
|
})
|
return fields
|
}
|
|
onSubmit = () => {
|
const { config } = this.props
|
const { plot, view } = this.state
|
|
if (view === 'normal') {
|
this.props.form.validateFieldsAndScroll((err, values) => {
|
if (!err) {
|
let _plot = {...plot, ...values}
|
|
this.setState({
|
plot: _plot,
|
visible: false
|
})
|
|
this.props.plotchange({...config, plot: _plot})
|
}
|
})
|
} else if (view === 'base') {
|
this.baseRef.handleConfirm().then(res => {
|
let _plot = {...plot, ...res}
|
|
this.setState({
|
plot: _plot,
|
visible: false
|
})
|
|
this.props.plotchange({...config, plot: _plot})
|
})
|
} else {
|
this.setState({
|
visible: false
|
})
|
|
this.props.plotchange({...config, plot: plot})
|
}
|
}
|
|
changeTab = (tab) => {
|
const { plot, view } = this.state
|
|
if (view === 'normal') {
|
this.props.form.validateFieldsAndScroll((err, values) => {
|
if (!err) {
|
let _plot = {...plot, ...values}
|
|
this.setState({
|
plot: _plot,
|
view: tab
|
})
|
}
|
})
|
} else if (view === 'base') {
|
this.baseRef.handleConfirm().then(res => {
|
let _plot = {...plot, ...res}
|
|
this.setState({
|
plot: _plot,
|
view: tab
|
})
|
})
|
} else {
|
this.setState({
|
view: tab
|
})
|
}
|
}
|
|
addColor = () => {
|
let plot = fromJS(this.state.plot).toJS()
|
plot.colors = plot.colors || []
|
|
plot.colors.push({
|
uuid: Utils.getuuid(),
|
tick: plot.maxValue || 1,
|
color: '#1890ff'
|
})
|
|
this.setState({plot})
|
}
|
|
changeColor = (colors) => {
|
const { plot } = this.state
|
|
this.setState({plot: {...plot, colors}})
|
}
|
|
render() {
|
const { config } = this.props
|
const { visible, plot, colorColumns, view, baseFormlist } = this.state
|
const formItemLayout = {
|
labelCol: {
|
xs: { span: 24 },
|
sm: { span: 6 }
|
},
|
wrapperCol: {
|
xs: { span: 24 },
|
sm: { span: 18 }
|
}
|
}
|
|
return (
|
<div className="line-chart-drawer-form">
|
<EditOutlined title="编辑" onClick={this.showDrawer} />
|
<Modal
|
wrapClassName="mk-pop-modal"
|
visible={visible}
|
width={850}
|
maskClosable={false}
|
onOk={this.onSubmit}
|
onCancel={() => { this.setState({ visible: false }) }}
|
destroyOnClose
|
>
|
{config.name ? <div className="mk-com-name">{config.name} - 编辑</div> : null}
|
<Tabs activeKey={view} className="menu-chart-edit-box" onChange={this.changeTab}>
|
<TabPane tab="组件设置" key="base">
|
<NormalForm formlist={baseFormlist} inputSubmit={this.onSubmit} wrappedComponentRef={(inst) => this.baseRef = inst}/>
|
</TabPane>
|
<TabPane tab="图表设置" key="normal">
|
<Form {...formItemLayout}>
|
<Row gutter={16}>{this.getFields()}</Row>
|
</Form>
|
</TabPane>
|
{plot ? <TabPane tab="颜色设置" key="color">
|
<div>
|
<Button className="color-add mk-green" onClick={this.addColor}>添加</Button>
|
<EditTable actions={['edit', 'move', 'del']} data={plot.colors || []} columns={colorColumns} onChange={this.changeColor}/>
|
</div>
|
</TabPane> : null}
|
</Tabs>
|
|
</Modal>
|
</div>
|
);
|
}
|
}
|
|
export default Form.create()(LineChartDrawerForm)
|