react-crud-mobile 1.3.97 → 1.3.99

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,788 +1,788 @@
1
- import React, {
2
- createContext,
3
- useContext,
4
- useEffect,
5
- useLayoutEffect,
6
- useRef,
7
- useState,
8
- } from 'react';
9
- import UIChildren from './UIChildren';
10
- import ElTabs from './tabs/ElTabs';
11
- import ElChart from './charts/ElChart';
12
- import UIComplete from './UIComplete';
13
- import {
14
- Crud,
15
- HtmlUtils,
16
- ScopeUtils,
17
- Utils,
18
- ElementType,
19
- MethodType,
20
- ActionType,
21
- ComponentUtils,
22
- CrudUtils,
23
- ViewUtils,
24
- } from 'react-crud-utils';
25
- import UILink from './core/UILink';
26
- import UIIcon from './core/UIIcon';
27
- import UIButton from './core/UIButton';
28
- import UISelect from './core/UISelect';
29
- import UISwitch from './core/UISwitch';
30
- import UISlider from './core/UISlider';
31
- import UIOption from './core/UIOption';
32
- import UIRadio from './core/UIRadio';
33
- import UIInput from './core/UIInput';
34
- import {
35
- Alert,
36
- Image,
37
- Linking,
38
- StyleSheet,
39
- Text,
40
- TouchableHighlight,
41
- View,
42
- } from 'react-native';
43
- import UIList from './core/UIList';
44
- import UIToggle from './core/UIToggle';
45
- import UIQuantity from './core/UIQuantity';
46
- import UIModal from './core/UIModal';
47
- import { Ionicons } from '@expo/vector-icons';
48
- import UIView from './core/UIView';
49
- import Toast from 'react-native-toast-message';
50
- import UIOrder from './core/UIOrder';
51
-
52
- const CrudContext = createContext<any>({});
53
-
54
- export default function UIElement(props: ElementType) {
55
- const ctx = useContext(CrudContext);
56
- const theme = Utils.nvl(props.theme, ctx?.theme);
57
-
58
- let crud: Crud = Utils.nvl(props.crud, ctx?.crud);
59
- let [scope] = useState(ScopeUtils.create({ crud, ...props, theme }));
60
- let [index, setIndex] = useState(0);
61
- let [error, setError]: string | any = useState(null);
62
-
63
- scope.compile(props);
64
-
65
- crud = scope.crud;
66
-
67
- let options: any = scope.getOptions();
68
-
69
- let original = scope.original;
70
- let ref = useRef(null);
71
-
72
- scope.update = () => {
73
- setIndex(++index);
74
- };
75
-
76
- scope.updateElement = () => {
77
- setIndex(++index);
78
- };
79
-
80
- scope.toast = (message: string, type = 'info', args?: any) => {
81
- Toast.show({
82
- type, // 'success' | 'error' | 'info'
83
- text1: message,
84
- position: 'bottom', // 'top' é outra opção
85
- visibilityTime: 3000, // tempo que fica visível em ms
86
- ...args,
87
- });
88
- };
89
-
90
- scope.prompt = (args: MethodType) => {
91
- let event = args.event as ActionType;
92
-
93
- if (event) {
94
- let message = 'Você tem certeza que deseja continuar?';
95
- let title = 'Atenção';
96
- let prompt = event.prompt;
97
-
98
- if (typeof prompt === 'string') {
99
- message = prompt;
100
- }
101
-
102
- if (typeof prompt === 'object') {
103
- message = Utils.nvl(prompt.message, message);
104
- title = Utils.nvl(prompt.title, title);
105
- }
106
-
107
- Alert.alert(
108
- title,
109
- message,
110
- [
111
- {
112
- text: 'Cancelar',
113
- style: 'cancel',
114
- },
115
- {
116
- text: 'Confirmar',
117
- onPress: () => scope.execute(args),
118
- },
119
- ],
120
- { cancelable: false }
121
- );
122
- }
123
- };
124
- const Custom = () => {
125
- let c: any = original.custom;
126
-
127
- if (c) {
128
- if (typeof c === 'string') {
129
- return (
130
- <UIElement
131
- element={{ value: c, type: 'dummy' }}
132
- crud={crud}
133
- ></UIElement>
134
- );
135
- }
136
-
137
- return (
138
- <UIElement
139
- type={c.type}
140
- tag={c.type}
141
- {...c.props}
142
- crud={crud}
143
- ></UIElement>
144
- );
145
- }
146
-
147
- return <></>;
148
- };
149
-
150
- if (scope.is('type', 'dummy')) {
151
- return <>{scope.getDisplayValue()}</>;
152
- }
153
-
154
- let onCheck = () => {
155
- let v = scope.getValue();
156
- let check = !(v === true);
157
-
158
- onChange({ target: { value: check } });
159
- };
160
-
161
- let onChange = (e: any) => {
162
- let val = e.target.value;
163
-
164
- if (scope.isType('integer', 'int', 'number')) {
165
- val = parseInt(val);
166
- } else if (scope.isType('decimal')) {
167
- val = parseFloat(val);
168
- }
169
-
170
- if (scope.isType('select', 'complete')) {
171
- val = scope.getSelectedItem(val);
172
- }
173
-
174
- scope.changeValue(val);
175
- scope.update();
176
- };
177
-
178
- let onClick = (e: any) => {
179
- if (scope.currentDialog?.component) return;
180
-
181
- scope.call('click');
182
- };
183
-
184
- let defaultsInput: any = {
185
- scope,
186
- crud,
187
- onChange: onChange,
188
- };
189
-
190
- if (scope.isType('password')) {
191
- defaultsInput.type = 'password';
192
- }
193
-
194
- let isChecked = () => {
195
- let v = scope.getValue();
196
-
197
- return v === true;
198
- };
199
-
200
- let hasChildren = () => {
201
- if (scope.isInput()) {
202
- return false;
203
- }
204
-
205
- return !Utils.isEmpty(props.children) || !Utils.isEmpty(props.elements);
206
- };
207
-
208
- let isInput = scope.is(
209
- 'type',
210
- 'text',
211
- 'number',
212
- 'integer',
213
- 'int',
214
- 'phone',
215
- 'postalCode',
216
- 'money',
217
- 'password',
218
- 'email'
219
- );
220
-
221
- const getStyle = (part?: string, extra?: any) => {
222
- let type = Utils.nvl(original.type, 'none');
223
- let key = Utils.nvl(part, 'root');
224
- let def = { ...styles[key] };
225
- let hasChild = hasChildren();
226
-
227
- type = Utils.nvl(original.layout, type);
228
-
229
- if (!part && !hasChild) {
230
- def = { ...def };
231
- }
232
-
233
- if (scope.isInput()) {
234
- def = { ...def, ...elementStyle.input[key] };
235
- }
236
-
237
- def = { ...def, ...elementStyle?.[type]?.[key] };
238
-
239
- if (hasChild && part) {
240
- def = { ...def, ...withChildStyles[part] };
241
- }
242
-
243
- return { ...def, ...scope.getStyle(part, { ...def, ...extra }) };
244
- };
245
-
246
- let elStyle = getStyle('element');
247
-
248
- let defaultsUI: any = {
249
- required: scope.isRequired(),
250
- size: 'small',
251
- scope,
252
- crud,
253
- style: elStyle,
254
- placeholder: scope.attr('placeholder', 'Digite aqui'),
255
- };
256
-
257
- scope.error = (msg: string) => {
258
- error = msg;
259
- setError(msg);
260
- };
261
-
262
- if (!original.list?.url && !original.load?.url) {
263
- scope.start();
264
- }
265
-
266
- useEffect(() => {
267
- scope.start();
268
- });
269
-
270
- const CustomIcon = () => {
271
- if (typeof original.icon === 'string') {
272
- return <Ionicons name={original.icon} style={scope.getStyle('icon')} />;
273
- }
274
- return <>{original.icon}</>;
275
- };
276
-
277
- scope.open = (args: any) => {
278
- Linking.openURL(args.url);
279
- };
280
-
281
- useLayoutEffect(() => {
282
- if (ref?.current && scope.is('type', 'card', 'list', 'tabs', 'stepper')) {
283
- let el: any = ref?.current;
284
-
285
- if (el?.classList) {
286
- let bg = HtmlUtils.getBGColor(el);
287
-
288
- if (bg === 'rgb(255, 255, 255)') {
289
- el.classList.add('ui-dark');
290
- } else {
291
- el.classList.add('ui-light');
292
- }
293
- }
294
- }
295
- });
296
-
297
- const isShowLabel = () => {
298
- if (
299
- typeof original.label !== 'undefined' &&
300
- original.label !== false &&
301
- !scope.isType('button', 'dialog', 'modal')
302
- ) {
303
- return true;
304
- }
305
-
306
- return false;
307
- };
308
-
309
- const isShowInner = () => {
310
- if (hasChildren()) {
311
- return false;
312
- }
313
- return true;
314
- };
315
-
316
- if (!scope.isRendered() || scope.is('type', 'define')) {
317
- return <></>;
318
- }
319
-
320
- const isShowChild = () => {
321
- if (
322
- scope.isType(
323
- 'tabs',
324
- 'view',
325
- 'grid',
326
- 'list',
327
- 'define',
328
- 'repeat',
329
- 'modal',
330
- 'dialog',
331
- 'chart'
332
- )
333
- ) {
334
- return false;
335
- }
336
-
337
- return true;
338
- };
339
-
340
- let isTouch =
341
- !scope.isType('input', 'grid', 'list', 'order', 'repeat') && original.click;
342
- let custom: any = {};
343
-
344
- if (isTouch) {
345
- custom.underlayColor = 'transparent';
346
- custom.onPress = onClick;
347
- }
348
-
349
- let Tag = (props: any) => {
350
- let Aux: any = View;
351
-
352
- if (isTouch) {
353
- Aux = TouchableHighlight;
354
- }
355
-
356
- if (scope.isType('dialog', 'order') || original.transient) {
357
- return <>{props.children}</>;
358
- }
359
-
360
- return <Aux {...props} />;
361
- };
362
-
363
- let Inner = () => {
364
- return (
365
- <>
366
- {scope.getPart('render', null, <></>)}
367
- {scope.is('type', 'button') && (
368
- <UIButton
369
- {...defaultsUI}
370
- onClick={onClick}
371
- variant={scope.attr('variant', 'outlined')}
372
- >
373
- {original.icon && <CustomIcon />}
374
- {original.label && (
375
- <Text style={scope.getPart('label', 'button')}>
376
- {scope.getLabel()}
377
- </Text>
378
- )}
379
- </UIButton>
380
- )}
381
- {scope.is('type', 'icon') && (
382
- <UIIcon
383
- {...defaultsUI}
384
- onClick={onClick}
385
- variant={scope.attr('variant', 'outlined')}
386
- >
387
- {scope.getDisplayValue()}
388
- </UIIcon>
389
- )}
390
- {scope.is('type', 'link') && (
391
- <UILink
392
- {...defaultsUI}
393
- onClick={onClick}
394
- variant={scope.attr('variant', 'outlined')}
395
- >
396
- {original.icon && <CustomIcon />}
397
- {original.label && (
398
- <Text style={scope.getPart('label', 'link')}>
399
- {scope.getLabel()}
400
- </Text>
401
- )}
402
- </UILink>
403
- )}
404
- {isInput && (
405
- <UIInput
406
- {...defaultsInput}
407
- {...defaultsUI}
408
- InputProps={{ ...original.inputProps }}
409
- />
410
- )}
411
- {scope.is('type', 'complete', 'autocomplete') && (
412
- <UIComplete
413
- scope={scope}
414
- defaultsInput={defaultsInput}
415
- defaultsUI={defaultsUI}
416
- />
417
- )}
418
- {scope.is('type', 'quantity') && (
419
- <UIQuantity
420
- scope={scope}
421
- defaultsInput={defaultsInput}
422
- defaultsUI={defaultsUI}
423
- />
424
- )}
425
- {scope.is('type', 'checkbox', 'boolean', 'switch') && (
426
- <UISwitch
427
- checked={isChecked()}
428
- {...defaultsInput}
429
- onChange={onCheck}
430
- />
431
- )}
432
- {scope.isType('slider') && (
433
- <UISlider {...defaultsInput} onChange={onCheck} />
434
- )}
435
- {scope.is('type', 'select') && (
436
- <UISelect
437
- {...defaultsInput}
438
- {...defaultsUI}
439
- value={scope.getSelectedValue()}
440
- />
441
- )}
442
- {scope.is('type', 'toggle') && (
443
- <UIToggle
444
- {...defaultsInput}
445
- {...defaultsUI}
446
- value={scope.getSelectedValue()}
447
- />
448
- )}
449
- {scope.is('type', 'radio') && (
450
- <UIRadio {...defaultsInput} {...defaultsUI} row>
451
- {options.map((row: any, i: number) => (
452
- <UIOption
453
- key={'i' + i}
454
- control={<UIRadio {...defaultsUI} />}
455
- label={row.label}
456
- value={row.value}
457
- />
458
- ))}
459
- </UIRadio>
460
- )}
461
- {scope.is('type', 'custom') && <Custom />}
462
- {scope.is('type', 'column') && (
463
- <>
464
- {scope.is('format', 'img') && (
465
- <Image source={scope.getDisplayValue()} />
466
- )}
467
- {scope.is('format', 'icon') && <UIIcon scope={scope} crud={crud} />}
468
- {!scope.is('format', 'icon', 'img') && (
469
- <Text>{scope.getDisplayValue()}</Text>
470
- )}
471
- </>
472
- )}
473
- {scope.is('type', 'output', 'value') && (
474
- <Text style={getStyle('value')}>{scope.getDisplayValue()}</Text>
475
- )}
476
- </>
477
- );
478
- };
479
-
480
- let Include = ({ name, style }: any) => {
481
- if (props[name]) {
482
- let define = ComponentUtils.getDefine(props, name);
483
-
484
- if (!Utils.isEmpty(define)) {
485
- return (
486
- <UIChildren
487
- {...props}
488
- scope={scope}
489
- crud={crud}
490
- style={getStyle(name, style)}
491
- >
492
- {define}
493
- </UIChildren>
494
- );
495
- }
496
- }
497
- return <></>;
498
- };
499
-
500
- let Container = () => {
501
- return (
502
- <>
503
- {isShowLabel() && (
504
- <View
505
- style={getStyle('outerLabel', {
506
- alignSelf: 'flex-start',
507
- flexDirection: 'row',
508
- display: 'flex',
509
- justifyContent: 'space-between',
510
- alignItems: 'center',
511
- width: '100%',
512
- })}
513
- >
514
- <Text style={getStyle('label')}>{scope.getLabel()}</Text>
515
- <Include name="actions" style={{ width: 'auto' }} />
516
- </View>
517
- )}
518
- {isShowInner() && (
519
- <>
520
- <View style={getStyle('inner')}>
521
- <Inner />
522
- </View>
523
- {error && <View style={getStyle('error')}>{error}</View>}
524
- </>
525
- )}
526
- {scope.isType('list', 'repeat') && (
527
- <UIList {...props} scope={scope} crud={crud} />
528
- )}
529
- {scope.isType('order') && (
530
- <UIOrder {...props} scope={scope} crud={crud} />
531
- )}
532
-
533
- {scope.isType('dialog') && (
534
- <UIModal {...props} scope={scope} crud={crud} />
535
- )}
536
-
537
- {scope.currentDialog?.component && (
538
- <UIModal
539
- scope={scope}
540
- crud={scope.currentDialog?.crud}
541
- open
542
- dialog={scope.currentDialog}
543
- />
544
- )}
545
-
546
- {scope.isType('chart') && (
547
- <ElChart {...props} scope={scope} crud={crud} />
548
- )}
549
- {scope.isType('tabs') && (
550
- <ElTabs {...props} scope={scope} crud={crud} />
551
- )}
552
-
553
- {scope.isType('view') && (
554
- <UIView {...props} scope={scope} crud={crud} />
555
- )}
556
-
557
- {isShowChild() && (
558
- <UIChildren
559
- {...props}
560
- scope={scope}
561
- crud={crud}
562
- style={getStyle('inner')}
563
- />
564
- )}
565
- </>
566
- );
567
- };
568
-
569
- let Card = (props: any) => {
570
- let isCard = scope.is('type|layout', 'card');
571
-
572
- if (isCard) {
573
- let box = {
574
- ...getStyle('box', { ...boxStyle.box, alignSelf: 'stretch' }),
575
- };
576
-
577
- return (
578
- <View style={getStyle('card', { ...box })}>
579
- <View
580
- style={getStyle('boxInner', {
581
- paddingHorizontal: 15,
582
- paddingVertical: 10,
583
- })}
584
- >
585
- {props.children}
586
- </View>
587
- </View>
588
- );
589
- }
590
-
591
- if (scope.isInput() || original.container) {
592
- return (
593
- <View style={getStyle('container', { padding: 5, width: '100%' })}>
594
- {props.children}
595
- </View>
596
- );
597
- }
598
- return <>{props.children}</>;
599
- };
600
-
601
- scope.dialogShow = (args?: MethodType) => {
602
- let main = ViewUtils.getCrud('view');
603
- let parent = main.dialog;
604
- let { crud } = args;
605
- let name = scope.getName('modal');
606
- let edit = args.edit === true;
607
- let def: any = {};
608
- let rowItem = null;
609
- let component = args?.event?.component;
610
-
611
- if (crud.is('row')) {
612
- def.parent = crud.parent.parent;
613
- def.search = crud.parent;
614
-
615
- rowItem = crud.data;
616
- } else if (crud.is('search')) {
617
- def.parent = crud.parent;
618
- def.search = crud;
619
- }
620
-
621
- let data = Utils.nvl(args.item, rowItem, {});
622
-
623
- let d = CrudUtils.create('dialog', {
624
- parent: crud,
625
- root: crud,
626
- name,
627
- data,
628
- edit,
629
- scope,
630
- ...def,
631
- });
632
-
633
- let dialog = { crud: d, parent, component };
634
-
635
- main.dialog = dialog;
636
- scope.toggleDialog(true);
637
- scope.currentDialog = dialog;
638
-
639
- scope.update();
640
- };
641
-
642
- scope.dialogHide = (args?: MethodType) => {
643
- let main = ViewUtils.getCrud('view');
644
- let old = scope.currentDialog;
645
-
646
- main.dialog = Utils.nvl(old.parent, null);
647
- scope.currentDialog = null;
648
-
649
- if (main.dialog?.crud?.scope) {
650
- main.dialog?.crud.scope.update();
651
- }
652
- scope.toggleDialog(false);
653
- scope.update();
654
- };
655
-
656
- return (
657
- <CrudContext.Provider value={{ crud, theme }}>
658
- <Tag ref={ref} style={getStyle()} {...custom}>
659
- <Card>
660
- <Container />
661
- </Card>
662
- </Tag>
663
- </CrudContext.Provider>
664
- );
665
- }
666
-
667
- let boxStyle = StyleSheet.create({
668
- box: {
669
- borderWidth: 0,
670
- borderColor: '#dedede',
671
- borderStyle: 'solid',
672
- backgroundColor: 'white',
673
- borderRadius: 12,
674
- width: '100%',
675
- shadowColor: '#000',
676
- shadowOpacity: 0.1,
677
- shadowRadius: 4,
678
- },
679
- });
680
-
681
- const box: any = {
682
- ...boxStyle.box,
683
- };
684
- //v2
685
- const elementStyle: any = {};
686
-
687
- elementStyle.view = {
688
- inner: {
689
- width: '100%',
690
- alignItems: 'normal',
691
- flex: 1,
692
- },
693
- container: {
694
- width: '100%',
695
- backgroundColor: 'background',
696
- flex: 1,
697
- gap: 10,
698
- },
699
- root: {
700
- width: '100%',
701
- flex: 1,
702
- alignItems: 'normal',
703
- padding: 0,
704
- },
705
- };
706
-
707
- elementStyle.input = StyleSheet.create({
708
- label: {
709
- paddingLeft: 0,
710
- },
711
- inner: {
712
- flex: 1,
713
- width: '100%',
714
- padding: 0,
715
- gap: 10,
716
- alignSelf: 'flex-start',
717
- flexDirection: 'row',
718
- flexWrap: 'wrap',
719
- },
720
- });
721
-
722
- elementStyle.switch = {
723
- inner: {
724
- padding: 5,
725
- },
726
- };
727
-
728
- elementStyle.quantity = {
729
- inner: {
730
- ...box,
731
- backgroundColor: 'primarySoft',
732
- fontWeight: 600,
733
- fontSize: 16,
734
- borderRadius: 25,
735
- borderWidth: 0,
736
- padding: 5,
737
- paddingHorizontal: 5,
738
- paddingVertical: 5,
739
- flexWrap: 'nowrap',
740
- flex: 1,
741
- flexDirection: 'row',
742
- justifyContent: 'center',
743
- alignItems: 'center',
744
- },
745
- };
746
-
747
- elementStyle.toggle = StyleSheet.create({
748
- inner: {
749
- ...box,
750
- flex: 1,
751
- width: '100%',
752
- gap: 10,
753
- justifyContent: 'center',
754
- flexDirection: 'row',
755
- paddingHorizontal: 10,
756
- paddingVertical: 0,
757
- alignSelf: 'flex-start',
758
- flexWrap: 'nowrap',
759
- },
760
- });
761
-
762
- const styles = StyleSheet.create({
763
- root: {
764
- gap: 5,
765
- flexDirection: 'column',
766
- flexWrap: 'wrap',
767
- width: '100%',
768
- alignItems: 'flex-start',
769
- },
770
- label: {
771
- fontWeight: 400,
772
- marginTop: 5,
773
- fontSize: 12,
774
- color: 'labelColor',
775
- },
776
- inner: { width: '100%' },
777
- });
778
-
779
- const withChildStyles = StyleSheet.create({
780
- root: {
781
- gap: 10,
782
- },
783
- label: {
784
- width: '100%',
785
- fontWeight: 500,
786
- fontSize: 24,
787
- },
788
- });
1
+ import React, {
2
+ createContext,
3
+ useContext,
4
+ useEffect,
5
+ useLayoutEffect,
6
+ useRef,
7
+ useState,
8
+ } from 'react';
9
+ import UIChildren from './UIChildren';
10
+ import ElTabs from './tabs/ElTabs';
11
+ import ElChart from './charts/ElChart';
12
+ import UIComplete from './UIComplete';
13
+ import {
14
+ Crud,
15
+ HtmlUtils,
16
+ ScopeUtils,
17
+ Utils,
18
+ ElementType,
19
+ MethodType,
20
+ ActionType,
21
+ ComponentUtils,
22
+ CrudUtils,
23
+ ViewUtils,
24
+ } from 'react-crud-utils';
25
+ import UILink from './core/UILink';
26
+ import UIIcon from './core/UIIcon';
27
+ import UIButton from './core/UIButton';
28
+ import UISelect from './core/UISelect';
29
+ import UISwitch from './core/UISwitch';
30
+ import UISlider from './core/UISlider';
31
+ import UIOption from './core/UIOption';
32
+ import UIRadio from './core/UIRadio';
33
+ import UIInput from './core/UIInput';
34
+ import {
35
+ Alert,
36
+ Image,
37
+ Linking,
38
+ StyleSheet,
39
+ Text,
40
+ TouchableHighlight,
41
+ View,
42
+ } from 'react-native';
43
+ import UIList from './core/UIList';
44
+ import UIToggle from './core/UIToggle';
45
+ import UIQuantity from './core/UIQuantity';
46
+ import UIModal from './core/UIModal';
47
+ import { Ionicons } from '@expo/vector-icons';
48
+ import UIView from './core/UIView';
49
+ import Toast from 'react-native-toast-message';
50
+ import UIOrder from './core/UIOrder';
51
+
52
+ const CrudContext = createContext<any>({});
53
+
54
+ export default function UIElement(props: ElementType) {
55
+ const ctx = useContext(CrudContext);
56
+ const theme = Utils.nvl(props.theme, ctx?.theme);
57
+
58
+ let crud: Crud = Utils.nvl(props.crud, ctx?.crud);
59
+ let [scope] = useState(ScopeUtils.create({ crud, ...props, theme }));
60
+ let [index, setIndex] = useState(0);
61
+ let [error, setError]: string | any = useState(null);
62
+
63
+ scope.compile(props);
64
+
65
+ crud = scope.crud;
66
+
67
+ let options: any = scope.getOptions();
68
+
69
+ let original = scope.original;
70
+ let ref = useRef(null);
71
+
72
+ scope.update = () => {
73
+ setIndex(++index);
74
+ };
75
+
76
+ scope.updateElement = () => {
77
+ setIndex(++index);
78
+ };
79
+
80
+ scope.toast = (message: string, type = 'info', args?: any) => {
81
+ Toast.show({
82
+ type, // 'success' | 'error' | 'info'
83
+ text1: message,
84
+ position: 'bottom', // 'top' é outra opção
85
+ visibilityTime: 3000, // tempo que fica visível em ms
86
+ ...args,
87
+ });
88
+ };
89
+
90
+ scope.prompt = (args: MethodType) => {
91
+ let event = args.event as ActionType;
92
+
93
+ if (event) {
94
+ let message = 'Você tem certeza que deseja continuar?';
95
+ let title = 'Atenção';
96
+ let prompt = event.prompt;
97
+
98
+ if (typeof prompt === 'string') {
99
+ message = prompt;
100
+ }
101
+
102
+ if (typeof prompt === 'object') {
103
+ message = Utils.nvl(prompt.message, message);
104
+ title = Utils.nvl(prompt.title, title);
105
+ }
106
+
107
+ Alert.alert(
108
+ title,
109
+ message,
110
+ [
111
+ {
112
+ text: 'Cancelar',
113
+ style: 'cancel',
114
+ },
115
+ {
116
+ text: 'Confirmar',
117
+ onPress: () => scope.execute(args),
118
+ },
119
+ ],
120
+ { cancelable: false }
121
+ );
122
+ }
123
+ };
124
+ const Custom = () => {
125
+ let c: any = original.custom;
126
+
127
+ if (c) {
128
+ if (typeof c === 'string') {
129
+ return (
130
+ <UIElement
131
+ element={{ value: c, type: 'dummy' }}
132
+ crud={crud}
133
+ ></UIElement>
134
+ );
135
+ }
136
+
137
+ return (
138
+ <UIElement
139
+ type={c.type}
140
+ tag={c.type}
141
+ {...c.props}
142
+ crud={crud}
143
+ ></UIElement>
144
+ );
145
+ }
146
+
147
+ return <></>;
148
+ };
149
+
150
+ if (scope.is('type', 'dummy')) {
151
+ return <>{scope.getDisplayValue()}</>;
152
+ }
153
+
154
+ let onCheck = () => {
155
+ let v = scope.getValue();
156
+ let check = !(v === true);
157
+
158
+ onChange({ target: { value: check } });
159
+ };
160
+
161
+ let onChange = (e: any) => {
162
+ let val = e.target.value;
163
+
164
+ if (scope.isType('integer', 'int', 'number')) {
165
+ val = parseInt(val);
166
+ } else if (scope.isType('decimal')) {
167
+ val = parseFloat(val);
168
+ }
169
+
170
+ if (scope.isType('select', 'complete')) {
171
+ val = scope.getSelectedItem(val);
172
+ }
173
+
174
+ scope.changeValue(val);
175
+ scope.update();
176
+ };
177
+
178
+ let onClick = (e: any) => {
179
+ if (scope.currentDialog?.component) return;
180
+
181
+ scope.call('click');
182
+ };
183
+
184
+ let defaultsInput: any = {
185
+ scope,
186
+ crud,
187
+ onChange: onChange,
188
+ };
189
+
190
+ if (scope.isType('password')) {
191
+ defaultsInput.type = 'password';
192
+ }
193
+
194
+ let isChecked = () => {
195
+ let v = scope.getValue();
196
+
197
+ return v === true;
198
+ };
199
+
200
+ let hasChildren = () => {
201
+ if (scope.isInput()) {
202
+ return false;
203
+ }
204
+
205
+ return !Utils.isEmpty(props.children) || !Utils.isEmpty(props.elements);
206
+ };
207
+
208
+ let isInput = scope.is(
209
+ 'type',
210
+ 'text',
211
+ 'number',
212
+ 'integer',
213
+ 'int',
214
+ 'phone',
215
+ 'postalCode',
216
+ 'money',
217
+ 'password',
218
+ 'email'
219
+ );
220
+
221
+ const getStyle = (part?: string, extra?: any) => {
222
+ let type = Utils.nvl(original.type, 'none');
223
+ let key = Utils.nvl(part, 'root');
224
+ let def = { ...styles[key] };
225
+ let hasChild = hasChildren();
226
+
227
+ type = Utils.nvl(original.layout, type);
228
+
229
+ if (!part && !hasChild) {
230
+ def = { ...def };
231
+ }
232
+
233
+ if (scope.isInput()) {
234
+ def = { ...def, ...elementStyle.input[key] };
235
+ }
236
+
237
+ def = { ...def, ...elementStyle?.[type]?.[key] };
238
+
239
+ if (hasChild && part) {
240
+ def = { ...def, ...withChildStyles[part] };
241
+ }
242
+
243
+ return { ...def, ...scope.getStyle(part, { ...def, ...extra }) };
244
+ };
245
+
246
+ let elStyle = getStyle('element');
247
+
248
+ let defaultsUI: any = {
249
+ required: scope.isRequired(),
250
+ size: 'small',
251
+ scope,
252
+ crud,
253
+ style: elStyle,
254
+ placeholder: scope.attr('placeholder', 'Digite aqui'),
255
+ };
256
+
257
+ scope.error = (msg: string) => {
258
+ error = msg;
259
+ setError(msg);
260
+ };
261
+
262
+ if (!original.list?.url && !original.load?.url) {
263
+ scope.start();
264
+ }
265
+
266
+ useEffect(() => {
267
+ scope.start();
268
+ });
269
+
270
+ const CustomIcon = () => {
271
+ if (typeof original.icon === 'string') {
272
+ return <Ionicons name={original.icon} style={scope.getStyle('icon')} />;
273
+ }
274
+ return <>{original.icon}</>;
275
+ };
276
+
277
+ scope.open = (args: any) => {
278
+ Linking.openURL(args.url);
279
+ };
280
+
281
+ useLayoutEffect(() => {
282
+ if (ref?.current && scope.is('type', 'card', 'list', 'tabs', 'stepper')) {
283
+ let el: any = ref?.current;
284
+
285
+ if (el?.classList) {
286
+ let bg = HtmlUtils.getBGColor(el);
287
+
288
+ if (bg === 'rgb(255, 255, 255)') {
289
+ el.classList.add('ui-dark');
290
+ } else {
291
+ el.classList.add('ui-light');
292
+ }
293
+ }
294
+ }
295
+ });
296
+
297
+ const isShowLabel = () => {
298
+ if (
299
+ typeof original.label !== 'undefined' &&
300
+ original.label !== false &&
301
+ !scope.isType('button', 'dialog', 'modal')
302
+ ) {
303
+ return true;
304
+ }
305
+
306
+ return false;
307
+ };
308
+
309
+ const isShowInner = () => {
310
+ if (hasChildren()) {
311
+ return false;
312
+ }
313
+ return true;
314
+ };
315
+
316
+ if (!scope.isRendered() || scope.is('type', 'define')) {
317
+ return <></>;
318
+ }
319
+
320
+ const isShowChild = () => {
321
+ if (
322
+ scope.isType(
323
+ 'tabs',
324
+ 'view',
325
+ 'grid',
326
+ 'list',
327
+ 'define',
328
+ 'repeat',
329
+ 'modal',
330
+ 'dialog',
331
+ 'chart'
332
+ )
333
+ ) {
334
+ return false;
335
+ }
336
+
337
+ return true;
338
+ };
339
+
340
+ let isTouch =
341
+ !scope.isType('input', 'grid', 'list', 'order', 'repeat') && original.click;
342
+ let custom: any = {};
343
+
344
+ if (isTouch) {
345
+ custom.underlayColor = 'transparent';
346
+ custom.onPress = onClick;
347
+ }
348
+
349
+ let Tag = (props: any) => {
350
+ let Aux: any = View;
351
+
352
+ if (isTouch) {
353
+ Aux = TouchableHighlight;
354
+ }
355
+
356
+ if (scope.isType('dialog', 'order') || original.transient) {
357
+ return <>{props.children}</>;
358
+ }
359
+
360
+ return <Aux {...props} />;
361
+ };
362
+
363
+ let Inner = () => {
364
+ return (
365
+ <>
366
+ {scope.getPart('render', null, <></>)}
367
+ {scope.is('type', 'button') && (
368
+ <UIButton
369
+ {...defaultsUI}
370
+ onClick={onClick}
371
+ variant={scope.attr('variant', 'outlined')}
372
+ >
373
+ {original.icon && <CustomIcon />}
374
+ {original.label && (
375
+ <Text style={scope.getPart('label', 'button')}>
376
+ {scope.getLabel()}
377
+ </Text>
378
+ )}
379
+ </UIButton>
380
+ )}
381
+ {scope.is('type', 'icon') && (
382
+ <UIIcon
383
+ {...defaultsUI}
384
+ onClick={onClick}
385
+ variant={scope.attr('variant', 'outlined')}
386
+ >
387
+ {scope.getDisplayValue()}
388
+ </UIIcon>
389
+ )}
390
+ {scope.is('type', 'link') && (
391
+ <UILink
392
+ {...defaultsUI}
393
+ onClick={onClick}
394
+ variant={scope.attr('variant', 'outlined')}
395
+ >
396
+ {original.icon && <CustomIcon />}
397
+ {original.label && (
398
+ <Text style={scope.getPart('label', 'link')}>
399
+ {scope.getLabel()}
400
+ </Text>
401
+ )}
402
+ </UILink>
403
+ )}
404
+ {isInput && (
405
+ <UIInput
406
+ {...defaultsInput}
407
+ {...defaultsUI}
408
+ InputProps={{ ...original.inputProps }}
409
+ />
410
+ )}
411
+ {scope.is('type', 'complete', 'autocomplete') && (
412
+ <UIComplete
413
+ scope={scope}
414
+ defaultsInput={defaultsInput}
415
+ defaultsUI={defaultsUI}
416
+ />
417
+ )}
418
+ {scope.is('type', 'quantity') && (
419
+ <UIQuantity
420
+ scope={scope}
421
+ defaultsInput={defaultsInput}
422
+ defaultsUI={defaultsUI}
423
+ />
424
+ )}
425
+ {scope.is('type', 'checkbox', 'boolean', 'switch') && (
426
+ <UISwitch
427
+ checked={isChecked()}
428
+ {...defaultsInput}
429
+ onChange={onCheck}
430
+ />
431
+ )}
432
+ {scope.isType('slider') && (
433
+ <UISlider {...defaultsInput} onChange={onCheck} />
434
+ )}
435
+ {scope.is('type', 'select') && (
436
+ <UISelect
437
+ {...defaultsInput}
438
+ {...defaultsUI}
439
+ value={scope.getSelectedValue()}
440
+ />
441
+ )}
442
+ {scope.is('type', 'toggle') && (
443
+ <UIToggle
444
+ {...defaultsInput}
445
+ {...defaultsUI}
446
+ value={scope.getSelectedValue()}
447
+ />
448
+ )}
449
+ {scope.is('type', 'radio') && (
450
+ <UIRadio {...defaultsInput} {...defaultsUI} row>
451
+ {options.map((row: any, i: number) => (
452
+ <UIOption
453
+ key={'i' + i}
454
+ control={<UIRadio {...defaultsUI} />}
455
+ label={row.label}
456
+ value={row.value}
457
+ />
458
+ ))}
459
+ </UIRadio>
460
+ )}
461
+ {scope.is('type', 'custom') && <Custom />}
462
+ {scope.is('type', 'column') && (
463
+ <>
464
+ {scope.is('format', 'img') && (
465
+ <Image source={scope.getDisplayValue()} />
466
+ )}
467
+ {scope.is('format', 'icon') && <UIIcon scope={scope} crud={crud} />}
468
+ {!scope.is('format', 'icon', 'img') && (
469
+ <Text>{scope.getDisplayValue()}</Text>
470
+ )}
471
+ </>
472
+ )}
473
+ {scope.is('type', 'output', 'value') && (
474
+ <Text style={getStyle('value')}>{scope.getDisplayValue()}</Text>
475
+ )}
476
+ </>
477
+ );
478
+ };
479
+
480
+ let Include = ({ name, style }: any) => {
481
+ if (props[name]) {
482
+ let define = ComponentUtils.getDefine(props, name);
483
+
484
+ if (!Utils.isEmpty(define)) {
485
+ return (
486
+ <UIChildren
487
+ {...props}
488
+ scope={scope}
489
+ crud={crud}
490
+ style={getStyle(name, style)}
491
+ >
492
+ {define}
493
+ </UIChildren>
494
+ );
495
+ }
496
+ }
497
+ return <></>;
498
+ };
499
+
500
+ let Container = () => {
501
+ return (
502
+ <>
503
+ {isShowLabel() && (
504
+ <View
505
+ style={getStyle('outerLabel', {
506
+ alignSelf: 'flex-start',
507
+ flexDirection: 'row',
508
+ display: 'flex',
509
+ justifyContent: 'space-between',
510
+ alignItems: 'center',
511
+ width: '100%',
512
+ })}
513
+ >
514
+ <Text style={getStyle('label')}>{scope.getLabel()}</Text>
515
+ <Include name="actions" style={{ width: 'auto' }} />
516
+ </View>
517
+ )}
518
+ {isShowInner() && (
519
+ <>
520
+ <View style={getStyle('inner')}>
521
+ <Inner />
522
+ </View>
523
+ {error && <View style={getStyle('error')}>{error}</View>}
524
+ </>
525
+ )}
526
+ {scope.isType('list', 'repeat') && (
527
+ <UIList {...props} scope={scope} crud={crud} />
528
+ )}
529
+ {scope.isType('order') && (
530
+ <UIOrder {...props} scope={scope} crud={crud} />
531
+ )}
532
+
533
+ {scope.isType('dialog') && (
534
+ <UIModal {...props} scope={scope} crud={crud} />
535
+ )}
536
+
537
+ {scope.currentDialog?.component && (
538
+ <UIModal
539
+ scope={scope}
540
+ crud={scope.currentDialog?.crud}
541
+ open
542
+ dialog={scope.currentDialog}
543
+ />
544
+ )}
545
+
546
+ {scope.isType('chart') && (
547
+ <ElChart {...props} scope={scope} crud={crud} />
548
+ )}
549
+ {scope.isType('tabs') && (
550
+ <ElTabs {...props} scope={scope} crud={crud} />
551
+ )}
552
+
553
+ {scope.isType('view') && (
554
+ <UIView {...props} scope={scope} crud={crud} />
555
+ )}
556
+
557
+ {isShowChild() && (
558
+ <UIChildren
559
+ {...props}
560
+ scope={scope}
561
+ crud={crud}
562
+ style={getStyle('inner')}
563
+ />
564
+ )}
565
+ </>
566
+ );
567
+ };
568
+
569
+ let Card = (props: any) => {
570
+ let isCard = scope.is('type|layout', 'card');
571
+
572
+ if (isCard) {
573
+ let box = {
574
+ ...getStyle('box', { ...boxStyle.box, alignSelf: 'stretch' }),
575
+ };
576
+
577
+ return (
578
+ <View style={getStyle('card', { ...box })}>
579
+ <View
580
+ style={getStyle('boxInner', {
581
+ paddingHorizontal: 15,
582
+ paddingVertical: 10,
583
+ })}
584
+ >
585
+ {props.children}
586
+ </View>
587
+ </View>
588
+ );
589
+ }
590
+
591
+ if (scope.isInput() || original.container) {
592
+ return (
593
+ <View style={getStyle('container', { padding: 5, width: '100%' })}>
594
+ {props.children}
595
+ </View>
596
+ );
597
+ }
598
+ return <>{props.children}</>;
599
+ };
600
+
601
+ scope.dialogShow = (args?: MethodType) => {
602
+ let main = ViewUtils.getCrud('view');
603
+ let parent = main.dialog;
604
+ let { crud } = args;
605
+ let name = scope.getName('modal');
606
+ let edit = args.edit === true;
607
+ let def: any = {};
608
+ let rowItem = null;
609
+ let component = args?.event?.component;
610
+
611
+ if (crud.is('row')) {
612
+ def.parent = crud.parent.parent;
613
+ def.search = crud.parent;
614
+
615
+ rowItem = crud.data;
616
+ } else if (crud.is('search')) {
617
+ def.parent = crud.parent;
618
+ def.search = crud;
619
+ }
620
+
621
+ let data = Utils.nvl(args.item, rowItem, {});
622
+
623
+ let d = CrudUtils.create('dialog', {
624
+ parent: crud,
625
+ root: crud,
626
+ name,
627
+ data,
628
+ edit,
629
+ scope,
630
+ ...def,
631
+ });
632
+
633
+ let dialog = { crud: d, parent, component };
634
+
635
+ main.dialog = dialog;
636
+ scope.toggleDialog(true);
637
+ scope.currentDialog = dialog;
638
+
639
+ scope.update();
640
+ };
641
+
642
+ scope.dialogHide = (args?: MethodType) => {
643
+ let main = ViewUtils.getCrud('view');
644
+ let old = scope.currentDialog;
645
+
646
+ main.dialog = Utils.nvl(old.parent, null);
647
+ scope.currentDialog = null;
648
+
649
+ if (main.dialog?.crud?.scope) {
650
+ main.dialog?.crud.scope.update();
651
+ }
652
+ scope.toggleDialog(false);
653
+ scope.update();
654
+ };
655
+
656
+ return (
657
+ <CrudContext.Provider value={{ crud, theme }}>
658
+ <Tag ref={ref} style={getStyle()} {...custom}>
659
+ <Card>
660
+ <Container />
661
+ </Card>
662
+ </Tag>
663
+ </CrudContext.Provider>
664
+ );
665
+ }
666
+
667
+ let boxStyle = StyleSheet.create({
668
+ box: {
669
+ borderWidth: 0,
670
+ borderColor: '#dedede',
671
+ borderStyle: 'solid',
672
+ backgroundColor: 'white',
673
+ borderRadius: 12,
674
+ width: '100%',
675
+ shadowColor: '#000',
676
+ shadowOpacity: 0.1,
677
+ shadowRadius: 4,
678
+ },
679
+ });
680
+
681
+ const box: any = {
682
+ ...boxStyle.box,
683
+ };
684
+ //v2
685
+ const elementStyle: any = {};
686
+
687
+ elementStyle.view = {
688
+ inner: {
689
+ width: '100%',
690
+ alignItems: 'normal',
691
+ flex: 1,
692
+ },
693
+ container: {
694
+ width: '100%',
695
+ backgroundColor: 'background',
696
+ flex: 1,
697
+ gap: 10,
698
+ },
699
+ root: {
700
+ width: '100%',
701
+ flex: 1,
702
+ alignItems: 'normal',
703
+ padding: 0,
704
+ },
705
+ };
706
+
707
+ elementStyle.input = StyleSheet.create({
708
+ label: {
709
+ paddingLeft: 0,
710
+ },
711
+ inner: {
712
+ flex: 1,
713
+ width: '100%',
714
+ padding: 0,
715
+ gap: 10,
716
+ alignSelf: 'flex-start',
717
+ flexDirection: 'row',
718
+ flexWrap: 'wrap',
719
+ },
720
+ });
721
+
722
+ elementStyle.switch = {
723
+ inner: {
724
+ padding: 5,
725
+ },
726
+ };
727
+
728
+ elementStyle.quantity = {
729
+ inner: {
730
+ ...box,
731
+ backgroundColor: 'primarySoft',
732
+ fontWeight: 600,
733
+ fontSize: 16,
734
+ borderRadius: 25,
735
+ borderWidth: 0,
736
+ padding: 5,
737
+ paddingHorizontal: 5,
738
+ paddingVertical: 5,
739
+ flexWrap: 'nowrap',
740
+ flex: 1,
741
+ flexDirection: 'row',
742
+ justifyContent: 'center',
743
+ alignItems: 'center',
744
+ },
745
+ };
746
+
747
+ elementStyle.toggle = StyleSheet.create({
748
+ inner: {
749
+ ...box,
750
+ flex: 1,
751
+ width: '100%',
752
+ gap: 10,
753
+ justifyContent: 'center',
754
+ flexDirection: 'row',
755
+ paddingHorizontal: 10,
756
+ paddingVertical: 0,
757
+ alignSelf: 'flex-start',
758
+ flexWrap: 'nowrap',
759
+ },
760
+ });
761
+
762
+ const styles = StyleSheet.create({
763
+ root: {
764
+ gap: 5,
765
+ flexDirection: 'column',
766
+ flexWrap: 'wrap',
767
+ width: '100%',
768
+ alignItems: 'flex-start',
769
+ },
770
+ label: {
771
+ fontWeight: 400,
772
+ marginTop: 5,
773
+ fontSize: 12,
774
+ color: 'labelColor',
775
+ },
776
+ inner: { width: '100%' },
777
+ });
778
+
779
+ const withChildStyles = StyleSheet.create({
780
+ root: {
781
+ gap: 10,
782
+ },
783
+ label: {
784
+ width: '100%',
785
+ fontWeight: 500,
786
+ fontSize: 24,
787
+ },
788
+ });