Skip to content

Commit 9566734

Browse files
Copilothotlong
andcommitted
refactor: move badge color constants outside components per code review feedback
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 6106b12 commit 9566734

1 file changed

Lines changed: 37 additions & 34 deletions

File tree

packages/fields/src/index.tsx

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,11 @@ export function DateCellRenderer({ value, field }: CellRendererProps): React.Rea
268268

269269
// Determine if date is overdue (in the past)
270270
const date = typeof value === 'string' ? new Date(value) : value;
271-
const isOverdue = date instanceof Date && !isNaN(date.getTime()) && date < new Date(new Date().setHours(0, 0, 0, 0));
272-
const isoString = date instanceof Date && !isNaN(date.getTime()) ? date.toISOString() : String(value);
271+
const isValidDate = date instanceof Date && !isNaN(date.getTime());
272+
const startOfToday = new Date();
273+
startOfToday.setHours(0, 0, 0, 0);
274+
const isOverdue = isValidDate && date < startOfToday;
275+
const isoString = isValidDate ? date.toISOString() : String(value);
273276

274277
return (
275278
<span
@@ -308,6 +311,36 @@ export function DateTimeCellRenderer({ value }: CellRendererProps): React.ReactE
308311
);
309312
}
310313

314+
// Priority semantic color mapping (auto-detect from value text)
315+
const PRIORITY_COLOR_MAP: Record<string, string> = {
316+
critical: 'red',
317+
urgent: 'red',
318+
high: 'orange',
319+
medium: 'yellow',
320+
normal: 'blue',
321+
low: 'gray',
322+
none: 'gray',
323+
};
324+
325+
// Color to Tailwind class mapping for custom Badge styling
326+
const BADGE_COLOR_MAP: Record<string, string> = {
327+
gray: 'bg-gray-100 text-gray-800 border-gray-300',
328+
red: 'bg-red-100 text-red-800 border-red-300',
329+
orange: 'bg-orange-100 text-orange-800 border-orange-300',
330+
yellow: 'bg-yellow-100 text-yellow-800 border-yellow-300',
331+
green: 'bg-green-100 text-green-800 border-green-300',
332+
blue: 'bg-blue-100 text-blue-800 border-blue-300',
333+
indigo: 'bg-indigo-100 text-indigo-800 border-indigo-300',
334+
purple: 'bg-purple-100 text-purple-800 border-purple-300',
335+
pink: 'bg-pink-100 text-pink-800 border-pink-300',
336+
};
337+
338+
function getBadgeColorClasses(color?: string, val?: string): string {
339+
const resolvedColor = color
340+
|| (val ? PRIORITY_COLOR_MAP[String(val).toLowerCase()] : undefined);
341+
return BADGE_COLOR_MAP[resolvedColor || ''] || 'bg-muted text-muted-foreground border-border';
342+
}
343+
311344
/**
312345
* Select field cell renderer (with badges)
313346
*/
@@ -317,44 +350,14 @@ export function SelectCellRenderer({ value, field }: CellRendererProps): React.R
317350

318351
if (!value) return <span>-</span>;
319352

320-
// Priority semantic color mapping (auto-detect from value text)
321-
const priorityColorMap: Record<string, string> = {
322-
critical: 'red',
323-
urgent: 'red',
324-
high: 'orange',
325-
medium: 'yellow',
326-
normal: 'blue',
327-
low: 'gray',
328-
none: 'gray',
329-
};
330-
331-
// Color to Tailwind class mapping for custom Badge styling
332-
const getColorClasses = (color?: string, val?: string) => {
333-
const colorMap: Record<string, string> = {
334-
gray: 'bg-gray-100 text-gray-800 border-gray-300',
335-
red: 'bg-red-100 text-red-800 border-red-300',
336-
orange: 'bg-orange-100 text-orange-800 border-orange-300',
337-
yellow: 'bg-yellow-100 text-yellow-800 border-yellow-300',
338-
green: 'bg-green-100 text-green-800 border-green-300',
339-
blue: 'bg-blue-100 text-blue-800 border-blue-300',
340-
indigo: 'bg-indigo-100 text-indigo-800 border-indigo-300',
341-
purple: 'bg-purple-100 text-purple-800 border-purple-300',
342-
pink: 'bg-pink-100 text-pink-800 border-pink-300',
343-
};
344-
// Use explicit color, then try priority semantic color, then default muted
345-
const resolvedColor = color
346-
|| (val ? priorityColorMap[String(val).toLowerCase()] : undefined);
347-
return colorMap[resolvedColor || ''] || 'bg-muted text-muted-foreground border-border';
348-
};
349-
350353
// Handle multiple values
351354
if (Array.isArray(value)) {
352355
return (
353356
<div className="flex flex-wrap gap-1">
354357
{value.map((val, idx) => {
355358
const option = options.find(opt => opt.value === val);
356359
const label = option?.label || val;
357-
const colorClasses = getColorClasses(option?.color, val);
360+
const colorClasses = getBadgeColorClasses(option?.color, val);
358361

359362
return (
360363
<Badge
@@ -373,7 +376,7 @@ export function SelectCellRenderer({ value, field }: CellRendererProps): React.R
373376
// Handle single value
374377
const option = options.find(opt => opt.value === value);
375378
const label = option?.label || value;
376-
const colorClasses = getColorClasses(option?.color, value);
379+
const colorClasses = getBadgeColorClasses(option?.color, value);
377380

378381
return (
379382
<Badge

0 commit comments

Comments
 (0)