Skip to content

Commit

Permalink
allow passing ref to RecordItem
Browse files Browse the repository at this point in the history
  • Loading branch information
talentlessguy committed Mar 16, 2024
1 parent 68056fb commit c9da69f
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 82 deletions.
178 changes: 97 additions & 81 deletions components/src/components/atoms/RecordItem/RecordItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,90 +161,106 @@ const TrailingIcon = styled.svg<{ $rotate?: boolean }>(
`,
)

export const RecordItem = ({
as: asProp = 'button',
link,
size = 'small',
inline = false,
icon,
keyLabel,
keySublabel,
value,
children,
...props
}: Props) => {
const { copy, copied } = useCopied()

const generatedProps =
asProp === 'a'
? ({
href: link,
rel: 'nofollow noreferrer',
target: '_blank',
...props,
} as NativeElementProps & NativeAnchorProps)
: ({
onClick: () => {
copy(value)
},
...props,
} as NativeElementProps & NativeButtonProps)

const hasPrefix = !!icon || !!keyLabel
const hasLabels = !!keyLabel || !!keySublabel

const KeyLabel =
typeof keyLabel === 'string' ? (
<PrefixLabel
$inline={inline}
color="grey"
ellipsis={!inline}
fontVariant={size === 'large' ? 'bodyBold' : 'smallBold'}
>
{keyLabel}
</PrefixLabel>
) : (
keyLabel
)
export const RecordItem = React.forwardRef<
HTMLAnchorElement | HTMLButtonElement,
Props
>(
(
{
as: asProp = 'button',
link,
size = 'small',
inline = false,
icon,
keyLabel,
keySublabel,
value,
children,
...props
},
ref,
) => {
const { copy, copied } = useCopied()

const generatedProps =
asProp === 'a'
? ({
href: link,
rel: 'nofollow noreferrer',
target: '_blank',
...props,
} as NativeElementProps & NativeAnchorProps)
: ({
onClick: () => {
copy(value)
},
...props,
} as NativeElementProps & NativeButtonProps)

const hasPrefix = !!icon || !!keyLabel
const hasLabels = !!keyLabel || !!keySublabel

const KeySublabel =
typeof keySublabel === 'string' ? (
<PrefixLabel
const KeyLabel =
typeof keyLabel === 'string' ? (
<PrefixLabel
$inline={inline}
color="grey"
ellipsis={!inline}
fontVariant={size === 'large' ? 'bodyBold' : 'smallBold'}
>
{keyLabel}
</PrefixLabel>
) : (
keyLabel
)

const KeySublabel =
typeof keySublabel === 'string' ? (
<PrefixLabel
$inline={inline}
color="grey"
ellipsis={!inline}
fontVariant={size === 'large' ? 'smallBold' : 'extraSmallBold'}
>
{keySublabel}
</PrefixLabel>
) : (
keySublabel
)
const PostfixProps = link
? { $rotate: true, as: UpArrowSVG }
: copied
? { as: CheckSVG }
: { as: CopySVG }

return (
<Container
$inline={inline}
color="grey"
ellipsis={!inline}
fontVariant={size === 'large' ? 'smallBold' : 'extraSmallBold'}
as={asProp}
{...generatedProps}
ref={ref as React.ForwardedRef<HTMLAnchorElement>}
>
{keySublabel}
</PrefixLabel>
) : (
keySublabel
{hasPrefix && (
<PrefixContainer $inline={inline} $size={size}>
{icon && <PrefixIcon>{icon}</PrefixIcon>}
{hasLabels && (
<PrefixLabelsContainer $inline={inline}>
{KeyLabel}
{KeySublabel}
</PrefixLabelsContainer>
)}
</PrefixContainer>
)}
<Label
$inline={inline}
fontVariant={size === 'large' ? 'body' : 'small'}
>
{children}
</Label>
<TrailingIcon {...PostfixProps} />
</Container>
)
const PostfixProps = link
? { $rotate: true, as: UpArrowSVG }
: copied
? { as: CheckSVG }
: { as: CopySVG }

return (
<Container $inline={inline} as={asProp} {...generatedProps}>
{hasPrefix && (
<PrefixContainer $inline={inline} $size={size}>
{icon && <PrefixIcon>{icon}</PrefixIcon>}
{hasLabels && (
<PrefixLabelsContainer $inline={inline}>
{KeyLabel}
{KeySublabel}
</PrefixLabelsContainer>
)}
</PrefixContainer>
)}
<Label $inline={inline} fontVariant={size === 'large' ? 'body' : 'small'}>
{children}
</Label>
<TrailingIcon {...PostfixProps} />
</Container>
)
}
},
)

RecordItem.displayName = 'RecordItem'
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,6 @@
"overrides": {
"ts-morph": "17.0.1"
}
}
},
"packageManager": "[email protected]"
}

0 comments on commit c9da69f

Please sign in to comment.