badge.tsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use client';
  2. import { type ComponentRenderProps } from '@json-render/react';
  3. import { useData } from '@json-render/react';
  4. import { getByPath } from '@json-render/core';
  5. function useResolvedValue<T>(value: T | { path: string } | null | undefined): T | undefined {
  6. const { data } = useData();
  7. if (value === null || value === undefined) return undefined;
  8. if (typeof value === 'object' && 'path' in value) {
  9. return getByPath(data, value.path) as T | undefined;
  10. }
  11. return value as T;
  12. }
  13. export function Badge({ element }: ComponentRenderProps) {
  14. const { text, variant } = element.props as { text: string | { path: string }; variant?: string | null };
  15. const resolvedText = useResolvedValue(text);
  16. const colors: Record<string, string> = {
  17. default: 'var(--foreground)',
  18. success: '#22c55e',
  19. warning: '#eab308',
  20. error: '#ef4444',
  21. info: 'var(--muted)',
  22. };
  23. return (
  24. <span
  25. style={{
  26. display: 'inline-block',
  27. padding: '2px 8px',
  28. borderRadius: 12,
  29. fontSize: 12,
  30. fontWeight: 500,
  31. background: 'var(--border)',
  32. color: colors[variant || 'default'],
  33. }}
  34. >
  35. {resolvedText}
  36. </span>
  37. );
  38. }