react-crud-mobile 1.0.648 → 1.0.650

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.
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.0.648",
2
+ "version": "1.0.650",
3
3
  "license": "MIT",
4
4
  "description": "Uma biblioteca de componentes para React Native",
5
5
  "main": "dist/index.js",
@@ -1,14 +1,8 @@
1
1
  import React, { useRef, useState } from 'react';
2
2
  import { ChildType, ScopeUtils, Utils, ViewUtils } from 'react-crud-utils';
3
3
  import UIChildren from '../UIChildren';
4
- import {
5
- Dimensions,
6
- findNodeHandle,
7
- StyleSheet,
8
- TouchableHighlight,
9
- UIManager,
10
- View,
11
- } from 'react-native';
4
+ import { StyleSheet, TouchableHighlight, View } from 'react-native';
5
+ import { useIsVisible } from '../../hooks/useIsVisible';
12
6
 
13
7
  interface UIListRowType extends ChildType {
14
8
  item: any;
@@ -36,37 +30,15 @@ export default function UIListRow(props: UIListRowType) {
36
30
  })
37
31
  );
38
32
 
39
- const main = ViewUtils.getCrud();
40
33
  const targetRef = useRef(null);
41
- const [visivel, setVisivel] = useState(false);
42
-
43
- const onScroll = () => {
44
- console.log(visivel);
45
-
46
- if (!visivel) {
47
- const handle = findNodeHandle(targetRef.current);
48
- if (handle) {
49
- UIManager.measure(handle, (x, y, width, height, pageX, pageY) => {
50
- const windowHeight = Dimensions.get('window').height;
51
- const topoVisivel = pageY >= 0 && pageY < windowHeight;
52
- const fundoVisivel =
53
- pageY + height > 0 && pageY + height < windowHeight;
54
- setVisivel(topoVisivel || fundoVisivel);
55
- });
56
- }
57
- }
58
- };
59
-
60
- row.onScroll = onScroll;
61
-
62
- main.scroll[row.getName()] = row;
34
+ const isVisible = useIsVisible(targetRef);
63
35
 
64
36
  const onClick = (item: any) => {
65
37
  row.call('click', { value: item, item, edit: true, index });
66
38
  };
67
39
 
68
40
  const Child = () => {
69
- if (!visivel) {
41
+ if (!isVisible) {
70
42
  return <></>;
71
43
  }
72
44
  return (
@@ -100,12 +72,7 @@ export default function UIListRow(props: UIListRowType) {
100
72
 
101
73
  if (!original.click) {
102
74
  return (
103
- <View
104
- key={key}
105
- style={getRowStyle()}
106
- ref={targetRef}
107
- onLayout={onScroll}
108
- >
75
+ <View key={key} style={getRowStyle()} ref={targetRef}>
109
76
  <Child />
110
77
  </View>
111
78
  );
@@ -116,7 +83,6 @@ export default function UIListRow(props: UIListRowType) {
116
83
  style={getRowStyle()}
117
84
  underlayColor={'transparent'}
118
85
  ref={targetRef}
119
- onLayout={onScroll}
120
86
  onPress={e => {
121
87
  e.stopPropagation();
122
88
  onClick(item);
@@ -0,0 +1,33 @@
1
+ import { useEffect, useState } from 'react';
2
+ import { Dimensions, Platform } from 'react-native';
3
+
4
+ export function useIsVisible(ref: any) {
5
+ const [isVisible, setIsVisible] = useState(false);
6
+
7
+ useEffect(() => {
8
+ const checkVisibility = () => {
9
+ if (!ref.current) return;
10
+
11
+ if (Platform.OS === 'web') {
12
+ const rect = ref.current.getBoundingClientRect?.();
13
+
14
+ if (rect && typeof window !== 'undefined') {
15
+ const windowHeight = window.innerHeight;
16
+ const visible = rect.top < windowHeight && rect.bottom > 0;
17
+ setIsVisible(visible);
18
+ }
19
+ } else {
20
+ ref.current.measureInWindow?.((x, y, width, height) => {
21
+ const windowHeight = Dimensions.get('window').height;
22
+ const visible = y < windowHeight && y + height > 0;
23
+ setIsVisible(visible);
24
+ });
25
+ }
26
+ };
27
+
28
+ const interval = setInterval(checkVisibility, 300); // roda a cada 300ms
29
+ return () => clearInterval(interval);
30
+ }, [ref]);
31
+
32
+ return isVisible;
33
+ }