forked from ukrbublik/react-awesome-query-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuery.jsx
More file actions
109 lines (92 loc) · 3.21 KB
/
Query.jsx
File metadata and controls
109 lines (92 loc) · 3.21 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
import React, { Component } from "react";
import merge from "lodash/merge";
import {connect} from "react-redux";
import context from "../stores/context";
import PropTypes from "prop-types";
import * as actions from "../actions";
import {immutableEqual} from "../utils/stuff";
import {useOnPropsChanged, liteShouldComponentUpdate, bindActionCreators} from "../utils/reactUtils";
class Query extends Component {
static propTypes = {
config: PropTypes.object.isRequired,
onChange: PropTypes.func,
renderBuilder: PropTypes.func,
tree: PropTypes.any, //instanceOf(Immutable.Map)
//dispatch: PropTypes.func.isRequired,
//__isInternalValueChange
//__lastAction
//getMemoizedTree: PropTypes.func.isRequired,
//getBasicConfig: PropTypes.func.isRequired,
//sanitizeTree
};
constructor(props) {
super(props);
useOnPropsChanged(this);
this._updateActions(props);
// For preventive validation (tree and config consistency)
// When config has changed from QueryContainer,
// but new dispatched validated tree value is not in redux store yet (tree prop is old)
this.validatedTree = props.getMemoizedTree(props.config, props.tree, undefined, props.sanitizeTree);
this.oldValidatedTree = this.validatedTree;
//props.onChange && props.onChange(this.validatedTree, props.config);
}
_updateActions (props) {
const {config, dispatch} = props;
this.actions = bindActionCreators({...actions.tree, ...actions.group, ...actions.rule}, config, dispatch);
}
shouldComponentUpdate = liteShouldComponentUpdate(this, {
tree: (nextValue) => {
if (nextValue === this.oldValidatedTree && this.oldValidatedTree === this.validatedTree) {
// Got value dispatched from QueryContainer
// Ignore, because we've just rendered it
return false;
}
return true;
}
});
onPropsChanged(nextProps) {
const {onChange} = nextProps;
const oldConfig = this.props.config;
const newTree = nextProps.tree;
const oldTree = this.props.tree;
const newConfig = nextProps.config;
this.oldValidatedTree = this.validatedTree;
this.validatedTree = newTree;
if (oldConfig !== newConfig) {
this._updateActions(nextProps);
this.validatedTree = nextProps.getMemoizedTree(newConfig, newTree, oldConfig);
}
const validatedTreeChanged = !immutableEqual(this.validatedTree, this.oldValidatedTree);
if (validatedTreeChanged) {
const newBasicConfig = nextProps.getBasicConfig(newConfig);
onChange && onChange(this.validatedTree, newBasicConfig, nextProps.__lastAction);
}
}
render() {
const {config, renderBuilder, dispatch, __isInternalValueChange} = this.props;
const builderProps = {
tree: this.validatedTree,
actions: this.actions,
config: config,
dispatch: dispatch,
__isInternalValueChange
};
return renderBuilder(builderProps);
}
}
const ConnectedQuery = connect(
(state) => {
return {
tree: state.tree,
__isInternalValueChange: state.__isInternalValueChange,
__lastAction: state.__lastAction,
};
},
null,
null,
{
context
}
)(Query);
ConnectedQuery.displayName = "ConnectedQuery";
export default ConnectedQuery;