-
-
Notifications
You must be signed in to change notification settings - Fork 505
/
config_base.ts
401 lines (379 loc) · 9.62 KB
/
config_base.ts
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*eslint @typescript-eslint/no-unused-vars: ["off", {"varsIgnorePattern": "^_"}]*/
import merge from "lodash/merge";
import {
BasicFuncs, CoreConfig,
// types:
Settings, Operators, Widgets, Fields, Config, Types, Conjunctions, LocaleSettings, Funcs, OperatorProximity, Func,
} from "@react-awesome-query-builder/core";
// Create a config for demo app based on CoreConfig - add fields, funcs, some overrides.
// Additional UI modifications are done in `./config` (like `asyncFetch`, `marks`, `factory`)
//
// ! Important !
// Don't use JS functions in config, since it can't be used with SSR.
// Instead add function to `ctx` and refer to it with a name, see `validateFirstName`.
// Or use JsonLogic functions, see `validateValue` for `login` field (advanced usage, but doesn't change `ctx`).
function createConfig(InitialConfig: CoreConfig): Config {
const fields: Fields = {
user: {
label: "User",
tooltip: "Group of fields",
type: "!struct",
subfields: {
firstName: {
label2: "Username",
type: "text",
excludeOperators: ["proximity"],
mainWidgetProps: {
valueLabel: "Name",
valuePlaceholder: "Enter name",
},
fieldSettings: {
validateValue: "validateFirstName",
// -or-
// validateValue: {
// "<": [ {strlen: {var: "val"}}, 10 ]
// },
// -incorrect-
// validateValue: (val: string) => {
// return (val.length < 10);
// },
},
},
login: {
type: "text",
excludeOperators: ["proximity"],
fieldSettings: {
validateValue: {
and: [
{ "<": [ {strlen: {var: "val"}}, 10 ] },
{ or: [
{ "===": [ {var: "val"}, "" ] },
{ regexTest: [ {var: "val"}, "^[A-Za-z0-9_-]+$" ] }
]}
]
}
// -incorrect-
// (val: string) => {
// return (val.length < 10 && (val === "" || val.match(/^[A-Za-z0-9_-]+$/) !== null));
// },
},
mainWidgetProps: {
valueLabel: "Login",
valuePlaceholder: "Enter login",
},
}
}
},
prox1: {
label: "prox",
tooltip: "Proximity search",
type: "text",
operators: ["proximity"],
},
num: {
label: "Number",
type: "number",
preferWidgets: ["number"],
fieldSettings: {
min: -1,
max: 5
},
funcs: ["LINEAR_REGRESSION"],
},
slider: {
label: "Slider",
type: "number",
preferWidgets: ["slider", "rangeslider"],
valueSources: ["value", "field"],
fieldSettings: {
min: 0,
max: 100,
step: 1,
},
//overrides
widgets: {
slider: {
widgetProps: {
valuePlaceholder: "..Slider",
}
}
},
},
date: {
label: "Date",
type: "date",
valueSources: ["value"],
},
time: {
label: "Time",
type: "time",
valueSources: ["value"],
operators: ["greater_or_equal", "less_or_equal", "between"],
defaultOperator: "between",
},
datetime: {
label: "DateTime",
type: "datetime",
valueSources: ["value"]
},
datetime2: {
label: "DateTime2",
type: "datetime",
valueSources: ["field"]
},
color: {
label: "Color",
type: "select",
valueSources: ["value"],
fieldSettings: {
listValues: [
{ value: "yellow", title: "Yellow" },
{ value: "green", title: "Green" },
{ value: "orange", title: "Orange" }
],
}
},
color2: {
label: "Color2",
type: "select",
fieldSettings: {
listValues: {
yellow: "Yellow",
green: "Green",
orange: "Orange",
purple: "Purple"
},
}
},
multicolor: {
label: "Colors",
type: "multiselect",
fieldSettings: {
listValues: {
yellow: "Yellow",
green: "Green",
orange: "Orange"
},
allowCustomValues: true
},
},
stock: {
label: "In stock",
type: "boolean",
defaultValue: true,
mainWidgetProps: {
labelYes: "+",
labelNo: "-"
}
},
autocomplete: {
label: "Autocomplete",
type: "select",
valueSources: ["value"],
fieldSettings: {
useAsyncSearch: true,
useLoadMore: true,
forceAsyncSearch: false,
allowCustomValues: false
},
},
autocompleteMultiple: {
label: "AutocompleteMultiple",
type: "multiselect",
valueSources: ["value"],
fieldSettings: {
useAsyncSearch: true,
useLoadMore: true,
forceAsyncSearch: false,
allowCustomValues: false
},
},
};
//////////////////////////////////////////////////////////////////////
const conjunctions: Conjunctions = {
AND: InitialConfig.conjunctions.AND,
OR: InitialConfig.conjunctions.OR,
};
const proximity: OperatorProximity = {
...InitialConfig.operators.proximity,
valueLabels: [
{ label: "Word 1", placeholder: "Enter first word" },
{ label: "Word 2", placeholder: "Enter second word" },
],
textSeparators: [
//'Word 1',
//'Word 2'
],
options: {
...InitialConfig.operators.proximity.options,
optionLabel: "Near", // label on top of "near" selectbox (for config.settings.showLabels==true)
optionTextBefore: "Near", // label before "near" selectbox (for config.settings.showLabels==false)
optionPlaceholder: "Select words between", // placeholder for "near" selectbox
minProximity: 2,
maxProximity: 10,
defaults: {
proximity: 2
},
customProps: {}
}
};
const operators: Operators = {
...InitialConfig.operators,
// examples of overriding
between: {
...InitialConfig.operators.between,
valueLabels: [
"Value from",
"Value to"
],
textSeparators: [
"from",
"to"
],
},
proximity,
};
const widgets: Widgets = {
...InitialConfig.widgets,
// examples of overriding
text: {
...InitialConfig.widgets.text,
},
slider: {
...InitialConfig.widgets.slider,
customProps: {
width: "300px"
}
},
rangeslider: {
...InitialConfig.widgets.rangeslider,
customProps: {
width: "300px"
}
},
date: {
...InitialConfig.widgets.date,
dateFormat: "DD.MM.YYYY",
valueFormat: "YYYY-MM-DD",
},
time: {
...InitialConfig.widgets.time,
timeFormat: "HH:mm",
valueFormat: "HH:mm:ss",
},
datetime: {
...InitialConfig.widgets.datetime,
timeFormat: "HH:mm",
dateFormat: "DD.MM.YYYY",
valueFormat: "YYYY-MM-DD HH:mm:ss",
},
func: {
...InitialConfig.widgets.func,
customProps: {
showSearch: true
}
},
treeselect: {
...InitialConfig.widgets.treeselect,
customProps: {
showSearch: true
}
},
};
const types: Types = {
...InitialConfig.types,
// examples of overriding
boolean: merge({}, InitialConfig.types.boolean, {
widgets: {
boolean: {
widgetProps: {
hideOperator: true,
operatorInlineLabel: "is",
}
},
},
}),
};
const localeSettings: LocaleSettings = {
locale: {
moment: "ru",
},
valueLabel: "Value",
valuePlaceholder: "Value",
fieldLabel: "Field",
operatorLabel: "Operator",
fieldPlaceholder: "Select field",
operatorPlaceholder: "Select operator",
deleteLabel: null,
addGroupLabel: "Add group",
addRuleLabel: "Add rule",
addSubRuleLabel: "Add sub rule",
addSubGroupLabel: "Add sub group",
delGroupLabel: null,
notLabel: "Not",
fieldSourcesPopupTitle: "Select source",
valueSourcesPopupTitle: "Select value source",
removeRuleConfirmOptions: {
title: "Are you sure delete this rule?",
okText: "Yes",
okType: "danger",
},
removeGroupConfirmOptions: {
title: "Are you sure delete this group?",
okText: "Yes",
okType: "danger",
},
};
const settings: Settings = {
...InitialConfig.settings,
...localeSettings,
useConfigCompress: true,
valueSourcesInfo: {
value: {
label: "Value"
},
field: {
label: "Field",
widget: "field",
},
func: {
label: "Function",
widget: "func",
}
},
fieldSources: ["field", "func"],
// canReorder: false,
// canRegroup: false,
// showNot: false,
// showLabels: true,
maxNesting: 3,
canLeaveEmptyGroup: true, //after deletion
};
const funcs: Funcs = {
LINEAR_REGRESSION: BasicFuncs.LINEAR_REGRESSION,
LOWER: BasicFuncs.LOWER,
};
// ! Important !
// Context is not saved to compressed config (zipConfig).
// You must provide `ctx` to `ConfigUtils.decompressConfig()`.
// `validateFirstName` should be defined in `components/demo/config_ctx` for using on client-side.
// Implementation here is used for server-side validation.
const ctx = {
...InitialConfig.ctx,
validateFirstName: (val: string) => {
return (val.length < 10);
},
};
const config: Config = {
conjunctions,
operators,
widgets,
types,
settings,
fields,
funcs,
ctx
};
return config;
}
export default createConfig(CoreConfig);