Skip to content

Commit

Permalink
fix: ensure 'as' cannot be used
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasdigby committed Aug 22, 2023
1 parent 83f214b commit b1c77a7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 26 deletions.
12 changes: 4 additions & 8 deletions lib/src/components/alert-dialog/alert-context/AlertDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,15 @@ export const Alert: React.FC<AlertDialogContentProps> = ({
{cancelActionText && (
<Button
appearance="outline"
as={AlertDialog.Cancel}
onClick={() => onAction(false)}
size="sm"
asChild
>
{cancelActionText}
<AlertDialog.Cancel>{cancelActionText}</AlertDialog.Cancel>
</Button>
)}
<Button
as={AlertDialog.Action}
onClick={() => onAction(true)}
size="sm"
>
{confirmActionText}
<Button onClick={() => onAction(true)} size="sm" asChild>
<AlertDialog.Action>{confirmActionText}</AlertDialog.Action>
</Button>
</Stack>
</AlertDialog.Content>
Expand Down
16 changes: 12 additions & 4 deletions lib/src/components/button/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Slot } from '@radix-ui/react-slot'
import type { VariantProps } from '@stitches/react'
import { darken, opacify } from 'color2k'
import * as React from 'react'
Expand All @@ -7,7 +8,6 @@ import { Icon, StyledIcon } from '~/components/icon'
import { Loader } from '~/components/loader'
import { styled, theme } from '~/stitches'
import { Override } from '~/utilities'
import { Slot } from '@radix-ui/react-slot'

const getButtonOutlineVariant = (
base: string,
Expand Down Expand Up @@ -196,17 +196,18 @@ export const StyledButton = styled('button', {
type ButtonProps = Override<
React.ComponentProps<typeof StyledButton>,
VariantProps<typeof StyledButton> & {
as?: React.ComponentType | React.ElementType
as?: never
asChild?: boolean
children: React.ReactNode
isLoading?: boolean
asChild?: boolean
}
>

export const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
(
{
children,
as,
asChild,
isLoading,
onClick,
Expand All @@ -227,7 +228,14 @@ export const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
}

if (asChild) {
return <StyledButton {...props} as={Slot} />
return (
<StyledButton
isLoading={isLoading || false}
onClick={!isLoading ? onClick : undefined}
{...props}
as={Slot}
/>
)
}

return (
Expand Down
22 changes: 12 additions & 10 deletions lib/src/components/file-input/FileInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@ export const FileInput: React.FC<FileInputProps> = ({
}

return (
<Button as="label" {...rest}>
<Icon is={Upload} />
{children}
<input
type="file"
onChange={handleFileSelect}
accept={accept}
multiple={multiple}
hidden
/>
<Button {...rest} asChild>
<label>
<Icon is={Upload} />
{children}
<input
type="file"
onChange={handleFileSelect}
accept={accept}
multiple={multiple}
hidden
/>
</label>
</Button>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ exports[`FileInput component renders 1`] = `
<div>
<label
class="c-fkUJsw c-fkUJsw-dXsdwv-size-md c-fkUJsw-gsycxF-cv"
type="button"
>
<svg
aria-hidden="true"
Expand Down
9 changes: 6 additions & 3 deletions lib/src/components/link/Link.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Slot } from '@radix-ui/react-slot'
import * as React from 'react'

import { styled } from '~/stitches'
Expand All @@ -7,7 +8,6 @@ import { StyledHeading } from '../heading/Heading'
import { StyledLi } from '../list/List'
import { StyledMarkdownEmphasis } from '../markdown-content/components'
import { StyledText, textVariants } from '../text/Text'
import { Slot } from '@radix-ui/react-slot'

export const StyledLink = styled('a', {
bg: 'unset',
Expand Down Expand Up @@ -37,8 +37,11 @@ export const StyledLink = styled('a', {

export const Link = React.forwardRef<
HTMLAnchorElement,
React.ComponentProps<typeof StyledLink> & { asChild?: boolean }
>(({ asChild, size = 'md', href, ...rest }, ref) => {
React.ComponentProps<typeof StyledLink> & {
asChild?: boolean
as?: never
}
>(({ as, asChild, size = 'md', href, ...rest }, ref) => {
if (asChild) {
return <StyledLink {...rest} as={Slot} noCapsize size={size} ref={ref} />
}
Expand Down

0 comments on commit b1c77a7

Please sign in to comment.