import { useFieldValue, useTranslate } from "ra-core";
import type { AnchorHTMLAttributes } from "react";
import * as React from "react";
import { cn } from "@/lib/utils";
import { genericMemo } from "@/lib/genericMemo";
import { FieldProps } from "@/lib/field.type";
/**
* Displays a URL as a clickable hyperlink.
*
* Click events are prevented from bubbling up, making it safe to use in DataTable rows with rowClick.
*
* @see {@link https://marmelab.com/shadcn-admin-kit/docs/urlfield/ UrlField documentation}
*
* @example
* import { List, DataTable, UrlField } from '@/components/admin';
*
* const WebsiteList = () => (
*
*
*
*
*
*
*
*
* );
*/
const UrlFieldImpl = <
//eslint-disable-next-line @typescript-eslint/no-explicit-any
RecordType extends Record = Record,
>(
inProps: UrlFieldProps,
) => {
const {
empty,
className,
defaultValue,
source,
record,
resource: _,
...rest
} = inProps;
const value = useFieldValue({ defaultValue, source, record });
const translate = useTranslate();
if (value == null) {
if (!empty) {
return null;
}
return (
{typeof empty === "string" ? translate(empty, { _: empty }) : empty}
);
}
return (
{value}
);
};
UrlFieldImpl.displayName = "UrlFieldImpl";
export const UrlField = genericMemo(UrlFieldImpl);
export interface UrlFieldProps<
//eslint-disable-next-line @typescript-eslint/no-explicit-any
RecordType extends Record = Record,
> extends FieldProps,
AnchorHTMLAttributes {}
// useful to prevent click bubbling in a DataTable with rowClick
const stopPropagation = (e: React.MouseEvent) =>
e.stopPropagation();