import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
// import { is, fromJS } from 'immutable'
|
import { Form, Row, Col, Input, Button, Select, DatePicker } from 'antd'
|
import moment from 'moment'
|
import Utils from '@/utils/utils.js'
|
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 => {
|
if (item.Type === 'date') {
|
// formats[item.FieldName] = dateFormat
|
formats[item.FieldName] = weekFormat
|
} else if (item.ID === 'WHE1400200905') {
|
formats[item.FieldName] = monthFormat
|
}
|
match[item.FieldName] = item.Op
|
})
|
this.setState({
|
formats: formats,
|
match: match
|
})
|
}
|
|
// shouldComponentUpdate (nextProps, nextState) {
|
// return !is(fromJS(this.props), fromJS(nextProps)) || !is(fromJS(this.state), fromJS(nextState))
|
// }
|
|
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)(<Input placeholder="" autoComplete="off" />)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.Type === 'select') { // 下拉搜索
|
fields.push(
|
<Col span={6} key={index}>
|
<Form.Item label={item.Label}>
|
{getFieldDecorator(item.FieldName, {initialValue: item.DynOptions[0].id })(
|
<Select
|
showSearch
|
onChange={(val) => {this.selectChange(item.FieldName, val)}}
|
filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
|
>
|
{item.DynOptions.map(option =>
|
<Select.Option id={option.id} title={option.text} key={option.id} value={option.id}>{option.text}</Select.Option>
|
)}
|
</Select>
|
)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.Type === 'date') { // 时间搜索
|
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} onChange={(val) => {this.timeChange(item.FieldName, val)}} />
|
)}
|
</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} onChange={(val) => {this.timeChange(item.FieldName, val)}} />
|
)}
|
</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 onChange={(val) => {this.timeChange(item.FieldName, val)}} />
|
)}
|
</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)
|
})
|
}
|
|
selectChange = (key, val) => {
|
// 条件选择切换
|
this.props.form.validateFields((err, values) => {
|
this.getFieldsValues(Object.assign({}, values, {[key]: val}))
|
})
|
}
|
|
timeChange = (key, val) => {
|
// 时间切换
|
this.props.form.validateFields((err, values) => {
|
this.getFieldsValues(Object.assign({}, values, {[key]: val}))
|
})
|
}
|
|
handleReset = () => {
|
// 重置
|
this.props.form.resetFields()
|
this.props.form.validateFields((err, values) => {
|
this.getFieldsValues(values)
|
})
|
}
|
|
getFieldsValues = (searches) => {
|
// 获取搜索条件值
|
let search = []
|
Object.keys(searches).forEach(key => {
|
if (searches[key] && typeof(searches[key]) === 'object') {
|
if (this.state.formats[key] === weekFormat) {
|
search.push({
|
type: 'date',
|
key: key,
|
value: moment(searches[key]).startOf('week').format(this.state.formats[key]) + ' ' + moment(searches[key]).endOf('week').format(this.state.formats[key]),
|
op: this.state.match[key]
|
})
|
} else {
|
search.push({
|
type: 'date',
|
key: key,
|
value: moment(searches[key]).format(this.state.formats[key]),
|
op: this.state.match[key]
|
})
|
}
|
} else if (searches[key] && searches[key] !== '-1') {
|
search.push({
|
type: 'text',
|
key: key,
|
value: searches[key],
|
op: this.state.match[key]
|
})
|
}
|
})
|
search = Utils.jointsearchkey(search)
|
this.props.refreshdata(search)
|
}
|
|
render() {
|
return (
|
<Form className="ant-advanced-search-form main-search" onSubmit={this.handleSearch}>
|
<Row gutter={24}>{this.getFields()}</Row>
|
</Form>
|
)
|
}
|
}
|
|
export default Form.create()(MainSearch)
|