@terreno/ui 58.0.5 → 58.1.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/CHANGELOG.md +58 -0
- package/dist/AiSuggestionBox.js +3 -3
- package/dist/AiSuggestionBox.js.map +1 -1
- package/dist/Common.d.ts +38 -0
- package/dist/Common.d.ts.map +1 -1
- package/dist/Common.js.map +1 -1
- package/dist/DataTable.d.ts.map +1 -1
- package/dist/DataTable.js +3 -1
- package/dist/DataTable.js.map +1 -1
- package/dist/DraggableList.d.ts +7 -4
- package/dist/DraggableList.d.ts.map +1 -1
- package/dist/DraggableList.js +206 -4
- package/dist/DraggableList.js.map +1 -1
- package/dist/FeedbackCommentField.d.ts +8 -0
- package/dist/FeedbackCommentField.d.ts.map +1 -0
- package/dist/FeedbackCommentField.js +39 -0
- package/dist/FeedbackCommentField.js.map +1 -0
- package/dist/GPTChat.d.ts +15 -1
- package/dist/GPTChat.d.ts.map +1 -1
- package/dist/GPTChat.js +148 -43
- package/dist/GPTChat.js.map +1 -1
- package/dist/LabeledCheckBox.d.ts +3 -2
- package/dist/LabeledCheckBox.d.ts.map +1 -1
- package/dist/LabeledCheckBox.js +3 -2
- package/dist/LabeledCheckBox.js.map +1 -1
- package/dist/TextField.d.ts.map +1 -1
- package/dist/TextField.js +10 -7
- package/dist/TextField.js.map +1 -1
- package/dist/ThumbsUpDownButtons.d.ts +9 -0
- package/dist/ThumbsUpDownButtons.d.ts.map +1 -0
- package/dist/ThumbsUpDownButtons.js +60 -0
- package/dist/ThumbsUpDownButtons.js.map +1 -0
- package/dist/ThumbsUpDownFeedback.d.ts +3 -2
- package/dist/ThumbsUpDownFeedback.d.ts.map +1 -1
- package/dist/ThumbsUpDownFeedback.js +10 -52
- package/dist/ThumbsUpDownFeedback.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/AiSuggestionBox.tsx +3 -3
- package/src/Common.ts +39 -0
- package/src/DataTable.test.tsx +31 -0
- package/src/DataTable.tsx +3 -1
- package/src/DraggableList.test.tsx +141 -1
- package/src/DraggableList.tsx +359 -6
- package/src/FeedbackCommentField.test.tsx +107 -0
- package/src/FeedbackCommentField.tsx +74 -0
- package/src/GPTChat.test.tsx +327 -14
- package/src/GPTChat.tsx +463 -259
- package/src/LabeledCheckBox.tsx +3 -2
- package/src/TextField.test.tsx +126 -1
- package/src/TextField.tsx +13 -7
- package/src/ThumbsUpDownButtons.tsx +120 -0
- package/src/ThumbsUpDownFeedback.test.tsx +154 -1
- package/src/ThumbsUpDownFeedback.tsx +26 -101
- package/src/__snapshots__/DataTable.test.tsx.snap +28 -28
- package/src/index.tsx +1 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {afterEach, beforeEach, describe, expect, it, mock, spyOn} from "bun:test";
|
|
2
|
-
import {act, render} from "@testing-library/react-native";
|
|
2
|
+
import {act, fireEvent, render} from "@testing-library/react-native";
|
|
3
3
|
import React from "react";
|
|
4
4
|
import {Platform, Text as RNText, View} from "react-native";
|
|
5
5
|
import {Gesture} from "react-native-gesture-handler";
|
|
@@ -332,6 +332,146 @@ describe("DraggableList", () => {
|
|
|
332
332
|
reactionSpy.mockRestore();
|
|
333
333
|
});
|
|
334
334
|
|
|
335
|
+
describe("variableHeight", () => {
|
|
336
|
+
const layout = (
|
|
337
|
+
height: number
|
|
338
|
+
): {nativeEvent: {layout: {height: number; width: number; x: number; y: number}}} => ({
|
|
339
|
+
nativeEvent: {layout: {height, width: 100, x: 0, y: 0}},
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
it("stacks rows in normal flow instead of a fixed-height absolute slot", () => {
|
|
343
|
+
const {getByTestId, toJSON} = render(
|
|
344
|
+
<DraggableList
|
|
345
|
+
callbackNewDataIds={() => {}}
|
|
346
|
+
dataIDs={["a", "b"]}
|
|
347
|
+
itemHeight={50}
|
|
348
|
+
itemsGap={10}
|
|
349
|
+
renderItem={renderItem}
|
|
350
|
+
variableHeight
|
|
351
|
+
/>
|
|
352
|
+
);
|
|
353
|
+
|
|
354
|
+
const listStyle = getByTestId("draggable-list").props.style as {
|
|
355
|
+
gap?: number;
|
|
356
|
+
minHeight?: number;
|
|
357
|
+
};
|
|
358
|
+
expect(listStyle.gap).toBe(10);
|
|
359
|
+
expect(listStyle.minHeight).toBeUndefined();
|
|
360
|
+
expect(JSON.stringify(toJSON())).not.toContain('"position":"absolute"');
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
it("drops a short row using the measured height of the row under it", () => {
|
|
364
|
+
const callbackNewDataIds = mock(() => {});
|
|
365
|
+
const {getByTestId} = render(
|
|
366
|
+
<DraggableList
|
|
367
|
+
callbackNewDataIds={callbackNewDataIds}
|
|
368
|
+
dataIDs={["a", "b"]}
|
|
369
|
+
itemHeight={50}
|
|
370
|
+
itemsGap={10}
|
|
371
|
+
renderItem={renderItem}
|
|
372
|
+
variableHeight
|
|
373
|
+
/>
|
|
374
|
+
);
|
|
375
|
+
|
|
376
|
+
act(() => {
|
|
377
|
+
fireEvent(getByTestId("draggable-item-a"), "layout", layout(40));
|
|
378
|
+
fireEvent(getByTestId("draggable-item-b"), "layout", layout(300));
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
const dragA = capturedGestures.at(-2);
|
|
382
|
+
act(() => {
|
|
383
|
+
dragA?.onBegin?.();
|
|
384
|
+
dragA?.onChange?.({translationY: 90});
|
|
385
|
+
dragA?.onFinalize?.();
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
expect(callbackNewDataIds).not.toHaveBeenCalled();
|
|
389
|
+
|
|
390
|
+
const dragAAgain = capturedGestures.at(-2);
|
|
391
|
+
act(() => {
|
|
392
|
+
dragAAgain?.onBegin?.();
|
|
393
|
+
dragAAgain?.onChange?.({translationY: 200});
|
|
394
|
+
dragAAgain?.onFinalize?.();
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
expect(callbackNewDataIds).toHaveBeenCalledWith(["b", "a"]);
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
it("moves a tall row above a short row once its center passes the short row's midpoint", () => {
|
|
401
|
+
const callbackNewDataIds = mock(() => {});
|
|
402
|
+
const {getByTestId} = render(
|
|
403
|
+
<DraggableList
|
|
404
|
+
callbackNewDataIds={callbackNewDataIds}
|
|
405
|
+
dataIDs={["a", "b"]}
|
|
406
|
+
itemHeight={50}
|
|
407
|
+
itemsGap={10}
|
|
408
|
+
renderItem={renderItem}
|
|
409
|
+
variableHeight
|
|
410
|
+
/>
|
|
411
|
+
);
|
|
412
|
+
|
|
413
|
+
act(() => {
|
|
414
|
+
fireEvent(getByTestId("draggable-item-a"), "layout", layout(40));
|
|
415
|
+
fireEvent(getByTestId("draggable-item-b"), "layout", layout(300));
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
const dragB = capturedGestures.at(-1);
|
|
419
|
+
act(() => {
|
|
420
|
+
dragB?.onBegin?.();
|
|
421
|
+
dragB?.onChange?.({translationY: -100});
|
|
422
|
+
dragB?.onFinalize?.();
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
expect(callbackNewDataIds).not.toHaveBeenCalled();
|
|
426
|
+
|
|
427
|
+
const dragBAgain = capturedGestures.at(-1);
|
|
428
|
+
act(() => {
|
|
429
|
+
dragBAgain?.onBegin?.();
|
|
430
|
+
dragBAgain?.onChange?.({translationY: -200});
|
|
431
|
+
dragBAgain?.onFinalize?.();
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
expect(callbackNewDataIds).toHaveBeenCalledWith(["b", "a"]);
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
it("does not reorder a tall row dragged a short distance past the row under it", () => {
|
|
438
|
+
const callbackNewDataIds = mock(() => {});
|
|
439
|
+
const {getByTestId} = render(
|
|
440
|
+
<DraggableList
|
|
441
|
+
callbackNewDataIds={callbackNewDataIds}
|
|
442
|
+
dataIDs={["a", "b"]}
|
|
443
|
+
itemHeight={50}
|
|
444
|
+
itemsGap={10}
|
|
445
|
+
renderItem={renderItem}
|
|
446
|
+
variableHeight
|
|
447
|
+
/>
|
|
448
|
+
);
|
|
449
|
+
|
|
450
|
+
act(() => {
|
|
451
|
+
fireEvent(getByTestId("draggable-item-a"), "layout", layout(300));
|
|
452
|
+
fireEvent(getByTestId("draggable-item-b"), "layout", layout(40));
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
const dragA = capturedGestures.at(-2);
|
|
456
|
+
act(() => {
|
|
457
|
+
dragA?.onBegin?.();
|
|
458
|
+
dragA?.onChange?.({translationY: 30});
|
|
459
|
+
dragA?.onFinalize?.();
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
expect(callbackNewDataIds).not.toHaveBeenCalled();
|
|
463
|
+
|
|
464
|
+
const dragAAgain = capturedGestures.at(-2);
|
|
465
|
+
act(() => {
|
|
466
|
+
dragAAgain?.onBegin?.();
|
|
467
|
+
dragAAgain?.onChange?.({translationY: 200});
|
|
468
|
+
dragAAgain?.onFinalize?.();
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
expect(callbackNewDataIds).toHaveBeenCalledWith(["b", "a"]);
|
|
472
|
+
});
|
|
473
|
+
});
|
|
474
|
+
|
|
335
475
|
describe("DragItem", () => {
|
|
336
476
|
const baseProps = {
|
|
337
477
|
index: 0,
|
package/src/DraggableList.tsx
CHANGED
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
// MIT License
|
|
6
6
|
// Copyright Patryk Jaworski @gerwld
|
|
7
7
|
|
|
8
|
-
import React, {useMemo, useState} from "react";
|
|
9
|
-
import {Platform, type StyleProp, View, type ViewStyle} from "react-native";
|
|
8
|
+
import React, {useCallback, useMemo, useRef, useState} from "react";
|
|
9
|
+
import {type LayoutChangeEvent, Platform, type StyleProp, View, type ViewStyle} from "react-native";
|
|
10
10
|
import {Gesture, GestureDetector} from "react-native-gesture-handler";
|
|
11
11
|
import Animated, {
|
|
12
12
|
runOnJS,
|
|
@@ -63,8 +63,14 @@ interface DragListProps {
|
|
|
63
63
|
backgroundOnHold?: string; // Background color when item is being dragged
|
|
64
64
|
dataIDs?: string[]; // Array of item IDs (preferred over data)
|
|
65
65
|
itemsGap?: number; // Spacing between items
|
|
66
|
-
itemHeight?: number; // Height of each item
|
|
66
|
+
itemHeight?: number; // Height of each item. With `variableHeight`, the placeholder until a row is measured.
|
|
67
67
|
itemBorderRadius?: number; // Border radius for items
|
|
68
|
+
/**
|
|
69
|
+
* Size each row to its content and stack the next row underneath.
|
|
70
|
+
* `itemHeight` stays the placeholder until measurement. Use this for rows whose
|
|
71
|
+
* height changes (nested forms, user lists) so later rows are not painted on top.
|
|
72
|
+
*/
|
|
73
|
+
variableHeight?: boolean;
|
|
68
74
|
}
|
|
69
75
|
|
|
70
76
|
/**
|
|
@@ -299,11 +305,350 @@ export const DragItem: React.FC<DragItemProps> = (props) => {
|
|
|
299
305
|
);
|
|
300
306
|
};
|
|
301
307
|
|
|
308
|
+
const measuredRowHeight = (
|
|
309
|
+
measured: Record<string, number>,
|
|
310
|
+
id: string,
|
|
311
|
+
fallback: number
|
|
312
|
+
): number => {
|
|
313
|
+
const height = measured[id];
|
|
314
|
+
if (typeof height === "number" && height > 0) {
|
|
315
|
+
return height;
|
|
316
|
+
}
|
|
317
|
+
return fallback;
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
/** Y offset of `id` inside `order`, using measured row heights. */
|
|
321
|
+
const offsetOfId = (
|
|
322
|
+
id: string,
|
|
323
|
+
order: string[],
|
|
324
|
+
measured: Record<string, number>,
|
|
325
|
+
fallback: number,
|
|
326
|
+
gap: number
|
|
327
|
+
): number => {
|
|
328
|
+
let y = 0;
|
|
329
|
+
for (const other of order) {
|
|
330
|
+
if (other === id) {
|
|
331
|
+
return y;
|
|
332
|
+
}
|
|
333
|
+
y += measuredRowHeight(measured, other, fallback) + gap;
|
|
334
|
+
}
|
|
335
|
+
return y;
|
|
336
|
+
};
|
|
337
|
+
|
|
302
338
|
/**
|
|
303
|
-
*
|
|
304
|
-
*
|
|
339
|
+
* Drop index for a dragged row. `order` is the drag-start order laid out in full, so
|
|
340
|
+
* the dragged row's center and the other rows' midpoints share one coordinate space.
|
|
305
341
|
*/
|
|
306
|
-
|
|
342
|
+
const insertIndexForDrag = (
|
|
343
|
+
draggedCenter: number,
|
|
344
|
+
draggedId: string,
|
|
345
|
+
order: string[],
|
|
346
|
+
measured: Record<string, number>,
|
|
347
|
+
fallback: number,
|
|
348
|
+
gap: number
|
|
349
|
+
): number => {
|
|
350
|
+
let y = 0;
|
|
351
|
+
let index = 0;
|
|
352
|
+
for (const id of order) {
|
|
353
|
+
const height = measuredRowHeight(measured, id, fallback);
|
|
354
|
+
if (id !== draggedId) {
|
|
355
|
+
if (draggedCenter < y + height / 2) {
|
|
356
|
+
return index;
|
|
357
|
+
}
|
|
358
|
+
index += 1;
|
|
359
|
+
}
|
|
360
|
+
y += height + gap;
|
|
361
|
+
}
|
|
362
|
+
return index;
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
interface VariableDragRowProps {
|
|
366
|
+
backgroundOnHold?: string;
|
|
367
|
+
dragMode: boolean;
|
|
368
|
+
dragTop: {value: number};
|
|
369
|
+
dragged: boolean;
|
|
370
|
+
id: string;
|
|
371
|
+
itemBorderRadius: number;
|
|
372
|
+
itemContainerStyle?: StyleProp<ViewStyle>;
|
|
373
|
+
offset: number;
|
|
374
|
+
onDragBegin: (id: string) => void;
|
|
375
|
+
onDragEnd: () => void;
|
|
376
|
+
onDragMove: (id: string, translationY: number) => void;
|
|
377
|
+
onMeasuredHeight: (id: string, height: number) => void;
|
|
378
|
+
passVibration?: () => void;
|
|
379
|
+
renderGrip?: React.ReactElement | (() => React.ReactElement);
|
|
380
|
+
renderItem: (props: {item: string}) => React.ReactElement;
|
|
381
|
+
startOffset: {value: number};
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
const DRAG_ROW_SHADOW = "0px 0px 5px rgba(0, 0, 0, 0.2)";
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* One variable-height row. At rest it sits in normal flow. During a drag every
|
|
388
|
+
* row is taken out of flow and positioned from measured heights.
|
|
389
|
+
*/
|
|
390
|
+
const VariableDragRow: React.FC<VariableDragRowProps> = ({
|
|
391
|
+
backgroundOnHold,
|
|
392
|
+
dragMode,
|
|
393
|
+
dragTop,
|
|
394
|
+
dragged,
|
|
395
|
+
id,
|
|
396
|
+
itemBorderRadius,
|
|
397
|
+
itemContainerStyle,
|
|
398
|
+
offset,
|
|
399
|
+
onDragBegin,
|
|
400
|
+
onDragEnd,
|
|
401
|
+
onDragMove,
|
|
402
|
+
onMeasuredHeight,
|
|
403
|
+
passVibration,
|
|
404
|
+
renderGrip,
|
|
405
|
+
renderItem,
|
|
406
|
+
startOffset,
|
|
407
|
+
}) => {
|
|
408
|
+
const isAndroid = Platform.OS === "android";
|
|
409
|
+
|
|
410
|
+
const handleLayout = useCallback(
|
|
411
|
+
(event: LayoutChangeEvent): void => {
|
|
412
|
+
if (dragMode) {
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
const nextHeight = Math.ceil(event.nativeEvent.layout.height);
|
|
416
|
+
if (nextHeight <= 0) {
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
onMeasuredHeight(id, nextHeight);
|
|
420
|
+
},
|
|
421
|
+
[dragMode, id, onMeasuredHeight]
|
|
422
|
+
);
|
|
423
|
+
|
|
424
|
+
const animatedStyle = useAnimatedStyle(() => {
|
|
425
|
+
if (!dragMode) {
|
|
426
|
+
return {
|
|
427
|
+
backgroundColor: "transparent",
|
|
428
|
+
boxShadow: "none",
|
|
429
|
+
position: "relative" as const,
|
|
430
|
+
top: 0,
|
|
431
|
+
width: "100%",
|
|
432
|
+
zIndex: 0,
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
return {
|
|
436
|
+
backgroundColor: dragged ? backgroundOnHold : "transparent",
|
|
437
|
+
boxShadow: dragged && !isAndroid ? DRAG_ROW_SHADOW : "none",
|
|
438
|
+
left: 0,
|
|
439
|
+
position: "absolute" as const,
|
|
440
|
+
top: dragged ? dragTop.value : offset,
|
|
441
|
+
width: "100%",
|
|
442
|
+
zIndex: dragged ? 2 : 0,
|
|
443
|
+
};
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
const pan = Gesture.Pan()
|
|
447
|
+
.onBegin(() => {
|
|
448
|
+
startOffset.value = offset;
|
|
449
|
+
dragTop.value = offset;
|
|
450
|
+
if (passVibration) {
|
|
451
|
+
runOnJS(passVibration)();
|
|
452
|
+
}
|
|
453
|
+
runOnJS(onDragBegin)(id);
|
|
454
|
+
})
|
|
455
|
+
.onChange((event) => {
|
|
456
|
+
dragTop.value = startOffset.value + event.translationY;
|
|
457
|
+
runOnJS(onDragMove)(id, event.translationY);
|
|
458
|
+
})
|
|
459
|
+
.onFinalize(() => {
|
|
460
|
+
runOnJS(onDragEnd)();
|
|
461
|
+
});
|
|
462
|
+
|
|
463
|
+
return (
|
|
464
|
+
<Animated.View
|
|
465
|
+
style={[
|
|
466
|
+
{borderRadius: itemBorderRadius, paddingRight: 10},
|
|
467
|
+
itemContainerStyle,
|
|
468
|
+
animatedStyle,
|
|
469
|
+
]}
|
|
470
|
+
>
|
|
471
|
+
<View
|
|
472
|
+
collapsable={false}
|
|
473
|
+
onLayout={handleLayout}
|
|
474
|
+
style={{alignItems: "center", flexDirection: "row", width: "100%"}}
|
|
475
|
+
testID={`draggable-item-${id}`}
|
|
476
|
+
>
|
|
477
|
+
<GestureDetector gesture={pan}>
|
|
478
|
+
{renderGrip ? (
|
|
479
|
+
<View
|
|
480
|
+
style={{
|
|
481
|
+
alignItems: "center",
|
|
482
|
+
flexBasis: 45,
|
|
483
|
+
flexGrow: 0,
|
|
484
|
+
flexShrink: 0,
|
|
485
|
+
justifyContent: "center",
|
|
486
|
+
minWidth: 45,
|
|
487
|
+
}}
|
|
488
|
+
>
|
|
489
|
+
{typeof renderGrip === "function" ? renderGrip() : renderGrip}
|
|
490
|
+
</View>
|
|
491
|
+
) : (
|
|
492
|
+
<Box justifyContent="center">
|
|
493
|
+
<Icon iconName="arrows-up-down-left-right" />
|
|
494
|
+
</Box>
|
|
495
|
+
)}
|
|
496
|
+
</GestureDetector>
|
|
497
|
+
<View style={{flex: 1, minWidth: 0}}>{renderItem({item: id})}</View>
|
|
498
|
+
</View>
|
|
499
|
+
</Animated.View>
|
|
500
|
+
);
|
|
501
|
+
};
|
|
502
|
+
|
|
503
|
+
/**
|
|
504
|
+
* Drag list whose rows keep their natural height. Resting layout is a column,
|
|
505
|
+
* so a tall row (a feature-flag rule with many users) pushes the next row down
|
|
506
|
+
* instead of letting it paint over the content.
|
|
507
|
+
*/
|
|
508
|
+
const VariableHeightDraggableList: React.FC<DragListProps> = (props) => {
|
|
509
|
+
const {
|
|
510
|
+
backgroundOnHold = "#e3e3e3",
|
|
511
|
+
callbackNewDataIds,
|
|
512
|
+
data,
|
|
513
|
+
itemContainerStyle,
|
|
514
|
+
passVibration,
|
|
515
|
+
renderGrip,
|
|
516
|
+
renderItem,
|
|
517
|
+
} = props;
|
|
518
|
+
const dataIDs = useMemo(() => props.dataIDs || data || [], [props.dataIDs, data]);
|
|
519
|
+
const itemsGap = props.itemsGap || 5;
|
|
520
|
+
const itemHeight = props.itemHeight || 50;
|
|
521
|
+
const itemBorderRadius = props.itemBorderRadius || 8;
|
|
522
|
+
|
|
523
|
+
const [measured, setMeasured] = useState<Record<string, number>>({});
|
|
524
|
+
const [previewOrder, setPreviewOrder] = useState<string[] | null>(null);
|
|
525
|
+
const [draggingId, setDraggingId] = useState<string | null>(null);
|
|
526
|
+
const measuredRef = useRef(measured);
|
|
527
|
+
const orderRef = useRef<string[]>(dataIDs);
|
|
528
|
+
const orderAtStartRef = useRef<string[]>(dataIDs);
|
|
529
|
+
const dragOpenRef = useRef(false);
|
|
530
|
+
const dragTop = useSharedValue(0);
|
|
531
|
+
const startOffset = useSharedValue(0);
|
|
532
|
+
const order = previewOrder ?? dataIDs;
|
|
533
|
+
measuredRef.current = measured;
|
|
534
|
+
orderRef.current = order;
|
|
535
|
+
|
|
536
|
+
const handleMeasuredHeight = useCallback((id: string, height: number): void => {
|
|
537
|
+
setMeasured((current) => {
|
|
538
|
+
if (current[id] === height) {
|
|
539
|
+
return current;
|
|
540
|
+
}
|
|
541
|
+
const next = {...current, [id]: height};
|
|
542
|
+
measuredRef.current = next;
|
|
543
|
+
return next;
|
|
544
|
+
});
|
|
545
|
+
}, []);
|
|
546
|
+
|
|
547
|
+
const handleDragBegin = useCallback((id: string): void => {
|
|
548
|
+
if (!dragOpenRef.current) {
|
|
549
|
+
orderAtStartRef.current = [...orderRef.current];
|
|
550
|
+
dragOpenRef.current = true;
|
|
551
|
+
}
|
|
552
|
+
setDraggingId(id);
|
|
553
|
+
}, []);
|
|
554
|
+
|
|
555
|
+
const handleDragMove = useCallback(
|
|
556
|
+
(id: string, translationY: number): void => {
|
|
557
|
+
if (!dragOpenRef.current) {
|
|
558
|
+
orderAtStartRef.current = [...orderRef.current];
|
|
559
|
+
dragOpenRef.current = true;
|
|
560
|
+
}
|
|
561
|
+
const current = orderRef.current;
|
|
562
|
+
const from = current.indexOf(id);
|
|
563
|
+
if (from < 0) {
|
|
564
|
+
return;
|
|
565
|
+
}
|
|
566
|
+
const draggedHeight = measuredRowHeight(measuredRef.current, id, itemHeight);
|
|
567
|
+
const draggedCenter = startOffset.value + translationY + draggedHeight / 2;
|
|
568
|
+
const nextIndex = insertIndexForDrag(
|
|
569
|
+
draggedCenter,
|
|
570
|
+
id,
|
|
571
|
+
orderAtStartRef.current,
|
|
572
|
+
measuredRef.current,
|
|
573
|
+
itemHeight,
|
|
574
|
+
itemsGap
|
|
575
|
+
);
|
|
576
|
+
if (nextIndex === from) {
|
|
577
|
+
return;
|
|
578
|
+
}
|
|
579
|
+
const next = [...current];
|
|
580
|
+
const [moved] = next.splice(from, 1);
|
|
581
|
+
if (!moved) {
|
|
582
|
+
return;
|
|
583
|
+
}
|
|
584
|
+
next.splice(nextIndex, 0, moved);
|
|
585
|
+
orderRef.current = next;
|
|
586
|
+
setPreviewOrder(next);
|
|
587
|
+
if (passVibration) {
|
|
588
|
+
passVibration();
|
|
589
|
+
}
|
|
590
|
+
},
|
|
591
|
+
[itemHeight, itemsGap, passVibration, startOffset]
|
|
592
|
+
);
|
|
593
|
+
|
|
594
|
+
const handleDragEnd = useCallback((): void => {
|
|
595
|
+
const next = orderRef.current;
|
|
596
|
+
const previous = orderAtStartRef.current;
|
|
597
|
+
dragOpenRef.current = false;
|
|
598
|
+
setDraggingId(null);
|
|
599
|
+
setPreviewOrder(null);
|
|
600
|
+
if (!callbackNewDataIds || next.join("\u0000") === previous.join("\u0000")) {
|
|
601
|
+
return;
|
|
602
|
+
}
|
|
603
|
+
callbackNewDataIds(next);
|
|
604
|
+
}, [callbackNewDataIds]);
|
|
605
|
+
|
|
606
|
+
const dragMode = draggingId !== null;
|
|
607
|
+
const totalHeight = order.reduce((sum, id, index) => {
|
|
608
|
+
return sum + measuredRowHeight(measured, id, itemHeight) + (index > 0 ? itemsGap : 0);
|
|
609
|
+
}, 0);
|
|
610
|
+
|
|
611
|
+
return (
|
|
612
|
+
<View
|
|
613
|
+
style={{
|
|
614
|
+
gap: dragMode ? 0 : itemsGap,
|
|
615
|
+
minHeight: dragMode ? totalHeight : undefined,
|
|
616
|
+
position: "relative",
|
|
617
|
+
width: "100%",
|
|
618
|
+
}}
|
|
619
|
+
testID="draggable-list"
|
|
620
|
+
>
|
|
621
|
+
{order.map((id) => (
|
|
622
|
+
<VariableDragRow
|
|
623
|
+
backgroundOnHold={backgroundOnHold}
|
|
624
|
+
dragged={draggingId === id}
|
|
625
|
+
dragMode={dragMode}
|
|
626
|
+
dragTop={dragTop}
|
|
627
|
+
id={id}
|
|
628
|
+
itemBorderRadius={itemBorderRadius}
|
|
629
|
+
itemContainerStyle={itemContainerStyle}
|
|
630
|
+
key={id}
|
|
631
|
+
offset={offsetOfId(id, order, measured, itemHeight, itemsGap)}
|
|
632
|
+
onDragBegin={handleDragBegin}
|
|
633
|
+
onDragEnd={handleDragEnd}
|
|
634
|
+
onDragMove={handleDragMove}
|
|
635
|
+
onMeasuredHeight={handleMeasuredHeight}
|
|
636
|
+
passVibration={passVibration}
|
|
637
|
+
renderGrip={renderGrip}
|
|
638
|
+
renderItem={renderItem}
|
|
639
|
+
startOffset={startOffset}
|
|
640
|
+
/>
|
|
641
|
+
))}
|
|
642
|
+
</View>
|
|
643
|
+
);
|
|
644
|
+
};
|
|
645
|
+
|
|
646
|
+
/**
|
|
647
|
+
* Main draggable list component that renders a list of draggable items.
|
|
648
|
+
* Fixed-height rows are positioned absolutely. `variableHeight` stacks rows
|
|
649
|
+
* in normal flow so content taller than `itemHeight` is not covered.
|
|
650
|
+
*/
|
|
651
|
+
const FixedDraggableList: React.FC<DragListProps> = (props) => {
|
|
307
652
|
const {
|
|
308
653
|
data,
|
|
309
654
|
callbackNewDataIds,
|
|
@@ -425,3 +770,11 @@ export const DraggableList: React.FC<DragListProps> = (props) => {
|
|
|
425
770
|
</View>
|
|
426
771
|
);
|
|
427
772
|
};
|
|
773
|
+
|
|
774
|
+
/** Fixed-height rows, or content-sized rows when `variableHeight` is set. */
|
|
775
|
+
export const DraggableList: React.FC<DragListProps> = (props) => {
|
|
776
|
+
if (props.variableHeight) {
|
|
777
|
+
return <VariableHeightDraggableList {...props} />;
|
|
778
|
+
}
|
|
779
|
+
return <FixedDraggableList {...props} />;
|
|
780
|
+
};
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import {describe, expect, it, jest} from "bun:test";
|
|
2
|
+
import {act, fireEvent, waitFor} from "@testing-library/react-native";
|
|
3
|
+
|
|
4
|
+
import {FeedbackCommentField} from "./FeedbackCommentField";
|
|
5
|
+
import {renderWithTheme} from "./test-utils";
|
|
6
|
+
|
|
7
|
+
// Typed node instead of `any` to satisfy the noExplicitAny lint rule.
|
|
8
|
+
const isDisabled = (node: {
|
|
9
|
+
props: Record<string, unknown> & {accessibilityState?: {disabled?: boolean}};
|
|
10
|
+
}): boolean =>
|
|
11
|
+
Boolean(
|
|
12
|
+
node.props.disabled || node.props.accessibilityState?.disabled || node.props["aria-disabled"]
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
describe("FeedbackCommentField", () => {
|
|
16
|
+
it("renders the input and Send button", () => {
|
|
17
|
+
const {getByTestId} = renderWithTheme(
|
|
18
|
+
<FeedbackCommentField onSubmit={jest.fn()} testID="fc" />
|
|
19
|
+
);
|
|
20
|
+
expect(getByTestId("fc-input")).toBeTruthy();
|
|
21
|
+
expect(getByTestId("fc-send")).toBeTruthy();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("uses the placeholder prop, defaulting to 'Tell us more (optional)'", () => {
|
|
25
|
+
const defaulted = renderWithTheme(<FeedbackCommentField onSubmit={jest.fn()} testID="fc" />);
|
|
26
|
+
expect(defaulted.getByPlaceholderText("Tell us more (optional)")).toBeTruthy();
|
|
27
|
+
const custom = renderWithTheme(
|
|
28
|
+
<FeedbackCommentField onSubmit={jest.fn()} placeholder="Why?" testID="fc" />
|
|
29
|
+
);
|
|
30
|
+
expect(custom.getByPlaceholderText("Why?")).toBeTruthy();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("submits the trimmed text once, then shows the sent state", async () => {
|
|
34
|
+
const onSubmit = jest.fn();
|
|
35
|
+
const {getByTestId, queryByTestId, getByText} = renderWithTheme(
|
|
36
|
+
<FeedbackCommentField onSubmit={onSubmit} testID="fc" />
|
|
37
|
+
);
|
|
38
|
+
fireEvent.changeText(getByTestId("fc-input"), " Too long ");
|
|
39
|
+
fireEvent.press(getByTestId("fc-send"));
|
|
40
|
+
await waitFor(() => expect(getByTestId("fc-sent")).toBeTruthy());
|
|
41
|
+
expect(onSubmit).toHaveBeenCalledTimes(1);
|
|
42
|
+
expect(onSubmit).toHaveBeenCalledWith("Too long");
|
|
43
|
+
expect(getByText("Thanks for the feedback")).toBeTruthy();
|
|
44
|
+
expect(queryByTestId("fc-input")).toBeNull();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("disables Send for empty and whitespace-only text and does not submit", () => {
|
|
48
|
+
const onSubmit = jest.fn();
|
|
49
|
+
const {getByTestId} = renderWithTheme(<FeedbackCommentField onSubmit={onSubmit} testID="fc" />);
|
|
50
|
+
expect(isDisabled(getByTestId("fc-send"))).toBe(true);
|
|
51
|
+
fireEvent.press(getByTestId("fc-send"));
|
|
52
|
+
fireEvent.changeText(getByTestId("fc-input"), " ");
|
|
53
|
+
expect(isDisabled(getByTestId("fc-send"))).toBe(true);
|
|
54
|
+
fireEvent.press(getByTestId("fc-send"));
|
|
55
|
+
expect(onSubmit).not.toHaveBeenCalled();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("enables Send once there is non-blank text", () => {
|
|
59
|
+
const {getByTestId} = renderWithTheme(
|
|
60
|
+
<FeedbackCommentField onSubmit={jest.fn()} testID="fc" />
|
|
61
|
+
);
|
|
62
|
+
fireEvent.changeText(getByTestId("fc-input"), "hi");
|
|
63
|
+
expect(isDisabled(getByTestId("fc-send"))).toBe(false);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("disables Send while a send is in flight and ignores a second press", async () => {
|
|
67
|
+
let resolve: () => void = () => {};
|
|
68
|
+
const onSubmit = jest.fn(
|
|
69
|
+
() =>
|
|
70
|
+
new Promise<void>((res) => {
|
|
71
|
+
resolve = res;
|
|
72
|
+
})
|
|
73
|
+
);
|
|
74
|
+
const {getByTestId} = renderWithTheme(<FeedbackCommentField onSubmit={onSubmit} testID="fc" />);
|
|
75
|
+
fireEvent.changeText(getByTestId("fc-input"), "hello");
|
|
76
|
+
fireEvent.press(getByTestId("fc-send"));
|
|
77
|
+
await waitFor(() => expect(isDisabled(getByTestId("fc-send"))).toBe(true));
|
|
78
|
+
fireEvent.press(getByTestId("fc-send"));
|
|
79
|
+
expect(onSubmit).toHaveBeenCalledTimes(1);
|
|
80
|
+
await act(async () => {
|
|
81
|
+
resolve();
|
|
82
|
+
});
|
|
83
|
+
await waitFor(() => expect(getByTestId("fc-sent")).toBeTruthy());
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("keeps the input and text and re-enables Send when onSubmit rejects", async () => {
|
|
87
|
+
const onSubmit = jest.fn().mockRejectedValue(new Error("nope"));
|
|
88
|
+
const {getByTestId, queryByTestId} = renderWithTheme(
|
|
89
|
+
<FeedbackCommentField onSubmit={onSubmit} testID="fc" />
|
|
90
|
+
);
|
|
91
|
+
fireEvent.changeText(getByTestId("fc-input"), "keep me");
|
|
92
|
+
fireEvent.press(getByTestId("fc-send"));
|
|
93
|
+
await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(1));
|
|
94
|
+
await waitFor(() => expect(isDisabled(getByTestId("fc-send"))).toBe(false));
|
|
95
|
+
expect(queryByTestId("fc-sent")).toBeNull();
|
|
96
|
+
expect(getByTestId("fc-input").props.value).toBe("keep me");
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("shows the sent state instead of the input when sentComment is provided", () => {
|
|
100
|
+
const {getByTestId, queryByTestId} = renderWithTheme(
|
|
101
|
+
<FeedbackCommentField onSubmit={jest.fn()} sentComment="Too long" testID="fc" />
|
|
102
|
+
);
|
|
103
|
+
expect(getByTestId("fc-sent")).toBeTruthy();
|
|
104
|
+
expect(queryByTestId("fc-input")).toBeNull();
|
|
105
|
+
expect(queryByTestId("fc-send")).toBeNull();
|
|
106
|
+
});
|
|
107
|
+
});
|