Skip to content

Commit 59ba8dc

Browse files
Copilothotlong
andcommitted
refactor: extract resolveColumnLabel helper for I18nLabel type safety in ObjectGrid
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 0c3236b commit 59ba8dc

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

packages/plugin-grid/src/ObjectGrid.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import React, { useEffect, useState, useCallback, useMemo } from 'react';
2525
import type { ObjectGridSchema, DataSource, ListColumn, ViewData } from '@object-ui/types';
26+
import type { I18nLabel } from '@objectstack/spec/ui';
2627
import { SchemaRenderer, useDataScope, useNavigationOverlay, useAction, useObjectTranslation, useSafeFieldLabel } from '@object-ui/react';
2728
import { getCellRenderer, formatCurrency, formatCompactCurrency, formatDate, formatPercent, humanizeLabel } from '@object-ui/fields';
2829
import {
@@ -90,6 +91,13 @@ function useGridTranslation() {
9091
}
9192
}
9293

94+
/** Resolve an I18nLabel (string | {key, defaultValue}) to a plain string. */
95+
function resolveColumnLabel(label: string | I18nLabel | undefined): string | undefined {
96+
if (label == null) return undefined;
97+
if (typeof label === 'string') return label;
98+
return label.defaultValue || label.key;
99+
}
100+
93101
export interface ObjectGridProps {
94102
schema: ObjectGridSchema;
95103
dataSource?: DataSource;
@@ -619,8 +627,7 @@ export const ObjectGrid: React.FC<ObjectGridProps> = ({
619627
return (cols as ListColumn[])
620628
.filter((col) => col?.field && typeof col.field === 'string' && !col.hidden)
621629
.map((col, colIndex) => {
622-
const rawLabel = col.label;
623-
const rawHeader = (typeof rawLabel === 'string' ? rawLabel : typeof rawLabel === 'object' && rawLabel ? (rawLabel as any).defaultValue || (rawLabel as any).key : null) || col.field.charAt(0).toUpperCase() + col.field.slice(1).replace(/_/g, ' ');
630+
const rawHeader = resolveColumnLabel(col.label) || col.field.charAt(0).toUpperCase() + col.field.slice(1).replace(/_/g, ' ');
624631
const header = schema.objectName ? resolveFieldLabel(schema.objectName, col.field, rawHeader) : rawHeader;
625632

626633
// Build custom cell renderer based on column configuration

0 commit comments

Comments
 (0)