import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { fromJS } from 'immutable'
|
import { Form, Row, Col, Input, Radio } from 'antd'
|
|
import Utils from '@/utils/utils.js'
|
import './index.scss'
|
|
class MainSearch extends Component {
|
static propTpyes = {
|
menu: PropTypes.any, // 菜单信息,新建时为null
|
type: PropTypes.string, // 操作类型
|
inputSubmit: PropTypes.func
|
}
|
|
state = {
|
formlist: [],
|
defaultMenu: [
|
{
|
type: 'text',
|
key: 'menuName',
|
label: '菜单名称',
|
initVal: '',
|
required: true,
|
readonly: false
|
},
|
{
|
type: 'radio',
|
key: 'openType',
|
label: '打开方式',
|
initVal: 'menu',
|
required: true,
|
options: [{
|
id: 'menu',
|
text: '菜单'
|
}, {
|
id: 'outpage',
|
text: '外部页面'
|
}]
|
},
|
{
|
type: 'text',
|
key: 'linkUrl',
|
label: '页面地址',
|
initVal: '',
|
hidden: true,
|
required: true
|
},
|
{
|
type: 'text',
|
key: 'linkProUrl',
|
label: '正式地址',
|
initVal: '',
|
hidden: true,
|
required: false
|
}
|
]
|
}
|
|
UNSAFE_componentWillMount () {
|
const { menu } = this.props
|
|
if (!menu) {
|
this.setState({
|
formlist: fromJS(this.state.defaultMenu).toJS()
|
})
|
} else {
|
this.setState({
|
formlist: fromJS(this.state.defaultMenu).toJS().map(item => {
|
if (item.key === 'menuName') {
|
item.initVal = menu.MenuName
|
} else if (item.key === 'openType') {
|
item.initVal = menu.PageParam.OpenType
|
} else if (item.key === 'linkUrl' || item.key === 'linkProUrl') {
|
item.initVal = menu.PageParam[item.key] || ''
|
if (menu.PageParam.OpenType === 'menu') {
|
item.hidden = true
|
} else if (menu.PageParam.OpenType === 'outpage') {
|
item.hidden = false
|
}
|
}
|
return item
|
})
|
})
|
}
|
}
|
|
openTypeChange = (key, value) => {
|
if (key === 'openType') {
|
let formlist = fromJS(this.state.formlist).toJS()
|
|
formlist.forEach(item => {
|
if (item.key === 'linkUrl' || item.key === 'linkProUrl') {
|
item.hidden = value !== 'outpage'
|
}
|
})
|
|
this.setState({formlist})
|
}
|
}
|
|
getFields() {
|
const { getFieldDecorator } = this.props.form
|
const fields = []
|
this.state.formlist.forEach((item, index) => {
|
if (item.hidden) return
|
|
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: '请输入' + item.label + '!'
|
}
|
]
|
})(<Input placeholder="" autoFocus={item.key.toLowerCase() === 'menuname'} autoComplete="off" disabled={item.readonly} />)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'radio') {
|
fields.push(
|
<Col span={24} key={index}>
|
<Form.Item label={item.label}>
|
{getFieldDecorator(item.key, {
|
initialValue: item.initVal || '',
|
rules: [
|
{
|
required: !!item.required,
|
message: '请选择' + item.label + '!'
|
}
|
]
|
})(
|
<Radio.Group onChange={(e) => {this.openTypeChange(item.key, e.target.value)}}>
|
{
|
item.options.map(option => {
|
return (
|
<Radio key={option.id} value={option.id}>{option.text}</Radio>
|
)
|
})
|
}
|
</Radio.Group>
|
)}
|
</Form.Item>
|
</Col>
|
)
|
}
|
})
|
|
return fields
|
}
|
|
handleConfirm = () => {
|
const { menu } = this.props
|
|
// 表单提交时检查输入值是否正确
|
return new Promise((resolve, reject) => {
|
this.props.form.validateFieldsAndScroll((err, values) => {
|
if (err) return
|
|
let PageParam = {
|
OpenType: values.openType
|
}
|
|
if (values.openType !== 'menu') {
|
PageParam.linkUrl = values.linkUrl || ''
|
if (values.linkProUrl) {
|
PageParam.linkProUrl = values.linkProUrl
|
}
|
}
|
|
if (!menu) {
|
resolve({
|
MenuID: Utils.getuuid(),
|
MenuName: values.menuName,
|
PageParam: JSON.stringify(PageParam)
|
})
|
} else {
|
resolve({
|
MenuID: menu.MenuID,
|
MenuName: values.menuName,
|
PageParam: JSON.stringify(PageParam)
|
})
|
}
|
})
|
})
|
}
|
|
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="first-menu-form-box" onKeyDown={this.onEnterSubmit}>
|
<Row gutter={24}>{this.getFields()}</Row>
|
</Form>
|
)
|
}
|
}
|
|
export default Form.create()(MainSearch)
|