import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { Form, Row, Col, Input, Select, Icon, Radio, notification, Tooltip } from 'antd'
|
import { btnIcons, btnClasses } from '@/utils/option.js'
|
import './index.scss'
|
|
const { TextArea } = Input
|
|
class MainSearch extends Component {
|
static propTpyes = {
|
dict: PropTypes.object, // 字典项
|
formlist: PropTypes.any,
|
card: PropTypes.any
|
}
|
|
state = {
|
formlist: null,
|
openType: null,
|
interType: null,
|
position: null,
|
reqOptionSgl: [{
|
MenuID: 'requiredSgl',
|
text: this.props.dict['header.form.requiredSgl']
|
}],
|
reqOptions: [{
|
MenuID: 'notRequired',
|
text: this.props.dict['header.form.notRequired']
|
}, {
|
MenuID: 'requiredSgl',
|
text: this.props.dict['header.form.requiredSgl']
|
}],
|
reqOptionsMutil: [{
|
MenuID: 'notRequired',
|
text: this.props.dict['header.form.notRequired']
|
}, {
|
MenuID: 'requiredSgl',
|
text: this.props.dict['header.form.requiredSgl']
|
}, {
|
MenuID: 'required',
|
text: this.props.dict['header.form.required']
|
}, {
|
MenuID: 'requiredOnce',
|
text: this.props.dict['header.form.requiredOnce']
|
}]
|
}
|
|
|
UNSAFE_componentWillMount () {
|
let _opentype = this.props.formlist.filter(form => form.key === 'OpenType')[0].initVal
|
let _intertype = this.props.formlist.filter(form => form.key === 'intertype')[0].initVal
|
let _position = this.props.formlist.filter(form => form.key === 'position')[0].initVal
|
let _options = null
|
if (_opentype === 'innerpage') { // 新页面(内部),可选模板
|
_options = ['label', 'Ot', 'OpenType', 'pageTemplate', 'icon', 'class', 'position']
|
} else if (_opentype === 'outerpage') { // 新页面(外部),需要页面地址
|
_options = ['label', 'Ot', 'OpenType', 'url', 'icon', 'class', 'position']
|
} else if (_opentype === 'blank' || _opentype === 'tab' || _opentype === 'popview') {
|
_options = ['label', 'Ot', 'OpenType', 'icon', 'class', 'position']
|
} else if (_opentype === 'excelIn' || _opentype === 'excelOut') {
|
if (_intertype === 'outer') {
|
_options = ['label', 'OpenType', 'intertype', 'innerFunc', 'interface', 'outerFunc', 'callbackFunc', 'icon', 'class', 'execSuccess', 'execError', 'method']
|
} else {
|
_options = ['label', 'OpenType', 'intertype', 'innerFunc', 'icon', 'class', 'execSuccess', 'execError']
|
}
|
} else {
|
if (_intertype === 'outer') {
|
_options = ['label', 'position', 'OpenType', 'intertype', 'innerFunc', 'interface', 'outerFunc', 'callbackFunc', 'Ot', 'icon', 'class', 'execSuccess', 'execError', 'method']
|
} else {
|
_options = ['label', 'position', 'OpenType', 'intertype', 'innerFunc', 'Ot', 'icon', 'class', 'execSuccess', 'execError', 'sql', 'sqlType']
|
}
|
}
|
this.setState({
|
openType: _opentype,
|
interType: _intertype,
|
position: _position,
|
formlist: this.props.formlist.map(item => {
|
if (item.key === 'class') {
|
item.options = btnClasses
|
} else if (item.key === 'icon') {
|
item.options = btnIcons
|
} else if (item.key === 'Ot') {
|
if (_opentype === 'innerpage' || _position === 'grid') {
|
item.options = this.state.reqOptionSgl
|
} else if (['outerpage', 'blank', 'tab', 'popview'].includes(_opentype)) {
|
item.options = this.state.reqOptions
|
} else {
|
item.options = this.state.reqOptionsMutil
|
}
|
}
|
item.hidden = !_options.includes(item.key)
|
return item
|
})
|
})
|
}
|
|
// 打开方式或显示位置变化
|
openTypeChange = (key, value) => {
|
if (key === 'OpenType') {
|
let _options = null
|
if (value === 'innerpage') {
|
_options = ['label', 'Ot', 'OpenType', 'pageTemplate', 'icon', 'class', 'position']
|
} else if (value === 'outerpage') {
|
_options = ['label', 'Ot', 'OpenType', 'url', 'icon', 'class', 'position']
|
} else if (value === 'blank' || value === 'tab' || value === 'popview') {
|
_options = ['label', 'Ot', 'OpenType', 'icon', 'class', 'position']
|
} else if (value === 'excelIn' || value === 'excelOut') {
|
if (this.state.interType === 'outer') {
|
_options = ['label', 'OpenType', 'intertype', 'innerFunc', 'interface', 'outerFunc', 'callbackFunc', 'icon', 'class', 'execSuccess', 'execError', 'method']
|
} else {
|
_options = ['label', 'OpenType', 'intertype', 'innerFunc', 'icon', 'class', 'execSuccess', 'execError']
|
}
|
} else {
|
if (this.state.interType === 'inner') {
|
_options = ['label', 'position', 'OpenType', 'intertype', 'innerFunc', 'Ot', 'icon', 'class', 'execSuccess', 'execError', 'sql', 'sqlType']
|
} else {
|
_options = ['label', 'position', 'OpenType', 'intertype', 'innerFunc', 'Ot', 'icon', 'class', 'execSuccess', 'execError', 'interface', 'outerFunc', 'callbackFunc', 'method']
|
}
|
}
|
this.setState({
|
openType: value,
|
formlist: this.state.formlist.map(item => {
|
if (_options) {
|
item.hidden = !_options.includes(item.key)
|
if (item.key === 'intertype') {
|
item.initVal = this.state.interType
|
}
|
}
|
if (item.key === 'Ot') {
|
if (value === 'innerpage' || this.state.position === 'grid') {
|
item.options = this.state.reqOptionSgl
|
item.initVal = 'requiredSgl'
|
item.hidden = true
|
} else if (['outerpage', 'blank', 'tab', 'popview'].includes(value)) {
|
item.options = this.state.reqOptions
|
item.initVal = 'requiredSgl'
|
item.hidden = true
|
} else {
|
item.options = this.state.reqOptionsMutil
|
}
|
}
|
return item
|
})
|
}, () => {
|
if (['excelIn', 'excelOut'].includes(value)) return
|
|
this.setState({
|
formlist: this.state.formlist.map(item => {
|
if (item.key === 'Ot') {
|
item.hidden = false
|
}
|
return item
|
})
|
})
|
})
|
} else if (key === 'position') {
|
this.setState({
|
position: value,
|
formlist: this.state.formlist.map(item => {
|
if (item.key === 'Ot') {
|
if (this.state.openType === 'innerpage' || value === 'grid') {
|
item.options = this.state.reqOptionSgl
|
item.initVal = 'requiredSgl'
|
item.hidden = true
|
} else if (['outerpage', 'blank', 'tab', 'pop', 'popview'].includes(this.state.openType)) {
|
item.options = this.state.reqOptions
|
item.initVal = 'requiredSgl'
|
item.hidden = true
|
} else {
|
item.options = this.state.reqOptionsMutil
|
}
|
}
|
return item
|
})
|
}, () => {
|
this.setState({
|
formlist: this.state.formlist.map(item => {
|
if (item.key === 'Ot') {
|
item.hidden = false
|
}
|
return item
|
})
|
})
|
})
|
}
|
}
|
|
onChange = (e, key) => {
|
let value = e.target.value
|
if (key === 'intertype') {
|
let _options = null
|
if (value === 'inner') {
|
_options = ['label', 'position', 'OpenType', 'intertype', 'innerFunc', 'Ot', 'icon', 'class', 'execSuccess', 'execError', 'sql', 'sqlType']
|
} else {
|
_options = ['label', 'position', 'OpenType', 'intertype', 'innerFunc', 'Ot', 'icon', 'class', 'execSuccess', 'execError', 'interface', 'outerFunc', 'callbackFunc', 'method']
|
}
|
this.setState({
|
interType: value,
|
formlist: this.state.formlist.map(item => {
|
item.hidden = !_options.includes(item.key)
|
return item
|
})
|
})
|
}
|
}
|
|
getFields() {
|
const { getFieldDecorator } = this.props.form
|
const fields = []
|
this.state.formlist.forEach((item, index) => {
|
if (item.hidden) return
|
|
if (item.type === 'text') { // 文本搜索
|
let _rules = []
|
if (item.key === 'innerFunc') {
|
let str = '^(' + item.fields.join('|') + ')'
|
let _patten = new RegExp(str + '[0-9a-zA-Z_]*', 'ig')
|
_rules = [{
|
pattern: _patten,
|
message: '名称只允许包含数字、字母和下划线,且以指定字符开始。'
|
}, {
|
min: 6,
|
message: '内部函数名称不小于6个字符。'
|
}, {
|
max: 100,
|
message: '内部函数名称不超过100个字符。'
|
}]
|
}
|
fields.push(
|
<Col span={12} key={index}>
|
<Form.Item label={item.tooltip ?
|
<Tooltip placement="topLeft" overlayClassName={item.tooltipClass} title={item.tooltip}>
|
<Icon type="question-circle" />
|
{item.label}
|
</Tooltip> : item.label
|
}>
|
{getFieldDecorator(item.key, {
|
initialValue: item.initVal || '',
|
rules: [
|
{
|
required: item.readonly ? false : !!item.required,
|
message: this.props.dict['form.required.input'] + item.label + '!'
|
},
|
..._rules
|
]
|
})(<Input placeholder="" autoComplete="off" disabled={item.readonly} />)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'select') { // 下拉搜索
|
fields.push(
|
<Col span={12} key={index}>
|
<Form.Item label={item.tooltip ?
|
<Tooltip placement="topLeft" overlayClassName={item.tooltipClass} title={item.tooltip}>
|
<Icon type="question-circle" />
|
{item.label}
|
</Tooltip> : 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[2].toLowerCase().indexOf(input.toLowerCase()) >= 0}
|
onChange={(value) => {this.openTypeChange(item.key, value)}}
|
getPopupContainer={() => document.getElementById('winter')}
|
>
|
{item.options.map(option =>
|
<Select.Option id={option.MenuID} title={option.text} key={option.MenuID} value={option.MenuID}>
|
{item.key === 'icon' && option.MenuID && <Icon type={option.MenuID} />} {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 onChange={(e) => {this.onChange(e, item.key)}}>
|
{
|
item.options.map(option => {
|
return (
|
<Radio key={option.MenuID} value={option.MenuID}>{option.text}</Radio>
|
)
|
})
|
}
|
</Radio.Group>
|
)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'textarea') {
|
fields.push(
|
<Col span={24} key={index}>
|
<Form.Item label={item.label} className="textarea">
|
{getFieldDecorator(item.key, {
|
initialValue: item.initVal
|
})(<TextArea rows={4} />)}
|
</Form.Item>
|
</Col>
|
)
|
}
|
})
|
return fields
|
}
|
|
handleConfirm = () => {
|
// 表单提交时检查输入值是否正确
|
return new Promise((resolve, reject) => {
|
this.props.form.validateFieldsAndScroll((err, values) => {
|
if (!err) {
|
values.id = this.props.card.id
|
values.uuid = this.props.card.uuid
|
|
if (values.OpenType === 'excelIn' || values.OpenType === 'excelOut') {
|
values.position = 'toolbar'
|
values.Ot = 'notRequired'
|
}
|
console.log(values)
|
if (values.innerFunc === '' && values.sql === '') {
|
notification.warning({
|
top: 92,
|
message: this.props.dict['header.form.actionhelp.datasource'],
|
duration: 10
|
})
|
} else if (values.innerFunc === '' && values.sql !== '' && values.sqlType === '') {
|
notification.warning({
|
top: 92,
|
message: this.props.dict['header.form.actionhelp.sqlType'],
|
duration: 10
|
})
|
} else {
|
resolve({
|
type: 'action',
|
values
|
})
|
}
|
} else {
|
reject(err)
|
}
|
})
|
})
|
}
|
|
render() {
|
const formItemLayout = {
|
labelCol: {
|
xs: { span: 24 },
|
sm: { span: 7 }
|
},
|
wrapperCol: {
|
xs: { span: 24 },
|
sm: { span: 17 }
|
}
|
}
|
return (
|
<Form {...formItemLayout} className="ant-advanced-search-form commontable-action-form" id="winter">
|
<Row gutter={24}>{this.getFields()}</Row>
|
</Form>
|
)
|
}
|
}
|
|
export default Form.create()(MainSearch)
|