import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { is, fromJS } from 'immutable'
|
import { Form, Row, Col, Input, Select, InputNumber, Radio, Tooltip, Modal, notification } from 'antd'
|
import { QuestionCircleOutlined } from '@ant-design/icons'
|
|
import { getColumnForm } from './formconfig'
|
import { formRule } from '@/utils/option.js'
|
import CodeMirror from '@/templates/zshare/codemirror'
|
import EditTable from '@/templates/zshare/modalform/modaleditable'
|
import './index.scss'
|
|
|
const { TextArea } = Input
|
const columnTypeOptions = {
|
text: ['label', 'field', 'type', 'Align', 'Hide', 'IsSort', 'Width', 'prefix', 'postfix', 'textFormat', 'editable', 'initval', 'blacklist'],
|
number: ['label', 'field', 'type', 'Align', 'Hide', 'IsSort', 'Width', 'decimal', 'format', 'prefix', 'postfix', 'editable', 'initval', 'sum', 'blacklist'],
|
textarea: ['label', 'field', 'type', 'Align', 'Hide', 'Width', 'prefix', 'initval', 'postfix', 'blacklist'],
|
custom: ['label', 'type', 'Align', 'Width', 'blacklist'],
|
colspan: ['label', 'type', 'Align', 'Hide', 'blacklist'],
|
action: ['label', 'type', 'Align', 'Width'],
|
formula: ['label', 'type', 'Align', 'Hide', 'Width', 'prefix', 'postfix', 'eval', 'formula', 'blacklist'],
|
index: ['label', 'type', 'Align', 'Width']
|
}
|
|
class EdiTableColumn extends Component {
|
static propTpyes = {
|
visible: PropTypes.bool,
|
column: PropTypes.object,
|
columns: PropTypes.array,
|
fields: PropTypes.array,
|
submitCol: PropTypes.func, // 提交事件
|
cancelCol: PropTypes.func // 取消时删除事件
|
}
|
|
state = {
|
visible: false,
|
formlist: null,
|
transfield: {}
|
}
|
|
column = null
|
|
UNSAFE_componentWillMount() {
|
let transfield = {}
|
this.props.columns.forEach(item => {
|
transfield[item.field] = item.label
|
})
|
|
this.setState({transfield})
|
}
|
|
UNSAFE_componentWillReceiveProps (nextProps) {
|
if (nextProps.column && !is(fromJS(this.props.column), fromJS(nextProps.column))) {
|
this.editColumn(nextProps.column)
|
}
|
}
|
|
getOptions = () => {
|
let _options = fromJS(columnTypeOptions[this.column.type]).toJS()
|
|
if (this.column.editable === 'true') {
|
_options.push('ctrlField')
|
if (this.column.ctrlField) {
|
_options.push('ctrlValue')
|
}
|
if (this.column.type === 'text') {
|
_options.push('editType')
|
|
if (this.column.editType === 'switch') {
|
_options.push('enter', 'openVal', 'closeVal', 'openText', 'closeText', 'editField')
|
} else if (this.column.editType === 'select') {
|
_options.push('required', 'enter', 'resourceType', 'linkSubField', 'editField', 'dropdown')
|
|
if (this.column.resourceType === '0') {
|
_options.push('options')
|
} else {
|
_options.push('dataSource', 'valueField', 'valueText', 'orderBy', 'orderType', 'disableField', 'database')
|
}
|
} else {
|
_options.push('required', 'enter')
|
}
|
} else if (this.column.type === 'number') {
|
_options.push('max', 'min', 'enter')
|
}
|
}
|
|
return _options
|
}
|
|
editColumn = (column) => {
|
let fields = fromJS(this.props.fields).toJS().map(item => {
|
item.text = `${item.field}(${item.label})`
|
return item
|
})
|
|
let formlist = getColumnForm(column, fields, this.props.columns)
|
|
this.column = fromJS(column).toJS()
|
this.column.editType = this.column.editType || 'text'
|
this.column.resourceType = this.column.resourceType || '0'
|
|
let _options = this.getOptions()
|
|
this.setState({
|
visible: true,
|
formlist: formlist.map(item => {
|
item.hidden = !_options.includes(item.key)
|
|
return item
|
})
|
})
|
if (column.focus) {
|
setTimeout(() => {
|
try {
|
let _form = document.getElementById('label')
|
_form && _form.select()
|
} catch (e) {
|
console.warn('表单focus失败!')
|
}
|
}, 200)
|
}
|
}
|
|
typeChange = (key, value, option) => {
|
this.column[key] = value
|
|
if (key === 'type') {
|
let _options = this.getOptions()
|
|
let _field = ''
|
if (value === 'formula') {
|
_field = this.props.form.getFieldValue('field') || ''
|
}
|
|
this.setState({
|
formlist: this.state.formlist.map(item => {
|
item.initVal = this.column[item.key] || item.initVal
|
item.hidden = !_options.includes(item.key)
|
|
return item
|
})
|
}, () => {
|
if (value === 'action' || value === 'colspan') {
|
this.props.form.setFieldsValue({Align: 'center'})
|
} else if (value === 'formula' && _field) {
|
this.props.form.setFieldsValue({formula: '@' + _field + '@'})
|
}
|
})
|
} else if (key === 'field') {
|
let values = {label: option.props.label || option.props.children}
|
if (/Decimal|int/ig.test(option.props.datatype)) {
|
let decimal = 0
|
if (/Decimal/ig.test(option.props.datatype)) {
|
decimal = +option.props.datatype.replace(/Decimal\(18,/ig, '').replace(')', '')
|
}
|
values.type = 'number'
|
values.decimal = decimal
|
} else {
|
values.type = 'text'
|
}
|
|
let _type = this.column.type
|
this.column.type = values.type
|
|
if (values.type !== _type) {
|
let _options = this.getOptions()
|
|
this.setState({
|
formlist: this.state.formlist.map(item => {
|
item.initVal = this.column[item.key] || item.initVal
|
item.hidden = !_options.includes(item.key)
|
|
return item
|
})
|
}, () => {
|
this.props.form.setFieldsValue(values)
|
})
|
} else {
|
this.props.form.setFieldsValue(values)
|
}
|
} else if (key === 'format' && value === 'percent') {
|
this.props.form.setFieldsValue({postfix: '%'})
|
} else if (key === 'editable' || key === 'editType' || key === 'resourceType' || key === 'ctrlField') {
|
let _options = this.getOptions()
|
|
this.setState({
|
formlist: this.state.formlist.map(item => {
|
item.initVal = this.column[item.key] || item.initVal
|
item.hidden = !_options.includes(item.key)
|
|
return item
|
})
|
})
|
}
|
}
|
|
multiselectChange = (key, value) => {
|
if (key !== 'linkSubField') return
|
|
this.column[key] = value
|
}
|
|
handleEmpty = () => {
|
let field = this.props.form.getFieldValue('valueField')
|
|
if (!field) {
|
notification.warning({
|
top: 92,
|
message: '请填写值·字段。',
|
duration: 5
|
})
|
return
|
}
|
|
let text = this.props.form.getFieldValue('valueText')
|
|
if (!text) {
|
notification.warning({
|
top: 92,
|
message: '请填写文本·字段。',
|
duration: 5
|
})
|
return
|
}
|
|
let resource = this.props.form.getFieldValue('dataSource') || ''
|
|
if (field === text) {
|
resource = `select '' as ${field} union all \n${resource}`
|
} else {
|
resource = `select '' as ${field},'全部' as ${text} union all \n${resource}`
|
}
|
|
this.props.form.setFieldsValue({dataSource: resource})
|
}
|
|
changeOptions = (data) => {
|
this.column.options = data || []
|
}
|
|
getFields() {
|
const { getFieldDecorator } = this.props.form
|
const { formlist, transfield } = this.state
|
const fields = []
|
|
if (!formlist) return null
|
|
formlist.forEach((item, index) => {
|
if (item.hidden || item.forbidden) return
|
|
let span = 12
|
let rules = []
|
let className = ''
|
let content = null
|
let extra = null
|
let initVal = item.initVal || ''
|
|
if (item.type === 'text') {
|
rules = [
|
{ required: item.required, message: '请输入' + item.label + '!' },
|
{
|
max: formRule.input.max,
|
message: formRule.input.message
|
}
|
]
|
|
content = <Input placeholder="" autoComplete="off" onPressEnter={this.handleSubmit} />
|
} else if (item.type === 'number') {
|
rules = [
|
{ required: item.required, message: '请输入' + item.label + '!' }
|
]
|
initVal = item.initVal
|
|
if (!item.unlimit) {
|
content = <InputNumber min={item.min} max={item.max} precision={item.precision} onPressEnter={this.handleSubmit}/>
|
} else {
|
content = <InputNumber onPressEnter={this.handleSubmit}/>
|
}
|
} else if (item.type === 'select') {
|
rules = [
|
{ required: item.required, message: '请选择' + item.label + '!' }
|
]
|
content = <Select
|
showSearch
|
allowClear={item.allowClear === true}
|
filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
|
onChange={(value, option) => {this.typeChange(item.key, value, option)}}
|
getPopupContainer={() => document.getElementById('edit-table-column-winter')}
|
>
|
{item.options.map((option, i) =>
|
<Select.Option key={i} datatype={option.datatype || ''} label={option.label || ''} value={(option.value || option.field || option.MenuID)}>
|
{(option.text || option.label || option.MenuName)}
|
</Select.Option>
|
)}
|
</Select>
|
} else if (item.type === 'radio') {
|
rules = [
|
{ required: item.required, message: '请选择' + item.label + '!' }
|
]
|
initVal = item.initVal
|
|
content = <Radio.Group onChange={(e) => {this.typeChange(item.key, e.target.value)}}>
|
{item.options.map(option => <Radio key={option.value} value={option.value}>{option.text}</Radio>)}
|
</Radio.Group>
|
} else if (item.type === 'multiselect') { // 多选
|
content = <Select
|
showSearch
|
mode="multiple"
|
filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
|
onChange={(value) => this.multiselectChange(item.key, value)}
|
>
|
{item.options.map((option, i) =>
|
<Select.Option key={i} value={option.value || option.field}>{option.text || option.label}</Select.Option>
|
)}
|
</Select>
|
} else if (item.type === 'textarea') {
|
span = 24
|
className = 'text-area'
|
rules = [
|
{ required: item.required, message: '请输入' + item.label + '!' }
|
]
|
|
content = <TextArea rows={item.rows || 2}/>
|
} else if (item.type === 'codemirror') {
|
rules = [
|
{ required: item.required, message: '请输入' + item.label + '!' }
|
]
|
span = 24
|
className = 'text-area'
|
|
extra = <span className="add-resource-empty" onClick={this.handleEmpty}>空</span>
|
|
content = <CodeMirror />
|
} else if (item.type === 'options') {
|
span = 24
|
className = 'text-area'
|
|
let linkSubFields = this.column.linkSubField || []
|
|
content = <EditTable type={'select'} module="form" transfield={transfield} linkSubFields={linkSubFields} onChange={this.changeOptions}/>
|
}
|
|
fields.push(
|
<Col span={span} key={index}>
|
<Form.Item className={className} extra={extra} label={item.tooltip ?
|
<Tooltip placement="topLeft" title={item.tooltip}>
|
<QuestionCircleOutlined className="mk-form-tip" />
|
{item.label}
|
</Tooltip> : item.label
|
}>
|
{getFieldDecorator(item.key, {
|
initialValue: initVal,
|
rules: rules
|
})(content)}
|
</Form.Item>
|
</Col>
|
)
|
})
|
return fields
|
}
|
|
handleSubmit = () => {
|
// 表单提交时检查输入值是否正确
|
this.props.form.validateFieldsAndScroll((err, values) => {
|
if (!err) {
|
if (values.type === 'number' && values.editable === 'true') {
|
if (typeof(values.max) === 'number' && typeof(values.min) === 'number' && values.max < values.min) {
|
notification.warning({
|
top: 92,
|
message: '最大值不可小于最小值!',
|
duration: 5
|
})
|
return
|
}
|
}
|
this.setState({visible: false, formlist: null})
|
this.props.submitCol(values)
|
|
this.column = null
|
}
|
})
|
}
|
|
editModalCancel = () => {
|
this.setState({visible: false, formlist: null})
|
|
this.props.cancelCol()
|
}
|
|
render() {
|
const { visible } = this.state
|
const formItemLayout = {
|
labelCol: {
|
xs: { span: 24 },
|
sm: { span: 6 }
|
},
|
wrapperCol: {
|
xs: { span: 24 },
|
sm: { span: 18 }
|
}
|
}
|
|
return (
|
<div style={{display: 'inline-block'}}>
|
<Modal
|
title="显示列编辑"
|
visible={visible}
|
width={900}
|
maskClosable={false}
|
onOk={this.handleSubmit}
|
onCancel={this.editModalCancel}
|
destroyOnClose
|
>
|
<Form {...formItemLayout} className="commontable-column-form" id="edit-table-column-winter">
|
<Row gutter={24}>{this.getFields()}</Row>
|
</Form>
|
</Modal>
|
</div>
|
)
|
}
|
}
|
|
export default Form.create()(EdiTableColumn)
|