Filter
展开收起按钮未完善
import React, { PureComponent } from "react";
import PropTypes from "prop-types";
import { Form, Row, Col, Button, Icon } from "antd";
import Field from "../XField";
import styles from "./index.less";
const formItemLayout = {
labelCol: {
xs: { span: 24 },
sm: { span: 8 },
xl: { span: 4 }
},
wrapperCol: {
xs: { span: 24 },
sm: { span: 16 },
xl: { span: 20 }
}
};
@Form.create()
export default class Filter extends PureComponent {
static propTypes = {
fields: PropTypes.array,
onSearch: PropTypes.func,
onReset: PropTypes.func,
onChange: PropTypes.func
};
static defaultProps = {
fields: [],
onSearch() {},
onReset() {},
onChange() {}
};
state = {
expandForm: false
};
getActionsOffset = fields => {
return Math.abs((fields.length % 3) - 2) * 8;
};
toggleForm = () => {
const { expandForm } = this.state;
this.setState({
expandForm: !expandForm
});
};
handleSearch = e => {
e.preventDefault();
const { form, onSearch } = this.props;
const values = form.getFieldsValue();
onSearch(values);
};
handleReset = () => {
const {
onReset,
form: { resetFields }
} = this.props;
resetFields();
onReset();
};
renderFields = fields => {
const {
form: { getFieldDecorator }
} = this.props;
return fields.map(field => {
const { label, name, ...restField } = field;
return (
<Col key={name} md={8} sm={24}>
<Form.Item label={label}>
{getFieldDecorator(name)(<Field {...restField}></Field>)}
</Form.Item>
</Col>
);
});
};
render() {
const { expandForm } = this.state;
const { fields } = this.props;
const actionsLayout = {
md: {
span: 8,
offset: this.getActionsOffset(fields)
},
sm: { span: 24 }
};
const fieldsProps = expandForm ? fields : fields.slice(0, 5);
const showExpandBtn = fields.length > 5;
return (
<Form
className={styles.filter}
{...formItemLayout}
onSubmit={this.handleSearch}
>
<Row gutter={{ md: 8, lg: 24, xl: 48 }}>
{this.renderFields(fieldsProps)}
<Col {...actionsLayout}>
<Form.Item label=" " colon={false}>
<div className={styles.actions}>
<Button type="primary" htmlType="submit">
查询
</Button>
<Button onClick={this.handleReset}>重置</Button>
{showExpandBtn && (
<a onClick={this.toggleForm}>
展开 <Icon type={expandForm ? "up" : "down"} />
</a>
)}
</div>
</Form.Item>
</Col>
</Row>
</Form>
);
}
}
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
121
122
123
124
125
126
127
128
129
130
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
121
122
123
124
125
126
127
128
129
130
← 自定义表单