import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { Form, Row, Col, Input, Button, Select, DatePicker, notification } from 'antd'
|
import moment from 'moment'
|
import './index.scss'
|
|
// const {MonthPicker, WeekPicker} = DatePicker
|
// const dateFormat = 'YYYY-MM-DD'
|
// const weekFormat = 'YYYYMMDD'
|
// const monthFormat = 'YYYY-MM'
|
|
class MainSearch extends Component {
|
static propTpyes = {
|
searchlist: PropTypes.array, // 搜索条件列表
|
dict: PropTypes.object // 字典项
|
}
|
|
state = {
|
formats: null, // 事件校验规则
|
match: null // 搜索条件匹配规则
|
}
|
|
UNSAFE_componentWillMount () {
|
let formats = {}
|
let match = {}
|
this.props.searchlist.forEach(item => {
|
match[item.FieldName] = item.Op
|
})
|
this.setState({
|
formats: formats,
|
match: match
|
})
|
}
|
|
getFields() {
|
const { getFieldDecorator } = this.props.form
|
const fields = []
|
this.props.searchlist.forEach((item, index) => {
|
if (item.Type === 'text' || item.Type === 'string') { // 文本搜索
|
fields.push(
|
<Col span={6} key={index}>
|
<Form.Item label={item.label}>
|
{getFieldDecorator(item.FieldName, {initialValue: item.InitVal })(<Input placeholder="" autoComplete="off" />)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.Type === 'select') { // 下拉搜索
|
let initval = item.FromField.filter(field => field.Selected === 'Selected')[0]
|
if (!initval) {
|
initval = item.FromField[0]
|
}
|
fields.push(
|
<Col span={6} key={index}>
|
<Form.Item label={item.label}>
|
{getFieldDecorator(item.FieldName, {initialValue: initval.IdField })(
|
<Select
|
showSearch
|
filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
|
>
|
{item.FromField.map(option =>
|
<Select.Option id={option.IdField} title={option.TextField} key={option.IdField} value={option.IdField}>{option.TextField}</Select.Option>
|
)}
|
</Select>
|
)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.Type === 'date') { // 时间搜索
|
let _initval = null
|
if (item.InitVal) {
|
_initval = moment().subtract(parseInt(item.InitVal), 'days')
|
}
|
fields.push(
|
<Col span={6} key={index}>
|
<Form.Item label={item.label}>
|
{getFieldDecorator(item.FieldName, {initialValue: _initval })(
|
<DatePicker />
|
)}
|
</Form.Item>
|
</Col>
|
)
|
// if (item.ID === 'WHE14002009024') {
|
// fields.push(
|
// <Col span={6} key={index}>
|
// <Form.Item label={item.label}>
|
// {getFieldDecorator(item.FieldName, {initialValue: moment('2019-09-14', dateFormat) })(
|
// <DatePicker format={dateFormat} />
|
// )}
|
// </Form.Item>
|
// </Col>
|
// )
|
// } else if (item.ID === 'WHE1400200905') {
|
// fields.push(
|
// <Col span={6} key={index}>
|
// <Form.Item label={item.label}>
|
// {getFieldDecorator(item.FieldName, {initialValue: moment('2019-09', monthFormat) })(
|
// <MonthPicker format={monthFormat} />
|
// )}
|
// </Form.Item>
|
// </Col>
|
// )
|
// } else if (item.ID === 'WHE1400200902') {
|
// fields.push(
|
// <Col span={6} key={index}>
|
// <Form.Item label={item.label}>
|
// {getFieldDecorator(item.FieldName, {initialValue: moment('20190906', weekFormat) })(
|
// <WeekPicker />
|
// )}
|
// </Form.Item>
|
// </Col>
|
// )
|
// }
|
}
|
})
|
|
if (this.props.searchlist.length >= 4) { // 添加搜索、重置按钮
|
fields.push(
|
<Col span={this.props.searchlist.length % 4 ? 6 : 24} style={{ textAlign: 'right' }} key="actions">
|
<Button type="primary" htmlType="submit">
|
{this.props.dict['main.search']}
|
</Button>
|
<Button style={{ marginLeft: 8 }} onClick={this.handleReset}>
|
{this.props.dict['main.reset']}
|
</Button>
|
</Col>
|
)
|
} else {
|
fields.push(
|
<Col span={6} style={{ paddingTop: '4px' }} key="actions">
|
<Button type="primary" htmlType="submit">
|
{this.props.dict['main.search']}
|
</Button>
|
<Button style={{ marginLeft: 8 }} onClick={this.handleReset}>
|
{this.props.dict['main.reset']}
|
</Button>
|
</Col>
|
)
|
}
|
|
return fields
|
}
|
|
handleSearch = (e) => {
|
// 回车或点击搜索
|
e.preventDefault()
|
this.props.form.validateFields((err, values) => {
|
this.getFieldsValues(values)
|
})
|
}
|
|
handleReset = () => {
|
// 重置搜索条件,通知页面刷新
|
this.props.form.resetFields()
|
this.props.refreshdata('', 'reset')
|
}
|
|
getFieldsValues = (searches) => {
|
// 获取搜索条件值
|
let search = {}
|
Object.keys(searches).forEach(key => {
|
let val = searches[key] || ''
|
if (searches[key] && typeof(searches[key]) === 'object') {
|
val = moment(searches[key]).format('YYYY-MM-DD')
|
}
|
search[key] = val
|
})
|
|
let valid = true // 校验必填项
|
this.props.searchlist.forEach(item => {
|
let required = false
|
if (item.Validate && item.Validate !== '{}') {
|
try {
|
required = !!JSON.parse(item.Validate).required
|
} catch (e) {
|
console.error('Validate Field parse error')
|
}
|
}
|
if (required && !search[item.FieldName]) {
|
valid = false
|
notification.warning({
|
top: 92,
|
message: this.props.dict['form.required.input'] + item.label,
|
duration: 5
|
})
|
}
|
})
|
if (valid) {
|
this.props.refreshdata(search)
|
}
|
}
|
|
render() {
|
return (
|
<Form className="ant-advanced-search-form datamanage-search" onSubmit={this.handleSearch}>
|
<Row gutter={24}>{this.getFields()}</Row>
|
</Form>
|
)
|
}
|
}
|
|
export default Form.create()(MainSearch)
|