import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { Form, Row, Col, Input, Button } from 'antd'
|
import './index.scss'
|
|
class MainSearch extends Component {
|
static propTpyes = {
|
searchlist: PropTypes.array, // 搜索条件列表
|
dict: PropTypes.object // 字典项
|
}
|
|
state = {
|
match: null, // 搜索条件匹配规则
|
style: null,
|
searchlist: null
|
}
|
|
UNSAFE_componentWillMount () {
|
let searchlist = JSON.parse(JSON.stringify(this.props.searchlist))
|
let match = {}
|
let style = {}
|
let _list = []
|
|
searchlist.forEach(item => {
|
match[item.field] = item.match
|
style[item.field] = item.type
|
|
_list.push(item)
|
})
|
|
this.setState({
|
match: match,
|
style: style,
|
searchlist: _list
|
})
|
}
|
|
getFields() {
|
const { getFieldDecorator } = this.props.form
|
const fields = []
|
|
this.state.searchlist.forEach((item, index) => {
|
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>
|
)
|
})
|
|
fields.push(
|
<Col span={6} style={{ whiteSpace: 'nowrap' }} key="actions">
|
<Form.Item label={' '} colon={false}>
|
<Button type="primary" htmlType="submit">
|
{this.props.dict['main.search']}
|
</Button>
|
<Button style={{ marginLeft: 8 }} onClick={this.handleReset}>
|
{this.props.dict['main.reset']}
|
</Button>
|
</Form.Item>
|
</Col>
|
)
|
|
return fields
|
}
|
|
handleSearch = (e) => {
|
// 回车或点击搜索
|
e.preventDefault()
|
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 => {
|
search.push({
|
type: this.state.style[key],
|
key: key,
|
value: values[key].replace(/(^\s*|\s*$)/ig, ''),
|
match: this.state.match[key]
|
})
|
})
|
return search
|
}
|
|
render() {
|
const formItemLayout = {
|
labelCol: {
|
xs: { span: 24 },
|
sm: { span: 8 }
|
},
|
wrapperCol: {
|
xs: { span: 24 },
|
sm: { span: 16 }
|
}
|
}
|
|
return (
|
<Form {...formItemLayout} className="verup-search-line" onSubmit={this.handleSearch}>
|
<Row gutter={24}>{this.getFields()}</Row>
|
</Form>
|
)
|
}
|
}
|
|
export default Form.create()(MainSearch)
|