@terreno/ui 57.3.0 → 57.4.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/dist/AnnouncementBanner.d.ts +12 -0
- package/dist/AnnouncementBanner.js +61 -0
- package/dist/AnnouncementBanner.js.map +1 -0
- package/dist/AnnouncementNavigator.d.ts +11 -0
- package/dist/AnnouncementNavigator.js +189 -0
- package/dist/AnnouncementNavigator.js.map +1 -0
- package/dist/AnnouncementScreen.d.ts +12 -0
- package/dist/AnnouncementScreen.js +28 -0
- package/dist/AnnouncementScreen.js.map +1 -0
- package/dist/AreaChart.d.ts +5 -0
- package/dist/AreaChart.js +69 -0
- package/dist/AreaChart.js.map +1 -0
- package/dist/BarChart.d.ts +5 -0
- package/dist/BarChart.js +89 -0
- package/dist/BarChart.js.map +1 -0
- package/dist/Button.js +17 -3
- package/dist/Button.js.map +1 -1
- package/dist/Common.d.ts +96 -0
- package/dist/Common.js.map +1 -1
- package/dist/DashboardGrid.d.ts +4 -0
- package/dist/DashboardGrid.js +38 -0
- package/dist/DashboardGrid.js.map +1 -0
- package/dist/DataTable.js +83 -11
- package/dist/DataTable.js.map +1 -1
- package/dist/DonutChart.d.ts +5 -0
- package/dist/DonutChart.js +84 -0
- package/dist/DonutChart.js.map +1 -0
- package/dist/Filter.js +12 -2
- package/dist/Filter.js.map +1 -1
- package/dist/GPTChat.d.ts +7 -1
- package/dist/GPTChat.js +28 -15
- package/dist/GPTChat.js.map +1 -1
- package/dist/LineChart.d.ts +5 -0
- package/dist/LineChart.js +68 -0
- package/dist/LineChart.js.map +1 -0
- package/dist/MarkdownEmbed.d.ts +6 -0
- package/dist/MarkdownEmbed.js +17 -0
- package/dist/MarkdownEmbed.js.map +1 -0
- package/dist/MarkdownView.js +38 -3
- package/dist/MarkdownView.js.map +1 -1
- package/dist/Modal.js +12 -10
- package/dist/Modal.js.map +1 -1
- package/dist/ModalSheet.d.ts +5 -1
- package/dist/ModalSheet.js +31 -13
- package/dist/ModalSheet.js.map +1 -1
- package/dist/MultiselectField.js +4 -9
- package/dist/MultiselectField.js.map +1 -1
- package/dist/NotificationBell.d.ts +16 -0
- package/dist/NotificationBell.js +15 -0
- package/dist/NotificationBell.js.map +1 -0
- package/dist/NotificationInbox.d.ts +21 -0
- package/dist/NotificationInbox.js +41 -0
- package/dist/NotificationInbox.js.map +1 -0
- package/dist/NotificationPreferences.d.ts +14 -0
- package/dist/NotificationPreferences.js +32 -0
- package/dist/NotificationPreferences.js.map +1 -0
- package/dist/TextField.js +83 -6
- package/dist/TextField.js.map +1 -1
- package/dist/announcementBuildVersion.d.ts +6 -0
- package/dist/announcementBuildVersion.js +21 -0
- package/dist/announcementBuildVersion.js.map +1 -0
- package/dist/announcementFrequency.d.ts +42 -0
- package/dist/announcementFrequency.js +163 -0
- package/dist/announcementFrequency.js.map +1 -0
- package/dist/announcementPlatform.d.ts +2 -0
- package/dist/announcementPlatform.js +11 -0
- package/dist/announcementPlatform.js.map +1 -0
- package/dist/charts/ChartFrame.d.ts +12 -0
- package/dist/charts/ChartFrame.js +13 -0
- package/dist/charts/ChartFrame.js.map +1 -0
- package/dist/charts/bars.d.ts +18 -0
- package/dist/charts/bars.js +29 -0
- package/dist/charts/bars.js.map +1 -0
- package/dist/charts/layout.d.ts +47 -0
- package/dist/charts/layout.js +56 -0
- package/dist/charts/layout.js.map +1 -0
- package/dist/charts/paths.d.ts +31 -0
- package/dist/charts/paths.js +52 -0
- package/dist/charts/paths.js.map +1 -0
- package/dist/charts/scales.d.ts +6 -0
- package/dist/charts/scales.js +43 -0
- package/dist/charts/scales.js.map +1 -0
- package/dist/charts/theme.d.ts +9 -0
- package/dist/charts/theme.js +15 -0
- package/dist/charts/theme.js.map +1 -0
- package/dist/charts/types/chartTypes.d.ts +17 -0
- package/dist/charts/types/chartTypes.js +2 -0
- package/dist/charts/types/chartTypes.js.map +1 -0
- package/dist/dashboardGridLayout.d.ts +22 -0
- package/dist/dashboardGridLayout.js +33 -0
- package/dist/dashboardGridLayout.js.map +1 -0
- package/dist/dataTableFilters.d.ts +33 -0
- package/dist/dataTableFilters.js +221 -0
- package/dist/dataTableFilters.js.map +1 -0
- package/dist/dataTableListQuery.d.ts +42 -0
- package/dist/dataTableListQuery.js +161 -0
- package/dist/dataTableListQuery.js.map +1 -0
- package/dist/index.d.ts +18 -1
- package/dist/index.js +11 -1
- package/dist/index.js.map +1 -1
- package/dist/lazyBoundaries/heavyOptionalExports.d.ts +12 -0
- package/dist/lazyBoundaries/heavyOptionalExports.js +8 -0
- package/dist/lazyBoundaries/heavyOptionalExports.js.map +1 -1
- package/dist/markdownEmbeds.d.ts +6 -0
- package/dist/markdownEmbeds.js +59 -0
- package/dist/markdownEmbeds.js.map +1 -0
- package/dist/testing/resolveTestId.js +2 -1
- package/dist/testing/resolveTestId.js.map +1 -1
- package/dist/testing/types.d.ts +3 -0
- package/dist/textFieldOscillationGuard.d.ts +25 -0
- package/dist/textFieldOscillationGuard.js +41 -0
- package/dist/textFieldOscillationGuard.js.map +1 -0
- package/dist/useAcknowledgeAnnouncement.d.ts +71 -0
- package/dist/useAcknowledgeAnnouncement.js +94 -0
- package/dist/useAcknowledgeAnnouncement.js.map +1 -0
- package/dist/useAnnouncements.d.ts +73 -0
- package/dist/useAnnouncements.js +66 -0
- package/dist/useAnnouncements.js.map +1 -0
- package/package.json +7 -18
- package/src/AnnouncementBanner.test.tsx +267 -0
- package/src/AnnouncementBanner.tsx +128 -0
- package/src/AnnouncementNavigator.test.tsx +759 -0
- package/src/AnnouncementNavigator.tsx +287 -0
- package/src/AnnouncementScreen.test.tsx +97 -0
- package/src/AnnouncementScreen.tsx +70 -0
- package/src/AreaChart.test.tsx +68 -0
- package/src/AreaChart.tsx +194 -0
- package/src/BarChart.test.tsx +96 -0
- package/src/BarChart.tsx +208 -0
- package/src/Button.test.tsx +66 -0
- package/src/Button.tsx +17 -2
- package/src/Common.ts +95 -0
- package/src/ConsentNavigator.test.tsx +55 -0
- package/src/DashboardGrid.test.tsx +75 -0
- package/src/DashboardGrid.tsx +69 -0
- package/src/DataTable.test.tsx +296 -1
- package/src/DataTable.tsx +210 -18
- package/src/DonutChart.test.tsx +121 -0
- package/src/DonutChart.tsx +160 -0
- package/src/EmojiSelector.test.tsx +51 -0
- package/src/Field.test.tsx +18 -0
- package/src/Filter.test.tsx +25 -0
- package/src/Filter.tsx +38 -7
- package/src/GPTChat.test.tsx +107 -0
- package/src/GPTChat.tsx +176 -86
- package/src/LineChart.test.tsx +166 -0
- package/src/LineChart.tsx +190 -0
- package/src/MarkdownEmbed.test.tsx +34 -0
- package/src/MarkdownEmbed.tsx +43 -0
- package/src/MarkdownView.test.tsx +59 -2
- package/src/MarkdownView.tsx +51 -3
- package/src/Modal.test.tsx +107 -14
- package/src/Modal.tsx +20 -8
- package/src/ModalSheet.test.tsx +121 -9
- package/src/ModalSheet.tsx +46 -11
- package/src/MultiselectField.test.tsx +21 -0
- package/src/MultiselectField.tsx +11 -13
- package/src/NotificationBell.test.tsx +91 -0
- package/src/NotificationBell.tsx +77 -0
- package/src/NotificationInbox.test.tsx +130 -0
- package/src/NotificationInbox.tsx +144 -0
- package/src/NotificationPreferences.test.tsx +25 -0
- package/src/NotificationPreferences.tsx +75 -0
- package/src/RootImportRegression.test.tsx +20 -0
- package/src/TextField.test.tsx +421 -0
- package/src/TextField.tsx +121 -4
- package/src/__snapshots__/Field.test.tsx.snap +22 -0
- package/src/__snapshots__/ModalSheet.test.tsx.snap +4 -1
- package/src/__snapshots__/TextArea.test.tsx.snap +5 -5
- package/src/__snapshots__/TextField.test.tsx.snap +5 -5
- package/src/announcementBuildVersion.test.ts +43 -0
- package/src/announcementBuildVersion.ts +21 -0
- package/src/announcementFrequency.test.ts +245 -0
- package/src/announcementFrequency.ts +246 -0
- package/src/announcementPlatform.test.ts +27 -0
- package/src/announcementPlatform.ts +13 -0
- package/src/charts/ChartFrame.tsx +67 -0
- package/src/charts/bars.test.ts +56 -0
- package/src/charts/bars.ts +53 -0
- package/src/charts/layout.test.ts +80 -0
- package/src/charts/layout.ts +99 -0
- package/src/charts/paths.test.ts +79 -0
- package/src/charts/paths.ts +100 -0
- package/src/charts/scales.test.ts +59 -0
- package/src/charts/scales.ts +60 -0
- package/src/charts/theme.test.ts +22 -0
- package/src/charts/theme.ts +24 -0
- package/src/charts/types/chartTypes.ts +19 -0
- package/src/dashboardGridLayout.test.ts +90 -0
- package/src/dashboardGridLayout.ts +60 -0
- package/src/dataTableFilters.test.tsx +552 -0
- package/src/dataTableFilters.tsx +441 -0
- package/src/dataTableListQuery.test.ts +220 -0
- package/src/dataTableListQuery.ts +201 -0
- package/src/index.tsx +21 -1
- package/src/lazyBoundaries/heavyOptionalExports.tsx +28 -0
- package/src/login/__snapshots__/LoginScreen.test.tsx.snap +22 -0
- package/src/markdownEmbeds.test.ts +53 -0
- package/src/markdownEmbeds.ts +62 -0
- package/src/pdfHtmlTemplate.test.ts +119 -0
- package/src/signUp/__snapshots__/SignUpScreen.test.tsx.snap +22 -0
- package/src/testing/resolveTestId.test.ts +3 -0
- package/src/testing/resolveTestId.ts +1 -0
- package/src/testing/types.ts +3 -0
- package/src/textFieldOscillationGuard.test.ts +63 -0
- package/src/textFieldOscillationGuard.ts +89 -0
- package/src/types/react-native-markdown-display.d.ts +6 -0
- package/src/types/react-native-swiper-flatlist.d.ts +5 -5
- package/src/useAcknowledgeAnnouncement.test.ts +223 -0
- package/src/useAcknowledgeAnnouncement.ts +166 -0
- package/src/useAnnouncements.test.ts +226 -0
- package/src/useAnnouncements.ts +150 -0
package/src/TextField.test.tsx
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import {afterEach, beforeEach, describe, expect, it, mock, spyOn} from "bun:test";
|
|
2
2
|
import {act, fireEvent, userEvent} from "@testing-library/react-native";
|
|
3
|
+
import {assert} from "chai";
|
|
4
|
+
import type {ReactElement} from "react";
|
|
5
|
+
import type {TextInput} from "react-native";
|
|
3
6
|
import {TextField} from "./TextField";
|
|
4
7
|
import {renderWithTheme} from "./test-utils";
|
|
5
8
|
|
|
@@ -21,6 +24,53 @@ describe("TextField", () => {
|
|
|
21
24
|
});
|
|
22
25
|
|
|
23
26
|
describe("basic rendering", () => {
|
|
27
|
+
it("warns when the device timezone cannot be determined", () => {
|
|
28
|
+
mock.module("expo-localization", () => ({
|
|
29
|
+
getCalendars: mock(() => []),
|
|
30
|
+
getLocales: mock(() => [
|
|
31
|
+
{
|
|
32
|
+
countryCode: "US",
|
|
33
|
+
decimalSeparator: ".",
|
|
34
|
+
digitGroupingSeparator: ",",
|
|
35
|
+
languageCode: "en",
|
|
36
|
+
measurementSystem: "US",
|
|
37
|
+
temperatureUnit: "F",
|
|
38
|
+
textDirection: "ltr",
|
|
39
|
+
},
|
|
40
|
+
]),
|
|
41
|
+
}));
|
|
42
|
+
const warnSpy = spyOn(console, "warn").mockImplementation(() => {});
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
renderWithTheme(<TextField onChange={mockOnChange} value="" />);
|
|
46
|
+
assert.equal(warnSpy.mock.calls.length, 1);
|
|
47
|
+
assert.equal(warnSpy.mock.calls[0]?.[0], "Could not automatically determine timezone.");
|
|
48
|
+
} finally {
|
|
49
|
+
mock.module("expo-localization", () => ({
|
|
50
|
+
getCalendars: mock(() => [
|
|
51
|
+
{
|
|
52
|
+
calendar: "gregorian",
|
|
53
|
+
id: "gregorian",
|
|
54
|
+
locale: "en-US",
|
|
55
|
+
timeZone: "America/New_York",
|
|
56
|
+
},
|
|
57
|
+
]),
|
|
58
|
+
getLocales: mock(() => [
|
|
59
|
+
{
|
|
60
|
+
countryCode: "US",
|
|
61
|
+
decimalSeparator: ".",
|
|
62
|
+
digitGroupingSeparator: ",",
|
|
63
|
+
languageCode: "en",
|
|
64
|
+
measurementSystem: "US",
|
|
65
|
+
temperatureUnit: "F",
|
|
66
|
+
textDirection: "ltr",
|
|
67
|
+
},
|
|
68
|
+
]),
|
|
69
|
+
}));
|
|
70
|
+
warnSpy.mockRestore();
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
|
|
24
74
|
it("should render with default props", () => {
|
|
25
75
|
const {getByDisplayValue} = renderWithTheme(
|
|
26
76
|
<TextField onChange={mockOnChange} value="test value" />
|
|
@@ -193,6 +243,214 @@ describe("TextField", () => {
|
|
|
193
243
|
expect(input.props.secureTextEntry).toBe(true);
|
|
194
244
|
});
|
|
195
245
|
|
|
246
|
+
describe("password visibility toggle", () => {
|
|
247
|
+
it("should reveal and re-hide the value when the toggle is pressed", () => {
|
|
248
|
+
const {getByDisplayValue, getByTestId} = renderWithTheme(
|
|
249
|
+
<TextField onChange={mockOnChange} testID="pw" type="password" value="hunter2" />
|
|
250
|
+
);
|
|
251
|
+
|
|
252
|
+
expect(getByDisplayValue("hunter2").props.secureTextEntry).toBe(true);
|
|
253
|
+
|
|
254
|
+
fireEvent.press(getByTestId("pw.visibility-toggle"));
|
|
255
|
+
expect(getByDisplayValue("hunter2").props.secureTextEntry).toBe(false);
|
|
256
|
+
|
|
257
|
+
fireEvent.press(getByTestId("pw.visibility-toggle"));
|
|
258
|
+
expect(getByDisplayValue("hunter2").props.secureTextEntry).toBe(true);
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
it("should describe the next action on the toggle", () => {
|
|
262
|
+
const {getByTestId} = renderWithTheme(
|
|
263
|
+
<TextField onChange={mockOnChange} testID="pw" type="password" value="hunter2" />
|
|
264
|
+
);
|
|
265
|
+
|
|
266
|
+
const toggle = getByTestId("pw.visibility-toggle");
|
|
267
|
+
expect(toggle.props.accessibilityLabel).toBe("Show password");
|
|
268
|
+
|
|
269
|
+
fireEvent.press(toggle);
|
|
270
|
+
expect(getByTestId("pw.visibility-toggle").props.accessibilityLabel).toBe("Hide password");
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it("should not render the toggle for non-password types", () => {
|
|
274
|
+
const {queryByTestId} = renderWithTheme(
|
|
275
|
+
<TextField onChange={mockOnChange} testID="email" type="email" value="" />
|
|
276
|
+
);
|
|
277
|
+
|
|
278
|
+
expect(queryByTestId("email.visibility-toggle")).toBeNull();
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it("should not render the toggle when showVisibilityToggle is false", () => {
|
|
282
|
+
const {queryByTestId} = renderWithTheme(
|
|
283
|
+
<TextField
|
|
284
|
+
onChange={mockOnChange}
|
|
285
|
+
showVisibilityToggle={false}
|
|
286
|
+
testID="pw"
|
|
287
|
+
type="password"
|
|
288
|
+
value="hunter2"
|
|
289
|
+
/>
|
|
290
|
+
);
|
|
291
|
+
|
|
292
|
+
expect(queryByTestId("pw.visibility-toggle")).toBeNull();
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it("should keep the value hidden when the field is disabled", () => {
|
|
296
|
+
const {getByDisplayValue, getByTestId} = renderWithTheme(
|
|
297
|
+
<TextField disabled onChange={mockOnChange} testID="pw" type="password" value="hunter2" />
|
|
298
|
+
);
|
|
299
|
+
|
|
300
|
+
fireEvent.press(getByTestId("pw.visibility-toggle"));
|
|
301
|
+
expect(getByDisplayValue("hunter2").props.secureTextEntry).toBe(true);
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
it("should accept a custom toggle test id", () => {
|
|
305
|
+
const {getByTestId} = renderWithTheme(
|
|
306
|
+
<TextField
|
|
307
|
+
onChange={mockOnChange}
|
|
308
|
+
testID="pw"
|
|
309
|
+
testIDs={{visibilityToggle: "custom-toggle"}}
|
|
310
|
+
type="password"
|
|
311
|
+
value="hunter2"
|
|
312
|
+
/>
|
|
313
|
+
);
|
|
314
|
+
|
|
315
|
+
expect(getByTestId("custom-toggle")).toBeTruthy();
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
it("should not blur the input when the visibility toggle is pressed", async () => {
|
|
319
|
+
const {getByDisplayValue, getByTestId} = renderWithTheme(
|
|
320
|
+
<TextField
|
|
321
|
+
onBlur={mockOnBlur}
|
|
322
|
+
onChange={mockOnChange}
|
|
323
|
+
testID="pw"
|
|
324
|
+
trimOnBlur
|
|
325
|
+
type="password"
|
|
326
|
+
value="secret "
|
|
327
|
+
/>
|
|
328
|
+
);
|
|
329
|
+
|
|
330
|
+
const input = getByDisplayValue("secret ");
|
|
331
|
+
fireEvent(input, "focus");
|
|
332
|
+
await act(async () => {
|
|
333
|
+
fireEvent.press(getByTestId("pw.visibility-toggle"));
|
|
334
|
+
await new Promise((resolve) => {
|
|
335
|
+
setTimeout(resolve, 0);
|
|
336
|
+
});
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
expect(mockOnBlur).not.toHaveBeenCalled();
|
|
340
|
+
expect(mockOnChange).not.toHaveBeenCalled();
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
it("refocuses and restores selection on web when toggled while focused", () => {
|
|
344
|
+
const reactNative = require("react-native") as {Platform: {OS: string}};
|
|
345
|
+
const originalOS = reactNative.Platform.OS;
|
|
346
|
+
const originalRequestAnimationFrame = globalThis.requestAnimationFrame;
|
|
347
|
+
const requestAnimationFrameSpy = mock((callback: FrameRequestCallback): number => {
|
|
348
|
+
callback(0);
|
|
349
|
+
return 1;
|
|
350
|
+
});
|
|
351
|
+
reactNative.Platform.OS = "web";
|
|
352
|
+
globalThis.requestAnimationFrame = requestAnimationFrameSpy;
|
|
353
|
+
|
|
354
|
+
try {
|
|
355
|
+
const {getByDisplayValue, getByTestId} = renderWithTheme(
|
|
356
|
+
<TextField onChange={mockOnChange} testID="pw" type="password" value="hunter2" />
|
|
357
|
+
);
|
|
358
|
+
const input = getByDisplayValue("hunter2");
|
|
359
|
+
const webInput: {
|
|
360
|
+
focus: () => void;
|
|
361
|
+
selectionEnd: number;
|
|
362
|
+
selectionStart: number;
|
|
363
|
+
setSelectionRange: (start: number, end: number) => void;
|
|
364
|
+
} = {
|
|
365
|
+
focus: () => {},
|
|
366
|
+
selectionEnd: 0,
|
|
367
|
+
selectionStart: 0,
|
|
368
|
+
setSelectionRange: () => {},
|
|
369
|
+
};
|
|
370
|
+
const focusSpy = mock(() => {});
|
|
371
|
+
const setSelectionRangeSpy = mock((_start: number, _end: number) => {});
|
|
372
|
+
const ref = (input.props as unknown as {ref?: (value: TextInput | null) => void}).ref;
|
|
373
|
+
assert.isFunction(ref);
|
|
374
|
+
webInput.focus = focusSpy;
|
|
375
|
+
webInput.selectionStart = 2;
|
|
376
|
+
webInput.selectionEnd = 5;
|
|
377
|
+
webInput.setSelectionRange = setSelectionRangeSpy;
|
|
378
|
+
|
|
379
|
+
fireEvent(input, "focus");
|
|
380
|
+
ref?.(webInput as unknown as TextInput);
|
|
381
|
+
const preventDefault = mock(() => {});
|
|
382
|
+
fireEvent(getByTestId("pw.visibility-toggle"), "mouseDown", {preventDefault});
|
|
383
|
+
fireEvent.press(getByTestId("pw.visibility-toggle"));
|
|
384
|
+
|
|
385
|
+
assert.equal(requestAnimationFrameSpy.mock.calls.length, 1);
|
|
386
|
+
assert.equal(focusSpy.mock.calls.length, 1);
|
|
387
|
+
assert.deepEqual(setSelectionRangeSpy.mock.calls[0], [2, 5]);
|
|
388
|
+
assert.equal(preventDefault.mock.calls.length, 1);
|
|
389
|
+
} finally {
|
|
390
|
+
reactNative.Platform.OS = originalOS;
|
|
391
|
+
globalThis.requestAnimationFrame = originalRequestAnimationFrame;
|
|
392
|
+
}
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
it("refocuses with a timeout when requestAnimationFrame is unavailable on web", async () => {
|
|
396
|
+
const reactNative = require("react-native") as {Platform: {OS: string}};
|
|
397
|
+
const originalOS = reactNative.Platform.OS;
|
|
398
|
+
const originalRequestAnimationFrame = globalThis.requestAnimationFrame;
|
|
399
|
+
const originalSetTimeout = globalThis.setTimeout;
|
|
400
|
+
let scheduledRefocus: (() => void) | undefined;
|
|
401
|
+
const setTimeoutSpy = spyOn(globalThis, "setTimeout").mockImplementation(
|
|
402
|
+
(handler, timeout, ...args) => {
|
|
403
|
+
scheduledRefocus = handler as unknown as () => void;
|
|
404
|
+
return originalSetTimeout(() => {}, timeout, ...args);
|
|
405
|
+
}
|
|
406
|
+
);
|
|
407
|
+
reactNative.Platform.OS = "web";
|
|
408
|
+
globalThis.requestAnimationFrame = undefined;
|
|
409
|
+
|
|
410
|
+
try {
|
|
411
|
+
const {getByDisplayValue, getByTestId} = renderWithTheme(
|
|
412
|
+
<TextField onChange={mockOnChange} testID="pw" type="password" value="hunter2" />
|
|
413
|
+
);
|
|
414
|
+
const input = getByDisplayValue("hunter2");
|
|
415
|
+
const webInput = {focus: () => void 0};
|
|
416
|
+
const ref = (input.props as unknown as {ref?: (value: TextInput | null) => void}).ref;
|
|
417
|
+
assert.isFunction(ref);
|
|
418
|
+
const focusSpy = mock(() => {});
|
|
419
|
+
webInput.focus = focusSpy;
|
|
420
|
+
|
|
421
|
+
fireEvent(input, "focus");
|
|
422
|
+
ref?.(webInput as unknown as TextInput);
|
|
423
|
+
fireEvent.press(getByTestId("pw.visibility-toggle"));
|
|
424
|
+
|
|
425
|
+
assert.isAtLeast(setTimeoutSpy.mock.calls.length, 1);
|
|
426
|
+
ref?.(webInput as unknown as TextInput);
|
|
427
|
+
scheduledRefocus?.();
|
|
428
|
+
assert.equal(focusSpy.mock.calls.length, 1);
|
|
429
|
+
} finally {
|
|
430
|
+
setTimeoutSpy.mockRestore();
|
|
431
|
+
reactNative.Platform.OS = originalOS;
|
|
432
|
+
globalThis.requestAnimationFrame = originalRequestAnimationFrame;
|
|
433
|
+
}
|
|
434
|
+
});
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
it("calls both onEnter and onSubmitEditing when submitting", () => {
|
|
438
|
+
const onSubmitEditing = mock(() => {});
|
|
439
|
+
const {getByDisplayValue} = renderWithTheme(
|
|
440
|
+
<TextField
|
|
441
|
+
onChange={mockOnChange}
|
|
442
|
+
onEnter={mockOnEnter}
|
|
443
|
+
onSubmitEditing={onSubmitEditing}
|
|
444
|
+
value=""
|
|
445
|
+
/>
|
|
446
|
+
);
|
|
447
|
+
|
|
448
|
+
getByDisplayValue("").props.onSubmitEditing();
|
|
449
|
+
|
|
450
|
+
assert.equal(mockOnEnter.mock.calls.length, 1);
|
|
451
|
+
assert.equal(onSubmitEditing.mock.calls.length, 1);
|
|
452
|
+
});
|
|
453
|
+
|
|
196
454
|
it("should render url type with correct keyboard", () => {
|
|
197
455
|
const {getByDisplayValue} = renderWithTheme(
|
|
198
456
|
<TextField onChange={mockOnChange} type="url" value="" />
|
|
@@ -539,5 +797,168 @@ describe("TextField", () => {
|
|
|
539
797
|
fireEvent(getByDisplayValue(""), "focus");
|
|
540
798
|
expect(mockOnFocus).toHaveBeenCalledTimes(1);
|
|
541
799
|
});
|
|
800
|
+
|
|
801
|
+
it("ignores onChangeText when the text matches the controlled value", async () => {
|
|
802
|
+
const onChange = mock(() => {});
|
|
803
|
+
const {getByDisplayValue} = renderWithTheme(
|
|
804
|
+
<TextField onChange={onChange} value="stable title" />
|
|
805
|
+
);
|
|
806
|
+
|
|
807
|
+
await act(async () => {
|
|
808
|
+
fireEvent.changeText(getByDisplayValue("stable title"), "stable title");
|
|
809
|
+
});
|
|
810
|
+
|
|
811
|
+
expect(onChange).not.toHaveBeenCalled();
|
|
812
|
+
});
|
|
813
|
+
|
|
814
|
+
it("keeps a stable onChangeText callback across value and onChange updates while invoking the latest onChange", async () => {
|
|
815
|
+
const onChange1 = mock(() => {});
|
|
816
|
+
const onChange2 = mock(() => {});
|
|
817
|
+
const {getByDisplayValue, rerender} = renderWithTheme(
|
|
818
|
+
<TextField onChange={onChange1} value="hello" />
|
|
819
|
+
);
|
|
820
|
+
|
|
821
|
+
const initialOnChangeText = getByDisplayValue("hello").props.onChangeText;
|
|
822
|
+
|
|
823
|
+
rerender(<TextField onChange={onChange2} value="world" />);
|
|
824
|
+
|
|
825
|
+
const input = getByDisplayValue("world");
|
|
826
|
+
expect(input.props.onChangeText).toBe(initialOnChangeText);
|
|
827
|
+
|
|
828
|
+
await act(async () => {
|
|
829
|
+
fireEvent.changeText(input, "updated");
|
|
830
|
+
});
|
|
831
|
+
|
|
832
|
+
expect(onChange2).toHaveBeenCalledWith("updated");
|
|
833
|
+
expect(onChange1).not.toHaveBeenCalled();
|
|
834
|
+
});
|
|
835
|
+
|
|
836
|
+
it("preserves per-character typing when onChangeText identity churn would trigger stale RN Web rebinds", async () => {
|
|
837
|
+
const savedOS = PlatformModule.OS;
|
|
838
|
+
const perCharTitle = "Review";
|
|
839
|
+
|
|
840
|
+
try {
|
|
841
|
+
PlatformModule.OS = "web";
|
|
842
|
+
let value = "";
|
|
843
|
+
const onChange = mock((next: string) => {
|
|
844
|
+
value = next;
|
|
845
|
+
});
|
|
846
|
+
|
|
847
|
+
const renderField = (): ReactElement => (
|
|
848
|
+
<TextField onChange={onChange} type="text" value={value} />
|
|
849
|
+
);
|
|
850
|
+
|
|
851
|
+
const view = renderWithTheme(renderField());
|
|
852
|
+
|
|
853
|
+
for (let i = 0; i < perCharTitle.length; i++) {
|
|
854
|
+
const partial = perCharTitle.slice(0, i + 1);
|
|
855
|
+
const field = view.getByDisplayValue(value);
|
|
856
|
+
const previousOnChangeText = field.props.onChangeText;
|
|
857
|
+
|
|
858
|
+
await act(async () => {
|
|
859
|
+
fireEvent.changeText(field, partial);
|
|
860
|
+
});
|
|
861
|
+
|
|
862
|
+
view.rerender(renderField());
|
|
863
|
+
|
|
864
|
+
const fieldAfterChange = view.getByDisplayValue(value);
|
|
865
|
+
expect(fieldAfterChange.props.onChangeText).toBe(previousOnChangeText);
|
|
866
|
+
expect(fieldAfterChange.props.value).toBe(partial);
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
expect(view.getByDisplayValue(value).props.value).toBe(perCharTitle);
|
|
870
|
+
} finally {
|
|
871
|
+
PlatformModule.OS = savedOS;
|
|
872
|
+
}
|
|
873
|
+
});
|
|
874
|
+
|
|
875
|
+
it("disables browser autocorrect and spellcheck on web", () => {
|
|
876
|
+
const savedOS = PlatformModule.OS;
|
|
877
|
+
try {
|
|
878
|
+
PlatformModule.OS = "web";
|
|
879
|
+
const {getByDisplayValue} = renderWithTheme(
|
|
880
|
+
<TextField onChange={mockOnChange} type="text" value="controlled title" />
|
|
881
|
+
);
|
|
882
|
+
const input = getByDisplayValue("controlled title");
|
|
883
|
+
expect(input.props.autoCorrect).toBe(false);
|
|
884
|
+
expect(input.props.spellCheck).toBe(false);
|
|
885
|
+
} finally {
|
|
886
|
+
PlatformModule.OS = savedOS;
|
|
887
|
+
}
|
|
888
|
+
});
|
|
889
|
+
|
|
890
|
+
it("does not recurse when synthetic onChange alternates by a single-space autocorrect delta", async () => {
|
|
891
|
+
const savedOS = PlatformModule.OS;
|
|
892
|
+
const withoutSpace =
|
|
893
|
+
"Tetginsbhep al attempt to trigger loop crash now via extended typed input to reachthreshold fo maximum update depth exc";
|
|
894
|
+
const withSpace =
|
|
895
|
+
"Tetginsbhep al attempt to trigger loop crash now via extended typed input to reach threshold fo maximum update depth exc";
|
|
896
|
+
|
|
897
|
+
try {
|
|
898
|
+
PlatformModule.OS = "web";
|
|
899
|
+
let value = withoutSpace;
|
|
900
|
+
const onChange = mock((next: string) => {
|
|
901
|
+
value = next;
|
|
902
|
+
});
|
|
903
|
+
|
|
904
|
+
const renderField = (): ReactElement => (
|
|
905
|
+
<TextField onChange={onChange} testID="admin-field-title" type="text" value={value} />
|
|
906
|
+
);
|
|
907
|
+
|
|
908
|
+
const view = renderWithTheme(renderField());
|
|
909
|
+
const alternates = [withSpace, withoutSpace];
|
|
910
|
+
|
|
911
|
+
for (let i = 0; i < 6; i++) {
|
|
912
|
+
const nextText = alternates[i % 2];
|
|
913
|
+
await act(async () => {
|
|
914
|
+
fireEvent.changeText(view.getByDisplayValue(value), nextText);
|
|
915
|
+
});
|
|
916
|
+
view.rerender(renderField());
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
expect(onChange.mock.calls.length).toBe(1);
|
|
920
|
+
expect(onChange.mock.calls[0]?.[0]).toBe(withSpace);
|
|
921
|
+
expect(value).toBe(withSpace);
|
|
922
|
+
} finally {
|
|
923
|
+
PlatformModule.OS = savedOS;
|
|
924
|
+
}
|
|
925
|
+
});
|
|
926
|
+
|
|
927
|
+
it("allows backspace and retype after the oscillation window", async () => {
|
|
928
|
+
const savedOS = PlatformModule.OS;
|
|
929
|
+
const originalNow = performance.now.bind(performance);
|
|
930
|
+
let fakeNow = 10_000;
|
|
931
|
+
|
|
932
|
+
try {
|
|
933
|
+
PlatformModule.OS = "web";
|
|
934
|
+
performance.now = () => fakeNow;
|
|
935
|
+
|
|
936
|
+
let value = "hello";
|
|
937
|
+
const onChange = mock((next: string) => {
|
|
938
|
+
value = next;
|
|
939
|
+
});
|
|
940
|
+
|
|
941
|
+
const view = renderWithTheme(
|
|
942
|
+
<TextField onChange={onChange} testID="admin-field-title" type="text" value={value} />
|
|
943
|
+
);
|
|
944
|
+
|
|
945
|
+
await act(async () => {
|
|
946
|
+
fireEvent.changeText(view.getByDisplayValue(value), "hell");
|
|
947
|
+
});
|
|
948
|
+
view.rerender(
|
|
949
|
+
<TextField onChange={onChange} testID="admin-field-title" type="text" value={value} />
|
|
950
|
+
);
|
|
951
|
+
|
|
952
|
+
fakeNow += 600;
|
|
953
|
+
await act(async () => {
|
|
954
|
+
fireEvent.changeText(view.getByDisplayValue(value), "hello");
|
|
955
|
+
});
|
|
956
|
+
|
|
957
|
+
expect(onChange.mock.calls.map((call) => call[0])).toEqual(["hell", "hello"]);
|
|
958
|
+
} finally {
|
|
959
|
+
performance.now = originalNow;
|
|
960
|
+
PlatformModule.OS = savedOS;
|
|
961
|
+
}
|
|
962
|
+
});
|
|
542
963
|
});
|
|
543
964
|
});
|
package/src/TextField.tsx
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {getCalendars} from "expo-localization";
|
|
2
|
-
import {type FC, useMemo, useState} from "react";
|
|
2
|
+
import {type FC, useCallback, useMemo, useRef, useState} from "react";
|
|
3
3
|
import {
|
|
4
4
|
type DimensionValue,
|
|
5
5
|
type KeyboardTypeOptions,
|
|
@@ -18,6 +18,11 @@ import {FieldTitle} from "./fieldElements/FieldTitle";
|
|
|
18
18
|
import {Icon} from "./Icon";
|
|
19
19
|
import {useTheme} from "./Theme";
|
|
20
20
|
import {resolveFieldTestIDsFromProps} from "./testing/resolveTestId";
|
|
21
|
+
import {
|
|
22
|
+
createTextFieldOscillationState,
|
|
23
|
+
recordTextFieldOscillation,
|
|
24
|
+
shouldSuppressTextFieldOscillation,
|
|
25
|
+
} from "./textFieldOscillationGuard";
|
|
21
26
|
|
|
22
27
|
const keyboardMap: {[id: string]: string | undefined} = {
|
|
23
28
|
date: "default",
|
|
@@ -58,6 +63,13 @@ const textContentMap: {
|
|
|
58
63
|
username: "username",
|
|
59
64
|
};
|
|
60
65
|
|
|
66
|
+
interface WebTextInputRef {
|
|
67
|
+
focus: () => void;
|
|
68
|
+
selectionEnd: number | null;
|
|
69
|
+
selectionStart: number | null;
|
|
70
|
+
setSelectionRange: (start: number, end: number) => void;
|
|
71
|
+
}
|
|
72
|
+
|
|
61
73
|
export const TextField: FC<TextFieldProps> = ({
|
|
62
74
|
title,
|
|
63
75
|
disabled,
|
|
@@ -71,6 +83,7 @@ export const TextField: FC<TextFieldProps> = ({
|
|
|
71
83
|
onIconClick,
|
|
72
84
|
trimOnBlur = true,
|
|
73
85
|
type = "text",
|
|
86
|
+
showVisibilityToggle = true,
|
|
74
87
|
autoComplete,
|
|
75
88
|
inputRef,
|
|
76
89
|
multiline,
|
|
@@ -97,6 +110,50 @@ export const TextField: FC<TextFieldProps> = ({
|
|
|
97
110
|
|
|
98
111
|
const [focused, setFocused] = useState(false);
|
|
99
112
|
const [height, setHeight] = useState(rows * 40);
|
|
113
|
+
const [isValueRevealed, setIsValueRevealed] = useState(false);
|
|
114
|
+
const textInputRef = useRef<TextInput | null>(null);
|
|
115
|
+
|
|
116
|
+
const isPasswordField = type === "password";
|
|
117
|
+
const hasVisibilityToggle = isPasswordField && showVisibilityToggle;
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Toggles password visibility without blurring the input. Refocuses when the field was focused so
|
|
121
|
+
* trim-on-blur, parent blur handlers, and the native keyboard stay intact.
|
|
122
|
+
*/
|
|
123
|
+
const handleVisibilityTogglePress = useCallback((): void => {
|
|
124
|
+
if (disabled) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
const wasFocused = focused;
|
|
128
|
+
const webInput =
|
|
129
|
+
Platform.OS === "web" ? (textInputRef.current as unknown as WebTextInputRef | null) : null;
|
|
130
|
+
const selection =
|
|
131
|
+
webInput?.selectionStart !== null &&
|
|
132
|
+
webInput?.selectionStart !== undefined &&
|
|
133
|
+
webInput.selectionEnd !== null
|
|
134
|
+
? {end: webInput.selectionEnd, start: webInput.selectionStart}
|
|
135
|
+
: undefined;
|
|
136
|
+
setIsValueRevealed((previous) => !previous);
|
|
137
|
+
if (wasFocused) {
|
|
138
|
+
const refocusInput = (): void => {
|
|
139
|
+
textInputRef.current?.focus();
|
|
140
|
+
if (selection && webInput) {
|
|
141
|
+
webInput.setSelectionRange(selection.start, selection.end);
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
if (typeof requestAnimationFrame === "function") {
|
|
145
|
+
requestAnimationFrame(refocusInput);
|
|
146
|
+
} else {
|
|
147
|
+
setTimeout(refocusInput, 0);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}, [disabled, focused]);
|
|
151
|
+
|
|
152
|
+
const preventVisibilityToggleBlur = useCallback((event: {preventDefault: () => void}): void => {
|
|
153
|
+
event.preventDefault();
|
|
154
|
+
}, []);
|
|
155
|
+
const visibilityToggleWebProps =
|
|
156
|
+
Platform.OS === "web" ? {onMouseDown: preventVisibilityToggleBlur} : {};
|
|
100
157
|
|
|
101
158
|
let borderColor = focused ? theme.border.focus : theme.border.dark;
|
|
102
159
|
if (disabled) {
|
|
@@ -139,8 +196,46 @@ export const TextField: FC<TextFieldProps> = ({
|
|
|
139
196
|
console.warn(`${type} is not yet supported`);
|
|
140
197
|
}
|
|
141
198
|
|
|
199
|
+
// RN Web browser autocorrect/spellcheck fights controlled `value` and can oscillate
|
|
200
|
+
// between variants (e.g. "reachthreshold" vs "reach threshold"), firing onChangeText forever.
|
|
142
201
|
const shouldAutocorrect =
|
|
143
|
-
|
|
202
|
+
Platform.OS !== "web" &&
|
|
203
|
+
["text", "textarea"].includes(type) &&
|
|
204
|
+
(!autoComplete || autoComplete === "on");
|
|
205
|
+
|
|
206
|
+
const valueRef = useRef(value);
|
|
207
|
+
const onChangeRef = useRef(onChange);
|
|
208
|
+
const oscillationRef = useRef(createTextFieldOscillationState());
|
|
209
|
+
valueRef.current = value;
|
|
210
|
+
onChangeRef.current = onChange;
|
|
211
|
+
|
|
212
|
+
const handleChangeText = useCallback((text: string): void => {
|
|
213
|
+
const currentValue = valueRef.current ?? "";
|
|
214
|
+
if (text === currentValue) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const now = performance.now();
|
|
219
|
+
const oscillationState = oscillationRef.current;
|
|
220
|
+
if (
|
|
221
|
+
shouldSuppressTextFieldOscillation({
|
|
222
|
+
currentValue,
|
|
223
|
+
now,
|
|
224
|
+
state: oscillationState,
|
|
225
|
+
text,
|
|
226
|
+
})
|
|
227
|
+
) {
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
recordTextFieldOscillation({
|
|
232
|
+
currentValue,
|
|
233
|
+
now,
|
|
234
|
+
state: oscillationState,
|
|
235
|
+
text,
|
|
236
|
+
});
|
|
237
|
+
onChangeRef.current(text);
|
|
238
|
+
}, []);
|
|
144
239
|
|
|
145
240
|
const keyboardType = keyboardMap[type];
|
|
146
241
|
const textContentType = textContentMap[type || "text"];
|
|
@@ -180,6 +275,7 @@ export const TextField: FC<TextFieldProps> = ({
|
|
|
180
275
|
}}
|
|
181
276
|
>
|
|
182
277
|
<TextInput
|
|
278
|
+
{...(Platform.OS === "web" ? {spellCheck: false} : {})}
|
|
183
279
|
accessibilityHint="Enter text here"
|
|
184
280
|
accessibilityState={{disabled}}
|
|
185
281
|
aria-label="Text input field"
|
|
@@ -208,7 +304,7 @@ export const TextField: FC<TextFieldProps> = ({
|
|
|
208
304
|
}
|
|
209
305
|
setFocused(false);
|
|
210
306
|
}}
|
|
211
|
-
onChangeText={
|
|
307
|
+
onChangeText={handleChangeText}
|
|
212
308
|
onContentSizeChange={(event) => {
|
|
213
309
|
if (!grow) {
|
|
214
310
|
return;
|
|
@@ -235,11 +331,12 @@ export const TextField: FC<TextFieldProps> = ({
|
|
|
235
331
|
placeholderTextColor={theme.text.secondaryLight}
|
|
236
332
|
readOnly={disabled}
|
|
237
333
|
ref={(ref) => {
|
|
334
|
+
textInputRef.current = ref;
|
|
238
335
|
if (inputRef) {
|
|
239
336
|
inputRef(ref);
|
|
240
337
|
}
|
|
241
338
|
}}
|
|
242
|
-
secureTextEntry={
|
|
339
|
+
secureTextEntry={isPasswordField && !isValueRevealed}
|
|
243
340
|
style={defaultTextInputStyles}
|
|
244
341
|
testID={fieldTestIDs.input}
|
|
245
342
|
textContentType={textContentType}
|
|
@@ -251,6 +348,26 @@ export const TextField: FC<TextFieldProps> = ({
|
|
|
251
348
|
<Icon iconName={iconName!} size="md" />
|
|
252
349
|
</Pressable>
|
|
253
350
|
)}
|
|
351
|
+
{hasVisibilityToggle && (
|
|
352
|
+
<Pressable
|
|
353
|
+
{...visibilityToggleWebProps}
|
|
354
|
+
accessibilityLabel={isValueRevealed ? "Hide password" : "Show password"}
|
|
355
|
+
accessibilityRole="button"
|
|
356
|
+
accessibilityState={{disabled, expanded: isValueRevealed}}
|
|
357
|
+
disabled={disabled}
|
|
358
|
+
hitSlop={8}
|
|
359
|
+
onPress={handleVisibilityTogglePress}
|
|
360
|
+
// Fixed width keeps the input from reflowing: the eye-slash glyph is wider than the eye.
|
|
361
|
+
style={{alignItems: "center", marginLeft: 8, width: 20}}
|
|
362
|
+
testID={fieldTestIDs.visibilityToggle}
|
|
363
|
+
>
|
|
364
|
+
<Icon
|
|
365
|
+
color={disabled ? "extraLight" : "link"}
|
|
366
|
+
iconName={isValueRevealed ? "eye-slash" : "eye"}
|
|
367
|
+
size="md"
|
|
368
|
+
/>
|
|
369
|
+
</Pressable>
|
|
370
|
+
)}
|
|
254
371
|
</View>
|
|
255
372
|
</View>
|
|
256
373
|
{Boolean(helperText) && <FieldHelperText testID={fieldTestIDs.helper} text={helperText!} />}
|
|
@@ -240,6 +240,28 @@ exports[`Field renders password field 1`] = `
|
|
|
240
240
|
},
|
|
241
241
|
"type": "TextInput",
|
|
242
242
|
},
|
|
243
|
+
{
|
|
244
|
+
"$$typeof": Symbol(react.test.json),
|
|
245
|
+
"children": null,
|
|
246
|
+
"props": {
|
|
247
|
+
"accessibilityLabel": "Show password",
|
|
248
|
+
"accessibilityRole": "button",
|
|
249
|
+
"accessibilityState": {
|
|
250
|
+
"disabled": undefined,
|
|
251
|
+
"expanded": false,
|
|
252
|
+
},
|
|
253
|
+
"disabled": undefined,
|
|
254
|
+
"hitSlop": 8,
|
|
255
|
+
"onPress": [Function],
|
|
256
|
+
"style": {
|
|
257
|
+
"alignItems": "center",
|
|
258
|
+
"marginLeft": 8,
|
|
259
|
+
"width": 20,
|
|
260
|
+
},
|
|
261
|
+
"testID": undefined,
|
|
262
|
+
},
|
|
263
|
+
"type": "Pressable",
|
|
264
|
+
},
|
|
243
265
|
],
|
|
244
266
|
"props": {
|
|
245
267
|
"style": {
|