| 1 |
import { h, type VNode } from 'vue' |
| 2 |
import IvyLink from '@/views/_components/link/IvyLink.vue' |
| 3 |
import type { CellRenderProps } from '@/views/_components/table/IvyCellRendererProps' |
| 4 |
|
| 5 |
export interface LinkCellOptions<Row> { |
| 6 |
text: (row: Row) => string | number |
| 7 |
href?: string | ((row: Row) => string) |
| 8 |
onClick?: (row: Row, e: Event) => void | Promise<void> |
| 9 |
class?: string | ((row: Row) => string | undefined) |
| 10 |
} |
| 11 |
|
| 12 |
export function linkCellRenderer<Row extends Record<string, unknown>>( |
| 13 |
options: LinkCellOptions<Row>, |
| 14 |
) { |
| 15 |
return (props: CellRenderProps): VNode | null => { |
| 16 |
const row = props.rowData as unknown as Row |
| 17 |
if (!row) return null |
| 18 |
|
| 19 |
const href = |
| 20 |
typeof options.href === 'function' |
| 21 |
? options.href(row) |
| 22 |
: (options.href ?? 'javascript:void(0)') |
| 23 |
|
| 24 |
const className = typeof options.class === 'function' ? options.class(row) : options.class |
| 25 |
|
| 26 |
return h( |
| 27 |
IvyLink, |
| 28 |
{ |
| 29 |
size: 's', |
| 30 |
href, |
| 31 |
class: className, |
| 32 |
onClick: options.onClick |
| 33 |
? (e: Event) => { |
| 34 |
if (href === '#' || href === 'javascript:void(0)') e.preventDefault() |
| 35 |
return options.onClick!(row, e) |
| 36 |
} |
| 37 |
: undefined, |
| 38 |
}, |
| 39 |
() => [String(options.text(row) ?? '')], |
| 40 |
) |
| 41 |
} |
| 42 |
} |
| 43 |
|