badge.tsx 1.2 KB

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