|
9 | 9 | import { ComponentRegistry } from '@object-ui/core'; |
10 | 10 | import type { TextareaSchema } from '@object-ui/types'; |
11 | 11 | import { Textarea, Label } from '../../ui'; |
| 12 | +import { cn } from '../../lib/utils'; |
| 13 | +import React from 'react'; |
12 | 14 |
|
13 | | -ComponentRegistry.register('textarea', |
14 | | - ({ schema, className, ...props }: { schema: TextareaSchema; className?: string; [key: string]: any }) => { |
15 | | - // Extract designer-related props |
16 | | - const { |
17 | | - 'data-obj-id': dataObjId, |
18 | | - 'data-obj-type': dataObjType, |
19 | | - style, |
20 | | - ...inputProps |
21 | | - } = props; |
| 15 | +const TextareaRenderer = ({ schema, className, onChange, value, ...props }: { schema: TextareaSchema; className?: string; onChange?: (val: any) => void; value?: any; [key: string]: any }) => { |
| 16 | + // Handle change for both raw inputs and form-bound inputs |
| 17 | + const handleChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => { |
| 18 | + if (onChange) { |
| 19 | + onChange(e.target.value); |
| 20 | + } |
| 21 | + }; |
| 22 | + |
| 23 | + // Extract designer-related props |
| 24 | + const { |
| 25 | + 'data-obj-id': dataObjId, |
| 26 | + 'data-obj-type': dataObjType, |
| 27 | + style, |
| 28 | + ...inputProps |
| 29 | + } = props; |
| 30 | + |
| 31 | + return ( |
| 32 | + <div |
| 33 | + className={cn("grid w-full gap-1.5", schema.wrapperClass)} |
| 34 | + data-obj-id={dataObjId} |
| 35 | + data-obj-type={dataObjType} |
| 36 | + style={style} |
| 37 | + > |
| 38 | + {schema.label && <Label htmlFor={schema.id} className={cn(schema.required && "text-destructive after:content-['*'] after:ml-0.5")}>{schema.label}</Label>} |
| 39 | + <Textarea |
| 40 | + id={schema.id} |
| 41 | + name={schema.name} |
| 42 | + placeholder={schema.placeholder} |
| 43 | + className={className} |
| 44 | + disabled={schema.disabled} |
| 45 | + readOnly={schema.readOnly} |
| 46 | + required={schema.required} |
| 47 | + rows={schema.rows} |
| 48 | + value={value ?? schema.value ?? ''} |
| 49 | + defaultValue={value === undefined ? schema.defaultValue : undefined} |
| 50 | + onChange={handleChange} |
| 51 | + {...inputProps} |
| 52 | + /> |
| 53 | + </div> |
| 54 | + ); |
| 55 | +}; |
22 | 56 |
|
23 | | - return ( |
24 | | - <div |
25 | | - className={`grid w-full gap-1.5 ${schema.wrapperClass || ''}`} |
26 | | - data-obj-id={dataObjId} |
27 | | - data-obj-type={dataObjType} |
28 | | - style={style} |
29 | | - > |
30 | | - {schema.label && <Label htmlFor={schema.id}>{schema.label}</Label>} |
31 | | - <Textarea |
32 | | - id={schema.id} |
33 | | - placeholder={schema.placeholder} |
34 | | - className={className} |
35 | | - {...inputProps} |
36 | | - /> |
37 | | - </div> |
38 | | - ); |
39 | | - }, |
| 57 | +ComponentRegistry.register('textarea', TextareaRenderer, |
40 | 58 | { |
41 | 59 | namespace: 'ui', |
42 | 60 | label: 'Textarea', |
43 | 61 | inputs: [ |
44 | 62 | { name: 'label', type: 'string', label: 'Label' }, |
45 | 63 | { name: 'placeholder', type: 'string', label: 'Placeholder' }, |
| 64 | + { name: 'rows', type: 'number', label: 'Rows' }, |
| 65 | + { name: 'required', type: 'boolean', label: 'Required' }, |
| 66 | + { name: 'disabled', type: 'boolean', label: 'Disabled' }, |
46 | 67 | { name: 'id', type: 'string', label: 'ID', required: true } |
47 | 68 | ], |
48 | 69 | defaultProps: { |
49 | 70 | label: 'Textarea label', |
50 | 71 | placeholder: 'Enter text here...', |
51 | | - id: 'textarea-field' // Will be made unique by designer's ensureNodeIds |
| 72 | + rows: 3, |
| 73 | + id: 'textarea-field' |
52 | 74 | } |
53 | 75 | } |
54 | 76 | ); |
0 commit comments