import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { Form, Row, Col, Input, Select } from 'antd'
|
|
import MkIcon from '@/components/mk-icon'
|
import './index.scss'
|
|
class MainSearch extends Component {
|
static propTpyes = {
|
dict: PropTypes.object, // 字典项
|
formlist: PropTypes.array,
|
inputSubmit: PropTypes.func
|
}
|
|
openTypeChange = (key, value) => {
|
if (key === 'openType') {
|
let formlist = this.state.formlist
|
if (value === 'newpage') {
|
formlist.forEach(item => {
|
if (item.key === 'linkUrl') {
|
item.hidden = false
|
item.initVal = 'service'
|
}
|
})
|
} else {
|
formlist.forEach(item => {
|
if (item.key === 'linkUrl') {
|
item.hidden = true
|
}
|
})
|
}
|
this.setState({formlist})
|
}
|
}
|
|
getFields() {
|
const { getFieldDecorator } = this.props.form
|
const fields = []
|
this.props.formlist.forEach((item, index) => {
|
if (item.type === 'text') { // 文本搜索
|
fields.push(
|
<Col span={24} key={index}>
|
<Form.Item label={item.label}>
|
{getFieldDecorator(item.key, {
|
initialValue: item.initVal || '',
|
rules: [
|
{
|
required: !!item.required,
|
message: this.props.dict['form.required.input'] + item.label + '!'
|
}
|
]
|
})(<Input placeholder="" autoFocus={item.key.toLowerCase() === 'menuname'} autoComplete="off" disabled={item.readonly} />)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'select') { // 下拉搜索
|
fields.push(
|
<Col span={24} 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[2].toLowerCase().indexOf(input.toLowerCase()) >= 0}
|
onChange={(value) => {this.openTypeChange(item.key, value)}}
|
getPopupContainer={() => document.getElementById('form-box')}
|
>
|
{item.options.map(option =>
|
<Select.Option id={option.MenuID} key={option.MenuID} value={option.MenuID}>
|
{item.key === 'icon' && <MkIcon type={option.text} />} {option.text || option.MenuName}
|
</Select.Option>
|
)}
|
</Select>
|
)}
|
</Form.Item>
|
</Col>
|
)
|
}
|
})
|
return fields
|
}
|
|
handleConfirm = () => {
|
// 表单提交时检查输入值是否正确
|
return new Promise((resolve, reject) => {
|
this.props.form.validateFieldsAndScroll((err, values) => {
|
if (!err) {
|
resolve(values)
|
} else {
|
reject(err)
|
}
|
})
|
})
|
}
|
|
onEnterSubmit = (e) => {
|
// 表单回车提交
|
if (e.key !== 'Enter') return
|
|
this.props.inputSubmit && this.props.inputSubmit()
|
}
|
|
render() {
|
const formItemLayout = {
|
labelCol: {
|
xs: { span: 24 },
|
sm: { span: 6 }
|
},
|
wrapperCol: {
|
xs: { span: 24 },
|
sm: { span: 18 }
|
}
|
}
|
return (
|
<Form {...formItemLayout} className="ant-advanced-search-form" id="form-box" onKeyDown={this.onEnterSubmit}>
|
<Row gutter={24}>{this.getFields()}</Row>
|
</Form>
|
)
|
}
|
}
|
|
export default Form.create()(MainSearch)
|