import type { HTMLAttributes } from "react";
import { useFieldValue, useTranslate } from "ra-core";
import type { FieldProps } from "@/lib/field.type";
/**
* Displays a text value from a record field inside a span element.
*
* This is the default field component used in DataTable columns and RecordField components.
*
* @see {@link https://marmelab.com/shadcn-admin-kit/docs/textfield/ TextField documentation}
*
* @example
* import { List, DataTable, TextField } from '@/components/admin';
*
* export const UserList = () => (
*
*
*
*
*
*
*
*
* );
*/
export const TextField = <
RecordType extends Record = Record,
>({
defaultValue,
source,
record,
empty,
...rest
}: TextFieldProps) => {
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 (
{typeof value !== "string" ? value.toString() : value}
);
};
export interface TextFieldProps<
RecordType extends Record = Record,
> extends FieldProps,
HTMLAttributes {}