Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Fix `TextConfirmModal` does not reset its state (#50406)

Gildas Garcia7ab2a0d84f7a
diff --git a/apps/studio/components/interfaces/Account/TOTPFactors/RegenerateRecoveryCodesModal.test.tsx b/apps/studio/components/interfaces/Account/TOTPFactors/RegenerateRecoveryCodesModal.test.tsx
index aab69bb6209..bc12a9f8181 100644
--- a/apps/studio/components/interfaces/Account/TOTPFactors/RegenerateRecoveryCodesModal.test.tsx
+++ b/apps/studio/components/interfaces/Account/TOTPFactors/RegenerateRecoveryCodesModal.test.tsx
@@ -103,7 +103,13 @@ describe('RegenerateRecoveryCodesModal', () => {
// Retry
fireEvent.click(await screen.findByRole('button', { name: 'Regenerate my recovery codes' }))
-
+ // Confirm regeneration
+ await userEvent.type(
+ await within(await screen.findByRole('dialog')).findByRole('textbox', {
+ name: /Type REGENERATE to confirm./,
+ }),
+ 'REGENERATE'
+ )
fireEvent.click(
await within(await screen.findByRole('dialog')).findByRole('button', { name: 'Regenerate' })
)
diff --git a/packages/ui-patterns/src/Dialogs/TextConfirmModal.test.tsx b/packages/ui-patterns/src/Dialogs/TextConfirmModal.test.tsx
new file mode 100644
index 00000000000..54ab669893d
--- /dev/null
+++ b/packages/ui-patterns/src/Dialogs/TextConfirmModal.test.tsx
@@ -0,0 +1,108 @@
+import { fireEvent, render, screen, waitFor } from '@testing-library/react'
+import userEvent from '@testing-library/user-event'
+import { describe, expect, it, vi } from 'vitest'
+
+import TextConfirmModal from './TextConfirmModal'
+
+describe('TextConfirmModal', () => {
+ it('requires users to enter the confirmation text', async () => {
+ const onConfirm = vi.fn()
+ const onCancel = vi.fn()
+ render(
+ <TextConfirmModal
+ visible
+ loading={false}
+ title="Confirm do stuff"
+ confirmPlaceholder="CONFIRM"
+ confirmString="CONFIRM"
+ onConfirm={onConfirm}
+ onCancel={onCancel}
+ />
+ )
+
+ fireEvent.click(await screen.findByRole('button', { name: 'Submit' }))
+ await screen.findByText('Value entered does not match')
+ expect(onConfirm).not.toHaveBeenCalled()
+ })
+ it('calls the confirm callback when users submit the form successfully', async () => {
+ const onConfirm = vi.fn()
+ const onCancel = vi.fn()
+ render(
+ <TextConfirmModal
+ visible
+ loading={false}
+ title="Confirm do stuff"
+ confirmPlaceholder="CONFIRM"
+ confirmString="CONFIRM"
+ onConfirm={onConfirm}
+ onCancel={onCancel}
+ />
+ )
+
+ await userEvent.type(await screen.findByRole('textbox'), 'CONFIRM')
+ fireEvent.click(await screen.findByRole('button', { name: 'Submit' }))
+ await waitFor(() => expect(onConfirm).toHaveBeenCalled())
+ })
+ it('calls the confirm callback when users cancel', async () => {
+ const onConfirm = vi.fn()
+ const onCancel = vi.fn()
+ render(
+ <TextConfirmModal
+ visible
+ loading={false}
+ title="Confirm do stuff"
+ confirmPlaceholder="CONFIRM"
+ confirmString="CONFIRM"
+ onConfirm={onConfirm}
+ onCancel={onCancel}
+ />
+ )
+
+ fireEvent.click(await screen.findByRole('button', { name: 'Close' }))
+ await waitFor(() => expect(onCancel).toHaveBeenCalled())
+ })
+ it('resets its state when closed', async () => {
+ const onConfirm = vi.fn()
+ const onCancel = vi.fn()
+ const { rerender } = render(
+ <TextConfirmModal
+ visible
+ loading={false}
+ title="Confirm do stuff"
+ confirmPlaceholder="CONFIRM"
+ confirmString="CONFIRM"
+ onConfirm={onConfirm}
+ onCancel={onCancel}
+ />
+ )
+
+ await userEvent.type(await screen.findByRole('textbox'), 'CONFIRM')
+
+ // Close the dialog
+ rerender(
+ <TextConfirmModal
+ visible={false}
+ loading={false}
+ title="Confirm do stuff"
+ confirmPlaceholder="CONFIRM"
+ confirmString="CONFIRM"
+ onConfirm={onConfirm}
+ onCancel={onCancel}
+ />
+ )
+
+ // Reopen the dialog
+ rerender(
+ <TextConfirmModal
+ visible
+ loading={false}
+ title="Confirm do stuff"
+ confirmPlaceholder="CONFIRM"
+ confirmString="CONFIRM"
+ onConfirm={onConfirm}
+ onCancel={onCancel}
+ />
+ )
+ expect(await screen.findByRole('textbox')).toHaveValue('')
+ })
+})
diff --git a/packages/ui-patterns/src/Dialogs/TextConfirmModal.tsx b/packages/ui-patterns/src/Dialogs/TextConfirmModal.tsx
index 250021f5538..2243999342e 100644
--- a/packages/ui-patterns/src/Dialogs/TextConfirmModal.tsx
+++ b/packages/ui-patterns/src/Dialogs/TextConfirmModal.tsx
@@ -109,8 +109,6 @@ export const TextConfirmModal = forwardRef<
},
})
- const isFormValid = form.formState.isValid
-
// 2. Define a submit handler.
function onSubmit(_values: z.infer<typeof formSchema>) {
// Do something with the form values.
@@ -122,6 +120,10 @@ export const TextConfirmModal = forwardRef<
if (confirmString) form.reset()
}, [confirmString])
+ useEffect(() => {
+ if (visible) form.reset()
+ }, [visible])
+
useEffect(() => {
if (!showCopied) return
const timer = setTimeout(() => setShowCopied(false), 2000)
@@ -137,6 +139,7 @@ export const TextConfirmModal = forwardRef<
{...props}
onOpenChange={() => {
if (visible) {
+ form.reset()
onCancel()
}
}}
@@ -183,18 +186,27 @@ export const TextConfirmModal = forwardRef<
<FormLabel {...label} enableSelection={!enableCopy}>
Type{' '}
{enableCopy ? (
- <Button
- className="h-[23px] px-1.5 py-0 border-muted text-sm whitespace-pre break-all"
- iconRight={
- showCopied ? <Check strokeWidth={2} className="text-brand" /> : <Copy />
- }
- onClick={() => {
- setShowCopied(true)
- copyToClipboard(confirmString)
- }}
- >
- {confirmString}
- </Button>
+ <>
+ <Button
+ className="h-[23px] px-1.5 py-0 border-muted text-sm whitespace-pre break-all"
+ iconRight={
+ showCopied ? (
+ <Check strokeWidth={2} className="text-brand" />
+ ) : (
+ <Copy />
+ )
+ }
+ onClick={() => {
+ setShowCopied(true)
+ copyToClipboard(confirmString)
+ }}
+ >
+ {confirmString}
+ </Button>
+ <span className="sr-only" role="status">
+ {showCopied ? `${confirmString} copied to clipboard` : ''}
+ </span>
+ </>
) : (
<span className="text-foreground break-all whitespace-pre">
{confirmString}
@@ -233,7 +245,7 @@ export const TextConfirmModal = forwardRef<
}
type="submit"
loading={loading}
- disabled={!isFormValid || loading}
+ disabled={loading}
className="truncate"
>
{confirmLabel}