-
-
Notifications
You must be signed in to change notification settings - Fork 503
/
index.tsx
145 lines (127 loc) · 4.24 KB
/
index.tsx
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
import React, { useState, useCallback, useRef } from "react";
import {
Query, Builder, Utils,
//types:
BuilderProps, ImmutableTree, Config, ActionMeta, Actions
} from "@react-awesome-query-builder/ui";
import throttle from "lodash/throttle";
import ImportSkinStyles from "../skins";
import loadConfig from "./config";
import {
useActions, useValidation, useBenchmark, useOutput, useInput, useInitFiles, useConfigChange, useSkins, useBlocksSwitcher,
} from "./blocks";
import { initTreeWithValidation, dispatchHmrUpdate, useHmrUpdate } from "./utils";
import type { DemoQueryBuilderState, DemoQueryBuilderMemo } from "./types";
import { emptyTree } from "./init_data";
import { defaultInitFile, initialSkin, validationTranslateOptions, defaultRenderBlocks } from "./options";
import "./i18n";
// Load config and initial tree
const loadedConfig = loadConfig(window._initialSkin || initialSkin);
const initTree = initTreeWithValidation(window._initFile || defaultInitFile, loadedConfig, validationTranslateOptions);
// Trick for HMR: triggers callback put in useHmrUpdate on every update from HMR
dispatchHmrUpdate(loadedConfig, initTree);
//
// Demo component
//
const DemoQueryBuilder: React.FC = () => {
const memo: React.MutableRefObject<DemoQueryBuilderMemo> = useRef({});
const [state, setState] = useState<DemoQueryBuilderState>({
tree: initTree,
config: loadedConfig,
skin: initialSkin,
spelStr: "",
spelErrors: [] as Array<string>,
renderBocks: defaultRenderBlocks,
initFile: defaultInitFile,
});
// Trick for HMR
useHmrUpdate(useCallback(({config}) => {
setState(state => ({ ...state, config }));
}, []));
const { renderRunActions } = useActions(state, setState, memo);
const { renderValidationHeader, renderValidationBlock } = useValidation(state, setState);
const { renderBenchmarkHeader } = useBenchmark(state, setState, memo);
const { renderOutput } = useOutput(state);
const { renderSpelInputBlock } = useInput(state, setState);
const { renderConfigChangeHeader } = useConfigChange(state, setState);
const { renderInitFilesHeader } = useInitFiles(state, setState);
const { renderSkinSelector } = useSkins(state, setState);
const { renderBlocksSwitcher } = useBlocksSwitcher(state, setState);
const renderBuilder = useCallback((bprops: BuilderProps) => {
return (
<div className="query-builder-container" style={{padding: "10px"}}>
<div className="query-builder qb-lite">
<Builder {...bprops} />
</div>
</div>
);
}, []);
const onChange = useCallback((immutableTree: ImmutableTree, config: Config, actionMeta?: ActionMeta, actions?: Actions) => {
const isInit = !actionMeta;
if (actionMeta && state.renderBocks.actions) {
console.info(actionMeta);
}
memo.current.immutableTree = immutableTree;
memo.current.config = config;
memo.current.actions = actions;
updateResult();
}, [state.renderBocks]);
const updateResult = throttle(() => {
setState(prevState => ({
...prevState,
tree: memo.current.immutableTree!,
config: memo.current.config!
}));
}, 100);
const clearValue = () => {
setState({
...state,
tree: Utils.loadTree(emptyTree),
});
};
return (
<div>
<div>
Settings:
{renderSkinSelector()}
{renderConfigChangeHeader()}
</div>
<div>
Output:
{renderBlocksSwitcher()}
</div>
<div>
Data:
{renderInitFilesHeader()}
<button onClick={clearValue}>Clear</button>
{renderRunActions()}
</div>
<div>
Validation:
{renderValidationHeader()}
</div>
<div>
Benchmark:
{renderBenchmarkHeader()}
</div>
<br />
{renderSpelInputBlock()}
<ImportSkinStyles skin={state.skin} />
{state.renderBocks.queryBuilder && <Query
{...state.config}
value={state.tree}
onInit={onChange}
onChange={onChange}
renderBuilder={renderBuilder}
/>}
<div className="query-builder-result">
<div>
{renderValidationBlock()}
{renderOutput()}
</div>
</div>
</div>
);
};
export default DemoQueryBuilder;