@terreno/ui 0.14.0 → 0.14.1
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/dist/ActionSheet.d.ts +1 -1
- package/dist/ActionSheet.js +2 -2
- package/dist/ActionSheet.js.map +1 -1
- package/dist/Common.d.ts +8 -2
- package/dist/Common.js +4 -4
- package/dist/Common.js.map +1 -1
- package/dist/ConsentFormScreen.js +3 -3
- package/dist/ConsentFormScreen.js.map +1 -1
- package/dist/DateUtilities.d.ts +25 -25
- package/dist/DateUtilities.js +31 -32
- package/dist/DateUtilities.js.map +1 -1
- package/dist/MediaQuery.d.ts +4 -4
- package/dist/MediaQuery.js +8 -8
- package/dist/MediaQuery.js.map +1 -1
- package/dist/Page.d.ts +1 -0
- package/dist/Page.js +6 -2
- package/dist/Page.js.map +1 -1
- package/dist/PickerSelect.d.ts +1 -1
- package/dist/PickerSelect.js +2 -2
- package/dist/PickerSelect.js.map +1 -1
- package/dist/TapToEdit.d.ts +1 -1
- package/dist/TapToEdit.js +2 -3
- package/dist/TapToEdit.js.map +1 -1
- package/dist/ToastNotifications.js +2 -2
- package/dist/ToastNotifications.js.map +1 -1
- package/dist/Tooltip.d.ts +24 -1
- package/dist/Tooltip.js +2 -2
- package/dist/Tooltip.js.map +1 -1
- package/dist/Unifier.d.ts +1 -1
- package/dist/Unifier.js +14 -11
- package/dist/Unifier.js.map +1 -1
- package/dist/Utilities.d.ts +8 -8
- package/dist/Utilities.js +12 -14
- package/dist/Utilities.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/signUp/PasswordRequirements.js +3 -3
- package/dist/signUp/PasswordRequirements.js.map +1 -1
- package/dist/table/TableHeaderCell.js +1 -9
- package/dist/table/TableHeaderCell.js.map +1 -1
- package/dist/table/tableContext.d.ts +1 -1
- package/dist/table/tableContext.js +2 -2
- package/dist/table/tableContext.js.map +1 -1
- package/package.json +1 -1
- package/src/ActionSheet.tsx +2 -2
- package/src/Banner.test.tsx +71 -0
- package/src/Common.ts +10 -4
- package/src/ConsentFormScreen.test.tsx +22 -0
- package/src/ConsentFormScreen.tsx +9 -3
- package/src/DataTable.test.tsx +217 -0
- package/src/DateUtilities.tsx +37 -38
- package/src/HeightActionSheet.test.tsx +16 -0
- package/src/HeightField.test.tsx +106 -1
- package/src/MediaQuery.ts +8 -8
- package/src/MobileAddressAutoComplete.test.tsx +20 -1
- package/src/Page.test.tsx +28 -0
- package/src/Page.tsx +17 -2
- package/src/PickerSelect.tsx +3 -3
- package/src/TapToEdit.test.tsx +31 -0
- package/src/TapToEdit.tsx +2 -3
- package/src/ToastNotifications.test.tsx +738 -0
- package/src/ToastNotifications.tsx +2 -2
- package/src/Tooltip.test.tsx +587 -2
- package/src/Tooltip.tsx +2 -2
- package/src/Unifier.ts +14 -11
- package/src/Utilities.tsx +14 -16
- package/src/WebAddressAutocomplete.test.tsx +138 -0
- package/src/WebDropdownMenu.test.tsx +23 -0
- package/src/index.tsx +1 -1
- package/src/login/LoginScreen.test.tsx +23 -1
- package/src/signUp/PasswordRequirements.tsx +9 -6
- package/src/signUp/__snapshots__/PasswordRequirements.test.tsx.snap +50 -2
- package/src/signUp/__snapshots__/SignUpScreen.test.tsx.snap +25 -1
- package/src/table/TableHeaderCell.tsx +8 -11
- package/src/table/TableRow.test.tsx +31 -1
- package/src/table/__snapshots__/TableHeaderCell.test.tsx.snap +2 -0
- package/src/table/tableContext.tsx +2 -2
- package/src/useStoredState.test.tsx +47 -0
|
@@ -61,7 +61,7 @@ import {
|
|
|
61
61
|
// useDimensions hook
|
|
62
62
|
// ============================================================================
|
|
63
63
|
|
|
64
|
-
|
|
64
|
+
const useDimensions = () => {
|
|
65
65
|
const [dimensions, setDimensions] = useState(Dimensions.get("window"));
|
|
66
66
|
|
|
67
67
|
const onChange = useCallback(({window}: {window: ScaledSize}) => {
|
|
@@ -77,7 +77,7 @@ function useDimensions() {
|
|
|
77
77
|
}, [onChange]);
|
|
78
78
|
|
|
79
79
|
return dimensions;
|
|
80
|
-
}
|
|
80
|
+
};
|
|
81
81
|
|
|
82
82
|
// ============================================================================
|
|
83
83
|
// Toast Options and Props
|
package/src/Tooltip.test.tsx
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {beforeAll, describe, expect, it, mock} from "bun:test";
|
|
1
|
+
import {afterEach, beforeAll, beforeEach, describe, expect, it, mock} from "bun:test";
|
|
2
2
|
import {act} from "@testing-library/react-native";
|
|
3
3
|
|
|
4
4
|
import {Text} from "./Text";
|
|
5
|
-
import {Tooltip} from "./Tooltip";
|
|
5
|
+
import {Arrow, getTooltipPosition, Tooltip} from "./Tooltip";
|
|
6
6
|
import {renderWithTheme} from "./test-utils";
|
|
7
7
|
|
|
8
8
|
// Minimal shape of the tree returned by toJSON() that we rely on here.
|
|
@@ -19,6 +19,15 @@ interface TestNode {
|
|
|
19
19
|
children: null | Array<TestNode | string>;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
type MeasureCallback = (
|
|
23
|
+
x: number,
|
|
24
|
+
y: number,
|
|
25
|
+
width: number,
|
|
26
|
+
height: number,
|
|
27
|
+
pageX: number,
|
|
28
|
+
pageY: number
|
|
29
|
+
) => void;
|
|
30
|
+
|
|
22
31
|
beforeAll(() => {
|
|
23
32
|
globalThis.requestAnimationFrame = (callback: FrameRequestCallback) => {
|
|
24
33
|
return setTimeout(() => callback(Date.now()), 0) as unknown as number;
|
|
@@ -267,6 +276,198 @@ describe("Tooltip", () => {
|
|
|
267
276
|
}
|
|
268
277
|
});
|
|
269
278
|
|
|
279
|
+
it("getTooltipPosition returns empty when not measured", () => {
|
|
280
|
+
const result = getTooltipPosition({
|
|
281
|
+
children: {},
|
|
282
|
+
measured: false,
|
|
283
|
+
tooltip: {},
|
|
284
|
+
});
|
|
285
|
+
expect(result).toEqual({});
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
it("getTooltipPosition places tooltip at top (default) when space allows", () => {
|
|
289
|
+
const result = getTooltipPosition({
|
|
290
|
+
children: {height: 40, pageX: 200, pageY: 300, width: 100},
|
|
291
|
+
idealPosition: "top",
|
|
292
|
+
measured: true,
|
|
293
|
+
tooltip: {height: 30, width: 150, x: 0, y: 0},
|
|
294
|
+
});
|
|
295
|
+
expect(result).toHaveProperty("finalPosition", "top");
|
|
296
|
+
expect(result).toHaveProperty("top");
|
|
297
|
+
expect(result).toHaveProperty("left");
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
it("getTooltipPosition places tooltip at bottom when specified", () => {
|
|
301
|
+
const result = getTooltipPosition({
|
|
302
|
+
children: {height: 40, pageX: 200, pageY: 100, width: 100},
|
|
303
|
+
idealPosition: "bottom",
|
|
304
|
+
measured: true,
|
|
305
|
+
tooltip: {height: 30, width: 150, x: 0, y: 0},
|
|
306
|
+
});
|
|
307
|
+
expect(result).toHaveProperty("finalPosition", "bottom");
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
it("getTooltipPosition places tooltip at left when space allows", () => {
|
|
311
|
+
const result = getTooltipPosition({
|
|
312
|
+
children: {height: 40, pageX: 400, pageY: 200, width: 100},
|
|
313
|
+
idealPosition: "left",
|
|
314
|
+
measured: true,
|
|
315
|
+
tooltip: {height: 30, width: 150, x: 0, y: 0},
|
|
316
|
+
});
|
|
317
|
+
expect(result).toHaveProperty("finalPosition", "left");
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
it("getTooltipPosition places tooltip at right when specified", () => {
|
|
321
|
+
const result = getTooltipPosition({
|
|
322
|
+
children: {height: 40, pageX: 50, pageY: 200, width: 100},
|
|
323
|
+
idealPosition: "right",
|
|
324
|
+
measured: true,
|
|
325
|
+
tooltip: {height: 30, width: 150, x: 0, y: 0},
|
|
326
|
+
});
|
|
327
|
+
expect(result).toHaveProperty("finalPosition", "right");
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
it("getTooltipPosition falls back to bottom when top overflows", () => {
|
|
331
|
+
const result = getTooltipPosition({
|
|
332
|
+
children: {height: 40, pageX: 200, pageY: 5, width: 100},
|
|
333
|
+
idealPosition: "top",
|
|
334
|
+
measured: true,
|
|
335
|
+
tooltip: {height: 30, width: 150, x: 0, y: 0},
|
|
336
|
+
});
|
|
337
|
+
expect(result).toHaveProperty("finalPosition", "bottom");
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
it("getTooltipPosition falls back to top when bottom overflows", () => {
|
|
341
|
+
const {Dimensions} = require("react-native");
|
|
342
|
+
const origGet = Dimensions.get;
|
|
343
|
+
Dimensions.get = () => ({fontScale: 1, height: 200, scale: 1, width: 800});
|
|
344
|
+
try {
|
|
345
|
+
const result = getTooltipPosition({
|
|
346
|
+
children: {height: 40, pageX: 200, pageY: 160, width: 100},
|
|
347
|
+
idealPosition: "bottom",
|
|
348
|
+
measured: true,
|
|
349
|
+
tooltip: {height: 30, width: 150, x: 0, y: 0},
|
|
350
|
+
});
|
|
351
|
+
expect(result).toHaveProperty("finalPosition", "top");
|
|
352
|
+
} finally {
|
|
353
|
+
Dimensions.get = origGet;
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
it("getTooltipPosition falls back to bottom when right overflows", () => {
|
|
358
|
+
const {Dimensions} = require("react-native");
|
|
359
|
+
const origGet = Dimensions.get;
|
|
360
|
+
Dimensions.get = () => ({fontScale: 1, height: 800, scale: 1, width: 300});
|
|
361
|
+
try {
|
|
362
|
+
const result = getTooltipPosition({
|
|
363
|
+
children: {height: 40, pageX: 200, pageY: 200, width: 100},
|
|
364
|
+
idealPosition: "right",
|
|
365
|
+
measured: true,
|
|
366
|
+
tooltip: {height: 30, width: 150, x: 0, y: 0},
|
|
367
|
+
});
|
|
368
|
+
// Fallback order is: bottom -> top -> left -> right
|
|
369
|
+
expect(result).toHaveProperty("finalPosition", "bottom");
|
|
370
|
+
} finally {
|
|
371
|
+
Dimensions.get = origGet;
|
|
372
|
+
}
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
it("getTooltipPosition falls back to left when right, bottom, and top overflow", () => {
|
|
376
|
+
const {Dimensions} = require("react-native");
|
|
377
|
+
const origGet = Dimensions.get;
|
|
378
|
+
Dimensions.get = () => ({fontScale: 1, height: 80, scale: 1, width: 300});
|
|
379
|
+
try {
|
|
380
|
+
const result = getTooltipPosition({
|
|
381
|
+
children: {height: 40, pageX: 200, pageY: 20, width: 40},
|
|
382
|
+
idealPosition: "right",
|
|
383
|
+
measured: true,
|
|
384
|
+
tooltip: {height: 30, width: 150, x: 0, y: 0},
|
|
385
|
+
});
|
|
386
|
+
expect(result).toHaveProperty("finalPosition", "left");
|
|
387
|
+
} finally {
|
|
388
|
+
Dimensions.get = origGet;
|
|
389
|
+
}
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
it("getTooltipPosition falls back to right when all other directions overflow", () => {
|
|
393
|
+
const {Dimensions} = require("react-native");
|
|
394
|
+
const origGet = Dimensions.get;
|
|
395
|
+
Dimensions.get = () => ({fontScale: 1, height: 50, scale: 1, width: 50});
|
|
396
|
+
try {
|
|
397
|
+
const result = getTooltipPosition({
|
|
398
|
+
children: {height: 40, pageX: 5, pageY: 5, width: 40},
|
|
399
|
+
idealPosition: "top",
|
|
400
|
+
measured: true,
|
|
401
|
+
tooltip: {height: 30, width: 150, x: 0, y: 0},
|
|
402
|
+
});
|
|
403
|
+
expect(result).toHaveProperty("finalPosition", "right");
|
|
404
|
+
} finally {
|
|
405
|
+
Dimensions.get = origGet;
|
|
406
|
+
}
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
it("getTooltipPosition with no idealPosition defaults to top", () => {
|
|
410
|
+
const result = getTooltipPosition({
|
|
411
|
+
children: {height: 40, pageX: 200, pageY: 300, width: 100},
|
|
412
|
+
measured: true,
|
|
413
|
+
tooltip: {height: 30, width: 150, x: 0, y: 0},
|
|
414
|
+
});
|
|
415
|
+
expect(result).toHaveProperty("finalPosition", "top");
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
it("getTooltipPosition falls back when left placement overflows", () => {
|
|
419
|
+
const result = getTooltipPosition({
|
|
420
|
+
children: {height: 40, pageX: 10, pageY: 200, width: 100},
|
|
421
|
+
idealPosition: "left",
|
|
422
|
+
measured: true,
|
|
423
|
+
tooltip: {height: 30, width: 150, x: 0, y: 0},
|
|
424
|
+
});
|
|
425
|
+
// Left overflows, should fall back to bottom (first available)
|
|
426
|
+
expect(result).toHaveProperty("finalPosition");
|
|
427
|
+
const pos = (result as {finalPosition: string}).finalPosition;
|
|
428
|
+
expect(["top", "bottom", "right"]).toContain(pos);
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
it("Arrow renders for each position", () => {
|
|
432
|
+
const positions = ["top", "bottom", "left", "right"] as const;
|
|
433
|
+
for (const position of positions) {
|
|
434
|
+
const {toJSON} = renderWithTheme(<Arrow color="#333" position={position} />);
|
|
435
|
+
expect(toJSON()).toBeTruthy();
|
|
436
|
+
}
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
it("exercises handleClick to hide visible tooltip on web press", async () => {
|
|
440
|
+
const {queryByTestId, toJSON} = renderWithTheme(
|
|
441
|
+
<Tooltip text="Click hides">
|
|
442
|
+
<Text>Click me</Text>
|
|
443
|
+
</Tooltip>
|
|
444
|
+
);
|
|
445
|
+
|
|
446
|
+
const tree = toJSON() as TestNode;
|
|
447
|
+
const root = tree.children?.[0] as TestNode;
|
|
448
|
+
|
|
449
|
+
// Show the tooltip
|
|
450
|
+
await act(async () => {
|
|
451
|
+
root.props.onPointerEnter?.();
|
|
452
|
+
});
|
|
453
|
+
await act(async () => {
|
|
454
|
+
await new Promise((resolve) => setTimeout(resolve, 900));
|
|
455
|
+
});
|
|
456
|
+
expect(queryByTestId("tooltip-container")).toBeTruthy();
|
|
457
|
+
|
|
458
|
+
// Find the wrapper and trigger onPress (web click)
|
|
459
|
+
const treeAfter = toJSON() as TestNode;
|
|
460
|
+
const wrapper = (treeAfter.children as Array<TestNode | string>)[
|
|
461
|
+
(treeAfter.children as Array<TestNode | string>).length - 1
|
|
462
|
+
] as TestNode;
|
|
463
|
+
|
|
464
|
+
if (wrapper.props && "onPress" in wrapper.props) {
|
|
465
|
+
await act(async () => {
|
|
466
|
+
(wrapper.props as {onPress?: () => void}).onPress?.();
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
});
|
|
470
|
+
|
|
270
471
|
it("renders tooltip with arrow at all idealPositions", async () => {
|
|
271
472
|
const positions: Array<"top" | "bottom" | "left" | "right"> = [
|
|
272
473
|
"top",
|
|
@@ -301,4 +502,388 @@ describe("Tooltip", () => {
|
|
|
301
502
|
// No assertions needed - just ensuring no crashes on unmount.
|
|
302
503
|
expect(true).toBe(true);
|
|
303
504
|
});
|
|
505
|
+
|
|
506
|
+
it("hides tooltip when pressing on the tooltip container", async () => {
|
|
507
|
+
const {queryByTestId, toJSON, getByTestId} = renderWithTheme(
|
|
508
|
+
<Tooltip text="Click to hide">
|
|
509
|
+
<Text>Hover me</Text>
|
|
510
|
+
</Tooltip>
|
|
511
|
+
);
|
|
512
|
+
|
|
513
|
+
const tree = toJSON() as TestNode;
|
|
514
|
+
const root = tree.children?.[0] as TestNode;
|
|
515
|
+
|
|
516
|
+
await act(async () => {
|
|
517
|
+
root.props.onPointerEnter?.();
|
|
518
|
+
});
|
|
519
|
+
await act(async () => {
|
|
520
|
+
await new Promise((resolve) => setTimeout(resolve, 900));
|
|
521
|
+
});
|
|
522
|
+
expect(queryByTestId("tooltip-container")).toBeTruthy();
|
|
523
|
+
|
|
524
|
+
const {fireEvent} = await import("@testing-library/react-native");
|
|
525
|
+
await act(async () => {
|
|
526
|
+
fireEvent.press(getByTestId("tooltip-container"));
|
|
527
|
+
});
|
|
528
|
+
expect(queryByTestId("tooltip-container")).toBeNull();
|
|
529
|
+
});
|
|
530
|
+
|
|
531
|
+
it("handleClick does nothing when tooltip is not visible", async () => {
|
|
532
|
+
const {queryByTestId, toJSON} = renderWithTheme(
|
|
533
|
+
<Tooltip text="Click test">
|
|
534
|
+
<Text>Click me</Text>
|
|
535
|
+
</Tooltip>
|
|
536
|
+
);
|
|
537
|
+
|
|
538
|
+
const tree = toJSON() as TestNode;
|
|
539
|
+
const root = tree.children?.[0] as TestNode;
|
|
540
|
+
|
|
541
|
+
// Call onPress when tooltip is not visible (no-op)
|
|
542
|
+
await act(async () => {
|
|
543
|
+
(root.props as {onPress?: () => void}).onPress?.();
|
|
544
|
+
});
|
|
545
|
+
expect(queryByTestId("tooltip-container")).toBeNull();
|
|
546
|
+
});
|
|
547
|
+
|
|
548
|
+
it("handles onLayout with measure callback and sets position", async () => {
|
|
549
|
+
const {queryByTestId, toJSON, UNSAFE_getAllByType} = renderWithTheme(
|
|
550
|
+
<Tooltip idealPosition="top" includeArrow text="Layout position test">
|
|
551
|
+
<Text>Trigger</Text>
|
|
552
|
+
</Tooltip>
|
|
553
|
+
);
|
|
554
|
+
|
|
555
|
+
const tree = toJSON() as TestNode;
|
|
556
|
+
const root = tree.children?.[0] as TestNode;
|
|
557
|
+
|
|
558
|
+
// Show the tooltip first
|
|
559
|
+
await act(async () => {
|
|
560
|
+
root.props.onTouchStart?.({nativeEvent: {}});
|
|
561
|
+
});
|
|
562
|
+
await act(async () => {
|
|
563
|
+
await new Promise((resolve) => setTimeout(resolve, 150));
|
|
564
|
+
});
|
|
565
|
+
expect(queryByTestId("tooltip-container")).toBeTruthy();
|
|
566
|
+
|
|
567
|
+
const {View: ViewComp} = await import("react-native");
|
|
568
|
+
const allViews = UNSAFE_getAllByType(ViewComp);
|
|
569
|
+
for (const v of allViews) {
|
|
570
|
+
const props = v.props as TestNode["props"];
|
|
571
|
+
if (props.onLayout) {
|
|
572
|
+
await act(async () => {
|
|
573
|
+
props.onLayout?.({
|
|
574
|
+
nativeEvent: {
|
|
575
|
+
layout: {height: 40, width: 200, x: 0, y: 0},
|
|
576
|
+
},
|
|
577
|
+
});
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
it("exercises getTooltipPosition with all ideal positions", async () => {
|
|
584
|
+
const positions: Array<"top" | "bottom" | "left" | "right"> = [
|
|
585
|
+
"top",
|
|
586
|
+
"bottom",
|
|
587
|
+
"left",
|
|
588
|
+
"right",
|
|
589
|
+
];
|
|
590
|
+
|
|
591
|
+
for (const pos of positions) {
|
|
592
|
+
const {queryByTestId, toJSON, UNSAFE_getAllByType, unmount} = renderWithTheme(
|
|
593
|
+
<Tooltip idealPosition={pos} includeArrow text={`${pos} test`}>
|
|
594
|
+
<Text>Position {pos}</Text>
|
|
595
|
+
</Tooltip>
|
|
596
|
+
);
|
|
597
|
+
|
|
598
|
+
const tree = toJSON() as TestNode;
|
|
599
|
+
const root = tree.children?.[0] as TestNode;
|
|
600
|
+
|
|
601
|
+
await act(async () => {
|
|
602
|
+
root.props.onTouchStart?.({nativeEvent: {}});
|
|
603
|
+
});
|
|
604
|
+
await act(async () => {
|
|
605
|
+
await new Promise((resolve) => setTimeout(resolve, 150));
|
|
606
|
+
});
|
|
607
|
+
expect(queryByTestId("tooltip-container")).toBeTruthy();
|
|
608
|
+
|
|
609
|
+
const {View: ViewComp} = await import("react-native");
|
|
610
|
+
const allViews = UNSAFE_getAllByType(ViewComp);
|
|
611
|
+
for (const v of allViews) {
|
|
612
|
+
const props = v.props as TestNode["props"];
|
|
613
|
+
if (props.onLayout) {
|
|
614
|
+
await act(async () => {
|
|
615
|
+
props.onLayout?.({
|
|
616
|
+
nativeEvent: {
|
|
617
|
+
layout: {height: 30, width: 100, x: 50, y: 50},
|
|
618
|
+
},
|
|
619
|
+
});
|
|
620
|
+
});
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
unmount();
|
|
624
|
+
}
|
|
625
|
+
});
|
|
626
|
+
|
|
627
|
+
it("renders arrow styles for all positions when tooltip is shown with arrow", async () => {
|
|
628
|
+
const positions: Array<"top" | "bottom" | "left" | "right"> = [
|
|
629
|
+
"top",
|
|
630
|
+
"bottom",
|
|
631
|
+
"left",
|
|
632
|
+
"right",
|
|
633
|
+
];
|
|
634
|
+
|
|
635
|
+
for (const pos of positions) {
|
|
636
|
+
const {queryByTestId, toJSON, unmount} = renderWithTheme(
|
|
637
|
+
<Tooltip idealPosition={pos} includeArrow text={`Arrow ${pos}`}>
|
|
638
|
+
<Text>Arrow</Text>
|
|
639
|
+
</Tooltip>
|
|
640
|
+
);
|
|
641
|
+
|
|
642
|
+
const tree = toJSON() as TestNode;
|
|
643
|
+
const root = tree.children?.[0] as TestNode;
|
|
644
|
+
|
|
645
|
+
await act(async () => {
|
|
646
|
+
root.props.onPointerEnter?.();
|
|
647
|
+
});
|
|
648
|
+
await act(async () => {
|
|
649
|
+
await new Promise((resolve) => setTimeout(resolve, 900));
|
|
650
|
+
});
|
|
651
|
+
expect(queryByTestId("tooltip-container")).toBeTruthy();
|
|
652
|
+
unmount();
|
|
653
|
+
}
|
|
654
|
+
});
|
|
655
|
+
|
|
656
|
+
it("mobilePressProps fires children onClick when not touched", async () => {
|
|
657
|
+
const onClick = mock(() => {});
|
|
658
|
+
const TestChild: React.FC<{onClick?: () => void}> = () => <Text>Pressable child</Text>;
|
|
659
|
+
|
|
660
|
+
const {toJSON} = renderWithTheme(
|
|
661
|
+
<Tooltip text="Mobile test">
|
|
662
|
+
<TestChild onClick={onClick} />
|
|
663
|
+
</Tooltip>
|
|
664
|
+
);
|
|
665
|
+
|
|
666
|
+
const tree = toJSON() as TestNode;
|
|
667
|
+
const root = tree.children?.[0] as TestNode;
|
|
668
|
+
|
|
669
|
+
// Fire onPress (mobilePressProps) without having touched first
|
|
670
|
+
await act(async () => {
|
|
671
|
+
(root.props as {onPress?: () => void}).onPress?.();
|
|
672
|
+
});
|
|
673
|
+
expect(onClick).toHaveBeenCalled();
|
|
674
|
+
});
|
|
675
|
+
|
|
676
|
+
it("getArrowContainerStyle returns empty when includeArrow is false", async () => {
|
|
677
|
+
const {queryByTestId, toJSON} = renderWithTheme(
|
|
678
|
+
<Tooltip text="No arrow test">
|
|
679
|
+
<Text>No arrow</Text>
|
|
680
|
+
</Tooltip>
|
|
681
|
+
);
|
|
682
|
+
|
|
683
|
+
const tree = toJSON() as TestNode;
|
|
684
|
+
const root = tree.children?.[0] as TestNode;
|
|
685
|
+
|
|
686
|
+
await act(async () => {
|
|
687
|
+
root.props.onPointerEnter?.();
|
|
688
|
+
});
|
|
689
|
+
await act(async () => {
|
|
690
|
+
await new Promise((resolve) => setTimeout(resolve, 900));
|
|
691
|
+
});
|
|
692
|
+
expect(queryByTestId("tooltip-container")).toBeTruthy();
|
|
693
|
+
});
|
|
694
|
+
|
|
695
|
+
describe("web platform behavior", () => {
|
|
696
|
+
let Platform: {OS: string};
|
|
697
|
+
|
|
698
|
+
beforeEach(async () => {
|
|
699
|
+
const rn = await import("react-native");
|
|
700
|
+
Platform = rn.Platform;
|
|
701
|
+
Platform.OS = "web";
|
|
702
|
+
});
|
|
703
|
+
|
|
704
|
+
afterEach(() => {
|
|
705
|
+
Platform.OS = "ios";
|
|
706
|
+
});
|
|
707
|
+
|
|
708
|
+
it("renders Arrow component when isWeb and includeArrow", async () => {
|
|
709
|
+
const {queryByTestId, toJSON} = renderWithTheme(
|
|
710
|
+
<Tooltip idealPosition="top" includeArrow text="Web arrow">
|
|
711
|
+
<Text>Arrow child</Text>
|
|
712
|
+
</Tooltip>
|
|
713
|
+
);
|
|
714
|
+
|
|
715
|
+
const tree = toJSON() as TestNode;
|
|
716
|
+
const root = tree.children?.[0] as TestNode;
|
|
717
|
+
|
|
718
|
+
await act(async () => {
|
|
719
|
+
root.props.onPointerEnter?.();
|
|
720
|
+
});
|
|
721
|
+
await act(async () => {
|
|
722
|
+
await new Promise((resolve) => setTimeout(resolve, 900));
|
|
723
|
+
});
|
|
724
|
+
expect(queryByTestId("tooltip-container")).toBeTruthy();
|
|
725
|
+
});
|
|
726
|
+
|
|
727
|
+
it("renders Arrow for all positions on web", async () => {
|
|
728
|
+
const positions: Array<"top" | "bottom" | "left" | "right"> = [
|
|
729
|
+
"top",
|
|
730
|
+
"bottom",
|
|
731
|
+
"left",
|
|
732
|
+
"right",
|
|
733
|
+
];
|
|
734
|
+
for (const pos of positions) {
|
|
735
|
+
const {queryByTestId, toJSON, unmount} = renderWithTheme(
|
|
736
|
+
<Tooltip idealPosition={pos} includeArrow text={`Web ${pos}`}>
|
|
737
|
+
<Text>{pos}</Text>
|
|
738
|
+
</Tooltip>
|
|
739
|
+
);
|
|
740
|
+
|
|
741
|
+
const tree = toJSON() as TestNode;
|
|
742
|
+
const root = tree.children?.[0] as TestNode;
|
|
743
|
+
|
|
744
|
+
await act(async () => {
|
|
745
|
+
root.props.onTouchStart?.({nativeEvent: {}});
|
|
746
|
+
});
|
|
747
|
+
await act(async () => {
|
|
748
|
+
await new Promise((resolve) => setTimeout(resolve, 150));
|
|
749
|
+
});
|
|
750
|
+
expect(queryByTestId("tooltip-container")).toBeTruthy();
|
|
751
|
+
unmount();
|
|
752
|
+
}
|
|
753
|
+
});
|
|
754
|
+
|
|
755
|
+
it("handleClick hides tooltip on web when visible", async () => {
|
|
756
|
+
const {queryByTestId, toJSON} = renderWithTheme(
|
|
757
|
+
<Tooltip text="Web click hide">
|
|
758
|
+
<Text>Click me</Text>
|
|
759
|
+
</Tooltip>
|
|
760
|
+
);
|
|
761
|
+
|
|
762
|
+
const tree = toJSON() as TestNode;
|
|
763
|
+
const root = tree.children?.[0] as TestNode;
|
|
764
|
+
|
|
765
|
+
await act(async () => {
|
|
766
|
+
root.props.onPointerEnter?.();
|
|
767
|
+
});
|
|
768
|
+
await act(async () => {
|
|
769
|
+
await new Promise((resolve) => setTimeout(resolve, 900));
|
|
770
|
+
});
|
|
771
|
+
expect(queryByTestId("tooltip-container")).toBeTruthy();
|
|
772
|
+
|
|
773
|
+
// On web, onPress is handleClick which hides tooltip when visible
|
|
774
|
+
const treeAfter = toJSON() as TestNode;
|
|
775
|
+
const wrapper = (treeAfter.children as TestNode[]).find(
|
|
776
|
+
(c: TestNode) => c.props && (c.props as {hitSlop?: object}).hitSlop !== undefined
|
|
777
|
+
) as TestNode | undefined;
|
|
778
|
+
await act(async () => {
|
|
779
|
+
(wrapper?.props as {onPress?: () => void}).onPress?.();
|
|
780
|
+
});
|
|
781
|
+
expect(queryByTestId("tooltip-container")).toBeNull();
|
|
782
|
+
});
|
|
783
|
+
|
|
784
|
+
it("exercises measure callback and getTooltipPosition for all positions", async () => {
|
|
785
|
+
const positions: Array<"top" | "bottom" | "left" | "right"> = [
|
|
786
|
+
"top",
|
|
787
|
+
"bottom",
|
|
788
|
+
"left",
|
|
789
|
+
"right",
|
|
790
|
+
];
|
|
791
|
+
|
|
792
|
+
for (const pos of positions) {
|
|
793
|
+
const {toJSON, UNSAFE_getAllByType, unmount} = renderWithTheme(
|
|
794
|
+
<Tooltip idealPosition={pos} includeArrow text={`Measure ${pos}`}>
|
|
795
|
+
<Text>{pos}</Text>
|
|
796
|
+
</Tooltip>
|
|
797
|
+
);
|
|
798
|
+
|
|
799
|
+
const tree = toJSON() as TestNode;
|
|
800
|
+
const root = tree.children?.[0] as TestNode;
|
|
801
|
+
|
|
802
|
+
await act(async () => {
|
|
803
|
+
root.props.onTouchStart?.({nativeEvent: {}});
|
|
804
|
+
});
|
|
805
|
+
await act(async () => {
|
|
806
|
+
await new Promise((resolve) => setTimeout(resolve, 150));
|
|
807
|
+
});
|
|
808
|
+
|
|
809
|
+
// Find the ref-holding View (has hitSlop) and inject measure via fiber ref
|
|
810
|
+
const {View: ViewComp} = await import("react-native");
|
|
811
|
+
const allViews = UNSAFE_getAllByType(ViewComp);
|
|
812
|
+
for (const v of allViews) {
|
|
813
|
+
if (!(v.props as {hitSlop?: object}).hitSlop) {
|
|
814
|
+
continue;
|
|
815
|
+
}
|
|
816
|
+
const fiber = (v as unknown as {_fiber?: {ref?: {current: unknown}}})._fiber;
|
|
817
|
+
if (fiber?.ref && typeof fiber.ref === "object") {
|
|
818
|
+
const pageX = pos === "left" ? 400 : pos === "right" ? 50 : 200;
|
|
819
|
+
const pageY = pos === "top" ? 400 : 100;
|
|
820
|
+
fiber.ref.current = {
|
|
821
|
+
measure: (cb: MeasureCallback) => {
|
|
822
|
+
cb(0, 0, 100, 40, pageX, pageY);
|
|
823
|
+
},
|
|
824
|
+
};
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
// Trigger onLayout to invoke measure and getTooltipPosition
|
|
829
|
+
for (const v of allViews) {
|
|
830
|
+
const props = v.props as TestNode["props"];
|
|
831
|
+
if (props.onLayout) {
|
|
832
|
+
await act(async () => {
|
|
833
|
+
props.onLayout?.({
|
|
834
|
+
nativeEvent: {layout: {height: 30, width: 80, x: 0, y: 0}},
|
|
835
|
+
});
|
|
836
|
+
});
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
unmount();
|
|
840
|
+
}
|
|
841
|
+
});
|
|
842
|
+
|
|
843
|
+
it("getTooltipPosition fallback when all positions overflow", async () => {
|
|
844
|
+
const {toJSON, UNSAFE_getAllByType, unmount} = renderWithTheme(
|
|
845
|
+
<Tooltip idealPosition="top" text="Overflow">
|
|
846
|
+
<Text>Overflow</Text>
|
|
847
|
+
</Tooltip>
|
|
848
|
+
);
|
|
849
|
+
|
|
850
|
+
const tree = toJSON() as TestNode;
|
|
851
|
+
const root = tree.children?.[0] as TestNode;
|
|
852
|
+
|
|
853
|
+
await act(async () => {
|
|
854
|
+
root.props.onTouchStart?.({nativeEvent: {}});
|
|
855
|
+
});
|
|
856
|
+
await act(async () => {
|
|
857
|
+
await new Promise((resolve) => setTimeout(resolve, 150));
|
|
858
|
+
});
|
|
859
|
+
|
|
860
|
+
const {View: ViewComp} = await import("react-native");
|
|
861
|
+
const allViews = UNSAFE_getAllByType(ViewComp);
|
|
862
|
+
for (const v of allViews) {
|
|
863
|
+
if (!(v.props as {hitSlop?: object}).hitSlop) {
|
|
864
|
+
continue;
|
|
865
|
+
}
|
|
866
|
+
const fiber = (v as unknown as {_fiber?: {ref?: {current: unknown}}})._fiber;
|
|
867
|
+
if (fiber?.ref && typeof fiber.ref === "object") {
|
|
868
|
+
fiber.ref.current = {
|
|
869
|
+
measure: (cb: MeasureCallback) => {
|
|
870
|
+
cb(0, 0, 900, 900, 0, 0);
|
|
871
|
+
},
|
|
872
|
+
};
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
for (const v of allViews) {
|
|
877
|
+
const props = v.props as TestNode["props"];
|
|
878
|
+
if (props.onLayout) {
|
|
879
|
+
await act(async () => {
|
|
880
|
+
props.onLayout?.({
|
|
881
|
+
nativeEvent: {layout: {height: 900, width: 900, x: 0, y: 0}},
|
|
882
|
+
});
|
|
883
|
+
});
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
unmount();
|
|
887
|
+
});
|
|
888
|
+
});
|
|
304
889
|
});
|
package/src/Tooltip.tsx
CHANGED
|
@@ -39,7 +39,7 @@ interface ChildrenProps {
|
|
|
39
39
|
onHoverOut?: () => void;
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
const getTooltipPosition = ({
|
|
42
|
+
export const getTooltipPosition = ({
|
|
43
43
|
children,
|
|
44
44
|
tooltip,
|
|
45
45
|
measured,
|
|
@@ -117,7 +117,7 @@ const getTooltipPosition = ({
|
|
|
117
117
|
}
|
|
118
118
|
};
|
|
119
119
|
|
|
120
|
-
const Arrow: FC<{position: TooltipPosition; color: string}> = ({position, color}) => {
|
|
120
|
+
export const Arrow: FC<{position: TooltipPosition; color: string}> = ({position, color}) => {
|
|
121
121
|
const getArrowStyle = (): ViewStyle => {
|
|
122
122
|
const arrowStyles = {
|
|
123
123
|
bottom: {
|
package/src/Unifier.ts
CHANGED
|
@@ -18,13 +18,18 @@ export type PlatformOS = "ios" | "android" | "web";
|
|
|
18
18
|
type Luminance = "light" | "lighter" | "dark" | "darker";
|
|
19
19
|
|
|
20
20
|
// Changes a color luminance
|
|
21
|
-
export
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
21
|
+
export const changeColorLuminance = (hex: string, luminanceChange: Luminance) => {
|
|
22
|
+
let normalizedHex = String(hex).replace(/[^0-9a-f]/gi, "");
|
|
23
|
+
if (normalizedHex.length === 3) {
|
|
24
|
+
normalizedHex =
|
|
25
|
+
normalizedHex[0] +
|
|
26
|
+
normalizedHex[0] +
|
|
27
|
+
normalizedHex[1] +
|
|
28
|
+
normalizedHex[1] +
|
|
29
|
+
normalizedHex[2] +
|
|
30
|
+
normalizedHex[2];
|
|
31
|
+
} else if (normalizedHex.length !== 6) {
|
|
32
|
+
throw new Error(`Invalid color hex: ${normalizedHex}`);
|
|
28
33
|
}
|
|
29
34
|
let luminance: number;
|
|
30
35
|
switch (luminanceChange) {
|
|
@@ -44,19 +49,17 @@ export function changeColorLuminance(hex: string, luminanceChange: Luminance) {
|
|
|
44
49
|
throw new Error(`Cannot change luminance to ${luminanceChange}`);
|
|
45
50
|
}
|
|
46
51
|
|
|
47
|
-
// Convert to decimal and change luminosity
|
|
48
52
|
let rgb = "#";
|
|
49
53
|
for (let i = 0; i < 3; i++) {
|
|
50
|
-
const decimal = parseInt(
|
|
54
|
+
const decimal = parseInt(normalizedHex.substr(i * 2, 2), 16);
|
|
51
55
|
const appliedLuminance = Math.round(
|
|
52
56
|
Math.min(Math.max(0, decimal + decimal * luminance), 255)
|
|
53
57
|
).toString(16);
|
|
54
|
-
// 0 pad, if necessary.
|
|
55
58
|
rgb += `00${appliedLuminance}`.substr(appliedLuminance.length);
|
|
56
59
|
}
|
|
57
60
|
|
|
58
61
|
return rgb;
|
|
59
|
-
}
|
|
62
|
+
};
|
|
60
63
|
|
|
61
64
|
class UnifierClass {
|
|
62
65
|
private _web = false;
|