@visns-studio/visns-components 1.0.1-2.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,673 +0,0 @@
1
- import React, { useState, useEffect, useRef } from "react";
2
- import CustomFetch from "../crm/visns-fetch";
3
- import Select from "../crm/visns-select";
4
- import MultiSelect from "../crm/visns-multi-select";
5
- import VisnsAutocomplete from "../crm/visns-autocomplete";
6
- import DatePicker from "react-datepicker";
7
- import _ from "lodash";
8
- import ReactQuill from "react-quill";
9
- import Toggle from "react-toggle";
10
- import { NumericFormat, PatternFormat } from "react-number-format";
11
- import { motion } from "framer-motion";
12
- import "react-toggle/style.css";
13
- import "react-quill/dist/quill.snow.css";
14
- import "react-datepicker/dist/react-datepicker.css";
15
-
16
- function Field(props) {
17
- const inputFile = useRef(null);
18
- const [options, setOptions] = useState([]);
19
- const [dataOptions, setDataOptions] = useState([]);
20
- const [formItemStyle, setFormItemStyle] = useState({});
21
- const [formItemClass, setFormItemClass] = useState("formItem");
22
- const {
23
- settings,
24
- inputValue,
25
- inputClass,
26
- onChange,
27
- onChangeFile,
28
- onChangeRicheditor,
29
- onChangeSelect,
30
- setFormData,
31
- uploadProgress,
32
- } = props;
33
-
34
- useEffect(() => {
35
- switch (settings.type) {
36
- case "hidden":
37
- setFormItemStyle({
38
- display: "none",
39
- });
40
- break;
41
- case "dropdown-ajax":
42
- let filter = {};
43
-
44
- if (
45
- settings.hasOwnProperty("where") &&
46
- settings.where.length > 0
47
- ) {
48
- filter = {
49
- where: settings.where,
50
- };
51
- }
52
-
53
- CustomFetch(settings.url, "POST", filter, function (result) {
54
- setOptions(result.data);
55
-
56
- let dataArray = [];
57
-
58
- result.data.forEach((a) => {
59
- let _tempObject = a;
60
-
61
- if (!_.isEmpty(_tempObject)) {
62
- let modifiedObject = {};
63
-
64
- Object.keys(_tempObject).forEach(function (c) {
65
- if (c !== "id" && c !== "label") {
66
- modifiedObject["data-" + c] =
67
- _tempObject[c];
68
- }
69
- });
70
-
71
- dataArray.push(modifiedObject);
72
- }
73
- });
74
-
75
- if (dataArray.length > 0) {
76
- setDataOptions(dataArray);
77
- }
78
- });
79
- break;
80
- case "multi-dropdown-ajax":
81
- let multiFilter = {};
82
-
83
- if (
84
- settings.hasOwnProperty("where") &&
85
- settings.where.length > 0
86
- ) {
87
- multiFilter = {
88
- where: settings.where,
89
- };
90
- }
91
-
92
- CustomFetch(
93
- settings.url,
94
- "POST",
95
- multiFilter,
96
- function (result) {
97
- setOptions(result.data);
98
-
99
- let dataArray = [];
100
-
101
- result.data.forEach((a) => {
102
- let _tempObject = a;
103
-
104
- if (!_.isEmpty(_tempObject)) {
105
- let modifiedObject = {};
106
-
107
- Object.keys(_tempObject).forEach(function (c) {
108
- if (c !== "id" && c !== "label") {
109
- modifiedObject["data-" + c] =
110
- _tempObject[c];
111
- }
112
- });
113
-
114
- dataArray.push(modifiedObject);
115
- }
116
- });
117
-
118
- if (dataArray.length > 0) {
119
- setDataOptions(dataArray);
120
- }
121
- }
122
- );
123
- break;
124
- default:
125
- break;
126
- }
127
-
128
- if (settings.size === "full") {
129
- setFormItemClass("formItem fwItem");
130
- }
131
- }, [settings]);
132
-
133
- useEffect(() => {
134
- let filter = {};
135
-
136
- if (settings.hasOwnProperty("child")) {
137
- if (inputValue > 0) {
138
- filter = {
139
- where: [
140
- {
141
- id: settings.id,
142
- value: inputValue,
143
- },
144
- ],
145
- };
146
-
147
- CustomFetch(
148
- settings.child.url,
149
- "POST",
150
- filter,
151
- function (result) {
152
- childDropdownCallback({
153
- id: settings.child.id,
154
- data: result.data,
155
- });
156
- }
157
- );
158
- }
159
- }
160
- }, [inputValue, settings]);
161
-
162
- useEffect(() => {
163
- if (settings.hasOwnProperty("options")) {
164
- if (settings.options.length > 0) {
165
- let dataArray = [];
166
-
167
- settings.options.forEach((a) => {
168
- let _tempObject = a;
169
-
170
- if (!_.isEmpty(_tempObject)) {
171
- let modifiedObject = {};
172
-
173
- Object.keys(_tempObject).forEach(function (c) {
174
- if (c !== "id" && c !== "label") {
175
- modifiedObject["data-" + c] = _tempObject[c];
176
- }
177
- });
178
-
179
- dataArray.push(modifiedObject);
180
- }
181
- });
182
-
183
- if (dataArray.length > 0) {
184
- setDataOptions(dataArray);
185
- }
186
- }
187
- }
188
- }, [settings.options, settings]);
189
-
190
- return (
191
- <div className={formItemClass} style={formItemStyle}>
192
- <label
193
- className={
194
- settings.type === "image"
195
- ? "fi__flabel"
196
- : settings.type === "richeditor"
197
- ? "fi__flabel"
198
- : "fi__label"
199
- }
200
- >
201
- {(() => {
202
- if (
203
- settings.type === "address" ||
204
- settings.type === "date" ||
205
- settings.type === "datetime" ||
206
- settings.type === "checkbox" ||
207
- settings.type === "multi-dropdown" ||
208
- settings.type === "multi-dropdown-ajax"
209
- ) {
210
- return (
211
- <span className="fi__span prefocus">
212
- {settings.label}{" "}
213
- {settings.required === true ? "*" : ""}
214
- </span>
215
- );
216
- }
217
- })()}
218
-
219
- {(() => {
220
- if (
221
- settings.type === "image" ||
222
- settings.type === "richeditor"
223
- ) {
224
- return (
225
- <>
226
- {settings.label}{" "}
227
- {settings.required === true ? "*" : ""}
228
- </>
229
- );
230
- }
231
- })()}
232
-
233
- {(() => {
234
- let htmlContainer;
235
- switch (settings.type) {
236
- case "text":
237
- htmlContainer = (
238
- <input
239
- type="text"
240
- name={settings.id}
241
- readOnly={
242
- settings.hasOwnProperty("readOnly")
243
- ? settings.readOnly
244
- : false
245
- }
246
- className={inputClass[settings.id]}
247
- placeholder=" "
248
- onChange={onChange}
249
- value={
250
- inputValue && inputValue !== "null"
251
- ? inputValue
252
- : ""
253
- }
254
- />
255
- );
256
- break;
257
- case "json":
258
- htmlContainer = (
259
- <input
260
- type="text"
261
- name={settings.id}
262
- data-jsonkey={settings.key}
263
- className={inputClass[settings.id]}
264
- placeholder=" "
265
- onChange={onChange}
266
- value={
267
- inputValue && inputValue !== "null"
268
- ? inputValue
269
- : ""
270
- }
271
- />
272
- );
273
- break;
274
- case "password":
275
- htmlContainer = (
276
- <input
277
- type="password"
278
- name={settings.id}
279
- className={inputClass[settings.id]}
280
- placeholder=" "
281
- onChange={onChange}
282
- value={
283
- inputValue && inputValue !== "null"
284
- ? inputValue
285
- : ""
286
- }
287
- />
288
- );
289
- break;
290
- case "phone":
291
- htmlContainer = (
292
- <PatternFormat
293
- name={settings.id}
294
- className={inputClass[settings.id]}
295
- onValueChange={(values) => {
296
- onChangeNumberFormat(
297
- values,
298
- settings.id
299
- );
300
- }}
301
- format="## #### ####"
302
- allowEmptyFormatting
303
- mask="_"
304
- value={
305
- inputValue && inputValue !== "null"
306
- ? inputValue
307
- : ""
308
- }
309
- />
310
- );
311
- break;
312
- case "mobile":
313
- htmlContainer = (
314
- <PatternFormat
315
- className={inputClass[settings.id]}
316
- onValueChange={(values) => {
317
- onChangeNumberFormat(
318
- values,
319
- settings.id
320
- );
321
- }}
322
- format="#### ### ###"
323
- allowEmptyFormatting
324
- mask="_"
325
- value={
326
- inputValue && inputValue !== "null"
327
- ? inputValue
328
- : ""
329
- }
330
- />
331
- );
332
- break;
333
- case "currency":
334
- htmlContainer = (
335
- <NumericFormat
336
- name={settings.id}
337
- className={inputClass[settings.id]}
338
- onValueChange={(values) => {
339
- onChangeNumberFormat(
340
- values,
341
- settings.id
342
- );
343
- }}
344
- thousandSeparator={true}
345
- prefix={"$"}
346
- value={
347
- inputValue && inputValue !== "null"
348
- ? inputValue
349
- : ""
350
- }
351
- />
352
- );
353
- break;
354
- case "textarea":
355
- htmlContainer = (
356
- <textarea
357
- name={settings.id}
358
- className={inputClass[settings.id]}
359
- onChange={onChange}
360
- value={
361
- inputValue && inputValue !== "null"
362
- ? inputValue
363
- : ""
364
- }
365
- style={{ height: "200px" }}
366
- ></textarea>
367
- );
368
- break;
369
- case "address":
370
- htmlContainer = (
371
- <VisnsAutocomplete
372
- onSelect={(place) =>
373
- autocompleteCallback(place)
374
- }
375
- field={settings.id}
376
- setFormData={setFormData}
377
- api="pk.eyJ1IjoidmlzbnMtc3R1ZGlvIiwiYSI6ImNrbGlraW5vZDBhMDYycHBsdmk5b3ljbGQifQ.ZGUBk0PWac5GahUeVHdTpg"
378
- inputValue={inputValue}
379
- />
380
- );
381
- break;
382
- case "dropdown":
383
- htmlContainer = (
384
- <Select
385
- settings={settings}
386
- className={inputClass[settings.id]}
387
- onChange={onChange}
388
- inputValue={
389
- inputValue === null ? "" : inputValue
390
- }
391
- options={settings.options}
392
- dataOptions={dataOptions}
393
- />
394
- );
395
- break;
396
- case "dropdown-ajax":
397
- htmlContainer = (
398
- <Select
399
- settings={settings}
400
- className={inputClass[settings.id]}
401
- onChange={onChange}
402
- inputValue={
403
- inputValue === null ? "" : inputValue
404
- }
405
- options={options}
406
- dataOptions={dataOptions}
407
- />
408
- );
409
- break;
410
- case "multi-dropdown":
411
- htmlContainer = (
412
- <MultiSelect
413
- settings={settings}
414
- className={inputClass[settings.id]}
415
- onChange={onChangeSelect}
416
- inputValue={
417
- inputValue === null ? "" : inputValue
418
- }
419
- options={settings.options}
420
- dataOptions={dataOptions}
421
- />
422
- );
423
- break;
424
- case "multi-dropdown-ajax":
425
- htmlContainer = (
426
- <MultiSelect
427
- settings={settings}
428
- className={inputClass[settings.id]}
429
- onChange={onChangeSelect}
430
- inputValue={
431
- inputValue === null ? "" : inputValue
432
- }
433
- options={options}
434
- dataOptions={dataOptions}
435
- />
436
- );
437
- break;
438
- case "file":
439
- htmlContainer = (
440
- <input
441
- type="file"
442
- name={settings.id}
443
- className={inputClass[settings.id]}
444
- onChange={onChangeFile}
445
- />
446
- );
447
- break;
448
-
449
- case "line-break":
450
- htmlContainer = (
451
- <div className="formbreak">
452
- <span>
453
- {settings.label}{" "}
454
- {settings.required === true ? "*" : ""}
455
- </span>
456
- </div>
457
- );
458
- break;
459
- case "checkbox":
460
- htmlContainer = (
461
- <input
462
- type="checkbox"
463
- name={settings.id}
464
- className={inputClass[settings.id]}
465
- placeholder=" "
466
- onChange={onChange}
467
- value={inputValue || ""}
468
- />
469
- );
470
- break;
471
- default:
472
- htmlContainer = "";
473
- break;
474
- }
475
-
476
- return htmlContainer;
477
- })()}
478
-
479
- {(() => {
480
- if (
481
- settings.type !== "address" &&
482
- settings.type !== "date" &&
483
- settings.type !== "datetime" &&
484
- settings.type !== "line-break" &&
485
- settings.type !== "checkbox" &&
486
- settings.type !== "multi-dropdown" &&
487
- settings.type !== "multi-dropdown-ajax" &&
488
- settings.type !== "toggle" &&
489
- settings.type !== "richeditor" &&
490
- settings.type !== "image"
491
- ) {
492
- return (
493
- <span className="fi__span">
494
- {settings.label}{" "}
495
- {settings.required === true ? "*" : ""}
496
- </span>
497
- );
498
- }
499
- })()}
500
- </label>
501
-
502
- {(() => {
503
- let htmlContainer;
504
- switch (settings.type) {
505
- case "date":
506
- htmlContainer = (
507
- <DatePicker
508
- dateFormat={
509
- settings.format
510
- ? settings.format
511
- : "dd-MM-yyyy"
512
- }
513
- className={inputClass[settings.id]}
514
- selected={inputValue || ""}
515
- showMonthDropdown
516
- showYearDropdown
517
- dropdownMode="select"
518
- onChange={(date) =>
519
- onChangeDate(date, settings.id)
520
- }
521
- />
522
- );
523
- break;
524
- case "datetime":
525
- htmlContainer = (
526
- <DatePicker
527
- dateFormat={
528
- settings.format
529
- ? settings.format
530
- : "dd-MM-yyyy hh:mm aa"
531
- }
532
- showTimeSelect
533
- className={inputClass[settings.id]}
534
- selected={inputValue || null}
535
- showMonthDropdown
536
- showYearDropdown
537
- dropdownMode="select"
538
- onChange={(date) =>
539
- onChangeDate(date, settings.id)
540
- }
541
- shouldCloseOnSelect={true}
542
- />
543
- );
544
- break;
545
- case "time":
546
- htmlContainer = (
547
- <DatePicker
548
- dateFormat={
549
- settings.format
550
- ? settings.format
551
- : "hh:mm aa"
552
- }
553
- showTimeSelect
554
- showTimeSelectOnly
555
- timeIntervals={15}
556
- timeCaption="Time"
557
- className={inputClass[settings.id]}
558
- selected={inputValue || ""}
559
- onChange={(date) =>
560
- onChangeDate(date, settings.id)
561
- }
562
- shouldCloseOnSelect={true}
563
- />
564
- );
565
- break;
566
- case "toggle":
567
- htmlContainer = (
568
- <div>
569
- <Toggle
570
- name={settings.id}
571
- data-show={
572
- settings.show
573
- ? JSON.stringify(settings.show)
574
- : ""
575
- }
576
- checked={
577
- inputValue !== undefined
578
- ? inputValue === ""
579
- ? false
580
- : inputValue === "on"
581
- ? false
582
- : inputValue
583
- : false
584
- }
585
- onChange={onChangeToggle}
586
- />
587
- <span className="fi__toggletxt">
588
- {settings.label}{" "}
589
- {settings.required === true ? "*" : ""}
590
- </span>
591
- </div>
592
- );
593
- break;
594
- case "richeditor":
595
- htmlContainer = (
596
- <ReactQuill
597
- theme="snow"
598
- name={settings.id}
599
- onChange={(value) => {
600
- onChangeRicheditor(value, settings.id);
601
- }}
602
- value={inputValue || ""}
603
- />
604
- );
605
- break;
606
- case "image":
607
- htmlContainer = (
608
- <>
609
- <motion.div
610
- className="progress__bar"
611
- initial={{ width: "0%" }}
612
- animate={{
613
- width:
614
- uploadProgress[settings.id] + "%",
615
- }}
616
- transition={{
617
- duration: 3,
618
- ease: [0.165, 0.84, 0.44, 1.0],
619
- }}
620
- />
621
- <input
622
- type="file"
623
- name={settings.id}
624
- className={inputClass[settings.id]}
625
- onChange={onChangeFile}
626
- ref={inputFile}
627
- style={{ display: "none" }}
628
- />
629
- <div
630
- onClick={() => {
631
- inputFile.current.click();
632
- }}
633
- >
634
- {inputValue && inputValue !== "" ? (
635
- <>
636
- {inputValue.file_url &&
637
- inputValue.file_url !== "" ? (
638
- <img
639
- src={inputValue.file_url}
640
- width="200"
641
- />
642
- ) : (
643
- <img
644
- src={URL.createObjectURL(
645
- inputValue
646
- )}
647
- width="200"
648
- />
649
- )}
650
- </>
651
- ) : (
652
- <div className="imageupload">
653
- <span>
654
- Click here to upload an image
655
- </span>
656
- </div>
657
- )}
658
- </div>
659
- </>
660
- );
661
- break;
662
- default:
663
- htmlContainer = "";
664
- break;
665
- }
666
-
667
- return htmlContainer;
668
- })()}
669
- </div>
670
- );
671
- }
672
-
673
- export default Field;