@terreno/ui 57.2.0 → 57.3.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/.ai/guidelines/core.md +1 -1
- package/README.md +17 -8
- package/dist/ActionSheet.d.ts +3 -3
- package/dist/ActionSheet.js +2 -2
- package/dist/ActionSheet.js.map +1 -1
- package/dist/Box.d.ts +1 -1
- package/dist/Box.js +219 -177
- package/dist/Box.js.map +1 -1
- package/dist/Button.js +101 -34
- package/dist/Button.js.map +1 -1
- package/dist/Common.d.ts +119 -9
- package/dist/Common.js.map +1 -1
- package/dist/ConsentFormScreen.js +46 -12
- package/dist/ConsentFormScreen.js.map +1 -1
- package/dist/ConsentNavigator.d.ts +5 -1
- package/dist/ConsentNavigator.js.map +1 -1
- package/dist/DataTable.d.ts +2 -2
- package/dist/DataTable.js +203 -44
- package/dist/DataTable.js.map +1 -1
- package/dist/DataTableHeaderInfoMarkdown.d.ts +6 -0
- package/dist/DataTableHeaderInfoMarkdown.js +7 -0
- package/dist/DataTableHeaderInfoMarkdown.js.map +1 -0
- package/dist/EmojiSelector.d.ts +2 -46
- package/dist/EmojiSelector.js +46 -56
- package/dist/EmojiSelector.js.map +1 -1
- package/dist/GPTChat.js +1 -1
- package/dist/GPTChat.js.map +1 -1
- package/dist/Heading.d.ts +5 -2
- package/dist/Heading.js +6 -8
- package/dist/Heading.js.map +1 -1
- package/dist/Hyperlink.js.map +1 -1
- package/dist/Icon.d.ts +1 -2
- package/dist/Icon.js +4 -1
- package/dist/Icon.js.map +1 -1
- package/dist/MarkdownView.d.ts +6 -3
- package/dist/MarkdownView.js +45 -43
- package/dist/MarkdownView.js.map +1 -1
- package/dist/MediaQuery.d.ts +5 -3
- package/dist/MediaQuery.js +9 -40
- package/dist/MediaQuery.js.map +1 -1
- package/dist/OpenAPIContext.js +7 -6
- package/dist/OpenAPIContext.js.map +1 -1
- package/dist/Page.js +7 -1
- package/dist/Page.js.map +1 -1
- package/dist/PickerSelect.js +8 -1
- package/dist/PickerSelect.js.map +1 -1
- package/dist/Popover.d.ts +12 -0
- package/dist/Popover.js +37 -0
- package/dist/Popover.js.map +1 -0
- package/dist/PortalHost.js +10 -1
- package/dist/PortalHost.js.map +1 -1
- package/dist/ResponsiveBreakpoint.d.ts +65 -0
- package/dist/ResponsiveBreakpoint.js +111 -0
- package/dist/ResponsiveBreakpoint.js.map +1 -0
- package/dist/SplitPage.d.ts +2 -2
- package/dist/SplitPage.js +7 -12
- package/dist/SplitPage.js.map +1 -1
- package/dist/SplitPage.native.d.ts +2 -2
- package/dist/SplitPage.native.js +1 -6
- package/dist/SplitPage.native.js.map +1 -1
- package/dist/TapToEdit.js +12 -10
- package/dist/TapToEdit.js.map +1 -1
- package/dist/TerrenoFontProvider.d.ts +7 -0
- package/dist/TerrenoFontProvider.js +27 -0
- package/dist/TerrenoFontProvider.js.map +1 -0
- package/dist/TerrenoProvider.js +4 -2
- package/dist/TerrenoProvider.js.map +1 -1
- package/dist/Text.d.ts +5 -2
- package/dist/Text.js +6 -11
- package/dist/Text.js.map +1 -1
- package/dist/TextFieldNumberActionSheet.js +9 -2
- package/dist/TextFieldNumberActionSheet.js.map +1 -1
- package/dist/Theme.js +10 -8
- package/dist/Theme.js.map +1 -1
- package/dist/ToastNotifications.d.ts +4 -2
- package/dist/ToastNotifications.js.map +1 -1
- package/dist/Tooltip.d.ts +4 -3
- package/dist/Tooltip.js +14 -3
- package/dist/Tooltip.js.map +1 -1
- package/dist/Unifier.d.ts +26 -1
- package/dist/Unifier.js.map +1 -1
- package/dist/WebAddressAutocomplete.js +6 -1
- package/dist/WebAddressAutocomplete.js.map +1 -1
- package/dist/emojiCategories.d.ts +46 -0
- package/dist/emojiCategories.js +47 -0
- package/dist/emojiCategories.js.map +1 -0
- package/dist/index.d.ts +13 -13
- package/dist/index.js +2 -13
- package/dist/index.js.map +1 -1
- package/dist/lazyBoundaries/createLazyComponentExport.d.ts +6 -0
- package/dist/lazyBoundaries/createLazyComponentExport.js +35 -0
- package/dist/lazyBoundaries/createLazyComponentExport.js.map +1 -0
- package/dist/lazyBoundaries/heavyOptionalExports.d.ts +41 -0
- package/dist/lazyBoundaries/heavyOptionalExports.js +43 -0
- package/dist/lazyBoundaries/heavyOptionalExports.js.map +1 -0
- package/dist/login/LoginScreen.d.ts +1 -0
- package/dist/login/LoginScreen.js +4 -2
- package/dist/login/LoginScreen.js.map +1 -1
- package/dist/login/loginTypes.d.ts +2 -0
- package/package.json +40 -1
- package/src/AIRequestExplorer.test.tsx +141 -0
- package/src/ActionSheet.tsx +9 -7
- package/src/AttachmentPreview.test.tsx +80 -0
- package/src/Box.test.tsx +97 -10
- package/src/Box.tsx +263 -200
- package/src/Button.test.tsx +207 -0
- package/src/Button.tsx +146 -48
- package/src/Common.ts +124 -25
- package/src/ConsentFormScreen.markdownGate.test.tsx +129 -0
- package/src/ConsentFormScreen.test.tsx +145 -8
- package/src/ConsentFormScreen.tsx +74 -25
- package/src/ConsentNavigator.tsx +13 -5
- package/src/DataTable.tsx +418 -147
- package/src/DataTableHeaderInfoMarkdown.test.tsx +21 -0
- package/src/DataTableHeaderInfoMarkdown.tsx +17 -0
- package/src/DataTableVirtualizationRegression.test.tsx +266 -0
- package/src/DateTimeField.test.tsx +54 -0
- package/src/EmojiSelector.test.tsx +33 -42
- package/src/EmojiSelector.tsx +60 -57
- package/src/FilePickerButton.test.tsx +167 -0
- package/src/FilterAccordion.test.tsx +37 -0
- package/src/FilterBoolean.test.tsx +96 -0
- package/src/FilterSelectMenu.test.tsx +23 -0
- package/src/GPTChat.test.tsx +696 -0
- package/src/GPTChat.tsx +2 -2
- package/src/GPTMemoryModal.test.tsx +67 -0
- package/src/Heading.tsx +8 -14
- package/src/HeightActionSheet.test.tsx +6 -9
- package/src/Hyperlink.tsx +5 -18
- package/src/Icon.tsx +6 -2
- package/src/MarkdownEditor.test.tsx +134 -0
- package/src/MarkdownEditorField.test.tsx +124 -0
- package/src/MarkdownView.test.tsx +55 -11
- package/src/MarkdownView.tsx +149 -150
- package/src/MediaQuery.ts +30 -34
- package/src/OpenAPIContext.tsx +36 -29
- package/src/OpenAPIRenderRegression.test.tsx +200 -0
- package/src/P0RenderRegression.test.tsx +214 -0
- package/src/P1RenderRegression.test.tsx +141 -0
- package/src/P2ButtonRenderRegression.test.tsx +158 -0
- package/src/Page.test.tsx +13 -0
- package/src/Page.tsx +25 -5
- package/src/PickerSelect.test.tsx +38 -0
- package/src/PickerSelect.tsx +7 -0
- package/src/Popover.test.tsx +151 -0
- package/src/Popover.tsx +211 -0
- package/src/PortalHost.tsx +12 -6
- package/src/ResponsiveBreakpoint.test.tsx +184 -0
- package/src/ResponsiveBreakpoint.tsx +195 -0
- package/src/RootImportRegression.test.tsx +62 -0
- package/src/SplitPage.native.tsx +5 -9
- package/src/SplitPage.tsx +16 -19
- package/src/TapToEdit.test.tsx +45 -0
- package/src/TapToEdit.tsx +23 -18
- package/src/TerrenoFontProvider.test.tsx +38 -0
- package/src/TerrenoFontProvider.tsx +50 -0
- package/src/TerrenoProvider.tsx +6 -1
- package/src/Text.tsx +8 -21
- package/src/TextFieldNumberActionSheet.test.tsx +100 -0
- package/src/TextFieldNumberActionSheet.tsx +11 -2
- package/src/Theme.test.tsx +27 -0
- package/src/Theme.tsx +15 -10
- package/src/Toast.tsx +1 -1
- package/src/ToastNotifications.tsx +4 -4
- package/src/Tooltip.test.tsx +64 -0
- package/src/Tooltip.tsx +18 -4
- package/src/Unifier.ts +26 -3
- package/src/WebAddressAutocomplete.tsx +6 -2
- package/src/__snapshots__/ActionSheet.test.tsx.snap +0 -36
- package/src/__snapshots__/AddressField.test.tsx.snap +1 -0
- package/src/__snapshots__/AttachmentPreview.test.tsx.snap +225 -0
- package/src/__snapshots__/CustomSelectField.test.tsx.snap +7 -0
- package/src/__snapshots__/DataTable.test.tsx.snap +793 -250
- package/src/__snapshots__/DecimalRangeActionSheet.test.tsx.snap +0 -8
- package/src/__snapshots__/EmojiSelector.test.tsx.snap +35555 -30
- package/src/__snapshots__/Field.test.tsx.snap +7 -0
- package/src/__snapshots__/FilterSelectMenu.test.tsx.snap +1 -0
- package/src/__snapshots__/HeightActionSheet.test.tsx.snap +0 -8
- package/src/__snapshots__/HeightField.test.tsx.snap +0 -20
- package/src/__snapshots__/NumberPickerActionSheet.test.tsx.snap +0 -8
- package/src/__snapshots__/Page.test.tsx.snap +13 -1
- package/src/__snapshots__/PickerSelect.test.tsx.snap +7 -0
- package/src/__snapshots__/SelectField.test.tsx.snap +6 -0
- package/src/__snapshots__/TimezonePicker.test.tsx.snap +6 -0
- package/src/benchmarks/importGraph.test.ts +71 -0
- package/src/benchmarks/importGraph.ts +136 -0
- package/src/benchmarks/importTiming.test.ts +42 -0
- package/src/benchmarks/importTiming.ts +86 -0
- package/src/bunSetup.ts +174 -32
- package/src/emojiCategories.test.ts +23 -0
- package/src/emojiCategories.ts +46 -0
- package/src/index.tsx +27 -13
- package/src/lazyBoundaries/createLazyComponentExport.test.tsx +104 -0
- package/src/lazyBoundaries/createLazyComponentExport.tsx +63 -0
- package/src/lazyBoundaries/heavyOptionalExports.test.tsx +33 -0
- package/src/lazyBoundaries/heavyOptionalExports.tsx +106 -0
- package/src/login/LoginScreen.test.tsx +9 -2
- package/src/login/LoginScreen.tsx +6 -2
- package/src/login/loginTypes.ts +2 -0
- package/src/table/TableDate.test.tsx +35 -1
- package/src/table/__snapshots__/TableBadge.test.tsx.snap +1 -0
package/src/Button.test.tsx
CHANGED
|
@@ -1,16 +1,23 @@
|
|
|
1
1
|
import {describe, expect, it, mock, spyOn} from "bun:test";
|
|
2
2
|
import {act, fireEvent, render, waitFor} from "@testing-library/react-native";
|
|
3
|
+
import {assert} from "chai";
|
|
3
4
|
|
|
4
5
|
import {Button} from "./Button";
|
|
5
6
|
import {isMobileDevice} from "./MediaQuery";
|
|
6
7
|
import * as ThemeModule from "./Theme";
|
|
7
8
|
import {renderWithIcons, renderWithTheme, TEST_CUSTOM_ICON_TEST_ID} from "./test-utils";
|
|
9
|
+
import {Unifier} from "./Unifier";
|
|
8
10
|
import * as Utilities from "./Utilities";
|
|
9
11
|
|
|
10
12
|
interface PressableTestProps {
|
|
11
13
|
onPress: () => Promise<void>;
|
|
12
14
|
}
|
|
13
15
|
|
|
16
|
+
interface CancelablePress {
|
|
17
|
+
(): void;
|
|
18
|
+
cancel: () => void;
|
|
19
|
+
}
|
|
20
|
+
|
|
14
21
|
describe("Button", () => {
|
|
15
22
|
it("renders correctly with default props", () => {
|
|
16
23
|
const {toJSON} = renderWithTheme(<Button onClick={() => {}} text="Click me" />);
|
|
@@ -153,6 +160,104 @@ describe("Button", () => {
|
|
|
153
160
|
expect(handleClick).toHaveBeenCalledTimes(1);
|
|
154
161
|
});
|
|
155
162
|
|
|
163
|
+
it("skips equivalent parent updates and redraws changed props", () => {
|
|
164
|
+
const handleClick = mock(() => Promise.resolve());
|
|
165
|
+
const mobileDeviceMock = isMobileDevice as ReturnType<typeof mock>;
|
|
166
|
+
mobileDeviceMock.mockClear();
|
|
167
|
+
const {rerender} = renderWithTheme(<Button onClick={handleClick} text="Stable" />);
|
|
168
|
+
const initialRenderCalls = mobileDeviceMock.mock.calls.length;
|
|
169
|
+
|
|
170
|
+
rerender(<Button onClick={handleClick} text="Stable" />);
|
|
171
|
+
assert.equal(mobileDeviceMock.mock.calls.length, initialRenderCalls);
|
|
172
|
+
|
|
173
|
+
rerender(<Button onClick={handleClick} text="Changed" />);
|
|
174
|
+
assert.equal(mobileDeviceMock.mock.calls.length, initialRenderCalls + 1);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("prevents another press while an async handler remains pending", async () => {
|
|
178
|
+
let resolveClick: (() => void) | undefined;
|
|
179
|
+
const handleClick = mock(
|
|
180
|
+
() =>
|
|
181
|
+
new Promise<void>((resolve) => {
|
|
182
|
+
resolveClick = resolve;
|
|
183
|
+
})
|
|
184
|
+
);
|
|
185
|
+
const {getByTestId} = renderWithTheme(
|
|
186
|
+
<Button onClick={handleClick} testID="delayed-button" text="Delayed" />
|
|
187
|
+
);
|
|
188
|
+
const button = getByTestId("delayed-button");
|
|
189
|
+
|
|
190
|
+
await act(async () => {
|
|
191
|
+
fireEvent.press(button);
|
|
192
|
+
await new Promise((resolve) => setTimeout(resolve, 600));
|
|
193
|
+
fireEvent.press(button);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
assert.lengthOf(handleClick.mock.calls, 1);
|
|
197
|
+
|
|
198
|
+
await act(async () => {
|
|
199
|
+
resolveClick?.();
|
|
200
|
+
await Promise.resolve();
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it("cancels the debounced press handler on unmount", () => {
|
|
205
|
+
const {getByTestId, unmount} = renderWithTheme(
|
|
206
|
+
<Button onClick={() => {}} testID="cleanup-button" text="Cleanup" />
|
|
207
|
+
);
|
|
208
|
+
const onPress = getByTestId("cleanup-button").props.onPress as CancelablePress;
|
|
209
|
+
const cancelSpy = spyOn(onPress, "cancel");
|
|
210
|
+
|
|
211
|
+
unmount();
|
|
212
|
+
|
|
213
|
+
assert.lengthOf(cancelSpy.mock.calls, 1);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it("does not start an action when unmounted during async press setup", async () => {
|
|
217
|
+
let resolveHaptic: (() => void) | undefined;
|
|
218
|
+
const hapticSpy = spyOn(Unifier.utils, "haptic").mockImplementation(
|
|
219
|
+
() =>
|
|
220
|
+
new Promise<void>((resolve) => {
|
|
221
|
+
resolveHaptic = resolve;
|
|
222
|
+
})
|
|
223
|
+
);
|
|
224
|
+
const handleClick = mock(() => Promise.resolve());
|
|
225
|
+
const {getByTestId, unmount} = renderWithTheme(
|
|
226
|
+
<Button onClick={handleClick} testID="unmount-button" text="Unmount" />
|
|
227
|
+
);
|
|
228
|
+
|
|
229
|
+
fireEvent.press(getByTestId("unmount-button"));
|
|
230
|
+
unmount();
|
|
231
|
+
await act(async () => {
|
|
232
|
+
resolveHaptic?.();
|
|
233
|
+
await Promise.resolve();
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
assert.lengthOf(handleClick.mock.calls, 0);
|
|
237
|
+
hapticSpy.mockRestore();
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it("keeps the confirmation modal path absent for plain buttons", async () => {
|
|
241
|
+
const handleClick = mock(() => Promise.resolve());
|
|
242
|
+
const {getByText, queryByText} = renderWithTheme(
|
|
243
|
+
<Button
|
|
244
|
+
confirmationText="Plain buttons never show this"
|
|
245
|
+
modalTitle="Unused confirmation"
|
|
246
|
+
onClick={handleClick}
|
|
247
|
+
text="Plain"
|
|
248
|
+
/>
|
|
249
|
+
);
|
|
250
|
+
|
|
251
|
+
await act(async () => {
|
|
252
|
+
fireEvent.press(getByText("Plain"));
|
|
253
|
+
await Promise.resolve();
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
assert.isNull(queryByText("Unused confirmation"));
|
|
257
|
+
assert.isNull(queryByText("Plain buttons never show this"));
|
|
258
|
+
assert.lengthOf(handleClick.mock.calls, 1);
|
|
259
|
+
});
|
|
260
|
+
|
|
156
261
|
// Confirmation modal tests
|
|
157
262
|
it("renders with confirmation modal props", () => {
|
|
158
263
|
const {toJSON} = renderWithTheme(
|
|
@@ -264,6 +369,108 @@ describe("Button", () => {
|
|
|
264
369
|
expect(handleClick).not.toHaveBeenCalled();
|
|
265
370
|
});
|
|
266
371
|
|
|
372
|
+
it("reopens the confirmation modal immediately after Cancel", async () => {
|
|
373
|
+
const handleClick = mock(() => Promise.resolve());
|
|
374
|
+
const {getByText, queryByText} = renderWithTheme(
|
|
375
|
+
<Button
|
|
376
|
+
confirmationText="Confirm action?"
|
|
377
|
+
modalTitle="Reopen Title"
|
|
378
|
+
onClick={handleClick}
|
|
379
|
+
text="Press Me"
|
|
380
|
+
withConfirmation
|
|
381
|
+
/>
|
|
382
|
+
);
|
|
383
|
+
|
|
384
|
+
await act(async () => {
|
|
385
|
+
fireEvent.press(getByText("Press Me"));
|
|
386
|
+
await Promise.resolve();
|
|
387
|
+
});
|
|
388
|
+
await waitFor(() => {
|
|
389
|
+
expect(queryByText("Reopen Title")).toBeTruthy();
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
await act(async () => {
|
|
393
|
+
fireEvent.press(getByText("Cancel"));
|
|
394
|
+
await Promise.resolve();
|
|
395
|
+
});
|
|
396
|
+
await waitFor(() => {
|
|
397
|
+
assert.isNull(queryByText("Reopen Title"));
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
// Re-press well inside the 500ms press debounce window.
|
|
401
|
+
await act(async () => {
|
|
402
|
+
fireEvent.press(getByText("Press Me"));
|
|
403
|
+
await Promise.resolve();
|
|
404
|
+
});
|
|
405
|
+
await waitFor(() => {
|
|
406
|
+
expect(queryByText("Reopen Title")).toBeTruthy();
|
|
407
|
+
});
|
|
408
|
+
assert.lengthOf(handleClick.mock.calls, 0);
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
it("reopens the confirmation modal immediately after Confirm", async () => {
|
|
412
|
+
const handleClick = mock(() => Promise.resolve());
|
|
413
|
+
const {getByText, queryByText} = renderWithTheme(
|
|
414
|
+
<Button
|
|
415
|
+
confirmationText="Confirm action?"
|
|
416
|
+
modalTitle="Confirm Reopen"
|
|
417
|
+
onClick={handleClick}
|
|
418
|
+
text="Press Me"
|
|
419
|
+
withConfirmation
|
|
420
|
+
/>
|
|
421
|
+
);
|
|
422
|
+
|
|
423
|
+
await act(async () => {
|
|
424
|
+
fireEvent.press(getByText("Press Me"));
|
|
425
|
+
await Promise.resolve();
|
|
426
|
+
});
|
|
427
|
+
await waitFor(() => {
|
|
428
|
+
expect(queryByText("Confirm Reopen")).toBeTruthy();
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
await act(async () => {
|
|
432
|
+
fireEvent.press(getByText("Confirm"));
|
|
433
|
+
await Promise.resolve();
|
|
434
|
+
});
|
|
435
|
+
await waitFor(() => {
|
|
436
|
+
assert.isNull(queryByText("Confirm Reopen"));
|
|
437
|
+
});
|
|
438
|
+
assert.lengthOf(handleClick.mock.calls, 1);
|
|
439
|
+
|
|
440
|
+
await act(async () => {
|
|
441
|
+
fireEvent.press(getByText("Press Me"));
|
|
442
|
+
await Promise.resolve();
|
|
443
|
+
});
|
|
444
|
+
await waitFor(() => {
|
|
445
|
+
expect(queryByText("Confirm Reopen")).toBeTruthy();
|
|
446
|
+
});
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
it("still debounces repeated presses that do not open the modal", async () => {
|
|
450
|
+
const handleClick = mock(() => Promise.resolve());
|
|
451
|
+
const hapticSpy = spyOn(Unifier.utils, "haptic");
|
|
452
|
+
const {getByText} = renderWithTheme(
|
|
453
|
+
<Button
|
|
454
|
+
confirmationText="Confirm action?"
|
|
455
|
+
modalTitle="Debounced Title"
|
|
456
|
+
onClick={handleClick}
|
|
457
|
+
text="Press Me"
|
|
458
|
+
withConfirmation
|
|
459
|
+
/>
|
|
460
|
+
);
|
|
461
|
+
|
|
462
|
+
const hapticCallsBefore = hapticSpy.mock.calls.length;
|
|
463
|
+
await act(async () => {
|
|
464
|
+
fireEvent.press(getByText("Press Me"));
|
|
465
|
+
fireEvent.press(getByText("Press Me"));
|
|
466
|
+
fireEvent.press(getByText("Press Me"));
|
|
467
|
+
await Promise.resolve();
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
assert.equal(hapticSpy.mock.calls.length - hapticCallsBefore, 1);
|
|
471
|
+
hapticSpy.mockRestore();
|
|
472
|
+
});
|
|
473
|
+
|
|
267
474
|
it("renders with tooltip on desktop (wrapped in Tooltip)", () => {
|
|
268
475
|
const {toJSON} = renderWithTheme(
|
|
269
476
|
<Button onClick={() => {}} text="Hover me" tooltipText="Tooltip text" />
|
package/src/Button.tsx
CHANGED
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
PressableWithoutFeedback,
|
|
8
8
|
} from "pressto";
|
|
9
9
|
import type React from "react";
|
|
10
|
-
import {lazy, Suspense, useCallback, useMemo, useState} from "react";
|
|
10
|
+
import {lazy, memo, Suspense, useCallback, useEffect, useMemo, useRef, useState} from "react";
|
|
11
11
|
import {ActivityIndicator, Pressable, type PressableProps, Text, View} from "react-native";
|
|
12
12
|
|
|
13
13
|
import {Box} from "./Box";
|
|
@@ -35,25 +35,65 @@ const PRESSABLE_BY_ANIMATION: Record<
|
|
|
35
35
|
|
|
36
36
|
type ButtonPressableProps = CustomPressableProps & PressableProps;
|
|
37
37
|
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
interface ButtonVisualProps extends Omit<ButtonProps, "onClick"> {
|
|
39
|
+
children?: React.ReactNode;
|
|
40
|
+
isLoading: boolean;
|
|
41
|
+
onPress: () => void;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
interface DebouncedPress {
|
|
45
|
+
endPressCooldown: () => void;
|
|
46
|
+
onPress: () => void;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const useDebouncedPress = (handlePress: () => Promise<void>): DebouncedPress => {
|
|
50
|
+
const debouncedHandlePress = useMemo(
|
|
51
|
+
() => debounce(handlePress, 500, {leading: true, trailing: false}),
|
|
52
|
+
[handlePress]
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
// Cancel retained debounce timers when the callback changes or the button unmounts.
|
|
56
|
+
useEffect((): (() => void) => {
|
|
57
|
+
return (): void => {
|
|
58
|
+
debouncedHandlePress.cancel();
|
|
59
|
+
};
|
|
60
|
+
}, [debouncedHandlePress]);
|
|
61
|
+
|
|
62
|
+
const endPressCooldown = useCallback((): void => {
|
|
63
|
+
debouncedHandlePress.cancel();
|
|
64
|
+
}, [debouncedHandlePress]);
|
|
65
|
+
|
|
66
|
+
return {endPressCooldown, onPress: debouncedHandlePress};
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const useMountedRef = (): React.RefObject<boolean> => {
|
|
70
|
+
const isMountedRef = useRef(true);
|
|
71
|
+
|
|
72
|
+
// Prevent async press completions from updating state after the button unmounts.
|
|
73
|
+
useEffect((): (() => void) => {
|
|
74
|
+
return (): void => {
|
|
75
|
+
isMountedRef.current = false;
|
|
76
|
+
};
|
|
77
|
+
}, []);
|
|
78
|
+
|
|
79
|
+
return isMountedRef;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const ButtonVisual: React.FC<ButtonVisualProps> = ({
|
|
83
|
+
children,
|
|
40
84
|
disabled = false,
|
|
41
85
|
fullWidth = false,
|
|
42
86
|
iconName,
|
|
43
87
|
iconPosition = "left",
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
modalSubTitle,
|
|
88
|
+
isLoading,
|
|
89
|
+
onPress,
|
|
47
90
|
pressAnimation = DEFAULT_BUTTON_PRESS_ANIMATION,
|
|
48
91
|
size = "default",
|
|
49
92
|
testID,
|
|
50
93
|
text,
|
|
51
94
|
variant = "primary",
|
|
52
95
|
withConfirmation = false,
|
|
53
|
-
onClick,
|
|
54
96
|
}) => {
|
|
55
|
-
const [loading, setLoading] = useState(propsLoading);
|
|
56
|
-
const [showConfirmation, setShowConfirmation] = useState(false);
|
|
57
97
|
const {theme} = useTheme();
|
|
58
98
|
const CustomIcon = useCustomIcon(iconName);
|
|
59
99
|
|
|
@@ -93,36 +133,11 @@ const ButtonComponent: React.FC<ButtonProps> = ({
|
|
|
93
133
|
};
|
|
94
134
|
}, [disabled, variant, theme]);
|
|
95
135
|
|
|
96
|
-
const handlePress = useCallback(async (): Promise<void> => {
|
|
97
|
-
await Unifier.utils.haptic();
|
|
98
|
-
setLoading(true);
|
|
99
|
-
|
|
100
|
-
try {
|
|
101
|
-
// If a confirmation is required, and the confirmation modal is not currently open,
|
|
102
|
-
// open it.
|
|
103
|
-
if (withConfirmation && !showConfirmation) {
|
|
104
|
-
setShowConfirmation(true);
|
|
105
|
-
} else if (!withConfirmation && onClick) {
|
|
106
|
-
// If a confirmation is not required, perform the action.
|
|
107
|
-
await onClick();
|
|
108
|
-
}
|
|
109
|
-
} catch (error) {
|
|
110
|
-
setLoading(false);
|
|
111
|
-
throw error;
|
|
112
|
-
}
|
|
113
|
-
setLoading(false);
|
|
114
|
-
}, [onClick, showConfirmation, withConfirmation]);
|
|
115
|
-
|
|
116
|
-
const debouncedHandlePress = useMemo(
|
|
117
|
-
() => debounce(handlePress, 500, {leading: true, trailing: false}),
|
|
118
|
-
[handlePress]
|
|
119
|
-
);
|
|
120
|
-
|
|
121
136
|
if (!theme) {
|
|
122
137
|
return null;
|
|
123
138
|
}
|
|
124
139
|
|
|
125
|
-
const isPressDisabled = disabled ||
|
|
140
|
+
const isPressDisabled = disabled || isLoading;
|
|
126
141
|
const PressableComponent = (
|
|
127
142
|
isPressDisabled ? Pressable : PRESSABLE_BY_ANIMATION[pressAnimation]
|
|
128
143
|
) as React.ComponentType<ButtonPressableProps>;
|
|
@@ -137,7 +152,7 @@ const ButtonComponent: React.FC<ButtonProps> = ({
|
|
|
137
152
|
accessibilityRole="button"
|
|
138
153
|
accessibilityState={{disabled: isPressDisabled}}
|
|
139
154
|
{...pressableInteractionProps}
|
|
140
|
-
onPress={
|
|
155
|
+
onPress={onPress}
|
|
141
156
|
style={{
|
|
142
157
|
alignItems: "center",
|
|
143
158
|
alignSelf: fullWidth ? "stretch" : "flex-start",
|
|
@@ -173,22 +188,96 @@ const ButtonComponent: React.FC<ButtonProps> = ({
|
|
|
173
188
|
)}
|
|
174
189
|
<Text style={{color, fontSize: size === "sm" ? 14 : 16, fontWeight: "700"}}>{text}</Text>
|
|
175
190
|
</View>
|
|
176
|
-
{
|
|
191
|
+
{isLoading && (
|
|
177
192
|
<Box marginLeft={2}>
|
|
178
193
|
<ActivityIndicator color={color} size="small" />
|
|
179
194
|
</Box>
|
|
180
195
|
)}
|
|
181
196
|
</View>
|
|
182
|
-
{
|
|
197
|
+
{children}
|
|
198
|
+
</PressableComponent>
|
|
199
|
+
);
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
const PlainButton: React.FC<ButtonProps> = (props) => {
|
|
203
|
+
const {loading = false, onClick} = props;
|
|
204
|
+
const {
|
|
205
|
+
confirmationText: _confirmationText,
|
|
206
|
+
modalSubTitle: _modalSubTitle,
|
|
207
|
+
modalTitle: _modalTitle,
|
|
208
|
+
onClick: _onClick,
|
|
209
|
+
withConfirmation: _withConfirmation,
|
|
210
|
+
...visualProps
|
|
211
|
+
} = props;
|
|
212
|
+
const [isHandlingPress, setIsHandlingPress] = useState(false);
|
|
213
|
+
const isMountedRef = useMountedRef();
|
|
214
|
+
const handlePress = useCallback(async (): Promise<void> => {
|
|
215
|
+
await Unifier.utils.haptic();
|
|
216
|
+
if (!isMountedRef.current) {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
setIsHandlingPress(true);
|
|
220
|
+
|
|
221
|
+
try {
|
|
222
|
+
await onClick();
|
|
223
|
+
} catch (error) {
|
|
224
|
+
if (isMountedRef.current) {
|
|
225
|
+
setIsHandlingPress(false);
|
|
226
|
+
}
|
|
227
|
+
throw error;
|
|
228
|
+
}
|
|
229
|
+
if (isMountedRef.current) {
|
|
230
|
+
setIsHandlingPress(false);
|
|
231
|
+
}
|
|
232
|
+
}, [isMountedRef, onClick]);
|
|
233
|
+
const {onPress} = useDebouncedPress(handlePress);
|
|
234
|
+
|
|
235
|
+
return <ButtonVisual {...visualProps} isLoading={loading || isHandlingPress} onPress={onPress} />;
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
const ConfirmationButton: React.FC<ButtonProps> = ({
|
|
239
|
+
confirmationText = "Are you sure you want to continue?",
|
|
240
|
+
loading = false,
|
|
241
|
+
modalSubTitle,
|
|
242
|
+
modalTitle = "Confirm",
|
|
243
|
+
onClick,
|
|
244
|
+
...props
|
|
245
|
+
}) => {
|
|
246
|
+
const [showConfirmation, setShowConfirmation] = useState(false);
|
|
247
|
+
const isMountedRef = useMountedRef();
|
|
248
|
+
const handlePress = useCallback(async (): Promise<void> => {
|
|
249
|
+
await Unifier.utils.haptic();
|
|
250
|
+
if (!isMountedRef.current) {
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
setShowConfirmation(true);
|
|
254
|
+
}, [isMountedRef]);
|
|
255
|
+
const {endPressCooldown, onPress} = useDebouncedPress(handlePress);
|
|
256
|
+
// The press debounce only guards opening the modal, so closing it must clear the
|
|
257
|
+
// cooldown or a quick reopen after Cancel, dismiss, or Confirm would be swallowed.
|
|
258
|
+
const handleDismiss = useCallback((): void => {
|
|
259
|
+
endPressCooldown();
|
|
260
|
+
if (isMountedRef.current) {
|
|
261
|
+
setShowConfirmation(false);
|
|
262
|
+
}
|
|
263
|
+
}, [endPressCooldown, isMountedRef]);
|
|
264
|
+
const handleConfirm = useCallback(async (): Promise<void> => {
|
|
265
|
+
await onClick();
|
|
266
|
+
endPressCooldown();
|
|
267
|
+
if (isMountedRef.current) {
|
|
268
|
+
setShowConfirmation(false);
|
|
269
|
+
}
|
|
270
|
+
}, [endPressCooldown, isMountedRef, onClick]);
|
|
271
|
+
|
|
272
|
+
return (
|
|
273
|
+
<ButtonVisual {...props} isLoading={loading} onPress={onPress} withConfirmation>
|
|
274
|
+
{showConfirmation && (
|
|
183
275
|
<Suspense fallback={null}>
|
|
184
276
|
<LazyModal
|
|
185
|
-
onDismiss={
|
|
186
|
-
primaryButtonOnClick={
|
|
187
|
-
await onClick();
|
|
188
|
-
setShowConfirmation(false);
|
|
189
|
-
}}
|
|
277
|
+
onDismiss={handleDismiss}
|
|
278
|
+
primaryButtonOnClick={handleConfirm}
|
|
190
279
|
primaryButtonText="Confirm"
|
|
191
|
-
secondaryButtonOnClick={
|
|
280
|
+
secondaryButtonOnClick={handleDismiss}
|
|
192
281
|
secondaryButtonText="Cancel"
|
|
193
282
|
subtitle={modalSubTitle}
|
|
194
283
|
text={confirmationText}
|
|
@@ -197,13 +286,18 @@ const ButtonComponent: React.FC<ButtonProps> = ({
|
|
|
197
286
|
/>
|
|
198
287
|
</Suspense>
|
|
199
288
|
)}
|
|
200
|
-
</
|
|
289
|
+
</ButtonVisual>
|
|
201
290
|
);
|
|
202
291
|
};
|
|
203
292
|
|
|
204
|
-
|
|
293
|
+
const ButtonRender: React.FC<ButtonProps> = (props) => {
|
|
205
294
|
const {tooltipText, tooltipIdealPosition, tooltipIncludeArrow = false} = props;
|
|
206
295
|
const isMobileOrNative = isMobileDevice() || isNative();
|
|
296
|
+
const button = props.withConfirmation ? (
|
|
297
|
+
<ConfirmationButton {...props} />
|
|
298
|
+
) : (
|
|
299
|
+
<PlainButton {...props} />
|
|
300
|
+
);
|
|
207
301
|
|
|
208
302
|
if (tooltipText && !isMobileOrNative) {
|
|
209
303
|
return (
|
|
@@ -212,10 +306,14 @@ export const Button: React.FC<ButtonProps> = (props) => {
|
|
|
212
306
|
includeArrow={tooltipIncludeArrow}
|
|
213
307
|
text={tooltipText}
|
|
214
308
|
>
|
|
215
|
-
|
|
309
|
+
{button}
|
|
216
310
|
</Tooltip>
|
|
217
311
|
);
|
|
218
312
|
}
|
|
219
313
|
|
|
220
|
-
return
|
|
314
|
+
return button;
|
|
221
315
|
};
|
|
316
|
+
|
|
317
|
+
const MemoizedButton = memo(ButtonRender);
|
|
318
|
+
|
|
319
|
+
export const Button: React.FC<ButtonProps> = MemoizedButton;
|