select.tsx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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: 14,
  27. outline: 'none',
  28. }}
  29. >
  30. {placeholder && <option value="">{placeholder}</option>}
  31. {options.map((opt) => (
  32. <option key={opt.value} value={opt.value}>{opt.label}</option>
  33. ))}
  34. </select>
  35. </div>
  36. );
  37. }