forked from react-querybuilder/react-querybuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
77 lines (67 loc) · 2.32 KB
/
main.js
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
import '../src/query-builder.scss';
import QueryBuilder from '../src/index';
import ReactDOM from "react-dom";
import React from 'react';
const fields = [
{name: 'firstName', label: 'First Name'},
{name: 'lastName', label: 'Last Name'},
{name: 'age', label: 'Age'},
{name: 'address', label: 'Address'},
{name: 'phone', label: 'Phone'},
{name: 'email', label: 'Email'},
{name: 'twitter', label: 'Twitter'},
{name: 'isDev', label: 'Is a Developer?', value: false},
];
class RootView extends React.Component {
constructor() {
super();
this.state = {
query: {}
};
}
render() {
let controlElements = {
valueEditor: this.customValueEditor()
}
return (
<div className="flex-box">
<div className="scroll">
<QueryBuilder fields={this.props.fields}
controlElements={controlElements}
controlClassnames={{fields: 'form-control'}}
onQueryChange={this.logQuery.bind(this)}/>
</div>
<div className="shrink query-log scroll">
<h4>Query</h4>
<pre>{JSON.stringify(this.state.query, null, 2)}</pre>
</div>
</div>
);
}
customValueEditor() {
let checkbox = class MyCheckbox extends React.Component {
constructor(props) {
super(props);
}
render() {
if (this.props.field !== 'isDev' || this.props.operator !== '=') {
return <input type="text"
value={this.props.value}
onChange={e => this.props.handleOnChange(e.target.value)}/>
}
return (
<span>
<input type="checkbox"
value={!!this.props.value}
onChange={e => this.props.handleOnChange(e.target.checked)}/>
</span>
);
}
};
return checkbox;
}
logQuery(query) {
this.setState({query});
}
}
ReactDOM.render(<RootView fields={fields}/>, document.querySelector('.container'));