@autoafleveren/ui 1.5.5 → 1.5.6

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@autoafleveren/ui",
3
- "version": "1.5.5",
3
+ "version": "1.5.6",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist/*",
@@ -18,6 +18,7 @@ export interface ContextMenuInstance {
18
18
  ref: AppContextMenu;
19
19
  instance: App;
20
20
  element: HTMLDivElement;
21
+ activeRow?: HTMLTableRowElement | null;
21
22
  }
22
23
 
23
24
  export interface Shortcut {
@@ -294,7 +294,8 @@
294
294
  .vue3-easy-data-table__body tr {
295
295
  @apply bg-white;
296
296
 
297
- &:hover {
297
+ &:hover,
298
+ &.app-context-menu-active-row {
298
299
  td {
299
300
  @apply bg-zinc-50;
300
301
  }
@@ -0,0 +1,98 @@
1
+ import { describe, it, expect, beforeAll, beforeEach, vi } from 'vitest';
2
+ import { flushPromises } from '@vue/test-utils';
3
+ import { activeRowClass, useContextMenu } from '../index';
4
+ import ResizeObserverMock from '~/tests/mocks/resize-observer';
5
+
6
+ function createRowWithCell(): { row: HTMLTableRowElement; cell: HTMLTableCellElement } {
7
+ const table = document.createElement('table');
8
+ const tbody = document.createElement('tbody');
9
+ const row = document.createElement('tr');
10
+ const cell = document.createElement('td');
11
+
12
+ row.append(cell);
13
+ tbody.append(row);
14
+ table.append(tbody);
15
+ document.body.append(table);
16
+
17
+ return { row, cell };
18
+ }
19
+
20
+ describe('the useContextMenu composable', () => {
21
+ const contextMenu = useContextMenu();
22
+
23
+ beforeAll(() => {
24
+ global.ResizeObserver = ResizeObserverMock;
25
+
26
+ window.scrollTo = vi.fn();
27
+ });
28
+
29
+ beforeEach(async () => {
30
+ contextMenu.closeAll();
31
+
32
+ await new Promise(resolve => setTimeout(resolve, 600));
33
+
34
+ document.body.innerHTML = '';
35
+ });
36
+
37
+ it('adds the active row class to the closest table row when opened from a cell event', async () => {
38
+ expect.assertions(3);
39
+
40
+ const { row, cell } = createRowWithCell();
41
+ const event = new MouseEvent('contextmenu', { bubbles: true });
42
+
43
+ Object.defineProperty(event, 'target', { value: cell, configurable: true });
44
+
45
+ contextMenu.open({ id: 1 }, event);
46
+
47
+ await flushPromises();
48
+
49
+ expect(row.classList.contains(activeRowClass)).toBe(true);
50
+ expect(document.querySelectorAll(`tr.${activeRowClass}`)).toHaveLength(1);
51
+
52
+ contextMenu.close();
53
+
54
+ await new Promise(resolve => setTimeout(resolve, 350));
55
+
56
+ expect(row.classList.contains(activeRowClass)).toBe(false);
57
+ });
58
+
59
+ it('does not throw and adds no class when the event target is not inside table row', async () => {
60
+ expect.assertions(1);
61
+
62
+ const target = document.createElement('div');
63
+ document.body.append(target);
64
+
65
+ const event = new MouseEvent('contextmenu', { bubbles: true });
66
+ Object.defineProperty(event, 'target', { value: target, configurable: true });
67
+
68
+ contextMenu.open({ id: 1 }, event);
69
+
70
+ await flushPromises();
71
+
72
+ expect(document.querySelectorAll(`.${activeRowClass}`)).toHaveLength(0);
73
+ });
74
+
75
+ it('removes the active row class from the previous row when a new context menu is opened', async () => {
76
+ expect.assertions(3);
77
+
78
+ const { row: firstRow, cell: firstCell } = createRowWithCell();
79
+ const firstEvent = new MouseEvent('contextmenu', { bubbles: true });
80
+ Object.defineProperty(firstEvent, 'target', { value: firstCell, configurable: true });
81
+
82
+ contextMenu.open({ id: 1 }, firstEvent);
83
+ await flushPromises();
84
+
85
+ expect(firstRow.classList.contains(activeRowClass)).toBe(true);
86
+
87
+ const { row: secondRow, cell: secondCell } = createRowWithCell();
88
+ const secondEvent = new MouseEvent('contextmenu', { bubbles: true });
89
+ Object.defineProperty(secondEvent, 'target', { value: secondCell, configurable: true });
90
+
91
+ contextMenu.open({ id: 2 }, secondEvent);
92
+ await flushPromises();
93
+
94
+ expect(firstRow.classList.contains(activeRowClass)).toBe(false);
95
+ expect(secondRow.classList.contains(activeRowClass)).toBe(true);
96
+ });
97
+ });
98
+
@@ -5,6 +5,8 @@ import { AppContextMenu } from '~components';
5
5
  import type { App, Ref } from 'vue';
6
6
  import type { ContextMenuInstance, Shortcut, Props } from '~components/AppContextMenu/index.d';
7
7
 
8
+ export const activeRowClass = 'app-context-menu-active-row';
9
+
8
10
  const state = {
9
11
  instances: ref<ContextMenuInstance[]>([]),
10
12
  shortcuts: ref<Shortcut[]>([]),
@@ -12,6 +14,7 @@ const state = {
12
14
  };
13
15
 
14
16
  function closeContextMenu(contextInstance: ContextMenuInstance): void {
17
+ contextInstance?.activeRow?.classList.remove(activeRowClass);
15
18
  contextInstance?.instance?.unmount();
16
19
  contextInstance?.element?.remove();
17
20
  }
@@ -42,6 +45,9 @@ export function useContextMenu<T>(): UseContextMenu<T> {
42
45
  const contextMenuRootElement = document.createElement('div');
43
46
  document.body.append(contextMenuRootElement);
44
47
 
48
+ const activeRow = (event.target as HTMLElement | null)?.closest('tr') ?? null;
49
+ activeRow?.classList.add(activeRowClass);
50
+
45
51
  let contextInstance = null as null | App;
46
52
  let contextMenuRef = null as null | typeof AppContextMenu;
47
53
 
@@ -73,6 +79,7 @@ export function useContextMenu<T>(): UseContextMenu<T> {
73
79
  ref: contextMenuRef,
74
80
  instance: contextInstance,
75
81
  element: contextMenuRootElement,
82
+ activeRow,
76
83
  };
77
84
 
78
85
  // @ts-expect-error contextInstance wrong type
@@ -85,6 +92,7 @@ export function useContextMenu<T>(): UseContextMenu<T> {
85
92
  // @ts-expect-error contextInstance can not be null
86
93
  instance: contextInstance,
87
94
  element: contextMenuRootElement,
95
+ activeRow,
88
96
  });
89
97
  }, 500);
90
98
  }