king
2020-09-16 c34bcb0a3054bdab29fbaff17e587c19d7b5de28
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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)