Skip to content

Commit 8d68253

Browse files
authored
fix: allow custom Link element on table row (#728)
1 parent c2dc462 commit 8d68253

File tree

6 files changed

+35
-13
lines changed

6 files changed

+35
-13
lines changed

src/components/ModalWrapper.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
// styling here mostly just for the overlay and animations
22
import * as Dialog from '@radix-ui/react-dialog'
33
import { FocusScope } from '@radix-ui/react-focus-scope'
4-
import { type ComponentPropsWithRef, type ReactNode } from 'react'
4+
import {
5+
CSSProperties,
6+
type ComponentPropsWithRef,
7+
type ReactNode,
8+
} from 'react'
59
import { VisuallyHidden } from 'react-aria'
6-
import styled, { type CSSObject, useTheme } from 'styled-components'
10+
import styled, { useTheme } from 'styled-components'
711

812
import WrapWithIf from './WrapWithIf'
913

@@ -12,7 +16,7 @@ const ANIMATION_SPEED = '150ms'
1216
export type ModalWrapperProps = {
1317
open: boolean
1418
onOpenChange?: (open: boolean) => void
15-
overlayStyles?: CSSObject
19+
overlayStyles?: CSSProperties
1620
children?: ReactNode
1721
} & ComponentPropsWithRef<typeof Dialog.Content>
1822

src/components/table/Table.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ function Table({
401401
<TdGhostLink
402402
key={cell.id}
403403
width={tableWidth}
404-
href={getRowLink?.(tableRow)}
404+
link={getRowLink?.(tableRow)}
405405
/>
406406
) : (
407407
<Td

src/components/table/Td.tsx

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import styled from 'styled-components'
22

3+
import { ReactElement } from 'react'
34
import {
45
tableFillLevelToBorder,
56
tableFillLevelToHeaderBorder,
@@ -108,13 +109,26 @@ export const TdBasic = styled.td({
108109
overflow: 'hidden',
109110
})
110111

111-
export function TdGhostLink({ width, href }: { width: number; href: string }) {
112+
export function TdGhostLink({
113+
width,
114+
link,
115+
}: {
116+
width: number
117+
link: Nullable<string | ReactElement>
118+
}) {
112119
return (
113-
<td style={{ position: 'relative', width: 0 }}>
114-
<a
115-
style={{ width, position: 'absolute', inset: '0 0 0 auto', zIndex: 0 }}
116-
href={href}
117-
/>
118-
</td>
120+
<TdGhostLinkSC style={{ ['--ghost-link-width']: `${width}px` }}>
121+
{typeof link === 'string' ? <a href={link} /> : link}
122+
</TdGhostLinkSC>
119123
)
120124
}
125+
const TdGhostLinkSC = styled.td(() => ({
126+
position: 'relative',
127+
width: 0,
128+
'& > *': {
129+
position: 'absolute',
130+
inset: '0 0 0 auto',
131+
zIndex: 0,
132+
width: 'var(--ghost-link-width)',
133+
},
134+
}))

src/components/table/tableUtils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
import type { VirtualItem } from '@tanstack/react-virtual'
99
import { useVirtualizer } from '@tanstack/react-virtual'
1010
import {
11+
ReactElement,
1112
type CSSProperties,
1213
type MouseEvent,
1314
type RefObject,
@@ -46,7 +47,7 @@ export type TableBaseProps = {
4647
>
4748
reactTableOptions?: Partial<Omit<TableOptions<any>, 'data' | 'columns'>>
4849
onRowClick?: (e: MouseEvent<HTMLTableRowElement>, row: Row<any>) => void
49-
getRowLink?: (row: Row<unknown>) => Nullable<string>
50+
getRowLink?: (row: Row<unknown>) => Nullable<string | ReactElement>
5051
emptyStateProps?: EmptyStateProps
5152
hasNextPage?: boolean
5253
fetchNextPage?: () => void

src/stories/Table.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ function SelectableTemplate(args: any) {
285285
<Table
286286
{...args}
287287
onRowClick={(_, row) => {
288-
setSelectedId(row.original.id)
288+
setSelectedId(row.original.id?.toString() ?? '')
289289
}}
290290
reactTableOptions={{
291291
state: { rowSelection: { [selectedId]: true } },

src/types/styled.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ declare module 'react' {
88
interface Attributes {
99
css?: CSSProp | undefined
1010
}
11+
interface CSSProperties {
12+
[key: `--${string}`]: Nullable<string | number>
13+
}
1114
}
1215

1316
type StyledTheme = typeof styledTheme

0 commit comments

Comments
 (0)