SearchForm.js
7.9 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import React,{PropTypes,createElement,Component} from 'react';
import {connect} from 'react-redux';
import { Field, reduxForm,formValueSelector,getFormValues,destroy} from 'redux-form';
import { Row,Col,Input } from 'antd';
import cx from 'classnames';
const InputGroup = Input.Group;
class SearchForm extends React.Component {
constructor(props) {
super(props);
this.searchList=this.searchList.bind(this);
this.changeValues = this.changeValues.bind(this);
this.state={
initload: true,
}
}
static propTypes = {
search :PropTypes.fun,
required: PropTypes.bool,
formItemLayout: PropTypes.object,
enableKeys:PropTypes.array,
rememberValue:PropTypes.bool,
initload:PropTypes.bool
}
static defaultProps={
formItemLayout:{
labelCol : 6 ,
wrapperCol : 14
},
label :"",
required:true,
rememberValue:false,
initload:true,
disableKeys:[],
search:(values)=>{
}
}
componentDidMount(){
// 针对分页的数目,可根据limit属性来个性化定制,默认是十条
const {initialize,dispatch,limit=10}=this.props;
this.multipleValues = false;
dispatch(initialize({
offset:0,
offsetNew:'0',
limit:limit,
total_count:0
}))
}
componentWillReceiveProps(nextProps){
const {myValues,enableKeys,change,formName,limit=10}=nextProps,self=this;
const {initialize,dispatch,rememberValue}=this.props;
let initObject = {
offset:0,
offsetNew:'0', // 针对下拉框分页器添加的属性,默认取值字符串'0',下拉选项的redux-form中的name命名为’offsetNew‘,并且搜索框的下拉option结构形如:[{id:'0,name:'0-10'},{id:'10,name:'11-20'}] 张浩-20181120
limit:10,
total_count:0
};
// if(sessionStorage&&sessionStorage[formName]){
// const sessionParmas = JSON.parse(sessionStorage[formName]);
// initObject = {...initObject,...sessionParmas};
// this.loadSession=true;
// }
// if(rememberValue){
// dispatch(initialize({...initObject,...myValues,total_count:0}));
// }else{
dispatch(initialize(initObject));
// }
}
componentWillReceiveProps(nextProps){
const {myValues,enableKeys,change,formName}=nextProps,self=this;
const { initload } = this.state;
const afterMyValues=Object.assign({}, myValues,{ offset:0,
offsetNew:'0',
limit:myValues.limit,
total_count:0,
from:'search',});
const beforeMyValues=Object.assign({}, this.props.myValues,{ offset:0,
offsetNew:'0',
limit:myValues.limit,
total_count:0,
from:'search',});
// if(this.loadSession){
// afterMyValues.offset = myValues.offset;
// afterMyValues.limit = myValues.limit;
// beforeMyValues.offset = myValues.offset;
// beforeMyValues.limit = myValues.limit;
// }
if(JSON.stringify(beforeMyValues)!=JSON.stringify(afterMyValues)){
let reload=true;
enableKeys.map((key,i)=>{
if(self.props.myValues&&myValues&&
(myValues[key]&&self.props.myValues&&self.props.myValues[key]&&myValues[key]!=self.props.myValues[key])||
(myValues[key]&&self.props.myValues&&!self.props.myValues[key])
){
reload=false;
}
});
if (reload && initload && !this.multipleValues){
// if(this.loadSession){
// this.loadSession=false;
// }else{
change("offset",0);
change("offsetNew",'0');
change("from", 'search');
// }
this.props.search(afterMyValues);
// if(sessionStorage)
// sessionStorage[formName] = JSON.stringify(afterMyValues);
}else{
this.setState({
initload:true
})
this.multipleValues = false;
}
}
}
componentWillUnmount(){
const {dispatch,formName,rememberValue}=this.props;
// if(!rememberValue)
dispatch(destroy(formName));
}
searchList(){
const {myValues,change,formName}=this.props;
change("offset",0);
change("offsetNew",'0');
change("from", 'search');
this.props.search({...myValues,offset:0,offsetNew:0});
// if(sessionStorage)
// sessionStorage[formName] = JSON.stringify({...myValues,offset:0});
}
changeValues(newValues) {
console.log('云.......newValues',newValues);
const { myValues, change, initialize } = this.props;
const values = { ...myValues, ...newValues, offset: 0 ,offsetNew:0};
this.multipleValues = true;
initialize(values);
this.props.search({ ...values });
}
render(){
const { formItemLayout:{labelCol,wrapperCol},label,required} = this.props;
return(
<div className='search-form-wrap'>
{this.props.children}
</div>
)
}
}
const submitForm=(values,dispatch,props)=>{
console.log('呵呵呵搭~~~');
const {change,formName}=props;
props.search({...values});
// if(sessionStorage)
// sessionStorage[formName] = JSON.stringify({...values});
}
// SearchForm = reduxForm({
// form : 'search_list',
// onSubmit : submitForm,
// initialValues: {
// offset:0,
// limit:10,
// total_count:0
// }
// })(SearchForm);
const mapState = (state,props) => {
const { formName ='search_list' } = props;
return {
myValues: getFormValues(formName)(state)
}
};
// export default connect(mapState, null, null, { withRef: true })(SearchForm);
/*
export default class SearchFormFactory extends React.Component {
constructor(props) {
super(props);
this.state={
}
}
static propTypes = {
formName:PropTypes.string
}
static defaultProps={
formName:'search_list'
}
componentDidMount(){
const { formName } = this.props;
let MySearchForm = reduxForm({
form : formName,
onSubmit : submitForm,
initialValues : { offset:0, limit:10, total_count:0 }
})(SearchForm);
MySearchForm = connect(mapState, null, null, { withRef: true })(MySearchForm);
this.setState({
form:<MySearchForm {...this.props} ></MySearchForm>
})
}
componentWillReceiveProps(nextProps){
}
componentWillUnmount(){
}
searchGrid(){
const { form } = this.state;
const {mySearchList}=this.refs;
form.getWrappedInstance().refs.wrapped.refs.wrappedInstance.refs.wrapped.searchList();
}
render(){
const {form} = this.state;
return form?form:null;
}
}
*/
export default class SearchFormFactory extends React.Component {
constructor(props) {
super(props);
this.changeValues = this.changeValues.bind(this);
this.state={
MyForm:null
}
}
static propTypes = {
formName:PropTypes.string
}
static defaultProps={
formName:'search_list'
}
componentDidMount(){
const { formName,initialValues } = this.props;
let MySearchForm = reduxForm({
form : formName,
onSubmit : submitForm,
initialValues : { offset:0, limit:10, total_count:0,...initialValues },
destroyOnUnmount: false,
forceUnregisterOnUnmount: true
})(SearchForm);
MySearchForm = connect(mapState, null, null, { withRef: true })(MySearchForm);
this.setState({
MyForm:MySearchForm
})
}
componentWillReceiveProps(nextProps){
}
componentWillUnmount(){
}
searchGrid(){
const {mySearchList}=this.refs;
mySearchList.getWrappedInstance().refs.wrapped.refs.wrappedInstance.refs.wrapped.searchList();
}
changeValues(values) {
const { mySearchList } = this.refs;
console.log('mySearchList', mySearchList);
// mySearchList.getWrappedInstance().ref.wrappedInstance.wrapped.changeValues(values);
mySearchList.getWrappedInstance().refs.wrapped.refs.wrappedInstance.refs.wrapped.changeValues(values);
}
render(){
const {MyForm} = this.state;
if(MyForm){
return(
<MyForm {...this.props} ref="mySearchList" ></MyForm>
)
}else{
return null;
}
}
}