react-crud-mobile 1.3.369 → 1.3.372

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,13 +1,12 @@
1
1
  import React, { useState, useRef, useEffect, useContext, useLayoutEffect, createContext } from 'react';
2
2
  import { Utils, ScopeUtils, CrudUtils, ComponentUtils, HtmlUtils, ViewUtils, ThemeUtils, useTheme } from 'react-crud-utils';
3
- import { View, StyleSheet, TouchableOpacity, Text, Linking, TouchableHighlight, Modal, SafeAreaView, ScrollView, Switch, TextInput, Platform, Dimensions, Alert, Image, StatusBar, KeyboardAvoidingView, TouchableWithoutFeedback, Keyboard } from 'react-native';
3
+ import { View, StyleSheet, TouchableOpacity, Text, Linking, TouchableHighlight, Modal, SafeAreaView, ScrollView, Switch, TextInput, Platform, Dimensions, Animated, PanResponder, Alert, Image, StatusBar, KeyboardAvoidingView, TouchableWithoutFeedback, Keyboard } from 'react-native';
4
4
  import { jsx, Fragment, jsxs } from 'react/jsx-runtime';
5
5
  import Ionicons from '@expo/vector-icons/Ionicons';
6
6
  import { Ionicons as Ionicons$1, AntDesign, Entypo, EvilIcons, MaterialCommunityIcons } from '@expo/vector-icons';
7
7
  import Slider from '@react-native-community/slider';
8
8
  import Toast, { BaseToast, ErrorToast } from 'react-native-toast-message';
9
9
  import { GestureHandlerRootView } from 'react-native-gesture-handler';
10
- import DraggableFlatList, { ScaleDecorator } from 'react-native-draggable-flatlist';
11
10
  import { SafeAreaProvider, SafeAreaView as SafeAreaView$1 } from 'react-native-safe-area-context';
12
11
 
13
12
  function _extends() {
@@ -1718,164 +1717,240 @@ var styles$9 = /*#__PURE__*/StyleSheet.create({
1718
1717
  view: {}
1719
1718
  });
1720
1719
 
1721
- function UIOrder(props) {
1720
+ var ITEM_HEIGHT = 70;
1721
+ function UIDraggableList(props) {
1722
1722
  var scope = props.scope;
1723
- var crud = scope.crud;
1724
- var original = scope.original;
1725
- var cols = Utils.nvl(scope.getPart('cols', undefined, 1));
1726
- var items = Utils.call(function () {
1727
- var _original$list;
1728
- var list = Utils.nvl(scope.getItems(), []);
1729
- if (original.search && !((_original$list = original.list) != null && _original$list.url)) {
1730
- var query = crud.get('query', '').toLowerCase().trim();
1731
- if (query.length > 1) {
1732
- var filters = [];
1733
- var filterBy = Utils.nvl(original.filterBy, 'label');
1734
- Utils.each(list, function (o) {
1735
- var label = o[filterBy];
1736
- if (label) {
1737
- if (label.includes(query)) {
1738
- filters.push(o);
1739
- }
1740
- }
1723
+ var initial = scope.getOptions();
1724
+ var _useState = useState(initial),
1725
+ items = _useState[0],
1726
+ setItems = _useState[1];
1727
+ var _useState2 = useState(null),
1728
+ draggingId = _useState2[0],
1729
+ setDraggingId = _useState2[1];
1730
+ // O tipo Animated.Value em um ambiente TypeScript puro não tem o método ._value,
1731
+ // mas é a propriedade interna usada para acessar o valor diretamente.
1732
+ // Usamos 'any' no acesso para suprimir o erro TS.
1733
+ var positions = useRef(Object.fromEntries(initial.map(function (it, i) {
1734
+ return [it.value, new Animated.Value(i * ITEM_HEIGHT)];
1735
+ }))).current;
1736
+ var panRespondersRef = useRef({});
1737
+ var activeItem = useRef(null);
1738
+ var activeIndex = useRef(-1);
1739
+ var offsetY = useRef(0);
1740
+ // Armazena a lista mais recente em uma ref para uso em funções de PanResponder
1741
+ var itemsRef = useRef(items);
1742
+ useEffect(function () {
1743
+ itemsRef.current = items;
1744
+ }, [items]);
1745
+ // 1. Atualiza posições apenas para itens existentes
1746
+ useEffect(function () {
1747
+ items.forEach(function (it, idx) {
1748
+ if (!positions[it.value]) positions[it.value] = new Animated.Value(idx * ITEM_HEIGHT);
1749
+ Animated.spring(positions[it.value], {
1750
+ toValue: idx * ITEM_HEIGHT,
1751
+ useNativeDriver: false
1752
+ }).start();
1753
+ });
1754
+ }, [items]);
1755
+ var createPanResponder = function createPanResponder(item) {
1756
+ return PanResponder.create({
1757
+ onStartShouldSetPanResponder: function onStartShouldSetPanResponder() {
1758
+ return true;
1759
+ },
1760
+ onPanResponderGrant: function onPanResponderGrant() {
1761
+ // Use itemsRef.current para obter a lista mais recente no momento do arrasto.
1762
+ var currentItems = itemsRef.current;
1763
+ if (!positions[item.value] || !currentItems.find(function (it) {
1764
+ return it.value === item.value;
1765
+ })) return;
1766
+ activeItem.current = item;
1767
+ activeIndex.current = currentItems.findIndex(function (it) {
1768
+ return it.value === item.value;
1741
1769
  });
1742
- return filters;
1743
- }
1744
- }
1745
- return list;
1746
- });
1747
- //v2
1748
- var renderItem = function renderItem(_ref) {
1749
- var item = _ref.item,
1750
- drag = _ref.drag,
1751
- isActive = _ref.isActive,
1752
- getIndex = _ref.getIndex;
1753
- var index = getIndex();
1754
- var name = "" + scope.key('row', index, '');
1755
- var _useState = useState(ScopeUtils.create(_extends({}, original, {
1756
- parent: scope,
1757
- name: name,
1758
- crud: scope.crud,
1759
- index: index,
1760
- type: 'row',
1761
- data: item
1762
- }))),
1763
- row = _useState[0];
1764
- var css = _extends({}, scope.getStyle('row'));
1765
- if (isActive) {
1766
- css = _extends({}, css, scope.getStyle('active'));
1767
- }
1768
- if (isActive) {
1769
- css = _extends({}, css, row.getStyle('rowSelected', {}));
1770
- } else {
1771
- css = _extends({}, css, row.getStyle('rowUnSelected', {}));
1772
- }
1773
- var Child = function Child() {
1774
- if (Utils.isEmpty(props.children)) {
1775
- return /*#__PURE__*/jsxs(Fragment, {
1776
- children: [/*#__PURE__*/jsx(MaterialCommunityIcons, {
1777
- name: "drag",
1778
- size: 24,
1779
- color: "black"
1780
- }), /*#__PURE__*/jsx(Text, {
1781
- style: styles$a.text,
1782
- children: scope.getItemLabel(item)
1783
- })]
1770
+ // CORREÇÃO: Usa a propriedade interna _value para acessar o valor atual
1771
+ offsetY.current = positions[item.value]._value; // 👈 CORREÇÃO APLICADA
1772
+ setDraggingId(item.value);
1773
+ },
1774
+ onPanResponderMove: function onPanResponderMove(_, gesture) {
1775
+ var currentItems = itemsRef.current;
1776
+ if (!positions[item.value] || !currentItems.find(function (it) {
1777
+ return it.value === item.value;
1778
+ })) return;
1779
+ var y = offsetY.current + gesture.dy;
1780
+ positions[item.value].setValue(y);
1781
+ var targetIndex = Math.max(0, Math.min(currentItems.length - 1, Math.floor((y + ITEM_HEIGHT / 2) / ITEM_HEIGHT)));
1782
+ currentItems.forEach(function (other, idx) {
1783
+ if (other.value === item.value || !positions[other.value]) return;
1784
+ var targetY = idx * ITEM_HEIGHT;
1785
+ if (idx > activeIndex.current && idx <= targetIndex) targetY = (idx - 1) * ITEM_HEIGHT;else if (idx < activeIndex.current && idx >= targetIndex) targetY = (idx + 1) * ITEM_HEIGHT;
1786
+ Animated.timing(positions[other.value], {
1787
+ toValue: targetY,
1788
+ duration: 120,
1789
+ useNativeDriver: false
1790
+ }).start();
1784
1791
  });
1792
+ },
1793
+ onPanResponderRelease: function onPanResponderRelease() {
1794
+ var moved = activeItem.current;
1795
+ var currentItems = itemsRef.current;
1796
+ if (!moved || !positions[moved.value] || !currentItems.find(function (it) {
1797
+ return it.value === moved.value;
1798
+ })) return;
1799
+ // CORREÇÃO: Usa a propriedade interna _value para obter a posição final
1800
+ var finalY = positions[moved.value]._value; // 👈 CORREÇÃO APLICADA
1801
+ var newIndex = Math.max(0, Math.min(currentItems.length - 1, Math.floor((finalY + ITEM_HEIGHT / 2) / ITEM_HEIGHT)));
1802
+ var oldIndex = activeIndex.current;
1803
+ var updated = [].concat(currentItems);
1804
+ var _updated$splice = updated.splice(oldIndex, 1),
1805
+ removed = _updated$splice[0];
1806
+ updated.splice(newIndex, 0, removed);
1807
+ // Atualiza o state com a nova ordem.
1808
+ setItems(updated);
1809
+ // A animação final agora está no useEffect, mas fazemos a limpeza das refs.
1810
+ setDraggingId(null);
1811
+ activeItem.current = null;
1812
+ activeIndex.current = -1;
1785
1813
  }
1786
- return /*#__PURE__*/jsx(Fragment, {
1787
- children: /*#__PURE__*/jsx(UIChildren, {
1788
- "transient": true,
1789
- scope: row,
1790
- crud: row.crud,
1791
- children: props.children
1792
- })
1793
- });
1794
- };
1795
- return /*#__PURE__*/jsx(ScaleDecorator, {
1796
- children: /*#__PURE__*/jsx(TouchableOpacity, {
1797
- style: [styles$a.row, {
1798
- backgroundColor: isActive ? 'lightblue' : 'white'
1799
- }, _extends({}, css)],
1800
- delayLongPress: 100,
1801
- onLongPress: drag // Initiate drag on long press
1802
- ,
1803
- children: /*#__PURE__*/jsx(Child, {})
1804
- })
1805
1814
  });
1806
1815
  };
1807
- var OrderView = function OrderView(_ref2) {
1808
- var children = _ref2.children;
1809
- if (original.gesture !== false) {
1810
- return /*#__PURE__*/jsx(GestureHandlerRootView, {
1811
- style: _extends({
1812
- flex: 1,
1813
- width: '100%'
1814
- }, scope.getStyle('order', {
1815
- justifyContent: 'flex-start'
1816
- })),
1817
- children: children
1818
- });
1816
+ // Cria/Reusa os PanResponders
1817
+ items.forEach(function (it) {
1818
+ if (!panRespondersRef.current[it.value]) {
1819
+ panRespondersRef.current[it.value] = createPanResponder(it);
1819
1820
  }
1820
- return /*#__PURE__*/jsx(Fragment, {
1821
- children: children
1822
- });
1823
- };
1824
- var Header = function Header(_ref3) {
1825
- var pos = _ref3.pos;
1826
- var hPos = Utils.nvl(original.headerPos, 'outer');
1827
- if (hPos !== pos) return /*#__PURE__*/jsx(Fragment, {});
1828
- return /*#__PURE__*/jsx(UIHeader, {
1829
- scope: scope
1821
+ });
1822
+ var removeItem = function removeItem(id) {
1823
+ if (draggingId === id) {
1824
+ setDraggingId(null);
1825
+ activeItem.current = null;
1826
+ activeIndex.current = -1;
1827
+ }
1828
+ // CORREÇÃO: Delete a referência Animated.Value *e* a referência PanResponder
1829
+ if (positions[id]) {
1830
+ delete positions[id];
1831
+ }
1832
+ if (panRespondersRef.current[id]) {
1833
+ delete panRespondersRef.current[id];
1834
+ }
1835
+ setItems(function (prev) {
1836
+ var updated = prev.filter(function (it) {
1837
+ return it.value !== id;
1838
+ });
1839
+ // Anima os itens restantes para seus novos índices
1840
+ updated.forEach(function (it, idx) {
1841
+ if (positions[it.value]) Animated.spring(positions[it.value], {
1842
+ toValue: idx * ITEM_HEIGHT,
1843
+ useNativeDriver: false
1844
+ }).start();
1845
+ });
1846
+ return updated;
1830
1847
  });
1831
1848
  };
1832
- var OrderData = function OrderData() {
1833
- var _useState2 = useState(items),
1834
- data = _useState2[0],
1835
- setData = _useState2[1];
1836
- return /*#__PURE__*/jsx(DraggableFlatList, {
1837
- data: data,
1838
- ListHeaderComponent: /*#__PURE__*/jsx(Header, {
1839
- pos: "inner"
1840
- }),
1841
- ListFooterComponent: /*#__PURE__*/jsx(Fragment, {
1842
- children: scope.getPart('footer')
1843
- }),
1844
- renderItem: renderItem,
1845
- containerStyle: _extends({}, scope.getStyle('list')),
1846
- keyExtractor: function keyExtractor(item) {
1847
- var key = scope.getItemValue(item);
1848
- if (original != null && original.debug) {
1849
- console.log(key);
1850
- }
1851
- return key;
1849
+ return /*#__PURE__*/jsxs(View, {
1850
+ style: styles$a.container,
1851
+ children: [/*#__PURE__*/jsx(Text, {
1852
+ style: styles$a.title,
1853
+ children: "Lista Drag\xE1vel (Esperando que a corre\xE7\xE3o final funcione \uD83D\uDE4F)"
1854
+ }), /*#__PURE__*/jsx(View, {
1855
+ style: {
1856
+ height: items.length * ITEM_HEIGHT,
1857
+ width: '100%'
1852
1858
  },
1853
- onDragEnd: function onDragEnd(_ref4) {
1854
- var data = _ref4.data;
1855
- setData(data);
1856
- scope.changeValue(data);
1857
- }
1858
- });
1859
- };
1860
- return /*#__PURE__*/jsxs(OrderView, {
1861
- children: [/*#__PURE__*/jsx(Header, {
1862
- pos: "outer"
1863
- }), /*#__PURE__*/jsx(OrderData, {})]
1859
+ children: items.map(function (item) {
1860
+ var _panRespondersRef$cur, _panRespondersRef$cur2;
1861
+ var y = positions[item.value];
1862
+ if (!y) return null;
1863
+ var isDragging = draggingId === item.value;
1864
+ return /*#__PURE__*/jsxs(Animated.View, _extends({}, (_panRespondersRef$cur = (_panRespondersRef$cur2 = panRespondersRef.current[item.value]) == null ? void 0 : _panRespondersRef$cur2.panHandlers) != null ? _panRespondersRef$cur : {}, {
1865
+ style: [styles$a.item, {
1866
+ position: 'absolute',
1867
+ left: 0,
1868
+ right: 0,
1869
+ height: ITEM_HEIGHT - 8,
1870
+ transform: [{
1871
+ translateY: y
1872
+ }],
1873
+ zIndex: isDragging ? 999 : 0,
1874
+ elevation: isDragging ? 999 : 0
1875
+ }],
1876
+ children: [/*#__PURE__*/jsx(View, {
1877
+ style: styles$a.handle,
1878
+ children: /*#__PURE__*/jsx(Text, {
1879
+ style: styles$a.handleText,
1880
+ children: "\u2261"
1881
+ })
1882
+ }), /*#__PURE__*/jsx(Text, {
1883
+ style: styles$a.itemText,
1884
+ children: item.label
1885
+ }), /*#__PURE__*/jsx(TouchableOpacity, {
1886
+ onPress: function onPress() {
1887
+ return removeItem(item.value);
1888
+ },
1889
+ style: styles$a.del,
1890
+ children: /*#__PURE__*/jsx(Text, {
1891
+ style: {
1892
+ color: '#fff'
1893
+ },
1894
+ children: "X"
1895
+ })
1896
+ })]
1897
+ }), item.value);
1898
+ })
1899
+ })]
1864
1900
  });
1865
1901
  }
1866
1902
  var styles$a = /*#__PURE__*/StyleSheet.create({
1867
- row: {
1903
+ container: {
1904
+ flex: 1,
1905
+ paddingHorizontal: 20,
1906
+ backgroundColor: '#f5f7fb'
1907
+ },
1908
+ title: {
1909
+ fontSize: 18,
1910
+ fontWeight: '600',
1911
+ marginBottom: 12
1912
+ },
1913
+ item: {
1914
+ backgroundColor: '#fff',
1915
+ borderRadius: 10,
1916
+ marginVertical: 4,
1917
+ paddingHorizontal: 12,
1918
+ alignItems: 'center',
1868
1919
  flexDirection: 'row',
1869
- gap: 10,
1870
- padding: 15,
1871
- borderBottomWidth: 1,
1872
- borderBottomColor: '#ccc'
1920
+ elevation: 3,
1921
+ shadowColor: '#000',
1922
+ shadowOpacity: 0.05,
1923
+ shadowRadius: 6
1873
1924
  },
1874
- text: {
1875
- fontSize: 18
1925
+ handle: {
1926
+ width: 40,
1927
+ alignItems: 'center',
1928
+ justifyContent: 'center'
1929
+ },
1930
+ handleText: {
1931
+ fontSize: 22,
1932
+ color: '#666'
1933
+ },
1934
+ itemText: {
1935
+ fontSize: 16,
1936
+ flex: 1
1937
+ },
1938
+ del: {
1939
+ backgroundColor: '#e74c3c',
1940
+ height: 34,
1941
+ width: 34,
1942
+ borderRadius: 6,
1943
+ alignItems: 'center',
1944
+ justifyContent: 'center'
1876
1945
  }
1877
1946
  });
1878
1947
 
1948
+ function UIOrder(props) {
1949
+ return /*#__PURE__*/jsx(Fragment, {
1950
+ children: /*#__PURE__*/jsx(UIDraggableList, _extends({}, props))
1951
+ });
1952
+ }
1953
+
1879
1954
  var CrudContext = /*#__PURE__*/createContext({});
1880
1955
  function UIElement(props) {
1881
1956
  var _original$list,