@popsure/dirty-swan 0.66.25 → 0.66.26

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.
@@ -80,4 +80,62 @@ describe('useTableNavigation', () => {
80
80
  });
81
81
  expect(onSelectionChanged).toHaveBeenCalledWith(0);
82
82
  });
83
+
84
+ it('starts on the section passed by the parent instead of the first one', () => {
85
+ const { result } = renderHook(() =>
86
+ useTableNavigation({
87
+ enabled: true,
88
+ containerRef,
89
+ onSelectionChanged,
90
+ initialSection: 1,
91
+ })
92
+ );
93
+
94
+ expect(result.current.activeSection).toBe(2);
95
+ expect(containerRef.current?.scroll).toHaveBeenCalledWith({
96
+ top: 0,
97
+ left: 400,
98
+ behavior: 'smooth',
99
+ });
100
+ });
101
+
102
+ it('does not report a section pushed in by the parent as a user selection', () => {
103
+ const { result } = renderHook(() =>
104
+ useTableNavigation({
105
+ enabled: true,
106
+ containerRef,
107
+ onSelectionChanged,
108
+ })
109
+ );
110
+ onSelectionChanged.mockClear();
111
+
112
+ act(() => {
113
+ result.current.syncActiveSection(1);
114
+ });
115
+
116
+ expect(result.current.activeSection).toBe(2);
117
+ expect(onSelectionChanged).not.toHaveBeenCalled();
118
+ });
119
+
120
+ it('keeps reporting user navigation after a parent sync', () => {
121
+ const { result } = renderHook(() =>
122
+ useTableNavigation({
123
+ enabled: true,
124
+ containerRef,
125
+ onSelectionChanged,
126
+ })
127
+ );
128
+
129
+ act(() => {
130
+ result.current.syncActiveSection(1);
131
+ });
132
+ onSelectionChanged.mockClear();
133
+
134
+ act(() => {
135
+ result.current.navigateTable(false);
136
+ });
137
+
138
+ expect(result.current.activeSection).toBe(1);
139
+ expect(onSelectionChanged).toHaveBeenCalledWith(1);
140
+ });
83
141
  });
@@ -1,24 +1,38 @@
1
1
  import debounce from "lodash.debounce";
2
- import { useCallback, useEffect, useState } from "react";
2
+ import { useCallback, useEffect, useRef, useState } from "react";
3
3
 
4
4
  interface UseTableNavigationReturn {
5
5
  activeSection: number;
6
6
  navigateTable: (increase?: boolean) => void;
7
7
  setActiveSection: (section: number) => void;
8
+ syncActiveSection: (section: number) => void;
8
9
  }
9
10
 
10
11
  interface UseTableNavigationProps {
11
12
  containerRef: React.RefObject<HTMLElement>,
12
13
  enabled?: boolean,
14
+ initialSection?: number,
13
15
  onSelectionChanged?: (index: number) => void
14
16
  }
15
17
 
16
18
  export const useTableNavigation = ({
17
19
  enabled,
18
20
  containerRef,
21
+ initialSection = 0,
19
22
  onSelectionChanged
20
23
  }: UseTableNavigationProps): UseTableNavigationReturn => {
21
- const [activeSection, setActiveSection] = useState(0);
24
+ const [activeSection, setActiveSection] = useState(initialSection);
25
+ const activeSectionRef = useRef(initialSection);
26
+ const skipNextReportRef = useRef(false);
27
+
28
+ const syncActiveSection = useCallback((section: number) => {
29
+ if (section === activeSectionRef.current) {
30
+ return;
31
+ }
32
+
33
+ skipNextReportRef.current = true;
34
+ setActiveSection(section);
35
+ }, []);
22
36
 
23
37
  const handleScrollToSection = (increase?: boolean) => {
24
38
  if (!enabled) {
@@ -55,11 +69,18 @@ export const useTableNavigation = ({
55
69
  }, [enabled]);
56
70
 
57
71
  useEffect(() => {
72
+ activeSectionRef.current = activeSection;
73
+
74
+ const skipReport = skipNextReportRef.current;
75
+ skipNextReportRef.current = false;
76
+
58
77
  if (!enabled) {
59
78
  return
60
79
  }
61
80
 
62
- onSelectionChanged?.(activeSection + 1);
81
+ if (!skipReport) {
82
+ onSelectionChanged?.(activeSection + 1);
83
+ }
63
84
 
64
85
  if (containerRef.current) {
65
86
  const containerWidth = containerRef.current.getBoundingClientRect().width;
@@ -77,5 +98,6 @@ export const useTableNavigation = ({
77
98
  activeSection: activeSection + 1,
78
99
  navigateTable: handleScrollToSection,
79
100
  setActiveSection,
101
+ syncActiveSection,
80
102
  }
81
103
  }
@@ -1 +0,0 @@
1
- {"version":3,"file":"useTableNavigation-CrxoXTVd.js","sources":["../../../src/lib/components/table/utils/useTableNavigation/useTableNavigation.ts"],"sourcesContent":["import debounce from \"lodash.debounce\";\nimport { useCallback, useEffect, useState } from \"react\";\n\ninterface UseTableNavigationReturn {\n activeSection: number;\n navigateTable: (increase?: boolean) => void;\n setActiveSection: (section: number) => void;\n}\n\ninterface UseTableNavigationProps {\n containerRef: React.RefObject<HTMLElement>,\n enabled?: boolean,\n onSelectionChanged?: (index: number) => void\n}\n\nexport const useTableNavigation = ({\n enabled,\n containerRef,\n onSelectionChanged\n}: UseTableNavigationProps): UseTableNavigationReturn => {\n const [activeSection, setActiveSection] = useState(0);\n\n const handleScrollToSection = (increase?: boolean) => {\n if (!enabled) {\n return;\n }\n\n setActiveSection((prevSection) => \n prevSection + (increase ? 1 : -1)\n );\n };\n\n const handleTableScroll = useCallback(() => {\n if (!containerRef.current || !enabled) {\n return;\n }\n\n const containerWidth = containerRef.current.getBoundingClientRect().width;\n const scrollLeft = containerRef.current.scrollLeft * 1.1;\n const cellWidth = containerWidth / 2;\n\n setActiveSection(Math.floor(scrollLeft / cellWidth));\n }, [activeSection, containerRef, enabled]);\n\n const debouncedTableScroll = debounce(handleTableScroll, 150);\n\n useEffect(() => {\n const container = containerRef.current;\n\n container?.addEventListener('scroll', debouncedTableScroll, {\n passive: true,\n });\n\n return container?.removeEventListener('scroll', handleTableScroll);\n }, [enabled]);\n\n useEffect(() => {\n if (!enabled) {\n return\n }\n\n onSelectionChanged?.(activeSection + 1);\n\n if (containerRef.current) {\n const containerWidth = containerRef.current.getBoundingClientRect().width;\n const cellWidth = containerWidth / 2;\n\n containerRef.current.scroll({\n top: 0,\n left: cellWidth * activeSection,\n behavior: 'smooth',\n });\n }\n }, [enabled, activeSection]);\n\n return {\n activeSection: activeSection + 1,\n navigateTable: handleScrollToSection,\n setActiveSection,\n }\n}"],"names":[],"mappings":";;;AAeO,IAAM,kBAAkB,GAAG,UAAC,EAIT,EAAA;AAHxB,IAAA,IAAA,OAAO,aAAA,EACP,YAAY,GAAA,EAAA,CAAA,YAAA,EACZ,kBAAkB,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