The column to get the header text from
The displayable header text as a string
export function getHeaderText<TData>(column: Column<TData, unknown>): string {
const header = column.columnDef.header;
if (header === undefined) return '';
if (typeof header === 'string') return header;
if (typeof header === 'function') {
// TanStack's functional header expects a `HeaderContext` we don't have here.
// Some headers read it (e.g. the price column derives its currency from
// `table.options.meta`) and throw when invoked context-free, so guard the
// call and fall back to the column-id label — the same label the
// column-visibility menu uses for functional headers.
try {
const rendered: unknown = (header as (...args: unknown[]) => unknown)();
return getRenderedHeaderText(rendered);
} catch {
return i18n(`column_${column.id}`);
}
}
return String(header);
}
Gets the displayable header text for a column. This is needed because the header text is not always a string. Sometimes it's an HTML or React element.