@navikt/ds-react 7.30.1 → 7.31.0
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/cjs/form/checkbox/Checkbox.js +1 -1
- package/cjs/form/checkbox/Checkbox.js.map +1 -1
- package/cjs/form/combobox/Combobox.js +15 -13
- package/cjs/form/combobox/Combobox.js.map +1 -1
- package/cjs/form/combobox/FilteredOptions/FilteredOptions.js +53 -3
- package/cjs/form/combobox/FilteredOptions/FilteredOptions.js.map +1 -1
- package/cjs/form/combobox/FilteredOptions/useVirtualFocus.js.map +1 -1
- package/cjs/form/combobox/Input/InputController.js +15 -14
- package/cjs/form/combobox/Input/InputController.js.map +1 -1
- package/cjs/form/radio/Radio.js +1 -1
- package/cjs/form/radio/Radio.js.map +1 -1
- package/cjs/overlays/floating/Floating.d.ts +11 -0
- package/cjs/overlays/floating/Floating.js +32 -8
- package/cjs/overlays/floating/Floating.js.map +1 -1
- package/cjs/overlays/overlay/hooks/useAnimationsFinished.d.ts +27 -0
- package/cjs/overlays/overlay/hooks/useAnimationsFinished.js +138 -0
- package/cjs/overlays/overlay/hooks/useAnimationsFinished.js.map +1 -0
- package/cjs/overlays/overlay/hooks/useEventCallback.d.ts +6 -0
- package/cjs/overlays/overlay/hooks/useEventCallback.js +89 -0
- package/cjs/overlays/overlay/hooks/useEventCallback.js.map +1 -0
- package/cjs/overlays/overlay/hooks/useLatestRef.d.ts +5 -0
- package/cjs/overlays/overlay/hooks/useLatestRef.js +23 -0
- package/cjs/overlays/overlay/hooks/useLatestRef.js.map +1 -0
- package/cjs/overlays/overlay/hooks/useOpenChangeComplete.d.ts +31 -0
- package/cjs/overlays/overlay/hooks/useOpenChangeComplete.js +35 -0
- package/cjs/overlays/overlay/hooks/useOpenChangeComplete.js.map +1 -0
- package/cjs/overlays/overlay/hooks/useRefWithInit.d.ts +7 -0
- package/cjs/overlays/overlay/hooks/useRefWithInit.js +14 -0
- package/cjs/overlays/overlay/hooks/useRefWithInit.js.map +1 -0
- package/cjs/table/ExpandableRow.d.ts +1 -1
- package/cjs/table/ExpandableRow.js +2 -10
- package/cjs/table/ExpandableRow.js.map +1 -1
- package/cjs/table/Row.d.ts +7 -0
- package/cjs/table/Row.js +13 -2
- package/cjs/table/Row.js.map +1 -1
- package/cjs/table/Table.utils.d.ts +9 -0
- package/cjs/table/Table.utils.js +57 -0
- package/cjs/table/Table.utils.js.map +1 -0
- package/esm/form/checkbox/Checkbox.js +1 -1
- package/esm/form/checkbox/Checkbox.js.map +1 -1
- package/esm/form/combobox/Combobox.js +15 -13
- package/esm/form/combobox/Combobox.js.map +1 -1
- package/esm/form/combobox/FilteredOptions/FilteredOptions.js +21 -4
- package/esm/form/combobox/FilteredOptions/FilteredOptions.js.map +1 -1
- package/esm/form/combobox/FilteredOptions/useVirtualFocus.js.map +1 -1
- package/esm/form/combobox/Input/InputController.js +15 -14
- package/esm/form/combobox/Input/InputController.js.map +1 -1
- package/esm/form/radio/Radio.js +1 -1
- package/esm/form/radio/Radio.js.map +1 -1
- package/esm/overlays/floating/Floating.d.ts +11 -0
- package/esm/overlays/floating/Floating.js +32 -8
- package/esm/overlays/floating/Floating.js.map +1 -1
- package/esm/overlays/overlay/hooks/useAnimationsFinished.d.ts +27 -0
- package/esm/overlays/overlay/hooks/useAnimationsFinished.js +99 -0
- package/esm/overlays/overlay/hooks/useAnimationsFinished.js.map +1 -0
- package/esm/overlays/overlay/hooks/useEventCallback.d.ts +6 -0
- package/esm/overlays/overlay/hooks/useEventCallback.js +53 -0
- package/esm/overlays/overlay/hooks/useEventCallback.js.map +1 -0
- package/esm/overlays/overlay/hooks/useLatestRef.d.ts +5 -0
- package/esm/overlays/overlay/hooks/useLatestRef.js +20 -0
- package/esm/overlays/overlay/hooks/useLatestRef.js.map +1 -0
- package/esm/overlays/overlay/hooks/useOpenChangeComplete.d.ts +31 -0
- package/esm/overlays/overlay/hooks/useOpenChangeComplete.js +32 -0
- package/esm/overlays/overlay/hooks/useOpenChangeComplete.js.map +1 -0
- package/esm/overlays/overlay/hooks/useRefWithInit.d.ts +7 -0
- package/esm/overlays/overlay/hooks/useRefWithInit.js +12 -0
- package/esm/overlays/overlay/hooks/useRefWithInit.js.map +1 -0
- package/esm/table/ExpandableRow.d.ts +1 -1
- package/esm/table/ExpandableRow.js +2 -10
- package/esm/table/ExpandableRow.js.map +1 -1
- package/esm/table/Row.d.ts +7 -0
- package/esm/table/Row.js +13 -2
- package/esm/table/Row.js.map +1 -1
- package/esm/table/Table.utils.d.ts +9 -0
- package/esm/table/Table.utils.js +55 -0
- package/esm/table/Table.utils.js.map +1 -0
- package/package.json +3 -3
- package/src/form/checkbox/Checkbox.tsx +5 -3
- package/src/form/combobox/Combobox.tsx +44 -41
- package/src/form/combobox/FilteredOptions/FilteredOptions.tsx +29 -4
- package/src/form/combobox/FilteredOptions/useVirtualFocus.ts +1 -0
- package/src/form/combobox/Input/InputController.tsx +33 -29
- package/src/form/radio/Radio.tsx +5 -3
- package/src/overlays/floating/Floating.tsx +110 -59
- package/src/overlays/overlay/hooks/useAnimationsFinished.ts +117 -0
- package/src/overlays/overlay/hooks/useEventCallback.ts +73 -0
- package/src/overlays/overlay/hooks/useLatestRef.ts +25 -0
- package/src/overlays/overlay/hooks/useOpenChangeComplete.ts +66 -0
- package/src/overlays/overlay/hooks/useRefWithInit.ts +25 -0
- package/src/table/ExpandableRow.tsx +4 -17
- package/src/table/Row.tsx +33 -1
- package/src/table/Table.utils.ts +65 -0
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import React, { useCallback, useEffect } from "react";
|
|
4
|
+
import ReactDOM from "react-dom";
|
|
5
|
+
import { useEventCallback } from "./useEventCallback";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Returns a stable function that, when invoked, waits for all current CSS/Web Animations
|
|
9
|
+
* on a target element (and its subtree) to finish before executing a callback.
|
|
10
|
+
*
|
|
11
|
+
* Why:
|
|
12
|
+
* - Coordinate logic (unmount, focus restore, measuring) after exit / enter animations.
|
|
13
|
+
* - Avoid `animationend` event bookkeeping across multiple animations / nested elements.
|
|
14
|
+
* - Batch detection using `requestAnimationFrame` so freshly-applied animations are discoverable.
|
|
15
|
+
*
|
|
16
|
+
* Mechanics:
|
|
17
|
+
* 1. Resolves the concrete `HTMLElement` (direct element or from ref) – early no-op if missing.
|
|
18
|
+
* 2. If `getAnimations` is unsupported or animations are globally disabled (`AKSEL_ANIMATIONS_DISABLED`),
|
|
19
|
+
* runs the callback immediately.
|
|
20
|
+
* 3. Schedules a frame so style/animation changes applied this render are committed.
|
|
21
|
+
* 4. Optionally schedules an additional frame (`waitForNextTick=true`) to catch animations that
|
|
22
|
+
* start only after layout (e.g. when an `open` class triggers the animation).
|
|
23
|
+
* 5. Captures all current animations, waits on their `.finished` promises (using `Promise.allSettled`
|
|
24
|
+
* so rejections don't block), then `flushSync` executes the callback (ensures React state updates
|
|
25
|
+
* inside run before the browser paints the next frame).
|
|
26
|
+
* 6. If an `AbortSignal` aborts while waiting, it silently cancels execution.
|
|
27
|
+
*
|
|
28
|
+
* @param elementOrRef HTMLElement or ref to observe.
|
|
29
|
+
* @param waitForNextTick If true, waits an extra frame to ensure enter animations are detectable.
|
|
30
|
+
* @returns Stable function (identity preserved) accepting (fn, abortSignal?).
|
|
31
|
+
*/
|
|
32
|
+
export function useAnimationsFinished(
|
|
33
|
+
elementOrRef: React.RefObject<HTMLElement | null> | HTMLElement | null,
|
|
34
|
+
waitForNextTick = false,
|
|
35
|
+
) {
|
|
36
|
+
const rootFrameRef = React.useRef<number | null>(null);
|
|
37
|
+
const nestedFrameRef = React.useRef<number | null>(null);
|
|
38
|
+
|
|
39
|
+
const cancelScheduled = useCallback(() => {
|
|
40
|
+
for (const ref of [rootFrameRef, nestedFrameRef]) {
|
|
41
|
+
if (ref.current !== null) {
|
|
42
|
+
cancelAnimationFrame(ref.current);
|
|
43
|
+
ref.current = null;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}, []);
|
|
47
|
+
|
|
48
|
+
/* Unmount cleanup */
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
return () => cancelScheduled();
|
|
51
|
+
}, [cancelScheduled]);
|
|
52
|
+
|
|
53
|
+
return useEventCallback(
|
|
54
|
+
(
|
|
55
|
+
/**
|
|
56
|
+
* A function to execute once all animations have finished.
|
|
57
|
+
*/
|
|
58
|
+
fnToExecute: () => void,
|
|
59
|
+
/**
|
|
60
|
+
* An optional [AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) that
|
|
61
|
+
* can be used to abort `fnToExecute` before all the animations have finished.
|
|
62
|
+
* @default null
|
|
63
|
+
*/
|
|
64
|
+
signal: AbortSignal | null = null,
|
|
65
|
+
) => {
|
|
66
|
+
// Cancel any in-flight scheduling from a previous invocation (next-frame debounce semantics)
|
|
67
|
+
cancelScheduled();
|
|
68
|
+
|
|
69
|
+
if (elementOrRef == null) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const element =
|
|
74
|
+
"current" in elementOrRef ? elementOrRef.current : elementOrRef;
|
|
75
|
+
if (element == null) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Fast path: no Web Animations API support OR animations globally disabled.
|
|
80
|
+
if (
|
|
81
|
+
typeof element.getAnimations !== "function" ||
|
|
82
|
+
// Flag hook for test envs.
|
|
83
|
+
(globalThis as any).AKSEL_ANIMATIONS_DISABLED
|
|
84
|
+
) {
|
|
85
|
+
fnToExecute();
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
rootFrameRef.current = requestAnimationFrame(() => {
|
|
90
|
+
function exec() {
|
|
91
|
+
if (!element) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
// Collect animations present at this moment; we don't continuously observe
|
|
95
|
+
// if new animations start after these settle, caller should invoke again.
|
|
96
|
+
Promise.allSettled(
|
|
97
|
+
element.getAnimations().map((anim) => anim.finished),
|
|
98
|
+
).then(() => {
|
|
99
|
+
if (signal?.aborted) return;
|
|
100
|
+
// Ensure any state updates inside the callback are flushed synchronously,
|
|
101
|
+
// guaranteeing that dependent logic observes the current
|
|
102
|
+
// tree rather than a stale in-progress update.
|
|
103
|
+
ReactDOM.flushSync(fnToExecute);
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Some animations (e.g. triggered by a class applied this same frame) only
|
|
108
|
+
// become observable after an extra frame; opt-in via flag.
|
|
109
|
+
if (waitForNextTick) {
|
|
110
|
+
nestedFrameRef.current = requestAnimationFrame(exec);
|
|
111
|
+
} else {
|
|
112
|
+
exec();
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
},
|
|
116
|
+
);
|
|
117
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Stable event callback: returns a function whose identity never changes but always
|
|
5
|
+
* invokes the latest `callback`. Avoids stale closures without re‑creating handlers.
|
|
6
|
+
*
|
|
7
|
+
* Why not `useCallback`? Its identity depends on a deps array:
|
|
8
|
+
* - omit deps -> stale; include deps -> new function each render.
|
|
9
|
+
* - This hook decouples identity from freshness.
|
|
10
|
+
*
|
|
11
|
+
* How it works: a single stable "trampoline" function delegates to a mutable ref. The current
|
|
12
|
+
* `callback` is promoted from `next` in an insertion/layout phase effect so abandoned concurrent
|
|
13
|
+
* renders cannot leak outdated handlers.
|
|
14
|
+
*
|
|
15
|
+
* Guarantees: stable identity; latest logic executed; no calls from uncommitted renders; dev
|
|
16
|
+
* error if invoked during render; safe when `callback` is undefined (no-op).
|
|
17
|
+
*/
|
|
18
|
+
import React, { useLayoutEffect } from "react";
|
|
19
|
+
import { useRefWithInit } from "./useRefWithInit";
|
|
20
|
+
|
|
21
|
+
/* https://github.com/mui/material-ui/issues/41190#issuecomment-2040873379 */
|
|
22
|
+
const useInsertionEffect = (React as any)[
|
|
23
|
+
`useInsertionEffect${Math.random().toFixed(1)}`.slice(0, -3)
|
|
24
|
+
];
|
|
25
|
+
|
|
26
|
+
const useSafeInsertionEffect =
|
|
27
|
+
// React 17 doesn't have useInsertionEffect.
|
|
28
|
+
useInsertionEffect &&
|
|
29
|
+
// Preact replaces useInsertionEffect with useLayoutEffect and fires too late.
|
|
30
|
+
useInsertionEffect !== useLayoutEffect
|
|
31
|
+
? useInsertionEffect
|
|
32
|
+
: (fn: any) => fn();
|
|
33
|
+
|
|
34
|
+
type Callback = (...args: any[]) => any;
|
|
35
|
+
|
|
36
|
+
type Stable<T extends Callback> = {
|
|
37
|
+
/** The next value for callback */
|
|
38
|
+
next: T | undefined;
|
|
39
|
+
/** The function to be called by trampoline. This must fail during the initial render phase. */
|
|
40
|
+
callback: T | undefined;
|
|
41
|
+
trampoline: T;
|
|
42
|
+
effect: () => void;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* TODO: Long term, replace `useCallbackRef` with this hook.
|
|
47
|
+
*/
|
|
48
|
+
export function useEventCallback<T extends Callback>(
|
|
49
|
+
callback: T | undefined,
|
|
50
|
+
): T {
|
|
51
|
+
const stable = useRefWithInit(createStableCallback).current as Stable<T>;
|
|
52
|
+
stable.next = callback;
|
|
53
|
+
useSafeInsertionEffect(stable.effect);
|
|
54
|
+
return stable.trampoline;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function createStableCallback() {
|
|
58
|
+
const stable: Stable<Callback> = {
|
|
59
|
+
next: undefined,
|
|
60
|
+
callback: assertNotCalled,
|
|
61
|
+
trampoline: (...args: any[]) => stable.callback?.(...args),
|
|
62
|
+
effect: () => {
|
|
63
|
+
stable.callback = stable.next;
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
return stable;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function assertNotCalled() {
|
|
70
|
+
if (process.env.NODE_ENV !== "production") {
|
|
71
|
+
throw new Error("Aksel: Cannot call an event handler while rendering.");
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useClientLayoutEffect } from "../../../util";
|
|
4
|
+
import { useRefWithInit } from "./useRefWithInit";
|
|
5
|
+
|
|
6
|
+
export function useLatestRef<T>(value: T) {
|
|
7
|
+
const latest = useRefWithInit(createLatestRef, value).current!;
|
|
8
|
+
|
|
9
|
+
latest.next = value;
|
|
10
|
+
|
|
11
|
+
useClientLayoutEffect(latest.effect);
|
|
12
|
+
|
|
13
|
+
return latest;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function createLatestRef<T>(value: T) {
|
|
17
|
+
const latest = {
|
|
18
|
+
current: value,
|
|
19
|
+
next: value,
|
|
20
|
+
effect: () => {
|
|
21
|
+
latest.current = latest.next;
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
return latest;
|
|
25
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import React, { useEffect } from "react";
|
|
4
|
+
import { useAnimationsFinished } from "./useAnimationsFinished";
|
|
5
|
+
import { useEventCallback } from "./useEventCallback";
|
|
6
|
+
import { useLatestRef } from "./useLatestRef";
|
|
7
|
+
|
|
8
|
+
interface useOpenChangeCompleteParameters {
|
|
9
|
+
/**
|
|
10
|
+
* Enable / disable the effect. Disabled => no animation tracking / callback.
|
|
11
|
+
* @default true
|
|
12
|
+
*/
|
|
13
|
+
enabled?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Current open state (e.g. popover open). When this flips we wait for any
|
|
16
|
+
* associated CSS/Web animations on `ref` to finish before firing `onComplete`.
|
|
17
|
+
*/
|
|
18
|
+
open?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Element whose animations/transition we observe. Should be stable while the
|
|
21
|
+
* open/close animation runs (typically the root animated node).
|
|
22
|
+
*/
|
|
23
|
+
ref?: React.RefObject<HTMLElement | null>;
|
|
24
|
+
/**
|
|
25
|
+
* Called exactly once per open-change cycle after animations finish OR
|
|
26
|
+
* immediately if animations are disabled / unsupported.
|
|
27
|
+
*/
|
|
28
|
+
onComplete: () => void;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Waits for the element's current Web Animations / CSS transitions to finish after an
|
|
33
|
+
* `open` state change, then invokes `onComplete`. Guards against race conditions by
|
|
34
|
+
* comparing the `open` value captured at scheduling time with the latest `open` via ref;
|
|
35
|
+
* if they differ (state flipped again mid‑animation) the callback is skipped.
|
|
36
|
+
*/
|
|
37
|
+
export function useOpenChangeComplete(
|
|
38
|
+
parameters: useOpenChangeCompleteParameters,
|
|
39
|
+
) {
|
|
40
|
+
const {
|
|
41
|
+
enabled = true,
|
|
42
|
+
open,
|
|
43
|
+
ref = null,
|
|
44
|
+
onComplete: onCompleteParam,
|
|
45
|
+
} = parameters;
|
|
46
|
+
|
|
47
|
+
const openRef = useLatestRef(open);
|
|
48
|
+
const onComplete = useEventCallback(onCompleteParam);
|
|
49
|
+
const runOnceAnimationsFinish = useAnimationsFinished(ref, open);
|
|
50
|
+
|
|
51
|
+
useEffect(() => {
|
|
52
|
+
if (!enabled) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/*
|
|
57
|
+
* Schedule completion once the *current* set of animations settle. If during
|
|
58
|
+
* that wait `open` toggles again, skip to avoid firing for an outdated cycle.
|
|
59
|
+
*/
|
|
60
|
+
runOnceAnimationsFinish(() => {
|
|
61
|
+
if (open === openRef.current) {
|
|
62
|
+
onComplete();
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}, [enabled, open, onComplete, runOnceAnimationsFinish, openRef]);
|
|
66
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import React, { useRef } from "react";
|
|
4
|
+
|
|
5
|
+
const UNINITIALIZED = {};
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* useRef initialized with a function on mount.
|
|
9
|
+
*/
|
|
10
|
+
function useRefWithInit<T>(init: () => T): React.RefObject<T>;
|
|
11
|
+
function useRefWithInit<T, U>(
|
|
12
|
+
init: (arg: U) => T,
|
|
13
|
+
initArg: U,
|
|
14
|
+
): React.RefObject<T>;
|
|
15
|
+
function useRefWithInit(init: (arg?: unknown) => unknown, initArg?: unknown) {
|
|
16
|
+
const ref = useRef(UNINITIALIZED as any);
|
|
17
|
+
|
|
18
|
+
if (ref.current === UNINITIALIZED) {
|
|
19
|
+
ref.current = init(initArg);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return ref;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export { useRefWithInit };
|
|
@@ -8,8 +8,10 @@ import { useI18n } from "../util/i18n/i18n.hooks";
|
|
|
8
8
|
import AnimateHeight from "./AnimateHeight";
|
|
9
9
|
import DataCell from "./DataCell";
|
|
10
10
|
import Row, { RowProps } from "./Row";
|
|
11
|
+
import { isElementInteractiveTarget } from "./Table.utils";
|
|
11
12
|
|
|
12
|
-
export interface ExpandableRowProps
|
|
13
|
+
export interface ExpandableRowProps
|
|
14
|
+
extends Omit<RowProps, "content" | "onRowClick"> {
|
|
13
15
|
/**
|
|
14
16
|
* Content of the expanded row
|
|
15
17
|
*/
|
|
@@ -96,7 +98,7 @@ export const ExpandableRow: ExpandableRowType = forwardRef(
|
|
|
96
98
|
const handleRowClick = (event: React.MouseEvent<HTMLTableRowElement>) => {
|
|
97
99
|
expandOnRowClick &&
|
|
98
100
|
!expansionDisabled &&
|
|
99
|
-
!
|
|
101
|
+
!isElementInteractiveTarget(event.target as HTMLElement) &&
|
|
100
102
|
expansionHandler(event);
|
|
101
103
|
};
|
|
102
104
|
|
|
@@ -167,19 +169,4 @@ export const ExpandableRow: ExpandableRowType = forwardRef(
|
|
|
167
169
|
},
|
|
168
170
|
);
|
|
169
171
|
|
|
170
|
-
function isInteractiveTarget(elm: HTMLElement) {
|
|
171
|
-
if (elm.nodeName === "TD" || elm.nodeName === "TH" || !elm.parentElement) {
|
|
172
|
-
return false;
|
|
173
|
-
}
|
|
174
|
-
if (
|
|
175
|
-
["BUTTON", "DETAILS", "LABEL", "SELECT", "TEXTAREA", "INPUT", "A"].includes(
|
|
176
|
-
elm.nodeName,
|
|
177
|
-
)
|
|
178
|
-
) {
|
|
179
|
-
return true;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
return isInteractiveTarget(elm.parentElement);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
172
|
export default ExpandableRow;
|
package/src/table/Row.tsx
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import React, { forwardRef } from "react";
|
|
2
2
|
import { useRenameCSS } from "../theme/Theme";
|
|
3
|
+
import { composeEventHandlers } from "../util/composeEventHandlers";
|
|
4
|
+
import { isElementInteractiveTarget } from "./Table.utils";
|
|
3
5
|
|
|
4
6
|
export interface RowProps extends React.HTMLAttributes<HTMLTableRowElement> {
|
|
5
7
|
/**
|
|
@@ -12,6 +14,13 @@ export interface RowProps extends React.HTMLAttributes<HTMLTableRowElement> {
|
|
|
12
14
|
* @default true
|
|
13
15
|
*/
|
|
14
16
|
shadeOnHover?: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Click handler for row. This differs from onClick by not being called
|
|
19
|
+
* when clicking on interactive elements within the row (buttons, links, inputs etc).
|
|
20
|
+
*
|
|
21
|
+
* **Warning:** This will not be accessible by keyboard! Provide an alternative way to select the row, e.g. a checkbox or a button.
|
|
22
|
+
*/
|
|
23
|
+
onRowClick?: (event: React.MouseEvent<HTMLTableRowElement>) => void;
|
|
15
24
|
}
|
|
16
25
|
|
|
17
26
|
export type RowType = React.ForwardRefExoticComponent<
|
|
@@ -19,8 +28,29 @@ export type RowType = React.ForwardRefExoticComponent<
|
|
|
19
28
|
>;
|
|
20
29
|
|
|
21
30
|
export const Row: RowType = forwardRef(
|
|
22
|
-
(
|
|
31
|
+
(
|
|
32
|
+
{
|
|
33
|
+
className,
|
|
34
|
+
selected = false,
|
|
35
|
+
shadeOnHover = true,
|
|
36
|
+
onClick,
|
|
37
|
+
onRowClick,
|
|
38
|
+
...rest
|
|
39
|
+
},
|
|
40
|
+
ref,
|
|
41
|
+
) => {
|
|
23
42
|
const { cn } = useRenameCSS();
|
|
43
|
+
|
|
44
|
+
const handleRowClick = (event: React.MouseEvent<HTMLTableRowElement>) => {
|
|
45
|
+
if (!onRowClick) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
if (isElementInteractiveTarget(event.target as HTMLElement)) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
onRowClick(event);
|
|
52
|
+
};
|
|
53
|
+
|
|
24
54
|
return (
|
|
25
55
|
<tr
|
|
26
56
|
{...rest}
|
|
@@ -29,6 +59,8 @@ export const Row: RowType = forwardRef(
|
|
|
29
59
|
"navds-table__row--selected": selected,
|
|
30
60
|
"navds-table__row--shade-on-hover": shadeOnHover,
|
|
31
61
|
})}
|
|
62
|
+
onClick={composeEventHandlers(onClick, handleRowClick)}
|
|
63
|
+
data-interactive={!!onRowClick}
|
|
32
64
|
/>
|
|
33
65
|
);
|
|
34
66
|
},
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
const INTERACTIVE_TAGS = new Set([
|
|
2
|
+
"BUTTON",
|
|
3
|
+
"A",
|
|
4
|
+
"INPUT",
|
|
5
|
+
"SELECT",
|
|
6
|
+
"TEXTAREA",
|
|
7
|
+
"DETAILS",
|
|
8
|
+
"SUMMARY",
|
|
9
|
+
"LABEL",
|
|
10
|
+
]);
|
|
11
|
+
|
|
12
|
+
const INTERACTIVE_ROLES = new Set([
|
|
13
|
+
"button",
|
|
14
|
+
"link",
|
|
15
|
+
"checkbox",
|
|
16
|
+
"radio",
|
|
17
|
+
"switch",
|
|
18
|
+
"menuitem",
|
|
19
|
+
"option",
|
|
20
|
+
"tab",
|
|
21
|
+
"textbox",
|
|
22
|
+
"combobox",
|
|
23
|
+
"spinbutton",
|
|
24
|
+
"slider",
|
|
25
|
+
]);
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Walks up from the event target until TR/TH (row / header) or root.
|
|
29
|
+
* Returns true if any ancestor is inherently interactive, explicitly focusable,
|
|
30
|
+
* or has an interactive ARIA role.
|
|
31
|
+
* Used to decide whether a row click should be treated as a row selection
|
|
32
|
+
* or ignored because the user interacted with an embedded control.
|
|
33
|
+
*/
|
|
34
|
+
function isElementInteractiveTarget(element: HTMLElement | null) {
|
|
35
|
+
for (
|
|
36
|
+
let node: HTMLElement | null = element;
|
|
37
|
+
node && node.nodeName !== "TR" && node.nodeName !== "TH";
|
|
38
|
+
node = node.parentElement
|
|
39
|
+
) {
|
|
40
|
+
const tag = node.nodeName;
|
|
41
|
+
|
|
42
|
+
/* Native interactive tag */
|
|
43
|
+
if (INTERACTIVE_TAGS.has(tag)) {
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/* Explicit interactive role */
|
|
48
|
+
const role = node.getAttribute("role");
|
|
49
|
+
if (role && INTERACTIVE_ROLES.has(role)) {
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/* Focusable via tabindex (exclude -1) */
|
|
54
|
+
if (node.hasAttribute("tabindex")) {
|
|
55
|
+
const ti = node.getAttribute("tabindex");
|
|
56
|
+
if (ti !== "-1") {
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export { isElementInteractiveTarget };
|