import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { Form, Row, Col, Input, Button, Select, DatePicker } from 'antd'
|
import moment from 'moment'
|
import './index.scss'
|
|
const {MonthPicker, WeekPicker, RangePicker} = DatePicker
|
|
class MainSearch extends Component {
|
static propTpyes = {
|
searchlist: PropTypes.array, // 搜索条件列表
|
dict: PropTypes.object // 字典项
|
}
|
|
state = {
|
match: null, // 搜索条件匹配规则
|
style: null
|
}
|
|
UNSAFE_componentWillMount () {
|
let match = {}
|
let style = {}
|
this.props.searchlist.forEach(item => {
|
match[item.field] = item.match
|
style[item.field] = item.type
|
})
|
this.setState({
|
match: match,
|
style: style
|
})
|
}
|
|
getFields() {
|
const { getFieldDecorator } = this.props.form
|
const fields = []
|
this.props.searchlist.forEach((item, index) => {
|
if (item.type === 'text') { // 文本搜索
|
fields.push(
|
<Col span={6} key={index}>
|
<Form.Item label={item.label}>
|
{getFieldDecorator(item.field, {initialValue: item.initval })(<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.field, {initialValue: item.initval })(
|
<Select
|
showSearch
|
onChange={this.searchChange}
|
filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
|
>
|
{item.options.map(option =>
|
<Select.Option id={option.key} title={option.Text} key={option.key} value={option.Value}>{option.Text}</Select.Option>
|
)}
|
</Select>
|
)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'date') { // 时间搜索
|
fields.push(
|
<Col span={6} key={index}>
|
<Form.Item label={item.label}>
|
{getFieldDecorator(item.field, {initialValue: item.initval ? moment().subtract(item.initval, 'days') : null })(
|
<DatePicker onChange={this.searchChange} />
|
)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'datemonth') {
|
fields.push(
|
<Col span={6} key={index}>
|
<Form.Item label={item.label}>
|
{getFieldDecorator(item.field, {initialValue: item.initval ? moment().subtract(item.initval, 'month') : null })(
|
<MonthPicker onChange={this.searchChange} />
|
)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'dateweek') {
|
fields.push(
|
<Col span={6} key={index}>
|
<Form.Item label={item.label}>
|
{getFieldDecorator(item.field, {initialValue: item.initval ? moment().subtract(item.initval * 7, 'days') : null })(
|
<WeekPicker onChange={this.searchChange} />
|
)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'daterange') {
|
fields.push(
|
<Col className="daterange" span={6} key={index}>
|
<Form.Item label={item.label}>
|
{getFieldDecorator(item.field,
|
{
|
initialValue: item.initval ? [moment().subtract(item.initval, 'days'), moment().subtract(item.initval === 1 ? 1 : 0, 'days')] : [null, null]
|
})(
|
<RangePicker
|
placeholder={['开始日期', '结束日期']}
|
renderExtraFooter={() => 'extra footer'}
|
onChange={this.searchChange}
|
/>
|
)}
|
</Form.Item>
|
</Col>
|
)
|
}
|
})
|
|
if (this.props.searchlist.length >= 4) { // 添加搜索、重置按钮
|
fields.push(
|
<Col span={this.props.searchlist.length % 4 ? 6 : 24} style={{paddingLeft: '112px'}} 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) => {
|
let searches = this.getFieldsValues(values)
|
this.props.refreshdata(searches)
|
})
|
}
|
|
searchChange = () => {
|
this.setState({}, () => {
|
this.props.form.validateFields((err, values) => {
|
let searches = this.getFieldsValues(values)
|
this.props.refreshdata(searches)
|
})
|
})
|
}
|
|
handleReset = () => {
|
// 重置
|
this.props.form.resetFields()
|
this.props.form.validateFields((err, values) => {
|
let searches = this.getFieldsValues(values)
|
this.props.refreshdata(searches)
|
})
|
}
|
|
getFieldsValues = (values) => {
|
// 获取搜索条件值
|
let search = []
|
Object.keys(values).forEach(key => {
|
if (this.state.style[key] === 'daterange') {
|
let _value = ''
|
if (values[key].length > 0) {
|
_value = [moment(values[key][0]).format('YYYY-MM-DD'), moment(values[key][1]).format('YYYY-MM-DD')]
|
}
|
search.push({
|
type: this.state.style[key],
|
key: key,
|
value: _value,
|
match: this.state.match[key]
|
})
|
} else if (this.state.style[key] === 'dateweek') {
|
let _value = ''
|
if (values[key]) {
|
_value = [moment(values[key]).startOf('week').format('YYYY-MM-DD'), moment(values[key]).endOf('week').format('YYYY-MM-DD')]
|
}
|
search.push({
|
type: this.state.style[key],
|
key: key,
|
value: _value,
|
match: this.state.match[key]
|
})
|
} else if (this.state.style[key] === 'date') {
|
let _value = ''
|
if (values[key]) {
|
_value = moment(values[key]).format('YYYY-MM-DD')
|
}
|
search.push({
|
type: this.state.style[key],
|
key: key,
|
value: _value,
|
match: this.state.match[key]
|
})
|
} else if (this.state.style[key] === 'datemonth') {
|
let _value = ''
|
if (values[key]) {
|
_value = moment(values[key]).format('YYYY-MM')
|
}
|
search.push({
|
type: this.state.style[key],
|
key: key,
|
value: _value,
|
match: this.state.match[key]
|
})
|
} else {
|
search.push({
|
type: this.state.style[key],
|
key: key,
|
value: values[key].replace(/(^\s*|\s*$)/ig, ''),
|
match: this.state.match[key]
|
})
|
}
|
})
|
return search
|
}
|
|
render() {
|
return (
|
<Form className="ant-advanced-search-form commontable-main-search" onSubmit={this.handleSearch}>
|
<Row gutter={24}>{this.getFields()}</Row>
|
</Form>
|
)
|
}
|
}
|
|
export default Form.create()(MainSearch)
|