Skip to content

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

Remove imperative Button ref, add test for ref forwarding + not crashing when wrapped by Link

Brandon Scott55f9a89294e3
diff --git a/packages/ui/src/components/Button/Button.test.tsx b/packages/ui/src/components/Button/Button.test.tsx
index a05fb6f2c02..93e88e8650c 100644
--- a/packages/ui/src/components/Button/Button.test.tsx
+++ b/packages/ui/src/components/Button/Button.test.tsx
@@ -2,6 +2,7 @@ import React from 'react'
import { render, screen, fireEvent } from '@testing-library/react'
import { Button, ButtonSize, ButtonType } from './Button'
import defaultTheme from '../../lib/theme/defaultTheme'
+import Link from 'next/link'
const SIZES: ButtonSize[] = ['tiny', 'small', 'medium', 'large', 'xlarge']
const TYPES: ButtonType[] = [
@@ -88,4 +89,24 @@ describe('#Button', () => {
expect(screen.queryByRole('button')).toHaveClass(expected)
})
+
+ it("shouldn't crash when wrapped with next/link", () => {
+ expect(() =>
+ render(
+ <Link href="https://supabase.com">
+ <Button>Button</Button>
+ </Link>
+ )
+ ).not.toThrow()
+ })
+
+ it('should forward ref', () => {
+ const ref: React.MutableRefObject<HTMLButtonElement | null> = {
+ current: null,
+ }
+
+ render(<Button ref={ref}>Button</Button>)
+
+ expect(ref.current).toBeInstanceOf(HTMLButtonElement)
+ })
})
diff --git a/packages/ui/src/components/Button/Button.tsx b/packages/ui/src/components/Button/Button.tsx
index 05972794131..145a9333ebb 100644
--- a/packages/ui/src/components/Button/Button.tsx
+++ b/packages/ui/src/components/Button/Button.tsx
@@ -1,4 +1,4 @@
-import React, { forwardRef, useRef, useImperativeHandle } from 'react'
+import React, { forwardRef } from 'react'
import { IconContext } from './../Icon/IconContext'
import { IconLoader } from './../Icon/icons/IconLoader'
@@ -45,12 +45,7 @@ export type ButtonType =
interface CustomButtonProps extends React.HTMLAttributes<HTMLButtonElement> {}
-interface RefHandle {
- // container: () => HTMLElement | null
- button: () => HTMLButtonElement | null
-}
-
-export const Button = forwardRef<RefHandle, ButtonProps>(
+export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
(
{
block,
@@ -78,16 +73,6 @@ export const Button = forwardRef<RefHandle, ButtonProps>(
}: ButtonProps,
ref
) => {
- // button ref
- // const containerRef = useRef<HTMLElement>(null)
- const buttonRef = useRef<HTMLButtonElement>(null)
-
- useImperativeHandle(ref, () => ({
- button: () => {
- return buttonRef.current
- },
- }))
-
let __styles = styleHandler('button')
// styles
@@ -153,7 +138,7 @@ export const Button = forwardRef<RefHandle, ButtonProps>(
// <span ref={containerRef} className={containerClasses.join(' ')}>
<button
{...props}
- ref={buttonRef}
+ ref={ref}
className={classes.join(' ')}
disabled={loading || (disabled && true)}
onClick={onClick}