@terreno/ui 57.18.0 → 57.20.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/CHANGELOG.md +2074 -0
- package/dist/Accordion.d.ts.map +1 -1
- package/dist/Accordion.js +3 -3
- package/dist/Accordion.js.map +1 -1
- package/dist/AreaChart.d.ts.map +1 -1
- package/dist/AreaChart.js +2 -2
- package/dist/AreaChart.js.map +1 -1
- package/dist/Avatar.d.ts.map +1 -1
- package/dist/Avatar.js +3 -1
- package/dist/Avatar.js.map +1 -1
- package/dist/BarChart.d.ts.map +1 -1
- package/dist/BarChart.js +2 -2
- package/dist/BarChart.js.map +1 -1
- package/dist/Box.d.ts.map +1 -1
- package/dist/Box.js +66 -8
- package/dist/Box.js.map +1 -1
- package/dist/Card.d.ts.map +1 -1
- package/dist/Card.js +5 -4
- package/dist/Card.js.map +1 -1
- package/dist/ChartCard.d.ts.map +1 -1
- package/dist/ChartCard.js +4 -2
- package/dist/ChartCard.js.map +1 -1
- package/dist/Common.d.ts +240 -14
- package/dist/Common.d.ts.map +1 -1
- package/dist/Common.js.map +1 -1
- package/dist/ConsentFormScreen.d.ts +1 -1
- package/dist/ConsentFormScreen.d.ts.map +1 -1
- package/dist/ConsentFormScreen.js +13 -4
- package/dist/ConsentFormScreen.js.map +1 -1
- package/dist/DateTimeField.d.ts.map +1 -1
- package/dist/DateTimeField.js +5 -3
- package/dist/DateTimeField.js.map +1 -1
- package/dist/DonutChart.d.ts.map +1 -1
- package/dist/DonutChart.js +2 -2
- package/dist/DonutChart.js.map +1 -1
- package/dist/Field.d.ts.map +1 -1
- package/dist/Field.js +2 -0
- package/dist/Field.js.map +1 -1
- package/dist/FormContainer.d.ts +68 -0
- package/dist/FormContainer.d.ts.map +1 -0
- package/dist/FormContainer.js +79 -0
- package/dist/FormContainer.js.map +1 -0
- package/dist/GPTChat.d.ts.map +1 -1
- package/dist/GPTChat.js +2 -1
- package/dist/GPTChat.js.map +1 -1
- package/dist/Heading.d.ts +1 -1
- package/dist/Heading.d.ts.map +1 -1
- package/dist/Heading.js +5 -2
- package/dist/Heading.js.map +1 -1
- package/dist/Icon.d.ts +4 -0
- package/dist/Icon.d.ts.map +1 -1
- package/dist/Icon.js +43 -2
- package/dist/Icon.js.map +1 -1
- package/dist/LabeledCheckBox.d.ts +22 -0
- package/dist/LabeledCheckBox.d.ts.map +1 -0
- package/dist/LabeledCheckBox.js +30 -0
- package/dist/LabeledCheckBox.js.map +1 -0
- package/dist/LineChart.d.ts.map +1 -1
- package/dist/LineChart.js +2 -2
- package/dist/LineChart.js.map +1 -1
- package/dist/ListDetailLayout.d.ts +36 -0
- package/dist/ListDetailLayout.d.ts.map +1 -0
- package/dist/ListDetailLayout.js +153 -0
- package/dist/ListDetailLayout.js.map +1 -0
- package/dist/MarkdownEditor.d.ts.map +1 -1
- package/dist/MarkdownEditor.js +2 -1
- package/dist/MarkdownEditor.js.map +1 -1
- package/dist/MediaQuery.d.ts +1 -1
- package/dist/MediaQuery.d.ts.map +1 -1
- package/dist/MediaQuery.js +1 -1
- package/dist/MediaQuery.js.map +1 -1
- package/dist/OtpField.d.ts +60 -0
- package/dist/OtpField.d.ts.map +1 -0
- package/dist/OtpField.js +154 -0
- package/dist/OtpField.js.map +1 -0
- package/dist/Page.d.ts +1 -0
- package/dist/Page.d.ts.map +1 -1
- package/dist/Page.js +10 -1
- package/dist/Page.js.map +1 -1
- package/dist/ProvisionalConsentSigner.d.ts +21 -0
- package/dist/ProvisionalConsentSigner.d.ts.map +1 -0
- package/dist/ProvisionalConsentSigner.js +199 -0
- package/dist/ProvisionalConsentSigner.js.map +1 -0
- package/dist/ResponsiveBreakpoint.d.ts +5 -0
- package/dist/ResponsiveBreakpoint.d.ts.map +1 -1
- package/dist/ResponsiveBreakpoint.js +7 -0
- package/dist/ResponsiveBreakpoint.js.map +1 -1
- package/dist/Scorecard.d.ts.map +1 -1
- package/dist/Scorecard.js +5 -2
- package/dist/Scorecard.js.map +1 -1
- package/dist/Skeleton.d.ts +39 -0
- package/dist/Skeleton.d.ts.map +1 -0
- package/dist/Skeleton.js +195 -0
- package/dist/Skeleton.js.map +1 -0
- package/dist/SocialLoginButton.d.ts +10 -0
- package/dist/SocialLoginButton.d.ts.map +1 -1
- package/dist/SocialLoginButton.js +26 -8
- package/dist/SocialLoginButton.js.map +1 -1
- package/dist/SplitPage.d.ts +12 -1
- package/dist/SplitPage.d.ts.map +1 -1
- package/dist/SplitPage.js +14 -7
- package/dist/SplitPage.js.map +1 -1
- package/dist/SplitPage.native.d.ts +1 -1
- package/dist/SplitPage.native.d.ts.map +1 -1
- package/dist/SplitPage.native.js +6 -3
- package/dist/SplitPage.native.js.map +1 -1
- package/dist/SplitPageLoading.d.ts +14 -0
- package/dist/SplitPageLoading.d.ts.map +1 -0
- package/dist/SplitPageLoading.js +19 -0
- package/dist/SplitPageLoading.js.map +1 -0
- package/dist/SyncStatusBanner.d.ts +7 -1
- package/dist/SyncStatusBanner.d.ts.map +1 -1
- package/dist/SyncStatusBanner.js +20 -4
- package/dist/SyncStatusBanner.js.map +1 -1
- package/dist/Text.d.ts +3 -1
- package/dist/Text.d.ts.map +1 -1
- package/dist/Text.js +2 -0
- package/dist/Text.js.map +1 -1
- package/dist/TextField.d.ts.map +1 -1
- package/dist/TextField.js +44 -10
- package/dist/TextField.js.map +1 -1
- package/dist/charts/ChartFrame.d.ts +2 -2
- package/dist/charts/ChartFrame.d.ts.map +1 -1
- package/dist/charts/ChartFrame.js +9 -2
- package/dist/charts/ChartFrame.js.map +1 -1
- package/dist/fieldElements/FieldError.d.ts.map +1 -1
- package/dist/fieldElements/FieldError.js +1 -1
- package/dist/fieldElements/FieldError.js.map +1 -1
- package/dist/index.d.ts +21 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +16 -1
- package/dist/index.js.map +1 -1
- package/dist/lazyBoundaries/heavyOptionalExports.d.ts +3 -0
- package/dist/lazyBoundaries/heavyOptionalExports.d.ts.map +1 -1
- package/dist/lazyBoundaries/heavyOptionalExports.js +2 -0
- package/dist/lazyBoundaries/heavyOptionalExports.js.map +1 -1
- package/dist/login/LastUsedBadge.d.ts +14 -0
- package/dist/login/LastUsedBadge.d.ts.map +1 -0
- package/dist/login/LastUsedBadge.js +23 -0
- package/dist/login/LastUsedBadge.js.map +1 -0
- package/dist/login/LoginAccountCard.d.ts +38 -0
- package/dist/login/LoginAccountCard.d.ts.map +1 -0
- package/dist/login/LoginAccountCard.js +72 -0
- package/dist/login/LoginAccountCard.js.map +1 -0
- package/dist/login/LoginAccountList.d.ts +33 -0
- package/dist/login/LoginAccountList.d.ts.map +1 -0
- package/dist/login/LoginAccountList.js +30 -0
- package/dist/login/LoginAccountList.js.map +1 -0
- package/dist/login/LoginScreen.d.ts +4 -4
- package/dist/login/LoginScreen.d.ts.map +1 -1
- package/dist/login/LoginScreen.js +294 -13
- package/dist/login/LoginScreen.js.map +1 -1
- package/dist/login/OutlinePill.d.ts +28 -0
- package/dist/login/OutlinePill.d.ts.map +1 -0
- package/dist/login/OutlinePill.js +48 -0
- package/dist/login/OutlinePill.js.map +1 -0
- package/dist/login/TwoFactorChallengeScreen.d.ts +26 -0
- package/dist/login/TwoFactorChallengeScreen.d.ts.map +1 -0
- package/dist/login/TwoFactorChallengeScreen.js +139 -0
- package/dist/login/TwoFactorChallengeScreen.js.map +1 -0
- package/dist/login/loginMethods.d.ts +27 -0
- package/dist/login/loginMethods.d.ts.map +1 -0
- package/dist/login/loginMethods.js +37 -0
- package/dist/login/loginMethods.js.map +1 -0
- package/dist/login/loginTypes.d.ts +202 -5
- package/dist/login/loginTypes.d.ts.map +1 -1
- package/dist/pdfHtmlTemplate.d.ts +17 -0
- package/dist/pdfHtmlTemplate.d.ts.map +1 -1
- package/dist/pdfHtmlTemplate.js +78 -0
- package/dist/pdfHtmlTemplate.js.map +1 -1
- package/dist/security/AccountSecurityScreen.d.ts +10 -0
- package/dist/security/AccountSecurityScreen.d.ts.map +1 -0
- package/dist/security/AccountSecurityScreen.js +50 -0
- package/dist/security/AccountSecurityScreen.js.map +1 -0
- package/dist/security/LinkedAccountsCard.d.ts +32 -0
- package/dist/security/LinkedAccountsCard.d.ts.map +1 -0
- package/dist/security/LinkedAccountsCard.js +145 -0
- package/dist/security/LinkedAccountsCard.js.map +1 -0
- package/dist/security/PasskeysCard.d.ts +39 -0
- package/dist/security/PasskeysCard.d.ts.map +1 -0
- package/dist/security/PasskeysCard.js +168 -0
- package/dist/security/PasskeysCard.js.map +1 -0
- package/dist/security/PasswordCard.d.ts +32 -0
- package/dist/security/PasswordCard.d.ts.map +1 -0
- package/dist/security/PasswordCard.js +198 -0
- package/dist/security/PasswordCard.js.map +1 -0
- package/dist/security/SessionsCard.d.ts +39 -0
- package/dist/security/SessionsCard.d.ts.map +1 -0
- package/dist/security/SessionsCard.js +168 -0
- package/dist/security/SessionsCard.js.map +1 -0
- package/dist/security/TwoFactorSettingsCard.d.ts +55 -0
- package/dist/security/TwoFactorSettingsCard.d.ts.map +1 -0
- package/dist/security/TwoFactorSettingsCard.js +458 -0
- package/dist/security/TwoFactorSettingsCard.js.map +1 -0
- package/dist/security/securityMethods.d.ts +40 -0
- package/dist/security/securityMethods.d.ts.map +1 -0
- package/dist/security/securityMethods.js +66 -0
- package/dist/security/securityMethods.js.map +1 -0
- package/dist/security/securityTypes.d.ts +467 -0
- package/dist/security/securityTypes.d.ts.map +1 -0
- package/dist/security/securityTypes.js +2 -0
- package/dist/security/securityTypes.js.map +1 -0
- package/dist/security/userAgent.d.ts +26 -0
- package/dist/security/userAgent.d.ts.map +1 -0
- package/dist/security/userAgent.js +80 -0
- package/dist/security/userAgent.js.map +1 -0
- package/dist/signUp/OAuthButtons.d.ts +28 -5
- package/dist/signUp/OAuthButtons.d.ts.map +1 -1
- package/dist/signUp/OAuthButtons.js +44 -5
- package/dist/signUp/OAuthButtons.js.map +1 -1
- package/dist/signUp/SignUpScreen.d.ts.map +1 -1
- package/dist/signUp/SignUpScreen.js +50 -5
- package/dist/signUp/SignUpScreen.js.map +1 -1
- package/dist/signUp/signUpTypes.d.ts +21 -1
- package/dist/signUp/signUpTypes.d.ts.map +1 -1
- package/dist/useConsentForms.d.ts +9 -0
- package/dist/useConsentForms.d.ts.map +1 -1
- package/dist/useConsentForms.js +18 -0
- package/dist/useConsentForms.js.map +1 -1
- package/dist/useProvisionalConsent.d.ts +124 -0
- package/dist/useProvisionalConsent.d.ts.map +1 -0
- package/dist/useProvisionalConsent.js +79 -0
- package/dist/useProvisionalConsent.js.map +1 -0
- package/dist/withLoading.d.ts +20 -0
- package/dist/withLoading.d.ts.map +1 -0
- package/dist/withLoading.js +24 -0
- package/dist/withLoading.js.map +1 -0
- package/package.json +4 -2
- package/src/Accordion.test.tsx +5 -0
- package/src/Accordion.tsx +3 -8
- package/src/AreaChart.test.tsx +15 -0
- package/src/AreaChart.tsx +6 -0
- package/src/Avatar.test.tsx +8 -0
- package/src/Avatar.tsx +2 -0
- package/src/BarChart.test.tsx +15 -0
- package/src/BarChart.tsx +6 -0
- package/src/Box.test.tsx +131 -1
- package/src/Box.tsx +77 -8
- package/src/Card.test.tsx +48 -0
- package/src/Card.tsx +13 -2
- package/src/ChartCard.test.tsx +30 -0
- package/src/ChartCard.tsx +18 -1
- package/src/Common.ts +263 -14
- package/src/ConsentFormScreen.test.tsx +84 -0
- package/src/ConsentFormScreen.tsx +28 -4
- package/src/DateTimeField.tsx +5 -3
- package/src/DonutChart.test.tsx +15 -0
- package/src/DonutChart.tsx +6 -0
- package/src/Field.tsx +2 -0
- package/src/FilePickerButton.test.tsx +35 -0
- package/src/FormContainer.test.tsx +242 -0
- package/src/FormContainer.tsx +177 -0
- package/src/GPTChat.test.tsx +16 -0
- package/src/GPTChat.tsx +8 -2
- package/src/Heading.test.tsx +14 -0
- package/src/Heading.tsx +6 -1
- package/src/Icon.test.tsx +52 -1
- package/src/Icon.tsx +92 -3
- package/src/IconButton.test.tsx +85 -0
- package/src/LabeledCheckBox.test.tsx +45 -0
- package/src/LabeledCheckBox.tsx +89 -0
- package/src/LineChart.test.tsx +18 -0
- package/src/LineChart.tsx +6 -0
- package/src/ListDetailLayout.tsx +247 -0
- package/src/MarkdownEditor.tsx +2 -1
- package/src/MediaQuery.ts +1 -0
- package/src/OtpField.test.tsx +280 -0
- package/src/OtpField.tsx +307 -0
- package/src/Page.test.tsx +38 -0
- package/src/Page.tsx +38 -19
- package/src/ProvisionalConsentSigner.test.tsx +625 -0
- package/src/ProvisionalConsentSigner.tsx +423 -0
- package/src/ResponsiveBreakpoint.test.tsx +7 -0
- package/src/ResponsiveBreakpoint.tsx +14 -0
- package/src/RootImportRegression.test.tsx +3 -0
- package/src/Scorecard.test.tsx +11 -0
- package/src/Scorecard.tsx +21 -0
- package/src/Skeleton.test.tsx +262 -0
- package/src/Skeleton.tsx +430 -0
- package/src/SocialLoginButton.test.tsx +14 -0
- package/src/SocialLoginButton.tsx +51 -7
- package/src/SplitPage.native.tsx +16 -3
- package/src/SplitPage.tsx +29 -12
- package/src/SplitPageLoading.test.tsx +51 -0
- package/src/SplitPageLoading.tsx +53 -0
- package/src/SyncStatusBanner.test.tsx +61 -4
- package/src/SyncStatusBanner.tsx +29 -3
- package/src/Text.tsx +4 -1
- package/src/TextField.test.tsx +150 -0
- package/src/TextField.tsx +66 -8
- package/src/__snapshots__/AddressField.test.tsx.snap +12 -0
- package/src/__snapshots__/AiSuggestionBox.test.tsx.snap +15 -0
- package/src/__snapshots__/Avatar.test.tsx.snap +1 -0
- package/src/__snapshots__/CustomSelectField.test.tsx.snap +3 -0
- package/src/__snapshots__/EmailField.test.tsx.snap +6 -0
- package/src/__snapshots__/Field.test.tsx.snap +37 -0
- package/src/__snapshots__/HeightField.test.tsx.snap +2 -0
- package/src/__snapshots__/MobileAddressAutoComplete.test.tsx.snap +3 -0
- package/src/__snapshots__/NumberField.test.tsx.snap +3 -0
- package/src/__snapshots__/Page.test.tsx.snap +28 -5
- package/src/__snapshots__/PhoneNumberField.test.tsx.snap +12 -0
- package/src/__snapshots__/Slider.test.tsx.snap +1 -0
- package/src/__snapshots__/SocialLoginButton.test.tsx.snap +43 -22
- package/src/__snapshots__/SplitPage.test.tsx.snap +16 -0
- package/src/__snapshots__/TapToEdit.test.tsx.snap +3 -0
- package/src/__snapshots__/TextArea.test.tsx.snap +17 -1
- package/src/__snapshots__/TextField.test.tsx.snap +19 -2
- package/src/__snapshots__/TypedSignatureField.test.tsx.snap +3 -0
- package/src/__snapshots__/UnifiedAddressAutoComplete.test.tsx.snap +18 -0
- package/src/__snapshots__/WebAddressAutocomplete.test.tsx.snap +12 -0
- package/src/bunSetup.ts +13 -1
- package/src/charts/ChartFrame.tsx +19 -8
- package/src/fieldElements/FieldError.tsx +1 -0
- package/src/fieldElements/__snapshots__/FieldError.test.tsx.snap +2 -0
- package/src/index.tsx +44 -1
- package/src/isolated/ListDetailLayout.isolated.tsx +634 -0
- package/src/lazyBoundaries/heavyOptionalExports.tsx +7 -0
- package/src/login/LastUsedBadge.tsx +40 -0
- package/src/login/LoginAccountCard.tsx +205 -0
- package/src/login/LoginAccountList.tsx +115 -0
- package/src/login/LoginScreen.test.tsx +837 -1
- package/src/login/LoginScreen.tsx +674 -91
- package/src/login/OutlinePill.tsx +110 -0
- package/src/login/TwoFactorChallengeScreen.test.tsx +376 -0
- package/src/login/TwoFactorChallengeScreen.tsx +307 -0
- package/src/login/__snapshots__/LoginScreen.test.tsx.snap +1454 -334
- package/src/login/loginMethods.test.ts +53 -0
- package/src/login/loginMethods.ts +60 -0
- package/src/login/loginTypes.ts +208 -5
- package/src/pdfHtmlTemplate.test.ts +243 -4
- package/src/pdfHtmlTemplate.ts +93 -0
- package/src/security/AccountSecurityScreen.test.tsx +262 -0
- package/src/security/AccountSecurityScreen.tsx +156 -0
- package/src/security/LinkedAccountsCard.test.tsx +371 -0
- package/src/security/LinkedAccountsCard.tsx +297 -0
- package/src/security/PasskeysCard.test.tsx +378 -0
- package/src/security/PasskeysCard.tsx +371 -0
- package/src/security/PasswordCard.test.tsx +445 -0
- package/src/security/PasswordCard.tsx +342 -0
- package/src/security/SessionsCard.test.tsx +349 -0
- package/src/security/SessionsCard.tsx +362 -0
- package/src/security/TwoFactorSettingsCard.test.tsx +700 -0
- package/src/security/TwoFactorSettingsCard.tsx +872 -0
- package/src/security/securityMethods.test.ts +101 -0
- package/src/security/securityMethods.ts +104 -0
- package/src/security/securityTypes.ts +480 -0
- package/src/security/userAgent.test.ts +119 -0
- package/src/security/userAgent.ts +102 -0
- package/src/signUp/OAuthButtons.test.tsx +27 -0
- package/src/signUp/OAuthButtons.tsx +153 -19
- package/src/signUp/SignUpScreen.test.tsx +170 -1
- package/src/signUp/SignUpScreen.tsx +87 -25
- package/src/signUp/__snapshots__/SignUpScreen.test.tsx.snap +22 -11
- package/src/signUp/signUpTypes.ts +23 -1
- package/src/useConsentForms.test.ts +37 -1
- package/src/useConsentForms.ts +25 -0
- package/src/useProvisionalConsent.test.ts +256 -0
- package/src/useProvisionalConsent.ts +218 -0
- package/src/withLoading.test.tsx +61 -0
- package/src/withLoading.tsx +50 -0
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import {describe, expect, it, mock} from "bun:test";
|
|
2
|
+
import {fireEvent, render} from "@testing-library/react-native";
|
|
3
|
+
import type {ReactElement} from "react";
|
|
4
|
+
import {useState} from "react";
|
|
5
|
+
|
|
6
|
+
import {OtpField} from "./OtpField";
|
|
7
|
+
import {ThemeProvider} from "./Theme";
|
|
8
|
+
import {renderWithTheme} from "./test-utils";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Renders with a node mock per cell so tests can see which cell the field focuses. Returns the
|
|
12
|
+
* focused cell numbers (1-based) in call order.
|
|
13
|
+
*/
|
|
14
|
+
const renderWithFocusSpy = (ui: ReactElement): ReturnType<typeof render> & {focused: number[]} => {
|
|
15
|
+
const focused: number[] = [];
|
|
16
|
+
const result = render(ui, {
|
|
17
|
+
createNodeMock: (element: ReactElement) => {
|
|
18
|
+
const cellTestID = (element.props as {testID?: string}).testID ?? "";
|
|
19
|
+
const match = /-cell-(\d+)$/.exec(cellTestID);
|
|
20
|
+
return {
|
|
21
|
+
focus: () => {
|
|
22
|
+
if (match) {
|
|
23
|
+
focused.push(Number(match[1]));
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
},
|
|
28
|
+
wrapper: ThemeProvider,
|
|
29
|
+
});
|
|
30
|
+
return Object.assign(result, {focused});
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const cellValues = (getByTestId: (id: string) => {props: {value?: string}}, length = 6): string[] =>
|
|
34
|
+
Array.from({length}, (_, index) => getByTestId(`otp-cell-${index + 1}`).props.value ?? "");
|
|
35
|
+
|
|
36
|
+
const ControlledOtp = ({
|
|
37
|
+
onComplete,
|
|
38
|
+
onChange,
|
|
39
|
+
}: {
|
|
40
|
+
onComplete?: (code: string) => void;
|
|
41
|
+
onChange?: (code: string) => void;
|
|
42
|
+
}): React.ReactElement => {
|
|
43
|
+
const [code, setCode] = useState("");
|
|
44
|
+
return (
|
|
45
|
+
<OtpField
|
|
46
|
+
onChange={(next) => {
|
|
47
|
+
setCode(next);
|
|
48
|
+
onChange?.(next);
|
|
49
|
+
}}
|
|
50
|
+
onComplete={onComplete}
|
|
51
|
+
testID="otp"
|
|
52
|
+
value={code}
|
|
53
|
+
/>
|
|
54
|
+
);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
describe("OtpField", () => {
|
|
58
|
+
it("renders six cells by default with digit labels", () => {
|
|
59
|
+
const {getByTestId, queryByTestId} = renderWithTheme(<OtpField testID="otp" />);
|
|
60
|
+
for (let k = 1; k <= 6; k++) {
|
|
61
|
+
const cell = getByTestId(`otp-cell-${k}`);
|
|
62
|
+
expect(cell.props["aria-label"]).toBe(`Digit ${k} of 6`);
|
|
63
|
+
expect(cell.props.accessibilityLabel).toBe(`Digit ${k} of 6`);
|
|
64
|
+
}
|
|
65
|
+
expect(queryByTestId("otp-cell-7")).toBeNull();
|
|
66
|
+
expect(getByTestId("otp").props.role).toBe("group");
|
|
67
|
+
expect(getByTestId("otp").props["aria-label"]).toBe("Verification code");
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("renders the requested number of cells", () => {
|
|
71
|
+
const {getByTestId, queryByTestId} = renderWithTheme(<OtpField length={4} testID="otp" />);
|
|
72
|
+
expect(getByTestId("otp-cell-4").props["aria-label"]).toBe("Digit 4 of 4");
|
|
73
|
+
expect(queryByTestId("otp-cell-5")).toBeNull();
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("uses a numeric keyboard and one-time-code autofill hints", () => {
|
|
77
|
+
const {getByTestId} = renderWithTheme(<OtpField testID="otp" />);
|
|
78
|
+
const cell = getByTestId("otp-cell-1");
|
|
79
|
+
expect(cell.props.keyboardType).toBe("number-pad");
|
|
80
|
+
expect(cell.props.inputMode).toBe("numeric");
|
|
81
|
+
expect(cell.props.autoComplete).toBe("one-time-code");
|
|
82
|
+
expect(cell.props.textContentType).toBe("oneTimeCode");
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("calls onEnter with the code on Enter in any cell", () => {
|
|
86
|
+
const onEnter = mock((_code: string) => {});
|
|
87
|
+
const {getByTestId} = renderWithTheme(<OtpField onEnter={onEnter} testID="otp" value="1234" />);
|
|
88
|
+
expect(getByTestId("otp-cell-5").props.enterKeyHint).toBe("done");
|
|
89
|
+
fireEvent(getByTestId("otp-cell-5"), "submitEditing");
|
|
90
|
+
expect(onEnter).toHaveBeenCalledWith("1234");
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("pasting a full code fills every cell and fires onComplete", () => {
|
|
94
|
+
const onComplete = mock((_code: string) => {});
|
|
95
|
+
const {getByTestId} = renderWithTheme(<OtpField onComplete={onComplete} testID="otp" />);
|
|
96
|
+
fireEvent.changeText(getByTestId("otp-cell-1"), "123456");
|
|
97
|
+
expect(cellValues(getByTestId)).toEqual(["1", "2", "3", "4", "5", "6"]);
|
|
98
|
+
expect(onComplete).toHaveBeenCalledTimes(1);
|
|
99
|
+
expect(onComplete).toHaveBeenCalledWith("123456");
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("pasting a full code into a later cell still fills from the first cell", () => {
|
|
103
|
+
const onComplete = mock((_code: string) => {});
|
|
104
|
+
const {getByTestId} = renderWithTheme(<ControlledOtp onComplete={onComplete} />);
|
|
105
|
+
fireEvent.changeText(getByTestId("otp-cell-1"), "9");
|
|
106
|
+
fireEvent.changeText(getByTestId("otp-cell-2"), "123 456");
|
|
107
|
+
expect(cellValues(getByTestId)).toEqual(["1", "2", "3", "4", "5", "6"]);
|
|
108
|
+
expect(onComplete).toHaveBeenCalledWith("123456");
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("typing one digit per cell builds the code and completes on the last digit", () => {
|
|
112
|
+
const onComplete = mock((_code: string) => {});
|
|
113
|
+
const onChange = mock((_code: string) => {});
|
|
114
|
+
const {getByTestId} = renderWithTheme(
|
|
115
|
+
<ControlledOtp onChange={onChange} onComplete={onComplete} />
|
|
116
|
+
);
|
|
117
|
+
"65432".split("").forEach((digit, index) => {
|
|
118
|
+
fireEvent.changeText(getByTestId(`otp-cell-${index + 1}`), digit);
|
|
119
|
+
});
|
|
120
|
+
expect(onComplete).not.toHaveBeenCalled();
|
|
121
|
+
expect(onChange).toHaveBeenLastCalledWith("65432");
|
|
122
|
+
fireEvent.changeText(getByTestId("otp-cell-6"), "1");
|
|
123
|
+
expect(onComplete).toHaveBeenCalledWith("654321");
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("ignores non-digit input", () => {
|
|
127
|
+
const onChange = mock((_code: string) => {});
|
|
128
|
+
const {getByTestId} = renderWithTheme(<OtpField onChange={onChange} testID="otp" />);
|
|
129
|
+
fireEvent.changeText(getByTestId("otp-cell-1"), "a");
|
|
130
|
+
expect(onChange).not.toHaveBeenCalled();
|
|
131
|
+
expect(getByTestId("otp-cell-1").props.value).toBe("");
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("replaces a filled cell's digit when the old digit was not selected", () => {
|
|
135
|
+
const {getByTestId} = renderWithTheme(<ControlledOtp />);
|
|
136
|
+
fireEvent.changeText(getByTestId("otp-cell-1"), "1");
|
|
137
|
+
fireEvent.changeText(getByTestId("otp-cell-2"), "2");
|
|
138
|
+
fireEvent.changeText(getByTestId("otp-cell-1"), "17");
|
|
139
|
+
expect(cellValues(getByTestId).slice(0, 2)).toEqual(["7", "2"]);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("backspace in an empty cell deletes the previous digit", () => {
|
|
143
|
+
const onChange = mock((_code: string) => {});
|
|
144
|
+
const {getByTestId} = renderWithTheme(<ControlledOtp onChange={onChange} />);
|
|
145
|
+
fireEvent.changeText(getByTestId("otp-cell-1"), "1");
|
|
146
|
+
fireEvent.changeText(getByTestId("otp-cell-2"), "2");
|
|
147
|
+
fireEvent(getByTestId("otp-cell-3"), "keyPress", {nativeEvent: {key: "Backspace"}});
|
|
148
|
+
expect(onChange).toHaveBeenLastCalledWith("1");
|
|
149
|
+
expect(cellValues(getByTestId).slice(0, 2)).toEqual(["1", ""]);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("backspace in a filled cell clears it through the text change", () => {
|
|
153
|
+
const onChange = mock((_code: string) => {});
|
|
154
|
+
const {getByTestId} = renderWithTheme(<ControlledOtp onChange={onChange} />);
|
|
155
|
+
fireEvent.changeText(getByTestId("otp-cell-1"), "1");
|
|
156
|
+
fireEvent.changeText(getByTestId("otp-cell-2"), "2");
|
|
157
|
+
onChange.mockClear();
|
|
158
|
+
fireEvent(getByTestId("otp-cell-2"), "keyPress", {nativeEvent: {key: "Backspace"}});
|
|
159
|
+
expect(onChange).not.toHaveBeenCalled();
|
|
160
|
+
fireEvent.changeText(getByTestId("otp-cell-2"), "");
|
|
161
|
+
expect(onChange).toHaveBeenLastCalledWith("1");
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("backspace in the first empty cell does nothing", () => {
|
|
165
|
+
const onChange = mock((_code: string) => {});
|
|
166
|
+
const {getByTestId} = renderWithTheme(<OtpField onChange={onChange} testID="otp" />);
|
|
167
|
+
fireEvent(getByTestId("otp-cell-1"), "keyPress", {nativeEvent: {key: "Backspace"}});
|
|
168
|
+
expect(onChange).not.toHaveBeenCalled();
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("shows a controlled value and the error text", () => {
|
|
172
|
+
const {getByTestId} = renderWithTheme(
|
|
173
|
+
<OtpField errorText="That code didn't work" testID="otp" value="12" />
|
|
174
|
+
);
|
|
175
|
+
expect(cellValues(getByTestId).slice(0, 3)).toEqual(["1", "2", ""]);
|
|
176
|
+
expect(getByTestId("otp-error")).toBeTruthy();
|
|
177
|
+
expect(getByTestId("otp-cell-1").props["aria-invalid"]).toBe(true);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it("disables every cell", () => {
|
|
181
|
+
const {getByTestId} = renderWithTheme(<OtpField disabled testID="otp" />);
|
|
182
|
+
expect(getByTestId("otp-cell-1").props.editable).toBe(false);
|
|
183
|
+
expect(getByTestId("otp-cell-6").props.editable).toBe(false);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("shows a focus border on the focused cell and drops it on blur", () => {
|
|
187
|
+
const {getByTestId} = renderWithTheme(<OtpField testID="otp" />);
|
|
188
|
+
const borderWidth = (): number => {
|
|
189
|
+
const style = getByTestId("otp-cell-1").props.style;
|
|
190
|
+
const flat = Array.isArray(style) ? Object.assign({}, ...style) : style;
|
|
191
|
+
return flat.borderWidth;
|
|
192
|
+
};
|
|
193
|
+
expect(borderWidth()).toBe(1);
|
|
194
|
+
fireEvent(getByTestId("otp-cell-1"), "focus");
|
|
195
|
+
expect(borderWidth()).toBe(3);
|
|
196
|
+
fireEvent(getByTestId("otp-cell-1"), "blur");
|
|
197
|
+
expect(borderWidth()).toBe(1);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it("moves focus to the first empty cell when a later cell is focused", () => {
|
|
201
|
+
const {getByTestId} = renderWithTheme(<OtpField autoFocus testID="otp" value="1" />);
|
|
202
|
+
const style = (k: number): {borderWidth: number} => {
|
|
203
|
+
const raw = getByTestId(`otp-cell-${k}`).props.style;
|
|
204
|
+
return Array.isArray(raw) ? Object.assign({}, ...raw) : raw;
|
|
205
|
+
};
|
|
206
|
+
fireEvent(getByTestId("otp-cell-5"), "focus");
|
|
207
|
+
// The tap on cell 5 is redirected, so cell 5 never shows as focused.
|
|
208
|
+
expect(style(5).borderWidth).toBe(1);
|
|
209
|
+
fireEvent(getByTestId("otp-cell-2"), "focus");
|
|
210
|
+
expect(style(2).borderWidth).toBe(3);
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it("pasting a full code into a filled cell drops the cell's old digit", () => {
|
|
214
|
+
const onComplete = mock((_code: string) => {});
|
|
215
|
+
const {getByTestId} = renderWithTheme(<ControlledOtp onComplete={onComplete} />);
|
|
216
|
+
fireEvent.changeText(getByTestId("otp-cell-1"), "1");
|
|
217
|
+
fireEvent.changeText(getByTestId("otp-cell-1"), "1123456");
|
|
218
|
+
expect(cellValues(getByTestId)).toEqual(["1", "2", "3", "4", "5", "6"]);
|
|
219
|
+
expect(onComplete).toHaveBeenCalledWith("123456");
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it("keeps only the cell taking the next digit in the tab order so Tab can leave", () => {
|
|
223
|
+
const {getByTestId} = renderWithTheme(<ControlledOtp />);
|
|
224
|
+
const tabStops = (): number[] =>
|
|
225
|
+
Array.from({length: 6}, (_, index) => getByTestId(`otp-cell-${index + 1}`).props.tabIndex);
|
|
226
|
+
expect(tabStops()).toEqual([0, -1, -1, -1, -1, -1]);
|
|
227
|
+
fireEvent.changeText(getByTestId("otp-cell-1"), "1");
|
|
228
|
+
fireEvent.changeText(getByTestId("otp-cell-2"), "2");
|
|
229
|
+
expect(tabStops()).toEqual([-1, -1, 0, -1, -1, -1]);
|
|
230
|
+
fireEvent.changeText(getByTestId("otp-cell-1"), "123456");
|
|
231
|
+
expect(tabStops()).toEqual([-1, -1, -1, -1, -1, 0]);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it("focusing the tab-stop cell keeps focus there", () => {
|
|
235
|
+
const {getByTestId, focused} = renderWithFocusSpy(<OtpField testID="otp" />);
|
|
236
|
+
fireEvent(getByTestId("otp-cell-1"), "focus");
|
|
237
|
+
expect(focused).toEqual([]);
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it("autoFocus focuses the first empty cell", () => {
|
|
241
|
+
const {focused} = renderWithFocusSpy(<OtpField autoFocus testID="otp" value="12" />);
|
|
242
|
+
expect(focused).toEqual([3]);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it("typing a digit moves focus to the next cell", () => {
|
|
246
|
+
const {getByTestId, focused} = renderWithFocusSpy(<ControlledOtp />);
|
|
247
|
+
fireEvent.changeText(getByTestId("otp-cell-1"), "4");
|
|
248
|
+
fireEvent.changeText(getByTestId("otp-cell-2"), "2");
|
|
249
|
+
expect(focused).toEqual([2, 3]);
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it("pasting a full code moves focus to the last cell", () => {
|
|
253
|
+
const {getByTestId, focused} = renderWithFocusSpy(<ControlledOtp />);
|
|
254
|
+
fireEvent.changeText(getByTestId("otp-cell-1"), "123456");
|
|
255
|
+
expect(focused).toEqual([6]);
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it("backspace in an empty cell moves focus to the previous cell", () => {
|
|
259
|
+
const {getByTestId, focused} = renderWithFocusSpy(<ControlledOtp />);
|
|
260
|
+
fireEvent.changeText(getByTestId("otp-cell-1"), "1");
|
|
261
|
+
fireEvent.changeText(getByTestId("otp-cell-2"), "2");
|
|
262
|
+
focused.length = 0;
|
|
263
|
+
fireEvent(getByTestId("otp-cell-3"), "keyPress", {nativeEvent: {key: "Backspace"}});
|
|
264
|
+
expect(focused).toEqual([2]);
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
it("a tap on a cell past the first empty one sends focus to that empty cell", () => {
|
|
268
|
+
const {getByTestId, focused} = renderWithFocusSpy(<OtpField testID="otp" value="1" />);
|
|
269
|
+
fireEvent(getByTestId("otp-cell-5"), "focus");
|
|
270
|
+
expect(focused).toEqual([2]);
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it("renders a title and helper text", () => {
|
|
274
|
+
const {getByTestId, getByText} = renderWithTheme(
|
|
275
|
+
<OtpField helperText="From your authenticator app" testID="otp" title="Code" />
|
|
276
|
+
);
|
|
277
|
+
expect(getByText("Code")).toBeTruthy();
|
|
278
|
+
expect(getByTestId("otp-helper")).toBeTruthy();
|
|
279
|
+
});
|
|
280
|
+
});
|
package/src/OtpField.tsx
ADDED
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import type {FC, ReactElement} from "react";
|
|
2
|
+
import {useCallback, useEffect, useRef, useState} from "react";
|
|
3
|
+
import {
|
|
4
|
+
Platform,
|
|
5
|
+
type StyleProp,
|
|
6
|
+
TextInput,
|
|
7
|
+
type TextInputKeyPressEvent,
|
|
8
|
+
type TextInputProps,
|
|
9
|
+
View,
|
|
10
|
+
} from "react-native";
|
|
11
|
+
|
|
12
|
+
import type {TextStyleWithOutline} from "./Common";
|
|
13
|
+
import {FieldError} from "./fieldElements/FieldError";
|
|
14
|
+
import {FieldHelperText} from "./fieldElements/FieldHelperText";
|
|
15
|
+
import {FieldTitle} from "./fieldElements/FieldTitle";
|
|
16
|
+
import {useTheme} from "./Theme";
|
|
17
|
+
|
|
18
|
+
const DEFAULT_LENGTH = 6;
|
|
19
|
+
const CELL_WIDTH = 48;
|
|
20
|
+
const CELL_HEIGHT = 56;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Props for {@link OtpField}.
|
|
24
|
+
*/
|
|
25
|
+
export interface OtpFieldProps {
|
|
26
|
+
/**
|
|
27
|
+
* Number of digits in the code.
|
|
28
|
+
* @default 6
|
|
29
|
+
*/
|
|
30
|
+
length?: number;
|
|
31
|
+
/**
|
|
32
|
+
* The current code (digits only). Pass it with `onChange` to control the field; leave it out to
|
|
33
|
+
* let the field keep its own state.
|
|
34
|
+
*/
|
|
35
|
+
value?: string;
|
|
36
|
+
/** Called with the digits entered so far whenever the code changes. */
|
|
37
|
+
onChange?: (value: string) => void;
|
|
38
|
+
/** Called once every cell holds a digit, after typing, pasting, or one-time-code autofill. */
|
|
39
|
+
onComplete?: (code: string) => void;
|
|
40
|
+
/** Called with the digits entered so far when the user presses Enter in a cell. */
|
|
41
|
+
onEnter?: (code: string) => void;
|
|
42
|
+
/** Label above the cells. */
|
|
43
|
+
title?: string;
|
|
44
|
+
/**
|
|
45
|
+
* Accessible name for the group of cells.
|
|
46
|
+
* @default "Verification code"
|
|
47
|
+
*/
|
|
48
|
+
accessibilityLabel?: string;
|
|
49
|
+
/** Helper text below the cells. */
|
|
50
|
+
helperText?: string;
|
|
51
|
+
/** Error text below the cells; the cells get an error border. */
|
|
52
|
+
errorText?: string;
|
|
53
|
+
/** Error border without rendering `errorText`, for a message the parent draws itself. */
|
|
54
|
+
invalid?: boolean;
|
|
55
|
+
/** Disables every cell. */
|
|
56
|
+
disabled?: boolean;
|
|
57
|
+
/** Focuses the first empty cell on mount. */
|
|
58
|
+
autoFocus?: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Root testID. Cells are `${testID}-cell-${k}` with `k` from 1 to `length`.
|
|
61
|
+
* @default "otp-field"
|
|
62
|
+
*/
|
|
63
|
+
testID?: string;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// react-native-web writes `autoComplete` straight to the input's `autocomplete` attribute.
|
|
67
|
+
// Android's autofill hint for SMS codes is "sms-otp"; iOS and web use "one-time-code".
|
|
68
|
+
const ONE_TIME_CODE_PROPS = {
|
|
69
|
+
autoComplete: Platform.select({android: "sms-otp", default: "one-time-code"}),
|
|
70
|
+
textContentType: "oneTimeCode",
|
|
71
|
+
} as Pick<TextInputProps, "autoComplete" | "textContentType">;
|
|
72
|
+
|
|
73
|
+
const toDigits = (text: string): string => text.replace(/\D/g, "");
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Applies a cell's new text to the code. A single digit replaces that cell. A full-length run of
|
|
77
|
+
* digits (paste or one-time-code autofill into any cell) replaces the whole code. A shorter run
|
|
78
|
+
* fills forward from the cell. Empty text removes the cell's digit and closes the gap.
|
|
79
|
+
*/
|
|
80
|
+
const applyCellText = ({
|
|
81
|
+
code,
|
|
82
|
+
index,
|
|
83
|
+
length,
|
|
84
|
+
text,
|
|
85
|
+
}: {
|
|
86
|
+
code: string;
|
|
87
|
+
index: number;
|
|
88
|
+
length: number;
|
|
89
|
+
text: string;
|
|
90
|
+
}): {code: string; focusIndex: number} => {
|
|
91
|
+
let incoming = toDigits(text);
|
|
92
|
+
const previous = code[index];
|
|
93
|
+
// A filled cell whose text was not selected receives its old digit plus the new one.
|
|
94
|
+
if (previous && incoming.length === 2 && incoming.includes(previous)) {
|
|
95
|
+
incoming = incoming.startsWith(previous) ? incoming.slice(1) : incoming.slice(0, 1);
|
|
96
|
+
}
|
|
97
|
+
// A full code pasted into a filled cell arrives with the cell's old digit in front of it.
|
|
98
|
+
if (previous && incoming.length === length + 1 && incoming.startsWith(previous)) {
|
|
99
|
+
incoming = incoming.slice(1);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (incoming.length === 0) {
|
|
103
|
+
if (text.length > 0) {
|
|
104
|
+
// Non-digit input: ignore it and keep the cell as it was.
|
|
105
|
+
return {code, focusIndex: index};
|
|
106
|
+
}
|
|
107
|
+
return {code: code.slice(0, index) + code.slice(index + 1), focusIndex: index};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (incoming.length >= length) {
|
|
111
|
+
const full = incoming.slice(0, length);
|
|
112
|
+
return {code: full, focusIndex: length - 1};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const start = Math.min(index, code.length);
|
|
116
|
+
const next = (code.slice(0, start) + incoming + code.slice(start + incoming.length)).slice(
|
|
117
|
+
0,
|
|
118
|
+
length
|
|
119
|
+
);
|
|
120
|
+
return {code: next, focusIndex: Math.min(start + incoming.length, length - 1)};
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* A one-time code input: one cell per digit, a numeric keyboard, paste and SMS / one-time-code
|
|
125
|
+
* autofill of the whole code, and backspace that steps back a cell. `onComplete` fires when every
|
|
126
|
+
* cell is filled.
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```tsx
|
|
130
|
+
* <OtpField
|
|
131
|
+
* autoFocus
|
|
132
|
+
* onComplete={(code) => verifyTotp({code})}
|
|
133
|
+
* testID="two-factor-otp"
|
|
134
|
+
* />
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
export const OtpField: FC<OtpFieldProps> = ({
|
|
138
|
+
length = DEFAULT_LENGTH,
|
|
139
|
+
value,
|
|
140
|
+
onChange,
|
|
141
|
+
onComplete,
|
|
142
|
+
onEnter,
|
|
143
|
+
title,
|
|
144
|
+
accessibilityLabel = "Verification code",
|
|
145
|
+
helperText,
|
|
146
|
+
errorText,
|
|
147
|
+
invalid = false,
|
|
148
|
+
disabled = false,
|
|
149
|
+
autoFocus = false,
|
|
150
|
+
testID = "otp-field",
|
|
151
|
+
}) => {
|
|
152
|
+
const {theme} = useTheme();
|
|
153
|
+
const isControlled = value !== undefined;
|
|
154
|
+
const [internalCode, setInternalCode] = useState("");
|
|
155
|
+
const code = toDigits(isControlled ? value : internalCode).slice(0, length);
|
|
156
|
+
// Roving tab stop: only the cell that takes the next digit is in the tab order, so Tab and
|
|
157
|
+
// Shift+Tab leave the field instead of landing on a cell that hands focus back.
|
|
158
|
+
const activeIndex = Math.min(code.length, length - 1);
|
|
159
|
+
|
|
160
|
+
const [focusedIndex, setFocusedIndex] = useState<number | null>(null);
|
|
161
|
+
const inputRefs = useRef<(TextInput | null)[]>([]);
|
|
162
|
+
|
|
163
|
+
const focusCell = useCallback((index: number): void => {
|
|
164
|
+
inputRefs.current[index]?.focus();
|
|
165
|
+
}, []);
|
|
166
|
+
|
|
167
|
+
// Focus the first empty cell on mount when asked, so the user can type or autofill at once.
|
|
168
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: mount only; later focus follows typing.
|
|
169
|
+
useEffect(() => {
|
|
170
|
+
if (autoFocus) {
|
|
171
|
+
focusCell(activeIndex);
|
|
172
|
+
}
|
|
173
|
+
}, []);
|
|
174
|
+
|
|
175
|
+
const commit = useCallback(
|
|
176
|
+
(next: string): void => {
|
|
177
|
+
if (next === code) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
if (!isControlled) {
|
|
181
|
+
setInternalCode(next);
|
|
182
|
+
}
|
|
183
|
+
onChange?.(next);
|
|
184
|
+
if (next.length === length) {
|
|
185
|
+
onComplete?.(next);
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
[code, isControlled, length, onChange, onComplete]
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
const handleChangeText = useCallback(
|
|
192
|
+
(index: number, text: string): void => {
|
|
193
|
+
const result = applyCellText({code, index, length, text});
|
|
194
|
+
commit(result.code);
|
|
195
|
+
if (result.focusIndex !== index) {
|
|
196
|
+
focusCell(result.focusIndex);
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
[code, commit, focusCell, length]
|
|
200
|
+
);
|
|
201
|
+
|
|
202
|
+
const handleKeyPress = useCallback(
|
|
203
|
+
(index: number, event: TextInputKeyPressEvent): void => {
|
|
204
|
+
if (event.nativeEvent.key !== "Backspace" || code[index] || index === 0) {
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
// Backspace in an empty cell deletes the previous digit and moves there.
|
|
208
|
+
commit(code.slice(0, index - 1) + code.slice(index));
|
|
209
|
+
focusCell(index - 1);
|
|
210
|
+
},
|
|
211
|
+
[code, commit, focusCell]
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
const handleFocus = useCallback(
|
|
215
|
+
(index: number): void => {
|
|
216
|
+
// Digits fill left to right: a tap past the active cell lands on that cell instead. Keyboard
|
|
217
|
+
// focus never needs this, because only the active cell is a tab stop.
|
|
218
|
+
if (index > activeIndex) {
|
|
219
|
+
focusCell(activeIndex);
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
setFocusedIndex(index);
|
|
223
|
+
},
|
|
224
|
+
[activeIndex, focusCell]
|
|
225
|
+
);
|
|
226
|
+
|
|
227
|
+
const handleBlur = useCallback((index: number): void => {
|
|
228
|
+
setFocusedIndex((current) => (current === index ? null : current));
|
|
229
|
+
}, []);
|
|
230
|
+
|
|
231
|
+
const renderCell = (index: number): ReactElement => {
|
|
232
|
+
const isFocused = focusedIndex === index;
|
|
233
|
+
let borderColor = theme.border.dark;
|
|
234
|
+
let borderWidth = 1;
|
|
235
|
+
if (errorText || invalid) {
|
|
236
|
+
borderColor = theme.border.error;
|
|
237
|
+
borderWidth = 2;
|
|
238
|
+
}
|
|
239
|
+
if (isFocused) {
|
|
240
|
+
borderColor = theme.border.focus;
|
|
241
|
+
borderWidth = 3;
|
|
242
|
+
}
|
|
243
|
+
return (
|
|
244
|
+
<TextInput
|
|
245
|
+
{...ONE_TIME_CODE_PROPS}
|
|
246
|
+
accessibilityLabel={`Digit ${index + 1} of ${length}`}
|
|
247
|
+
accessibilityState={{disabled}}
|
|
248
|
+
aria-invalid={Boolean(errorText) || invalid}
|
|
249
|
+
aria-label={`Digit ${index + 1} of ${length}`}
|
|
250
|
+
autoCapitalize="none"
|
|
251
|
+
autoCorrect={false}
|
|
252
|
+
editable={!disabled}
|
|
253
|
+
enterKeyHint="done"
|
|
254
|
+
inputMode="numeric"
|
|
255
|
+
key={index}
|
|
256
|
+
keyboardType="number-pad"
|
|
257
|
+
nativeID={`${testID}-cell-${index + 1}`}
|
|
258
|
+
onBlur={() => handleBlur(index)}
|
|
259
|
+
onChangeText={(text: string) => handleChangeText(index, text)}
|
|
260
|
+
onFocus={() => handleFocus(index)}
|
|
261
|
+
onKeyPress={(event: TextInputKeyPressEvent) => handleKeyPress(index, event)}
|
|
262
|
+
onSubmitEditing={() => onEnter?.(code)}
|
|
263
|
+
ref={(ref) => {
|
|
264
|
+
inputRefs.current[index] = ref;
|
|
265
|
+
}}
|
|
266
|
+
selectTextOnFocus
|
|
267
|
+
style={
|
|
268
|
+
{
|
|
269
|
+
backgroundColor: disabled ? theme.surface.neutralLight : theme.surface.base,
|
|
270
|
+
borderColor,
|
|
271
|
+
borderRadius: 4,
|
|
272
|
+
borderWidth,
|
|
273
|
+
color: theme.text.primary,
|
|
274
|
+
fontSize: 24,
|
|
275
|
+
fontWeight: "700",
|
|
276
|
+
height: CELL_HEIGHT,
|
|
277
|
+
padding: 0,
|
|
278
|
+
textAlign: "center",
|
|
279
|
+
width: CELL_WIDTH,
|
|
280
|
+
...(Platform.OS === "web" ? {outline: "none"} : {}),
|
|
281
|
+
} as StyleProp<TextStyleWithOutline>
|
|
282
|
+
}
|
|
283
|
+
tabIndex={index === activeIndex ? 0 : -1}
|
|
284
|
+
testID={`${testID}-cell-${index + 1}`}
|
|
285
|
+
underlineColorAndroid="transparent"
|
|
286
|
+
value={code[index] ?? ""}
|
|
287
|
+
/>
|
|
288
|
+
);
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
return (
|
|
292
|
+
<View style={{flexDirection: "column", gap: 4}}>
|
|
293
|
+
{Boolean(title) && <FieldTitle testID={`${testID}-title`} text={title!} />}
|
|
294
|
+
<View
|
|
295
|
+
accessibilityLabel={accessibilityLabel}
|
|
296
|
+
aria-label={accessibilityLabel}
|
|
297
|
+
role="group"
|
|
298
|
+
style={{flexDirection: "row", flexWrap: "wrap", gap: 8}}
|
|
299
|
+
testID={testID}
|
|
300
|
+
>
|
|
301
|
+
{Array.from({length}, (_, index) => renderCell(index))}
|
|
302
|
+
</View>
|
|
303
|
+
{Boolean(errorText) && <FieldError testID={`${testID}-error`} text={errorText!} />}
|
|
304
|
+
{Boolean(helperText) && <FieldHelperText testID={`${testID}-helper`} text={helperText!} />}
|
|
305
|
+
</View>
|
|
306
|
+
);
|
|
307
|
+
};
|
package/src/Page.test.tsx
CHANGED
|
@@ -59,9 +59,13 @@ mock.module("expo-router", () => ({
|
|
|
59
59
|
}));
|
|
60
60
|
|
|
61
61
|
import {Page} from "./Page";
|
|
62
|
+
import {SkeletonList} from "./Skeleton";
|
|
62
63
|
import {Text} from "./Text";
|
|
63
64
|
import {renderWithTheme} from "./test-utils";
|
|
64
65
|
|
|
66
|
+
// Skeletons are hidden from assistive tech, so queries opt in to hidden elements.
|
|
67
|
+
const HIDDEN = {includeHiddenElements: true};
|
|
68
|
+
|
|
65
69
|
// Restore the global mocks set up by bunSetup.ts after this file finishes so
|
|
66
70
|
// that other test files (e.g. IconButton.test.tsx, ConsentFormScreen.test.tsx)
|
|
67
71
|
// are not affected by the overrides above.
|
|
@@ -327,4 +331,38 @@ describe("Page", () => {
|
|
|
327
331
|
// No crash; the optional-chained call handles the missing prop.
|
|
328
332
|
expect(getByText("Go")).toBeTruthy();
|
|
329
333
|
});
|
|
334
|
+
|
|
335
|
+
it("renders loadingComponent instead of the spinner when provided", () => {
|
|
336
|
+
const {getByTestId, queryByText} = renderWithTheme(
|
|
337
|
+
<Page
|
|
338
|
+
loading
|
|
339
|
+
loadingComponent={<SkeletonList testID="page-skeleton" />}
|
|
340
|
+
navigation={mockNavigation}
|
|
341
|
+
testID="page"
|
|
342
|
+
>
|
|
343
|
+
<Text>Content</Text>
|
|
344
|
+
</Page>
|
|
345
|
+
);
|
|
346
|
+
expect(getByTestId("page-skeleton", HIDDEN)).toBeTruthy();
|
|
347
|
+
expect(getByTestId("page-loading").props["aria-busy"]).toBe(true);
|
|
348
|
+
expect(queryByText("Content")).toBeNull();
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
it("announces loadingLabel and is busy while showing the default spinner", () => {
|
|
352
|
+
const {getByTestId, getByText} = renderWithTheme(
|
|
353
|
+
<Page
|
|
354
|
+
loading
|
|
355
|
+
loadingLabel="Loading patients"
|
|
356
|
+
loadingText="Fetching..."
|
|
357
|
+
navigation={mockNavigation}
|
|
358
|
+
testID="page"
|
|
359
|
+
>
|
|
360
|
+
<Text>Content</Text>
|
|
361
|
+
</Page>
|
|
362
|
+
);
|
|
363
|
+
const region = getByTestId("page-loading");
|
|
364
|
+
expect(region.props["aria-busy"]).toBe(true);
|
|
365
|
+
expect(region.props.accessibilityLabel).toBe("Loading patients");
|
|
366
|
+
expect(getByText("Fetching...", HIDDEN)).toBeTruthy();
|
|
367
|
+
});
|
|
330
368
|
});
|