Skip to content

Commit 8139075

Browse files
committed
fix: add optional chaining for field properties in various field components
1 parent 295a877 commit 8139075

File tree

6 files changed

+18
-16
lines changed

6 files changed

+18
-16
lines changed

packages/fields/src/widgets/CurrencyField.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ function formatCurrency(value: number, currency: string = 'USD'): string {
1818

1919
export function CurrencyField({ value, onChange, field, readonly, errorMessage, ...props }: FieldWidgetProps<number>) {
2020
const currencyField = field as any;
21-
const currency = currencyField.currency || 'USD';
22-
const precision = currencyField.precision ?? 2;
21+
const currency = currencyField?.currency || 'USD';
22+
const precision = currencyField?.precision ?? 2;
2323

2424
if (readonly) {
2525
if (value == null) return <span className="text-sm">-</span>;
@@ -51,7 +51,7 @@ export function CurrencyField({ value, onChange, field, readonly, errorMessage,
5151
onChange(val as any);
5252
}}
5353
onBlur={handleBlur}
54-
placeholder={field.placeholder || '0.00'}
54+
placeholder={field?.placeholder || '0.00'}
5555
disabled={readonly}
5656
className={`pl-8 ${props.className || ''}`}
5757
step={Math.pow(10, -precision).toFixed(precision)}

packages/fields/src/widgets/NumberField.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export function NumberField({ value, onChange, field, readonly, ...props }: Fiel
88
return <span className="text-sm">{value ?? '-'}</span>;
99
}
1010

11-
const precision = (field as unknown as NumberFieldMetadata).precision;
11+
const precision = (field as unknown as NumberFieldMetadata)?.precision;
1212

1313
return (
1414
<Input
@@ -18,7 +18,7 @@ export function NumberField({ value, onChange, field, readonly, ...props }: Fiel
1818
const val = e.target.value;
1919
onChange(val === '' ? (null as any) : Number(val));
2020
}}
21-
placeholder={field.placeholder}
21+
placeholder={field?.placeholder}
2222
disabled={readonly}
2323
step={precision ? Math.pow(10, -precision) : 'any'}
2424
className={props.className}

packages/fields/src/widgets/PercentField.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { FieldWidgetProps } from './types';
44

55
export function PercentField({ value, onChange, field, readonly, errorMessage, ...props }: FieldWidgetProps<number>) {
66
const percentField = field as any;
7-
const precision = percentField.precision ?? 2;
7+
const precision = percentField?.precision ?? 2;
88

99
if (readonly) {
1010
if (value == null) return <span className="text-sm">-</span>;
@@ -34,7 +34,7 @@ export function PercentField({ value, onChange, field, readonly, errorMessage, .
3434
type="number"
3535
value={displayValue}
3636
onChange={handleChange}
37-
placeholder={field.placeholder || '0'}
37+
placeholder={field?.placeholder || '0'}
3838
disabled={readonly}
3939
className={`pr-8 ${props.className || ''}`}
4040
step={Math.pow(10, -precision).toFixed(precision)}

packages/fields/src/widgets/RichTextField.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ export function RichTextField({ value, onChange, field, readonly, errorMessage,
1919
}
2020

2121
const richField = field as any;
22-
const rows = richField.rows || 8;
23-
const format = richField.format || 'markdown'; // 'markdown' or 'html'
22+
const rows = richField?.rows || 8;
23+
const format = richField?.format || 'markdown'; // 'markdown' or 'html'
2424

2525
return (
2626
<div className="space-y-2">
@@ -31,7 +31,7 @@ export function RichTextField({ value, onChange, field, readonly, errorMessage,
3131
<Textarea
3232
value={value || ''}
3333
onChange={(e) => onChange(e.target.value)}
34-
placeholder={field.placeholder || 'Enter text...'}
34+
placeholder={field?.placeholder || 'Enter text...'}
3535
disabled={readonly}
3636
rows={rows}
3737
className={`font-mono text-sm ${props.className || ''}`}

packages/fields/src/widgets/TextAreaField.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function TextAreaField({ value, onChange, field, readonly, errorMessage,
1111
);
1212
}
1313

14-
const textareaField = field as any;
14+
const textareaField = (field || (props as any).schema) as any;
1515
const rows = textareaField?.rows || 4;
1616
const maxLength = textareaField?.max_length;
1717

@@ -20,7 +20,7 @@ export function TextAreaField({ value, onChange, field, readonly, errorMessage,
2020
<Textarea
2121
value={value || ''}
2222
onChange={(e) => onChange(e.target.value)}
23-
placeholder={field.placeholder}
23+
placeholder={textareaField?.placeholder}
2424
disabled={readonly}
2525
rows={rows}
2626
maxLength={maxLength}

packages/fields/src/widgets/TextField.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@ import { TextareaFieldMetadata } from '@object-ui/types';
44
import { FieldWidgetProps } from './types';
55

66
export function TextField({ value, onChange, field, readonly, ...props }: FieldWidgetProps<string>) {
7+
const fieldData = field || (props as any).schema;
8+
79
if (readonly) {
810
return <span className="text-sm">{value || '-'}</span>;
911
}
1012

1113
// Cast for rows property
12-
const rows = (field as unknown as TextareaFieldMetadata)?.rows;
14+
const rows = (fieldData as unknown as TextareaFieldMetadata)?.rows;
1315

1416
if (rows && rows > 1) {
1517
return (
1618
<Textarea
1719
value={value || ''}
1820
onChange={(e) => onChange(e.target.value)}
19-
placeholder={field?.placeholder}
21+
placeholder={fieldData?.placeholder}
2022
disabled={readonly}
2123
className={props.className}
2224
/>
@@ -25,10 +27,10 @@ export function TextField({ value, onChange, field, readonly, ...props }: FieldW
2527

2628
return (
2729
<Input
28-
type={field?.type === 'password' ? 'password' : 'text'}
30+
type={fieldData?.type === 'password' ? 'password' : 'text'}
2931
value={value || ''}
3032
onChange={(e) => onChange(e.target.value)}
31-
placeholder={field.placeholder}
33+
placeholder={fieldData?.placeholder}
3234
disabled={readonly}
3335
className={props.className}
3436
{...props}

0 commit comments

Comments
 (0)