Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Propriedade asChild nos componentes Button e Text #17

Merged
merged 9 commits into from
Feb 10, 2023
Prev Previous commit
Next Next commit
Adicionado propriedade asChild ao componente Button
  • Loading branch information
Leandro Pedroso committed Feb 9, 2023
commit b92d963f3ed3c9a3d5b04c875270fafdf6683da2
34 changes: 34 additions & 0 deletions apps/docs/src/stories/Button/Root.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ export default {
},
type: { name: 'boolean', required: false },
},
asChild: {
control: { type: 'boolean' },
description:
'Ao passar essa propriedade com o valor true, o componente irá se transformar no componente filho. É obrigatório passar um children ao utilizar essa propriedade. <b>Verifique a tag ao inspecionar o componente na DOM</b>',
table: {
type: { summary: 'boolean' },
defaultValue: { summary: false },
},
type: { name: 'boolean', required: false },
},
onClick: {
action: 'clicked',
description: 'Define o evento de click',
Expand All @@ -65,10 +75,16 @@ export default {
ghost: false,
light: false,
disabled: false,
asChild: false,
},
} as Meta<ButtonRootProps>

export const Comum: StoryObj<ButtonRootProps> = {
argTypes: {
asChild: {
control: 'none',
},
},
args: {
children: 'Clique aqui',
},
Expand All @@ -79,6 +95,9 @@ export const ComIcone: StoryObj<ButtonRootProps> = {
children: {
control: 'none',
},
asChild: {
control: 'none',
},
},
args: {
children: [
Expand All @@ -95,6 +114,9 @@ export const ComIconeNaEsquerda: StoryObj<ButtonRootProps> = {
children: {
control: 'none',
},
asChild: {
control: 'none',
},
},
args: {
children: [
Expand All @@ -105,3 +127,15 @@ export const ComIconeNaEsquerda: StoryObj<ButtonRootProps> = {
],
},
}

export const ComoTagAnchor: StoryObj<ButtonRootProps> = {
argTypes: {
children: {
control: 'none',
},
},
args: {
asChild: true,
children: <a href="/">Link</a>,
},
}
14 changes: 12 additions & 2 deletions packages/visu/src/__tests__/Button/Root.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { fireEvent, render, screen } from '@testing-library/react'

import { Button } from '@library'
import { fireEvent, render, screen } from '@testing-library/react'

describe('Button Root tests', () => {
const buttonText = 'Clique aqui'
Expand Down Expand Up @@ -55,4 +54,15 @@ describe('Button Root tests', () => {
fireEvent.click(element)
expect(buttonClick).toHaveBeenCalledTimes(1)
})

it('Should render a different element using the "asChild" property', () => {
render(
<Button.Root data-testid="element" asChild>
<h1>Hello World</h1>
</Button.Root>
)
const element = screen.getByTestId('element')

expect(element.tagName).toBe('H1')
})
})
35 changes: 31 additions & 4 deletions packages/visu/src/library/Button/index.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,44 @@
import { ButtonHTMLAttributes, ComponentProps, HTMLAttributes } from 'react'
import { CSS } from '@/stitches.config'
import { Slot } from '@radix-ui/react-slot'
import { VariantProps } from '@stitches/react'
import { ButtonHTMLAttributes, HTMLAttributes } from 'react'

import * as Component from './style'

// ========================= ROOT =========================

export interface ButtonRootProps
extends ButtonHTMLAttributes<HTMLButtonElement>,
ComponentProps<typeof Component.Root> {
VariantProps<typeof Component.RootStyle> {
asChild?: boolean
children: React.ReactNode
css?: CSS
}

const ButtonRoot = ({ children, ...rest }: ButtonRootProps): JSX.Element => {
return <Component.Root {...rest}>{children}</Component.Root>
const ButtonRoot = ({
children,
asChild = false,
css,
ghost = false,
light = false,
size = 'md',
...rest
}: ButtonRootProps): JSX.Element => {
const ComponentRoot = asChild ? Slot : 'button'

return (
<ComponentRoot
className={Component.RootStyle({
css,
ghost,
light,
size,
})}
{...rest}
>
{children}
</ComponentRoot>
)
}

ButtonRoot.displayName = 'Button.Root'
Expand Down
4 changes: 2 additions & 2 deletions packages/visu/src/library/Button/style.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { styled } from '@/stitches.config'
import { css, styled } from '@/stitches.config'

export const Icon = styled('div', {
height: '$6',
Expand All @@ -10,7 +10,7 @@ export const Icon = styled('div', {
},
})

export const Root = styled('button', {
export const RootStyle = css({
display: 'flex',
gap: '$2half',
alignItems: 'center',
Expand Down