import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { Form, Row, Col, Input, Select, Icon, Radio } from 'antd'
|
import moment from 'moment'
|
import EditTable from '../editable'
|
import './index.scss'
|
|
const { TextArea } = Input
|
|
class MainSearch extends Component {
|
static propTpyes = {
|
dict: PropTypes.object, // 字典项
|
formlist: PropTypes.array
|
}
|
|
state = {
|
card: null,
|
inputType: 'text',
|
selectType: 0,
|
options: null
|
}
|
|
openTypeChange = (key, value) => {
|
if (key === 'type') {
|
this.setState({
|
inputType: value,
|
selectType: 0
|
})
|
}
|
}
|
|
onChange = e => {
|
this.setState({
|
selectType: e.target.value
|
})
|
}
|
|
getFields() {
|
const { getFieldDecorator } = this.props.form
|
const fields = []
|
this.props.formlist.forEach((item, index) => {
|
if (item.type === 'text') { // 文本搜索
|
let placeholder = ''
|
if (item.key === 'initval' && this.state.inputType === 'dateday') {
|
placeholder = '例:' + moment().format('YYYY-MM-DD')
|
} else if (item.key === 'initval' && this.state.inputType === 'datetime') {
|
placeholder = '例:' + moment().format('YYYY-MM-DD HH:mm:ss')
|
}
|
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={placeholder} 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} title={option.text} key={option.MenuID} value={option.MenuID}>
|
{item.key === 'icon' && <Icon type={option.text} />} {option.text}
|
</Select.Option>
|
)}
|
</Select>
|
)}
|
</Form.Item>
|
</Col>
|
)
|
if (item.key === 'type' && this.state.inputType === 'select') {
|
fields.push(
|
<Col span={24} key={'radio' + index}>
|
<Form.Item label={'选项'}>
|
<Radio.Group onChange={this.onChange} value={this.state.selectType}>
|
<Radio value={0}>自定义</Radio>
|
<Radio value={1}>数据源</Radio>
|
</Radio.Group>
|
</Form.Item>
|
</Col>
|
)
|
if (this.state.selectType === 0) {
|
fields.push(
|
<Col span={18} offset={6} key={'table' + index}>
|
<EditTable data={this.state.card.options} ref="editTable"/>
|
</Col>
|
)
|
} else {
|
fields.push(
|
<Col span={18} offset={6} key={'table' + index}>
|
<Form.Item className="text-area">
|
{getFieldDecorator('datasource', {
|
initialValue: '',
|
rules: [
|
{
|
required: true,
|
message: this.props.dict['form.required.input'] + 'datasource !'
|
}
|
]
|
})(<TextArea rows={4} />)}
|
</Form.Item>
|
</Col>
|
)
|
}
|
}
|
}
|
})
|
return fields
|
}
|
|
handleConfirm = () => {
|
// 表单提交时检查输入值是否正确
|
return new Promise((resolve, reject) => {
|
this.props.form.validateFieldsAndScroll((err, values) => {
|
if (!err) {
|
if (this.state.inputType === 'select') {
|
values.resourceType = this.state.selectType
|
if (this.state.selectType === 0) {
|
values.options = this.refs.editTable.state.dataSource
|
} else {
|
values.options = []
|
}
|
}
|
values.id = this.state.card.id
|
values.uuid = this.state.card.uuid
|
resolve(values)
|
} else {
|
reject(err)
|
}
|
})
|
})
|
}
|
|
UNSAFE_componentWillMount () {
|
let _item = this.props.formlist.filter(cell => cell.key === 'type')[0]
|
if (_item.initVal === 'select') {
|
this.setState({
|
inputType: 'select',
|
card: _item.card,
|
selectType: _item.card.resourceType
|
})
|
} else {
|
this.setState({
|
inputType: _item.card.type,
|
card: _item.card
|
})
|
}
|
}
|
|
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 commontable-form" id="form-box">
|
<Row gutter={24}>{this.getFields()}</Row>
|
</Form>
|
)
|
}
|
}
|
|
export default Form.create()(MainSearch)
|