Skip to content

Commit e023222

Browse files
Copilothuangyiirene
andcommitted
Changes before error encountered
Co-authored-by: huangyiirene <7665279+huangyiirene@users.noreply.github.com>
1 parent a7882a4 commit e023222

1 file changed

Lines changed: 37 additions & 11 deletions

File tree

  • packages/components/src/renderers/form

packages/components/src/renderers/form/form.tsx

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@ import { ComponentRegistry } from '@object-ui/core';
22
import { useForm } from 'react-hook-form';
33
import { Form, FormField, FormItem, FormLabel, FormControl, FormMessage, FormDescription } from '@/ui/form';
44
import { Button } from '@/ui/button';
5+
import { Input } from '@/ui/input';
6+
import { Textarea } from '@/ui/textarea';
7+
import { Checkbox } from '@/ui/checkbox';
8+
import {
9+
Select,
10+
SelectTrigger,
11+
SelectValue,
12+
SelectContent,
13+
SelectItem
14+
} from '@/ui/select';
515
import { renderChildren } from '../../lib/utils';
616
import { Alert, AlertDescription } from '@/ui/alert';
717
import { AlertCircle, Loader2 } from 'lucide-react';
@@ -176,8 +186,9 @@ ComponentRegistry.register('form',
176186
{renderFieldComponent(type, {
177187
...fieldProps,
178188
...formField,
189+
inputType: fieldProps.inputType,
190+
options: fieldProps.options,
179191
disabled: disabled || fieldDisabled || isSubmitting,
180-
schema: { type, ...fieldProps },
181192
})}
182193
</FormControl>
183194
{description && (
@@ -294,32 +305,47 @@ ComponentRegistry.register('form',
294305

295306
// Helper function to render field components
296307
function renderFieldComponent(type: string, props: any) {
297-
const { schema, ...fieldProps } = props;
308+
const { schema, inputType, options, ...fieldProps } = props;
298309

299310
switch (type) {
300311
case 'input':
301-
return <input className="flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-base shadow-sm transition-colors file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 md:text-sm" {...fieldProps} />;
312+
return <Input type={inputType || 'text'} {...fieldProps} />;
302313

303314
case 'textarea':
304-
return <textarea className="flex min-h-[60px] w-full rounded-md border border-input bg-transparent px-3 py-2 text-base shadow-sm placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 md:text-sm" {...fieldProps} />;
315+
return <Textarea {...fieldProps} />;
305316

306317
case 'checkbox':
318+
// For checkbox, we need to handle the value differently
319+
const { value, onChange, ...checkboxProps } = fieldProps;
307320
return (
308321
<div className="flex items-center space-x-2">
309-
<input type="checkbox" className="h-4 w-4 rounded border-gray-300" {...fieldProps} />
322+
<Checkbox
323+
checked={value}
324+
onCheckedChange={onChange}
325+
{...checkboxProps}
326+
/>
310327
</div>
311328
);
312329

313330
case 'select':
331+
// For select with react-hook-form, we need to handle the onChange
332+
const { value: selectValue, onChange: selectOnChange, ...selectProps } = fieldProps;
314333
return (
315-
<select className="flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-base shadow-sm transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 md:text-sm" {...fieldProps}>
316-
{schema.options?.map((opt: any) => (
317-
<option key={opt.value} value={opt.value}>{opt.label}</option>
318-
))}
319-
</select>
334+
<Select value={selectValue} onValueChange={selectOnChange} {...selectProps}>
335+
<SelectTrigger>
336+
<SelectValue placeholder="Select an option" />
337+
</SelectTrigger>
338+
<SelectContent>
339+
{options?.map((opt: any) => (
340+
<SelectItem key={opt.value} value={opt.value}>
341+
{opt.label}
342+
</SelectItem>
343+
))}
344+
</SelectContent>
345+
</Select>
320346
);
321347

322348
default:
323-
return <input className="flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-base shadow-sm transition-colors file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 md:text-sm" {...fieldProps} />;
349+
return <Input type={inputType || 'text'} {...fieldProps} />;
324350
}
325351
}

0 commit comments

Comments
 (0)