select.tsx 1.3 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. export function Select({ element }: ComponentRenderProps) {
  6. const { label, valuePath, options, placeholder } = element.props as {
  7. label: string;
  8. valuePath: string;
  9. options: Array<{ value: string; label: string }>;
  10. placeholder?: string | null;
  11. };
  12. const { data, set } = useData();
  13. const value = getByPath(data, valuePath) as string | undefined;
  14. return (
  15. <div style={{ display: "flex", flexDirection: "column", gap: 4 }}>
  16. <label style={{ fontSize: 14, fontWeight: 500 }}>{label}</label>
  17. <select
  18. value={value ?? ""}
  19. onChange={(e) => set(valuePath, e.target.value)}
  20. style={{
  21. padding: "8px 12px",
  22. borderRadius: "var(--radius)",
  23. border: "1px solid var(--border)",
  24. background: "var(--card)",
  25. color: "var(--foreground)",
  26. fontSize: 16,
  27. outline: "none",
  28. }}
  29. >
  30. {placeholder && <option value="">{placeholder}</option>}
  31. {options.map((opt) => (
  32. <option key={opt.value} value={opt.value}>
  33. {opt.label}
  34. </option>
  35. ))}
  36. </select>
  37. </div>
  38. );
  39. }