@terreno/ui 57.19.2 → 57.21.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 +443 -0
- package/dist/Accordion.d.ts.map +1 -1
- package/dist/Accordion.js +3 -3
- package/dist/Accordion.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/Box.d.ts.map +1 -1
- package/dist/Box.js +38 -2
- package/dist/Box.js.map +1 -1
- package/dist/Card.js +2 -2
- package/dist/Card.js.map +1 -1
- package/dist/Common.d.ts +175 -34
- 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 +11 -9
- package/dist/DateTimeField.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/HeightField.js +8 -8
- package/dist/HeightField.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/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/PickerSelect.js +9 -9
- package/dist/PickerSelect.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/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 +11 -0
- package/dist/SplitPage.d.ts.map +1 -1
- package/dist/SplitPage.js +11 -5
- package/dist/SplitPage.js.map +1 -1
- package/dist/SplitPage.native.d.ts.map +1 -1
- package/dist/SplitPage.native.js +3 -1
- package/dist/SplitPage.native.js.map +1 -1
- 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/TapToEdit.d.ts +1 -1
- package/dist/TapToEdit.d.ts.map +1 -1
- package/dist/TapToEdit.js.map +1 -1
- package/dist/TextField.d.ts.map +1 -1
- package/dist/TextField.js +48 -14
- package/dist/TextField.js.map +1 -1
- package/dist/Theme.d.ts.map +1 -1
- package/dist/Theme.js +16 -0
- package/dist/Theme.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 +19 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -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/package.json +5 -2
- package/src/Accordion.test.tsx +5 -0
- package/src/Accordion.tsx +3 -8
- package/src/ActionSheet.test.tsx +13 -13
- package/src/AddressField.test.tsx +9 -3
- package/src/AnnouncementNavigator.test.tsx +4 -4
- package/src/AreaChart.test.tsx +15 -0
- package/src/Avatar.test.tsx +8 -0
- package/src/Avatar.tsx +2 -0
- package/src/Badge.test.tsx +1 -1
- package/src/Banner.test.tsx +0 -1
- package/src/BarChart.test.tsx +15 -0
- package/src/Box.test.tsx +143 -75
- package/src/Box.tsx +44 -2
- package/src/Button.test.tsx +11 -6
- package/src/Card.test.tsx +2 -2
- package/src/Card.tsx +2 -2
- package/src/Common.ts +194 -36
- package/src/ConflictSheet.test.tsx +5 -5
- package/src/ConsentFormScreen.test.tsx +87 -1
- package/src/ConsentFormScreen.tsx +28 -4
- package/src/ConsentNavigator.test.tsx +1 -2
- package/src/CustomSelectField.test.tsx +16 -4
- package/src/DataTable.test.tsx +4 -4
- package/src/DataTableVirtualizationRegression.test.tsx +1 -0
- package/src/DateTimeActionSheet.test.tsx +2 -0
- package/src/DateTimeField.test.tsx +3 -12
- package/src/DateTimeField.tsx +11 -9
- package/src/DismissButton.test.tsx +22 -4
- package/src/DonutChart.test.tsx +15 -0
- package/src/EmailField.test.tsx +13 -22
- package/src/EmojiSelector.test.tsx +2 -2
- package/src/ErrorBoundary.test.tsx +5 -3
- package/src/Field.test.tsx +20 -44
- 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 +40 -16
- package/src/GPTChat.tsx +8 -2
- package/src/Heading.test.tsx +15 -1
- package/src/Heading.tsx +6 -1
- package/src/HeightField.tsx +8 -8
- package/src/Icon.test.tsx +54 -3
- package/src/Icon.tsx +92 -3
- package/src/IconButton.test.tsx +85 -0
- package/src/Image.test.tsx +1 -1
- package/src/LabeledCheckBox.test.tsx +45 -0
- package/src/LabeledCheckBox.tsx +89 -0
- package/src/Link.test.tsx +11 -4
- package/src/ListDetailLayout.tsx +247 -0
- package/src/MarkdownEditor.tsx +2 -1
- package/src/MarkdownView.test.tsx +1 -1
- package/src/MediaQuery.ts +1 -0
- package/src/MobileAddressAutoComplete.test.tsx +2 -1
- package/src/Modal.test.tsx +7 -2
- package/src/ModalSheet.test.tsx +5 -4
- package/src/MultiselectField.test.tsx +17 -3
- package/src/NumberField.test.tsx +14 -14
- package/src/NumberPickerActionSheet.test.tsx +1 -1
- package/src/OpenAPIContext.test.tsx +4 -4
- package/src/OpenAPIRenderRegression.test.tsx +1 -1
- package/src/OtpField.test.tsx +280 -0
- package/src/OtpField.tsx +307 -0
- package/src/P0RenderRegression.test.tsx +7 -1
- package/src/P1RenderRegression.test.tsx +1 -0
- package/src/P2ButtonRenderRegression.test.tsx +1 -0
- package/src/Page.test.tsx +1 -1
- package/src/PhoneNumberField.test.tsx +14 -38
- package/src/PickerSelect.tsx +9 -9
- package/src/Popover.test.tsx +2 -2
- package/src/ProvisionalConsentSigner.test.tsx +625 -0
- package/src/ProvisionalConsentSigner.tsx +423 -0
- package/src/RadioField.test.tsx +7 -2
- package/src/ResponsiveBreakpoint.test.tsx +7 -0
- package/src/ResponsiveBreakpoint.tsx +14 -0
- package/src/RootImportRegression.test.tsx +3 -0
- package/src/SegmentedControl.test.tsx +21 -12
- package/src/SidebarNavigation.test.tsx +18 -6
- package/src/Signature.test.tsx +20 -23
- package/src/Slider.test.tsx +1 -1
- package/src/SocialLoginButton.test.tsx +14 -0
- package/src/SocialLoginButton.tsx +51 -7
- package/src/SplitPage.native.tsx +3 -1
- package/src/SplitPage.test.tsx +15 -5
- package/src/SplitPage.tsx +11 -5
- package/src/SyncStatusBanner.test.tsx +61 -4
- package/src/SyncStatusBanner.tsx +29 -3
- package/src/TapToEdit.test.tsx +38 -15
- package/src/TapToEdit.tsx +1 -1
- package/src/TerrenoFontProvider.test.tsx +1 -1
- package/src/Text.test.tsx +4 -4
- package/src/TextField.test.tsx +232 -9
- package/src/TextField.tsx +70 -12
- package/src/Theme.test.tsx +49 -0
- package/src/Theme.tsx +16 -0
- package/src/Toast.test.tsx +5 -5
- package/src/ToastNotifications.test.tsx +73 -70
- package/src/ToastNotifications.webModal.test.tsx +2 -2
- package/src/Tooltip.test.tsx +3 -3
- package/src/UnifiedAddressAutoComplete.test.tsx +1 -1
- package/src/UserInactivity.test.tsx +4 -2
- package/src/WebAddressAutocomplete.test.tsx +12 -12
- package/src/WebDropdownMenu.test.tsx +16 -15
- 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__/Card.test.tsx.snap +1 -1
- package/src/__snapshots__/CustomSelectField.test.tsx.snap +3 -0
- package/src/__snapshots__/DismissButton.test.tsx.snap +3 -3
- package/src/__snapshots__/EmailField.test.tsx.snap +6 -0
- package/src/__snapshots__/Field.test.tsx.snap +41 -2
- package/src/__snapshots__/Heading.test.tsx.snap +1 -1
- package/src/__snapshots__/HeightField.test.tsx.snap +2 -0
- package/src/__snapshots__/Image.test.tsx.snap +1 -1
- 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 +2 -2
- 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__/TapToEdit.test.tsx.snap +3 -0
- package/src/__snapshots__/Text.test.tsx.snap +1 -1
- 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/asks/AskCard.test.tsx +16 -13
- package/src/asks/AskFiles.test.tsx +5 -1
- package/src/asks/askFileRefs.test.ts +1 -1
- package/src/asks/askSummary.test.ts +2 -2
- package/src/benchmarks/importTiming.ts +1 -1
- package/src/blocks/BlocksView.test.tsx +20 -11
- package/src/bunSetup.ts +14 -2
- package/src/dataTableFilters.test.tsx +1 -1
- package/src/fieldElements/FieldError.tsx +1 -0
- package/src/fieldElements/__snapshots__/FieldError.test.tsx.snap +2 -0
- package/src/index.tsx +42 -1
- package/src/isolated/ListDetailLayout.isolated.tsx +632 -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 +839 -3
- 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 +171 -2
- 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/table/tableContext.test.tsx +5 -5
- package/src/test-utils.tsx +56 -1
- package/src/testing/reactDomServer.d.ts +7 -0
- package/src/useConsentForms.test.ts +38 -2
- package/src/useConsentForms.ts +25 -0
- package/src/useProvisionalConsent.test.ts +256 -0
- package/src/useProvisionalConsent.ts +218 -0
package/src/Field.test.tsx
CHANGED
|
@@ -6,33 +6,23 @@ import {renderWithTheme} from "./test-utils";
|
|
|
6
6
|
|
|
7
7
|
describe("Field", () => {
|
|
8
8
|
it("renders text field by default", () => {
|
|
9
|
-
const {toJSON} = renderWithTheme(<Field
|
|
9
|
+
const {toJSON} = renderWithTheme(<Field onChange={() => {}} value="" />);
|
|
10
10
|
expect(toJSON()).toMatchSnapshot();
|
|
11
11
|
});
|
|
12
12
|
|
|
13
13
|
it("renders text field with type text", () => {
|
|
14
|
-
const {toJSON} = renderWithTheme(
|
|
15
|
-
<Field label="Name" onChange={() => {}} type="text" value="" />
|
|
16
|
-
);
|
|
14
|
+
const {toJSON} = renderWithTheme(<Field onChange={() => {}} type="text" value="" />);
|
|
17
15
|
expect(toJSON()).toMatchSnapshot();
|
|
18
16
|
});
|
|
19
17
|
|
|
20
18
|
it("renders password field", () => {
|
|
21
|
-
const {toJSON} = renderWithTheme(
|
|
22
|
-
<Field label="Password" onChange={() => {}} type="password" value="" />
|
|
23
|
-
);
|
|
19
|
+
const {toJSON} = renderWithTheme(<Field onChange={() => {}} type="password" value="" />);
|
|
24
20
|
expect(toJSON()).toMatchSnapshot();
|
|
25
21
|
});
|
|
26
22
|
|
|
27
23
|
it("reveals the password value from the visibility toggle", () => {
|
|
28
24
|
const {getByDisplayValue, getByTestId} = renderWithTheme(
|
|
29
|
-
<Field
|
|
30
|
-
label="Password"
|
|
31
|
-
onChange={() => {}}
|
|
32
|
-
testID="field-password"
|
|
33
|
-
type="password"
|
|
34
|
-
value="hunter2"
|
|
35
|
-
/>
|
|
25
|
+
<Field onChange={() => {}} testID="field-password" type="password" value="hunter2" />
|
|
36
26
|
);
|
|
37
27
|
|
|
38
28
|
expect(getByDisplayValue("hunter2").props.secureTextEntry).toBe(true);
|
|
@@ -42,58 +32,46 @@ describe("Field", () => {
|
|
|
42
32
|
});
|
|
43
33
|
|
|
44
34
|
it("renders email field", () => {
|
|
45
|
-
const {toJSON} = renderWithTheme(
|
|
46
|
-
<Field label="Email" onChange={() => {}} type="email" value="" />
|
|
47
|
-
);
|
|
35
|
+
const {toJSON} = renderWithTheme(<Field onChange={() => {}} type="email" value="" />);
|
|
48
36
|
expect(toJSON()).toMatchSnapshot();
|
|
49
37
|
});
|
|
50
38
|
|
|
51
39
|
it("renders url field", () => {
|
|
52
|
-
const {toJSON} = renderWithTheme(
|
|
53
|
-
<Field label="Website" onChange={() => {}} type="url" value="" />
|
|
54
|
-
);
|
|
40
|
+
const {toJSON} = renderWithTheme(<Field onChange={() => {}} type="url" value="" />);
|
|
55
41
|
expect(toJSON()).toMatchSnapshot();
|
|
56
42
|
});
|
|
57
43
|
|
|
58
44
|
it("renders number field", () => {
|
|
59
|
-
const {toJSON} = renderWithTheme(
|
|
60
|
-
<Field label="Age" onChange={() => {}} type="number" value="" />
|
|
61
|
-
);
|
|
45
|
+
const {toJSON} = renderWithTheme(<Field onChange={() => {}} type="number" value="" />);
|
|
62
46
|
expect(toJSON()).toMatchSnapshot();
|
|
63
47
|
});
|
|
64
48
|
|
|
65
49
|
it("renders textarea field", () => {
|
|
66
|
-
const {toJSON} = renderWithTheme(
|
|
67
|
-
<Field label="Description" onChange={() => {}} type="textarea" value="" />
|
|
68
|
-
);
|
|
50
|
+
const {toJSON} = renderWithTheme(<Field onChange={() => {}} type="textarea" value="" />);
|
|
69
51
|
expect(toJSON()).toMatchSnapshot();
|
|
70
52
|
});
|
|
71
53
|
|
|
72
54
|
it("renders boolean field", () => {
|
|
73
55
|
const handleChange = mock((_value: boolean) => {});
|
|
74
56
|
const {toJSON} = renderWithTheme(
|
|
75
|
-
<Field
|
|
57
|
+
<Field onChange={handleChange} type="boolean" value={false} />
|
|
76
58
|
);
|
|
77
59
|
expect(toJSON()).toMatchSnapshot();
|
|
78
60
|
});
|
|
79
61
|
|
|
80
62
|
it("renders date field", () => {
|
|
81
|
-
const {toJSON} = renderWithTheme(
|
|
82
|
-
<Field label="Birth Date" onChange={() => {}} type="date" value={null} />
|
|
83
|
-
);
|
|
63
|
+
const {toJSON} = renderWithTheme(<Field onChange={() => {}} type="date" value={undefined} />);
|
|
84
64
|
expect(toJSON()).toMatchSnapshot();
|
|
85
65
|
});
|
|
86
66
|
|
|
87
67
|
it("renders time field", () => {
|
|
88
|
-
const {toJSON} = renderWithTheme(
|
|
89
|
-
<Field label="Start Time" onChange={() => {}} type="time" value={null} />
|
|
90
|
-
);
|
|
68
|
+
const {toJSON} = renderWithTheme(<Field onChange={() => {}} type="time" value={undefined} />);
|
|
91
69
|
expect(toJSON()).toMatchSnapshot();
|
|
92
70
|
});
|
|
93
71
|
|
|
94
72
|
it("renders datetime field", () => {
|
|
95
73
|
const {toJSON} = renderWithTheme(
|
|
96
|
-
<Field
|
|
74
|
+
<Field onChange={() => {}} type="datetime" value={undefined} />
|
|
97
75
|
);
|
|
98
76
|
expect(toJSON()).toMatchSnapshot();
|
|
99
77
|
});
|
|
@@ -101,7 +79,6 @@ describe("Field", () => {
|
|
|
101
79
|
it("renders select field", () => {
|
|
102
80
|
const {toJSON} = renderWithTheme(
|
|
103
81
|
<Field
|
|
104
|
-
label="Country"
|
|
105
82
|
onChange={() => {}}
|
|
106
83
|
options={[
|
|
107
84
|
{label: "USA", value: "us"},
|
|
@@ -117,12 +94,12 @@ describe("Field", () => {
|
|
|
117
94
|
it("renders multiselect field", () => {
|
|
118
95
|
const {toJSON} = renderWithTheme(
|
|
119
96
|
<Field
|
|
120
|
-
label="Skills"
|
|
121
97
|
onChange={() => {}}
|
|
122
98
|
options={[
|
|
123
99
|
{label: "JavaScript", value: "js"},
|
|
124
100
|
{label: "Python", value: "py"},
|
|
125
101
|
]}
|
|
102
|
+
title="Skills"
|
|
126
103
|
type="multiselect"
|
|
127
104
|
value={[]}
|
|
128
105
|
/>
|
|
@@ -132,22 +109,23 @@ describe("Field", () => {
|
|
|
132
109
|
|
|
133
110
|
it("renders address field", () => {
|
|
134
111
|
const {toJSON} = renderWithTheme(
|
|
135
|
-
<Field
|
|
112
|
+
<Field
|
|
113
|
+
onChange={() => {}}
|
|
114
|
+
type="address"
|
|
115
|
+
value={{address1: "", city: "", state: "", zipcode: ""}}
|
|
116
|
+
/>
|
|
136
117
|
);
|
|
137
118
|
expect(toJSON()).toMatchSnapshot();
|
|
138
119
|
});
|
|
139
120
|
|
|
140
121
|
it("renders phoneNumber field", () => {
|
|
141
|
-
const {toJSON} = renderWithTheme(
|
|
142
|
-
<Field label="Phone" onChange={() => {}} type="phoneNumber" value="" />
|
|
143
|
-
);
|
|
122
|
+
const {toJSON} = renderWithTheme(<Field onChange={() => {}} type="phoneNumber" value="" />);
|
|
144
123
|
expect(toJSON()).toMatchSnapshot();
|
|
145
124
|
});
|
|
146
125
|
|
|
147
126
|
it("renders customSelect field", () => {
|
|
148
127
|
const {toJSON} = renderWithTheme(
|
|
149
128
|
<Field
|
|
150
|
-
label="Custom"
|
|
151
129
|
onChange={() => {}}
|
|
152
130
|
options={[
|
|
153
131
|
{label: "Option A", value: "a"},
|
|
@@ -161,9 +139,7 @@ describe("Field", () => {
|
|
|
161
139
|
});
|
|
162
140
|
|
|
163
141
|
it("renders signature field", () => {
|
|
164
|
-
const {toJSON} = renderWithTheme(
|
|
165
|
-
<Field label="Sign here" onChange={() => {}} type="signature" value="" />
|
|
166
|
-
);
|
|
142
|
+
const {toJSON} = renderWithTheme(<Field onChange={() => {}} type="signature" value="" />);
|
|
167
143
|
expect(toJSON()).toMatchSnapshot();
|
|
168
144
|
});
|
|
169
145
|
});
|
package/src/Field.tsx
CHANGED
|
@@ -90,6 +90,8 @@ export const Field: FC<FieldProps> = ({type, ...rest}) => {
|
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
+
// Defaults only: a password field that sets a new password passes autoComplete="new-password",
|
|
94
|
+
// which the spread below applies.
|
|
93
95
|
return (
|
|
94
96
|
<TextField
|
|
95
97
|
autoComplete={autoComplete}
|
|
@@ -271,4 +271,39 @@ describe("FilePickerButton", () => {
|
|
|
271
271
|
globals.FileReader = originalFileReader;
|
|
272
272
|
}
|
|
273
273
|
});
|
|
274
|
+
|
|
275
|
+
it("resolves an empty selection when the browser file dialog is cancelled", async () => {
|
|
276
|
+
const listeners: Record<string, () => void> = {};
|
|
277
|
+
const input = {
|
|
278
|
+
accept: "",
|
|
279
|
+
addEventListener: (event: string, handler: () => void): void => {
|
|
280
|
+
listeners[event] = handler;
|
|
281
|
+
},
|
|
282
|
+
click: mock(() => {}),
|
|
283
|
+
files: [],
|
|
284
|
+
multiple: false,
|
|
285
|
+
remove: mock(() => {}),
|
|
286
|
+
style: {display: ""},
|
|
287
|
+
type: "",
|
|
288
|
+
};
|
|
289
|
+
const globals = globalThis as unknown as Record<string, unknown>;
|
|
290
|
+
const originalDocument = globals.document;
|
|
291
|
+
const originalOS = Platform.OS;
|
|
292
|
+
const {files, getByText} = openModal();
|
|
293
|
+
globals.document = {
|
|
294
|
+
body: {appendChild: mock(() => {})},
|
|
295
|
+
createElement: (): typeof input => input,
|
|
296
|
+
};
|
|
297
|
+
Platform.OS = "web";
|
|
298
|
+
try {
|
|
299
|
+
fireEvent.press(getByText("Document"));
|
|
300
|
+
listeners.cancel?.();
|
|
301
|
+
expect(input.remove).toHaveBeenCalledTimes(1);
|
|
302
|
+
expect(files).toHaveLength(0);
|
|
303
|
+
expect(getDocumentAsync).not.toHaveBeenCalled();
|
|
304
|
+
} finally {
|
|
305
|
+
Platform.OS = originalOS;
|
|
306
|
+
globals.document = originalDocument;
|
|
307
|
+
}
|
|
308
|
+
});
|
|
274
309
|
});
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import {afterEach, beforeEach, describe, expect, it, mock, spyOn} from "bun:test";
|
|
2
|
+
import {act, fireEvent, render} from "@testing-library/react-native";
|
|
3
|
+
import {createRef} from "react";
|
|
4
|
+
import {Platform, Text} from "react-native";
|
|
5
|
+
|
|
6
|
+
import {FormContainer, type FormContainerHandle} from "./FormContainer";
|
|
7
|
+
import {ThemeProvider} from "./Theme";
|
|
8
|
+
import {renderWithTheme} from "./test-utils";
|
|
9
|
+
|
|
10
|
+
const setOS = (os: string): string => {
|
|
11
|
+
const original = Platform.OS;
|
|
12
|
+
Object.defineProperty(Platform, "OS", {configurable: true, value: os});
|
|
13
|
+
return original;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
describe("FormContainer", () => {
|
|
17
|
+
describe("native", () => {
|
|
18
|
+
it("renders its children in a Box, with no form element", () => {
|
|
19
|
+
const {getByTestId, getByText, UNSAFE_queryAllByType} = renderWithTheme(
|
|
20
|
+
<FormContainer gap={2} testID="sign-in-form">
|
|
21
|
+
<Text>Child</Text>
|
|
22
|
+
</FormContainer>
|
|
23
|
+
);
|
|
24
|
+
expect(getByTestId("sign-in-form")).toBeTruthy();
|
|
25
|
+
expect(getByText("Child")).toBeTruthy();
|
|
26
|
+
expect(UNSAFE_queryAllByType("form" as never)).toHaveLength(0);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("renders no hidden username input", () => {
|
|
30
|
+
const {UNSAFE_queryAllByType} = renderWithTheme(
|
|
31
|
+
<FormContainer username="ada@example.com">
|
|
32
|
+
<Text>Child</Text>
|
|
33
|
+
</FormContainer>
|
|
34
|
+
);
|
|
35
|
+
expect(UNSAFE_queryAllByType("input" as never)).toHaveLength(0);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("calls onSubmit when requestSubmit is called", async () => {
|
|
39
|
+
const onSubmit = mock(() => {});
|
|
40
|
+
const ref = createRef<FormContainerHandle>();
|
|
41
|
+
renderWithTheme(
|
|
42
|
+
<FormContainer onSubmit={onSubmit} ref={ref} testID="sign-in-form">
|
|
43
|
+
<Text>Child</Text>
|
|
44
|
+
</FormContainer>
|
|
45
|
+
);
|
|
46
|
+
await act(async () => {
|
|
47
|
+
await ref.current?.requestSubmit();
|
|
48
|
+
});
|
|
49
|
+
expect(onSubmit).toHaveBeenCalledTimes(1);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("does nothing on requestSubmit without onSubmit", async () => {
|
|
53
|
+
const ref = createRef<FormContainerHandle>();
|
|
54
|
+
renderWithTheme(
|
|
55
|
+
<FormContainer ref={ref}>
|
|
56
|
+
<Text>Child</Text>
|
|
57
|
+
</FormContainer>
|
|
58
|
+
);
|
|
59
|
+
await expect(ref.current?.requestSubmit()).resolves.toBeUndefined();
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe("web", () => {
|
|
64
|
+
let originalOS: string;
|
|
65
|
+
|
|
66
|
+
beforeEach(() => {
|
|
67
|
+
originalOS = setOS("web");
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
afterEach(() => {
|
|
71
|
+
setOS(originalOS);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("renders a real form that skips browser validation and does not take part in layout", () => {
|
|
75
|
+
const {UNSAFE_getByType, getByText} = renderWithTheme(
|
|
76
|
+
<FormContainer accessibilityLabel="Sign in" gap={4} testID="sign-in-form">
|
|
77
|
+
<Text>Child</Text>
|
|
78
|
+
</FormContainer>
|
|
79
|
+
);
|
|
80
|
+
const form = UNSAFE_getByType("form" as never);
|
|
81
|
+
expect(form.props.noValidate).toBe(true);
|
|
82
|
+
expect(form.props["data-testid"]).toBe("sign-in-form");
|
|
83
|
+
expect(form.props["aria-label"]).toBe("Sign in");
|
|
84
|
+
expect(form.props.style).toEqual({display: "contents"});
|
|
85
|
+
expect(getByText("Child")).toBeTruthy();
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("adds a hidden username input so password managers know whose password changes", () => {
|
|
89
|
+
const {UNSAFE_getByType} = renderWithTheme(
|
|
90
|
+
<FormContainer testID="password-form" username="ada@example.com">
|
|
91
|
+
<Text>Child</Text>
|
|
92
|
+
</FormContainer>
|
|
93
|
+
);
|
|
94
|
+
const input = UNSAFE_getByType("input" as never);
|
|
95
|
+
expect(input.props.autoComplete).toBe("username");
|
|
96
|
+
expect(input.props.value).toBe("ada@example.com");
|
|
97
|
+
expect(input.props.hidden).toBe(true);
|
|
98
|
+
expect(input.props.readOnly).toBe(true);
|
|
99
|
+
expect(input.props.tabIndex).toBe(-1);
|
|
100
|
+
expect(input.props["data-testid"]).toBe("password-form-username");
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("leaves out the hidden username input without a username", () => {
|
|
104
|
+
const {UNSAFE_queryAllByType} = renderWithTheme(
|
|
105
|
+
<FormContainer>
|
|
106
|
+
<Text>Child</Text>
|
|
107
|
+
</FormContainer>
|
|
108
|
+
);
|
|
109
|
+
expect(UNSAFE_queryAllByType("input" as never)).toHaveLength(0);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("prevents the page reload and calls onSubmit on a submit event", () => {
|
|
113
|
+
const onSubmit = mock(() => {});
|
|
114
|
+
const preventDefault = mock(() => {});
|
|
115
|
+
const {UNSAFE_getByType} = renderWithTheme(
|
|
116
|
+
<FormContainer onSubmit={onSubmit}>
|
|
117
|
+
<Text>Child</Text>
|
|
118
|
+
</FormContainer>
|
|
119
|
+
);
|
|
120
|
+
fireEvent(UNSAFE_getByType("form" as never), "submit", {preventDefault});
|
|
121
|
+
expect(preventDefault).toHaveBeenCalledTimes(1);
|
|
122
|
+
expect(onSubmit).toHaveBeenCalledTimes(1);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("prevents the page reload even without onSubmit", () => {
|
|
126
|
+
const preventDefault = mock(() => {});
|
|
127
|
+
const {UNSAFE_getByType} = renderWithTheme(
|
|
128
|
+
<FormContainer>
|
|
129
|
+
<Text>Child</Text>
|
|
130
|
+
</FormContainer>
|
|
131
|
+
);
|
|
132
|
+
fireEvent(UNSAFE_getByType("form" as never), "submit", {preventDefault});
|
|
133
|
+
expect(preventDefault).toHaveBeenCalledTimes(1);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("routes requestSubmit through the form element so password managers see a submit", async () => {
|
|
137
|
+
const onSubmit = mock(() => {});
|
|
138
|
+
const requestSubmit = mock(() => {});
|
|
139
|
+
const ref = createRef<FormContainerHandle>();
|
|
140
|
+
render(
|
|
141
|
+
<FormContainer onSubmit={onSubmit} ref={ref}>
|
|
142
|
+
<Text>Child</Text>
|
|
143
|
+
</FormContainer>,
|
|
144
|
+
{
|
|
145
|
+
createNodeMock: (element) => (element.type === "form" ? {requestSubmit} : null),
|
|
146
|
+
wrapper: ThemeProvider,
|
|
147
|
+
}
|
|
148
|
+
);
|
|
149
|
+
await act(async () => {
|
|
150
|
+
await ref.current?.requestSubmit();
|
|
151
|
+
});
|
|
152
|
+
expect(requestSubmit).toHaveBeenCalledTimes(1);
|
|
153
|
+
// The browser fires the submit event, which calls onSubmit; requestSubmit does not.
|
|
154
|
+
expect(onSubmit).not.toHaveBeenCalled();
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("resolves requestSubmit after the browser-run onSubmit settles", async () => {
|
|
158
|
+
let resolveSubmit: () => void = () => {};
|
|
159
|
+
const onSubmit = mock(
|
|
160
|
+
() =>
|
|
161
|
+
new Promise<void>((resolve) => {
|
|
162
|
+
resolveSubmit = resolve;
|
|
163
|
+
})
|
|
164
|
+
);
|
|
165
|
+
const ref = createRef<FormContainerHandle>();
|
|
166
|
+
const {UNSAFE_getByType} = render(
|
|
167
|
+
<FormContainer onSubmit={onSubmit} ref={ref}>
|
|
168
|
+
<Text>Child</Text>
|
|
169
|
+
</FormContainer>,
|
|
170
|
+
{
|
|
171
|
+
createNodeMock: (element) =>
|
|
172
|
+
element.type === "form"
|
|
173
|
+
? {
|
|
174
|
+
requestSubmit: (): void => {
|
|
175
|
+
fireEvent(UNSAFE_getByType("form" as never), "submit", {
|
|
176
|
+
preventDefault: () => {},
|
|
177
|
+
});
|
|
178
|
+
},
|
|
179
|
+
}
|
|
180
|
+
: null,
|
|
181
|
+
wrapper: ThemeProvider,
|
|
182
|
+
}
|
|
183
|
+
);
|
|
184
|
+
let isSettled = false;
|
|
185
|
+
const pending = ref.current?.requestSubmit().then(() => {
|
|
186
|
+
isSettled = true;
|
|
187
|
+
});
|
|
188
|
+
await Promise.resolve();
|
|
189
|
+
expect(onSubmit).toHaveBeenCalledTimes(1);
|
|
190
|
+
expect(isSettled).toBe(false);
|
|
191
|
+
resolveSubmit();
|
|
192
|
+
await pending;
|
|
193
|
+
expect(isSettled).toBe(true);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it("warns instead of leaving a rejection unhandled on a browser-started submit", async () => {
|
|
197
|
+
const warn = spyOn(console, "warn").mockImplementation(() => {});
|
|
198
|
+
const {UNSAFE_getByType} = renderWithTheme(
|
|
199
|
+
<FormContainer onSubmit={() => Promise.reject(new Error("nope"))}>
|
|
200
|
+
<Text>Child</Text>
|
|
201
|
+
</FormContainer>
|
|
202
|
+
);
|
|
203
|
+
fireEvent(UNSAFE_getByType("form" as never), "submit", {preventDefault: () => {}});
|
|
204
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
205
|
+
expect(warn).toHaveBeenCalledWith("FormContainer: onSubmit rejected", expect.any(Error));
|
|
206
|
+
warn.mockRestore();
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it("renders a nested FormContainer as a Box, since HTML forms cannot nest", async () => {
|
|
210
|
+
const onInnerSubmit = mock(() => {});
|
|
211
|
+
const innerRef = createRef<FormContainerHandle>();
|
|
212
|
+
const {UNSAFE_queryAllByType, getByTestId} = renderWithTheme(
|
|
213
|
+
<FormContainer testID="outer">
|
|
214
|
+
<FormContainer onSubmit={onInnerSubmit} ref={innerRef} testID="inner">
|
|
215
|
+
<Text>Child</Text>
|
|
216
|
+
</FormContainer>
|
|
217
|
+
</FormContainer>
|
|
218
|
+
);
|
|
219
|
+
expect(UNSAFE_queryAllByType("form" as never)).toHaveLength(1);
|
|
220
|
+
expect(getByTestId("inner")).toBeTruthy();
|
|
221
|
+
await act(async () => {
|
|
222
|
+
await innerRef.current?.requestSubmit();
|
|
223
|
+
});
|
|
224
|
+
expect(onInnerSubmit).toHaveBeenCalledTimes(1);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it("calls onSubmit directly when the browser has no requestSubmit", async () => {
|
|
228
|
+
const onSubmit = mock(() => {});
|
|
229
|
+
const ref = createRef<FormContainerHandle>();
|
|
230
|
+
render(
|
|
231
|
+
<FormContainer onSubmit={onSubmit} ref={ref}>
|
|
232
|
+
<Text>Child</Text>
|
|
233
|
+
</FormContainer>,
|
|
234
|
+
{createNodeMock: () => ({}), wrapper: ThemeProvider}
|
|
235
|
+
);
|
|
236
|
+
await act(async () => {
|
|
237
|
+
await ref.current?.requestSubmit();
|
|
238
|
+
});
|
|
239
|
+
expect(onSubmit).toHaveBeenCalledTimes(1);
|
|
240
|
+
});
|
|
241
|
+
});
|
|
242
|
+
});
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import type {FC, Ref} from "react";
|
|
2
|
+
import {createContext, useCallback, useContext, useImperativeHandle, useRef} from "react";
|
|
3
|
+
import {Platform} from "react-native";
|
|
4
|
+
|
|
5
|
+
import {Box} from "./Box";
|
|
6
|
+
import type {BoxPropsBase} from "./Common";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Imperative handle for {@link FormContainer}, passed through its `ref` prop.
|
|
10
|
+
*/
|
|
11
|
+
export interface FormContainerHandle {
|
|
12
|
+
/**
|
|
13
|
+
* Submits the form. On web it calls the form element's `requestSubmit()`, so the browser fires a
|
|
14
|
+
* real submit event (password managers watch for it to offer "Save password") and the form's
|
|
15
|
+
* submit handler calls `onSubmit`. On native, and in browsers without `requestSubmit`, it calls
|
|
16
|
+
* `onSubmit` directly. Resolves when `onSubmit` settles, so a button awaiting it shows its
|
|
17
|
+
* loading state.
|
|
18
|
+
*/
|
|
19
|
+
requestSubmit: () => Promise<void>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Props for {@link FormContainer}. Layout props are the same as {@link Box}'s.
|
|
24
|
+
*/
|
|
25
|
+
export interface FormContainerProps
|
|
26
|
+
extends Omit<BoxPropsBase, "onClick" | "onHoverStart" | "onHoverEnd" | "scroll"> {
|
|
27
|
+
/**
|
|
28
|
+
* Called when the form is submitted: from `ref.requestSubmit()`, or on web when the browser
|
|
29
|
+
* submits the form. The browser's page reload is always prevented.
|
|
30
|
+
*/
|
|
31
|
+
onSubmit?: () => void | Promise<void>;
|
|
32
|
+
/** Names the form for screen readers (on web the form then becomes a "form" landmark). */
|
|
33
|
+
accessibilityLabel?: string;
|
|
34
|
+
/**
|
|
35
|
+
* The signed-in user's username or email, for forms without a username field such as a change
|
|
36
|
+
* password form. On web it goes in a hidden `autocomplete="username"` input, so password managers
|
|
37
|
+
* update that account's saved password. Ignored on native.
|
|
38
|
+
*/
|
|
39
|
+
username?: string;
|
|
40
|
+
/** Receives {@link FormContainerHandle}, to submit the form from a field's Enter key or a button. */
|
|
41
|
+
ref?: Ref<FormContainerHandle>;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** True inside a FormContainer's <form>; HTML does not allow a form inside another form. */
|
|
45
|
+
const InsideFormContext = createContext(false);
|
|
46
|
+
|
|
47
|
+
/** The DOM form element's submit method; absent in Safari before 16. */
|
|
48
|
+
interface WebFormElement {
|
|
49
|
+
requestSubmit?: () => void;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Groups a form's fields. On web it renders a real `<form>` around them, so browsers and password
|
|
54
|
+
* managers can pair a username with its password, autofill saved credentials, and offer to save
|
|
55
|
+
* new ones (Chrome warns "Password field is not contained in a form" without it). The form uses
|
|
56
|
+
* `display: contents`, so the children lay out exactly as in the inner {@link Box}, which takes all
|
|
57
|
+
* of Box's layout props. On native it renders just the Box.
|
|
58
|
+
*
|
|
59
|
+
* The form sets `noValidate` and always prevents the browser's page reload. Fields keep their own
|
|
60
|
+
* Enter handling: `TextField` calls `onEnter` and stops the browser's implicit submission, so call
|
|
61
|
+
* `ref.requestSubmit()` from the last field's `onEnter` and from the submit button. A FormContainer
|
|
62
|
+
* inside another one renders a Box (HTML forms cannot nest); its `requestSubmit` calls its own
|
|
63
|
+
* `onSubmit`.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```tsx
|
|
67
|
+
* const formRef = useRef<FormContainerHandle>(null);
|
|
68
|
+
*
|
|
69
|
+
* <FormContainer gap={4} onSubmit={signIn} ref={formRef}>
|
|
70
|
+
* <TextField autoComplete="username" onChange={setEmail} title="Email" type="email" value={email} />
|
|
71
|
+
* <TextField
|
|
72
|
+
* autoComplete="current-password"
|
|
73
|
+
* onChange={setPassword}
|
|
74
|
+
* onEnter={() => formRef.current?.requestSubmit()}
|
|
75
|
+
* title="Password"
|
|
76
|
+
* type="password"
|
|
77
|
+
* value={password}
|
|
78
|
+
* />
|
|
79
|
+
* <Button onClick={() => formRef.current?.requestSubmit()} text="Log In" />
|
|
80
|
+
* </FormContainer>
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export const FormContainer: FC<FormContainerProps> = ({
|
|
84
|
+
onSubmit,
|
|
85
|
+
accessibilityLabel,
|
|
86
|
+
username,
|
|
87
|
+
ref,
|
|
88
|
+
testID,
|
|
89
|
+
children,
|
|
90
|
+
...boxProps
|
|
91
|
+
}) => {
|
|
92
|
+
const isInsideForm = useContext(InsideFormContext);
|
|
93
|
+
const formElementRef = useRef<WebFormElement | null>(null);
|
|
94
|
+
// A ref keeps requestSubmit stable while still calling the latest onSubmit.
|
|
95
|
+
const onSubmitRef = useRef(onSubmit);
|
|
96
|
+
onSubmitRef.current = onSubmit;
|
|
97
|
+
|
|
98
|
+
// The browser runs the submit handler inside requestSubmit(); this carries its result back out.
|
|
99
|
+
const pendingSubmitRef = useRef<Promise<void> | undefined>(undefined);
|
|
100
|
+
|
|
101
|
+
const handleFormSubmit = useCallback((event: {preventDefault: () => void}): void => {
|
|
102
|
+
event.preventDefault();
|
|
103
|
+
const result = Promise.resolve(onSubmitRef.current?.());
|
|
104
|
+
pendingSubmitRef.current = result;
|
|
105
|
+
// A submit the browser starts on its own has no caller to report a rejection to.
|
|
106
|
+
result.catch((error: unknown) => {
|
|
107
|
+
console.warn("FormContainer: onSubmit rejected", error);
|
|
108
|
+
});
|
|
109
|
+
}, []);
|
|
110
|
+
|
|
111
|
+
useImperativeHandle(
|
|
112
|
+
ref,
|
|
113
|
+
(): FormContainerHandle => ({
|
|
114
|
+
requestSubmit: async (): Promise<void> => {
|
|
115
|
+
const form = formElementRef.current;
|
|
116
|
+
if (Platform.OS === "web" && typeof form?.requestSubmit === "function") {
|
|
117
|
+
pendingSubmitRef.current = undefined;
|
|
118
|
+
form.requestSubmit();
|
|
119
|
+
await pendingSubmitRef.current;
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
await onSubmitRef.current?.();
|
|
123
|
+
},
|
|
124
|
+
}),
|
|
125
|
+
[]
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
if (Platform.OS !== "web") {
|
|
129
|
+
return (
|
|
130
|
+
<Box {...boxProps} testID={testID}>
|
|
131
|
+
{children}
|
|
132
|
+
</Box>
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const usernameInput = Boolean(username) && (
|
|
137
|
+
<input
|
|
138
|
+
aria-hidden
|
|
139
|
+
autoComplete="username"
|
|
140
|
+
data-testid={testID ? `${testID}-username` : undefined}
|
|
141
|
+
hidden
|
|
142
|
+
name="username"
|
|
143
|
+
readOnly
|
|
144
|
+
tabIndex={-1}
|
|
145
|
+
type="text"
|
|
146
|
+
value={username}
|
|
147
|
+
/>
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
// Nested in another FormContainer, the outer <form> already groups these fields.
|
|
151
|
+
if (isInsideForm) {
|
|
152
|
+
return (
|
|
153
|
+
<Box {...boxProps} testID={testID}>
|
|
154
|
+
{usernameInput}
|
|
155
|
+
{children}
|
|
156
|
+
</Box>
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return (
|
|
161
|
+
<form
|
|
162
|
+
aria-label={accessibilityLabel}
|
|
163
|
+
data-testid={testID}
|
|
164
|
+
noValidate
|
|
165
|
+
onSubmit={handleFormSubmit}
|
|
166
|
+
ref={(element: WebFormElement | null): void => {
|
|
167
|
+
formElementRef.current = element;
|
|
168
|
+
}}
|
|
169
|
+
style={{display: "contents"}}
|
|
170
|
+
>
|
|
171
|
+
{usernameInput}
|
|
172
|
+
<InsideFormContext.Provider value>
|
|
173
|
+
<Box {...boxProps}>{children}</Box>
|
|
174
|
+
</InsideFormContext.Provider>
|
|
175
|
+
</form>
|
|
176
|
+
);
|
|
177
|
+
};
|