import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { Form, Row, Col, Input, Select, Icon, Radio, notification, Tooltip, InputNumber } from 'antd'
|
import { btnIcons, btnClasses, formRule } from '@/utils/option.js'
|
import Utils from '@/utils/utils.js'
|
import './index.scss'
|
|
const { TextArea } = Input
|
|
class MainSearch extends Component {
|
static propTpyes = {
|
dict: PropTypes.object, // 字典项
|
formlist: PropTypes.any, // 表单信息
|
card: PropTypes.any, // 按钮信息
|
tabs: PropTypes.array // 所有标签页
|
}
|
|
state = {
|
formlist: null, // 表单信息
|
openType: null, // 打开方式
|
interType: null, // 接口类型:内部、外部
|
position: null, // 按钮位置
|
reqOptionSgl: [{
|
value: 'requiredSgl',
|
text: this.props.dict['header.form.requiredSgl']
|
}],
|
reqOptions: [{
|
value: 'notRequired',
|
text: this.props.dict['header.form.notRequired']
|
}, {
|
value: 'requiredSgl',
|
text: this.props.dict['header.form.requiredSgl']
|
}],
|
reqOptionsMutil: [{
|
value: 'notRequired',
|
text: this.props.dict['header.form.notRequired']
|
}, {
|
value: 'requiredSgl',
|
text: this.props.dict['header.form.requiredSgl']
|
}, {
|
value: 'required',
|
text: this.props.dict['header.form.required']
|
}, {
|
value: 'requiredOnce',
|
text: this.props.dict['header.form.requiredOnce']
|
}],
|
insertUpdateOptions: [{
|
value: '',
|
text: this.props.dict['header.form.empty']
|
}, {
|
value: 'insert',
|
text: this.props.dict['header.form.action.insert']
|
}, {
|
value: 'update',
|
text: this.props.dict['header.form.action.update']
|
}]
|
}
|
|
|
UNSAFE_componentWillMount () {
|
let _intertype = ''
|
let _options = null
|
|
this.props.formlist.forEach(form => {
|
if (form.key === 'intertype') {
|
_intertype = form.initVal
|
}
|
})
|
|
if (_intertype === 'outer') {
|
_options = ['label', 'OpenType', 'intertype', 'innerFunc', 'interface', 'outerFunc', 'callbackFunc', 'sysInterface', 'icon', 'class', 'execSuccess', 'execError', 'method']
|
} else {
|
_options = ['label', 'OpenType', 'intertype', 'innerFunc', 'icon', 'class', 'execSuccess', 'execError', 'sql', 'sqlType']
|
}
|
|
this.setState({
|
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 === 'sqlType') {
|
item.options = this.state.insertUpdateOptions
|
}
|
item.hidden = !_options.includes(item.key)
|
return item
|
})
|
})
|
}
|
|
componentDidMount () {
|
const { card } = this.props
|
|
if (card.focus) {
|
try {
|
let _form = document.getElementById('label')
|
_form.select()
|
} catch {
|
console.warn('表单focus失败!')
|
}
|
}
|
}
|
|
onChange = (e, key) => {
|
let value = e.target.value
|
if (key === 'intertype') {
|
let _options = null
|
if (value === 'inner') {
|
_options = ['label', 'OpenType', 'intertype', 'innerFunc', 'icon', 'class', 'execSuccess', 'execError', 'sql', 'sqlType']
|
} else {
|
_options = ['label', 'OpenType', 'intertype', 'innerFunc', 'icon', 'class', 'execSuccess', 'execError', 'sysInterface', '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 + formRule.func.innerPattern + '$', 'g')
|
_rules = [{
|
pattern: _patten,
|
message: formRule.func.innerMessage
|
}, {
|
max: formRule.func.max,
|
message: formRule.func.maxMessage
|
}]
|
} else if (item.key === 'outerFunc' || item.key === 'callbackFunc') {
|
_rules = [{
|
pattern: formRule.func.pattern,
|
message: formRule.func.message
|
}, {
|
max: formRule.func.max,
|
message: formRule.func.maxMessage
|
}]
|
} else {
|
_rules = [{
|
max: formRule.input.max,
|
message: formRule.input.message
|
}]
|
}
|
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 === 'number') { // 文本搜索
|
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
|
})(<InputNumber min={1} max={10000} precision={0} />)}
|
</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}
|
getPopupContainer={() => document.getElementById('winter')}
|
>
|
{item.options.map((option, index) =>
|
<Select.Option id={`${index}`} title={option.text} key={`${index}`} value={option.value}>
|
{item.key === 'icon' && option.value && <Icon type={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 onChange={(e) => {this.onChange(e, item.key)}}>
|
{
|
item.options.map(option => {
|
return (
|
<Radio key={option.value} value={option.value}>{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
|
values.verify = this.props.card.verify || null
|
|
if (values.OpenType === 'excelIn' || values.OpenType === 'excelOut') {
|
values.position = 'toolbar'
|
values.Ot = 'notRequired'
|
} else if (values.OpenType === 'popview' && !values.linkTab) { // 没有关联标签(新建时),创建新标签Id
|
values.linkTab = Utils.getuuid()
|
}
|
|
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)
|