fix(ui): default Button type to button (#1252)
Co-authored-by: Isaac Sanchez <isanchez-hawkins@arize.com>
This commit is contained in:
committed by
GitHub
co-authored by
Isaac Sanchez
parent
1e1a7e02b7
commit
58c6a22036
@@ -0,0 +1,23 @@
|
|||||||
|
import { describe, expect, test } from "bun:test"
|
||||||
|
|
||||||
|
import { Button } from "./button"
|
||||||
|
|
||||||
|
describe("Button", () => {
|
||||||
|
test("defaults native buttons to type button", () => {
|
||||||
|
const element = Button({ children: "Cancel" })
|
||||||
|
|
||||||
|
expect(element.props.type).toBe("button")
|
||||||
|
})
|
||||||
|
|
||||||
|
test("preserves explicit native button types", () => {
|
||||||
|
const element = Button({ children: "Save", type: "submit" })
|
||||||
|
|
||||||
|
expect(element.props.type).toBe("submit")
|
||||||
|
})
|
||||||
|
|
||||||
|
test("does not default asChild buttons", () => {
|
||||||
|
const element = Button({ asChild: true, children: "Link" })
|
||||||
|
|
||||||
|
expect(element.props.type).toBe(undefined)
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -93,17 +93,22 @@ function Button({
|
|||||||
variant,
|
variant,
|
||||||
size,
|
size,
|
||||||
asChild = false,
|
asChild = false,
|
||||||
|
type,
|
||||||
...props
|
...props
|
||||||
}: React.ComponentProps<"button"> &
|
}: React.ComponentProps<"button"> &
|
||||||
VariantProps<typeof buttonVariants> & {
|
VariantProps<typeof buttonVariants> & {
|
||||||
asChild?: boolean
|
asChild?: boolean
|
||||||
}) {
|
}) {
|
||||||
const Comp = asChild ? Slot : "button"
|
const Comp = asChild ? Slot : "button"
|
||||||
|
const typeProps = asChild
|
||||||
|
? (type === undefined ? {} : { type })
|
||||||
|
: { type: type ?? "button" }
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Comp
|
<Comp
|
||||||
data-slot="button"
|
data-slot="button"
|
||||||
className={cn(buttonVariants({ variant, size, className }))}
|
className={cn(buttonVariants({ variant, size, className }))}
|
||||||
|
{...typeProps}
|
||||||
{...props}
|
{...props}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user