import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { Form, Row, Col, Input, Select, InputNumber, Radio, Tooltip } from 'antd'
|
import { QuestionCircleOutlined } from '@ant-design/icons'
|
|
import { formRule } from '@/utils/option.js'
|
// import './index.scss'
|
|
class MainSearch extends Component {
|
static propTpyes = {
|
dict: PropTypes.object, // 字典项
|
formlist: PropTypes.any,
|
card: PropTypes.any,
|
inputSubmit: PropTypes.any // 回车提交事件
|
}
|
|
state = {
|
formlist: null,
|
oriformlist: null,
|
columns: null,
|
actions: []
|
}
|
|
UNSAFE_componentWillMount () {
|
const { card } = this.props
|
|
let columns = []
|
let actions = []
|
|
let formlist = this.props.formlist.filter(item => !item.forbid)
|
formlist.forEach(item => {
|
if (item.key === 'field') {
|
columns = JSON.parse(JSON.stringify(item.options))
|
} else if (item.key === 'actions') {
|
actions = JSON.parse(JSON.stringify(item.options))
|
}
|
})
|
|
this.setState({
|
columns: columns,
|
actions: actions,
|
oriformlist: formlist,
|
formlist: formlist.map(item => {
|
if (item.key === 'content' && card.datatype === 'dynamic') {
|
item.hidden = true
|
} else if (item.key === 'field' && card.datatype === 'static') {
|
item.hidden = true
|
} else if (item.key === 'field' && card.elemType === 'avatar') {
|
item.options = columns.filter(option => option.type === card.type)
|
} else if (item.key === 'size' && card.elemType === 'avatar' && card.type === 'icon') {
|
item.hidden = false
|
} else if (item.key === 'width' && card.elemType === 'avatar' && card.type === 'picture') {
|
item.hidden = false
|
} else if (item.key === 'radius' && card.elemType === 'avatar' && card.type === 'picture') {
|
item.hidden = false
|
}
|
return item
|
})
|
})
|
}
|
|
typeChange = (key, value) => {
|
const { columns, oriformlist } = this.state
|
|
if (key === 'datatype') {
|
this.setState({
|
formlist: oriformlist.map(item => {
|
item.hidden = false
|
if (item.key === 'content' && value === 'dynamic') {
|
item.hidden = true
|
} else if (item.key === 'field' && value === 'static') {
|
item.hidden = true
|
}
|
|
return item
|
})
|
})
|
} else if (key === 'type') {
|
this.setState({
|
formlist: oriformlist.map(item => {
|
if (item.key === 'field') {
|
item.options = columns.filter(option => option.type === value)
|
} else if (item.key === 'width') {
|
item.hidden = value !== 'picture'
|
} else if (item.key === 'size') {
|
item.hidden = value !== 'icon'
|
} else if (item.key === 'radius') {
|
item.hidden = value !== 'picture'
|
}
|
|
return item
|
})
|
}, () => {
|
if (this.props.form.getFieldValue('field') !== undefined) {
|
this.props.form.setFieldsValue({field: ''})
|
}
|
})
|
}
|
}
|
|
handleSubmit = (e) => {
|
e.preventDefault()
|
|
if (this.props.inputSubmit) {
|
this.props.inputSubmit()
|
}
|
}
|
|
getFields() {
|
const { getFieldDecorator } = this.props.form
|
const fields = []
|
this.state.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" overlayClassName={item.tooltipClass} title={item.tooltip}>
|
<QuestionCircleOutlined className="mk-form-tip" />
|
{item.label}
|
</Tooltip> : item.label
|
}>
|
{getFieldDecorator(item.key, {
|
initialValue: item.initVal || '',
|
rules: [
|
{
|
required: !!item.required,
|
message: this.props.dict['form.required.input'] + item.label + '!'
|
},
|
{
|
max: formRule.input.max,
|
message: formRule.input.message
|
}
|
]
|
})(<Input placeholder="" autoComplete="off" disabled={item.readonly} onPressEnter={this.handleSubmit} />)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'number') {
|
fields.push(
|
<Col span={12} key={index}>
|
<Form.Item label={item.tooltip ?
|
<Tooltip placement="topLeft" overlayClassName={item.tooltipClass} title={item.tooltip}>
|
<QuestionCircleOutlined className="mk-form-tip" />
|
{item.label}
|
</Tooltip> : item.label
|
}>
|
{getFieldDecorator(item.key, {
|
initialValue: item.initVal,
|
rules: [
|
{
|
required: !!item.required,
|
message: this.props.dict['form.required.input'] + item.label + '!'
|
}
|
]
|
})(<InputNumber min={item.min} max={item.max} precision={item.precision || 0} />)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'select') { // 下拉搜索
|
fields.push(
|
<Col span={12} key={index}>
|
<Form.Item label={item.label}>
|
{getFieldDecorator(item.key, {
|
initialValue: item.initVal || '',
|
rules: [
|
{
|
required: !!item.required,
|
message: this.props.dict['form.required.select'] + item.label + '!'
|
}
|
]
|
})(
|
<Select
|
showSearch
|
filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
|
onChange={(value) => {this.typeChange(item.key, value)}}
|
getPopupContainer={() => document.getElementById('card-detail-edit-form')}
|
>
|
{item.options.map((option, index) =>
|
<Select.Option id={index} title={option.text} key={index} value={option.value}>
|
{option.text}
|
</Select.Option>
|
)}
|
</Select>
|
)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'radio') {
|
fields.push(
|
<Col span={12} key={index}>
|
<Form.Item label={item.label}>
|
{getFieldDecorator(item.key, {
|
initialValue: item.initVal,
|
rules: [
|
{
|
required: !!item.required,
|
message: this.props.dict['form.required.select'] + item.label + '!'
|
}
|
]
|
})(
|
<Radio.Group style={{whiteSpace: 'nowrap'}} disabled={item.readonly} onChange={(e) => {this.typeChange(item.key, e.target.value)}}>
|
{
|
item.options.map(option => {
|
return (
|
<Radio key={option.value} value={option.value}>{option.text}</Radio>
|
)
|
})
|
}
|
</Radio.Group>
|
)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'multiselect') { // 多选
|
fields.push(
|
<Col span={12} key={index}>
|
<Form.Item label={item.label}>
|
{getFieldDecorator(item.key, {
|
initialValue: item.initVal || []
|
})(
|
<Select
|
showSearch
|
mode="multiple"
|
filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
|
>
|
{item.options.map((option, i) =>
|
<Select.Option id={i} key={i} value={option.value}>{option.text}</Select.Option>
|
)}
|
</Select>
|
)}
|
</Form.Item>
|
</Col>
|
)
|
}
|
})
|
return fields
|
}
|
|
|
handleConfirm = () => {
|
const { columns, actions } = this.state
|
// 表单提交时检查输入值是否正确
|
return new Promise((resolve, reject) => {
|
this.props.form.validateFieldsAndScroll((err, values) => {
|
if (!err) {
|
if (this.props.card.uuid) {
|
values.uuid = this.props.card.uuid
|
}
|
values.elemType = this.props.card.elemType
|
|
if (['detail', 'header'].includes(values.elemType)) {
|
if (values.field && !values.content) {
|
values.content = columns.filter(col => col.value === values.field)[0].text
|
}
|
}
|
if (values.elemType === 'bottom' && values.actions) {
|
values.actions = values.actions.map(item => actions.filter(action => action.value === item)[0])
|
}
|
if (values.elemType === 'header' && values.actions) {
|
values.actions = actions.filter(action => action.value === values.actions)
|
}
|
|
if (['avatar'].includes(values.elemType)) {
|
if (values.type === 'icon') {
|
let column = columns.filter(col => col.value === values.field)[0]
|
values.icon = column.icon
|
values.color = column.color
|
|
values.width = values.size
|
}
|
}
|
|
resolve(values)
|
} else {
|
reject(err)
|
}
|
})
|
})
|
}
|
|
render() {
|
const formItemLayout = {
|
labelCol: {
|
xs: { span: 24 },
|
sm: { span: 7 }
|
},
|
wrapperCol: {
|
xs: { span: 24 },
|
sm: { span: 17 }
|
}
|
}
|
return (
|
<Form {...formItemLayout} style={{minHeight: '190px'}} id="card-detail-edit-form">
|
<Row gutter={24}>{this.getFields()}</Row>
|
</Form>
|
)
|
}
|
}
|
|
export default Form.create()(MainSearch)
|