Skip to content

Commit efc116d

Browse files
Copilothotlong
andcommitted
Changes before error encountered
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 86d13d7 commit efc116d

3 files changed

Lines changed: 75 additions & 14 deletions

File tree

docs/plugins/plugin-object.mdx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ The Object plugin provides seamless integration with ObjectQL backends through s
4545
schema={{
4646
type: "object-form",
4747
objectName: "user",
48-
fields: [
48+
mode: "create",
49+
customFields: [
4950
{
5051
name: "name",
5152
label: "Full Name",
@@ -75,7 +76,7 @@ The Object plugin provides seamless integration with ObjectQL backends through s
7576
defaultValue: "Active"
7677
}
7778
],
78-
submitLabel: "Create User"
79+
submitText: "Create User"
7980
}}
8081
title="ObjectForm Component"
8182
description="Smart form with validation and schema integration"

packages/plugin-object/src/ObjectForm.tsx

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ export interface ObjectFormProps {
2626

2727
/**
2828
* ObjectQL data source
29+
* Optional when using inline field definitions (customFields or fields array with field objects)
2930
*/
30-
dataSource: ObjectQLDataSource;
31+
dataSource?: ObjectQLDataSource;
3132

3233
/**
3334
* Additional CSS class
@@ -63,10 +64,24 @@ export const ObjectForm: React.FC<ObjectFormProps> = ({
6364
const [loading, setLoading] = useState(true);
6465
const [error, setError] = useState<Error | null>(null);
6566

66-
// Fetch object schema from ObjectQL
67+
// Check if using inline fields (fields defined as objects, not just names)
68+
const hasInlineFields = schema.customFields && schema.customFields.length > 0;
69+
70+
// Initialize with inline data if provided
71+
useEffect(() => {
72+
if (hasInlineFields) {
73+
setInitialData(schema.initialData || schema.initialValues || {});
74+
setLoading(false);
75+
}
76+
}, [hasInlineFields, schema.initialData, schema.initialValues]);
77+
78+
// Fetch object schema from ObjectQL (skip if using inline fields)
6779
useEffect(() => {
6880
const fetchObjectSchema = async () => {
6981
try {
82+
if (!dataSource) {
83+
throw new Error('DataSource is required when using ObjectQL schema fetching (inline fields not provided)');
84+
}
7085
const schemaData = await dataSource.getObjectSchema(schema.objectName);
7186
setObjectSchema(schemaData);
7287
} catch (err) {
@@ -75,16 +90,34 @@ export const ObjectForm: React.FC<ObjectFormProps> = ({
7590
}
7691
};
7792

78-
if (schema.objectName && dataSource) {
93+
// Skip fetching if we have inline fields
94+
if (hasInlineFields) {
95+
// Use a minimal schema for inline fields
96+
setObjectSchema({
97+
name: schema.objectName,
98+
fields: {} as Record<string, any>,
99+
});
100+
} else if (schema.objectName && dataSource) {
79101
fetchObjectSchema();
80102
}
81-
}, [schema.objectName, dataSource]);
103+
}, [schema.objectName, dataSource, hasInlineFields]);
82104

83-
// Fetch initial data for edit/view modes
105+
// Fetch initial data for edit/view modes (skip if using inline data)
84106
useEffect(() => {
85107
const fetchInitialData = async () => {
86108
if (!schema.recordId || schema.mode === 'create') {
87-
setInitialData(schema.initialValues || {});
109+
setInitialData(schema.initialData || schema.initialValues || {});
110+
return;
111+
}
112+
113+
// Skip fetching if using inline data
114+
if (hasInlineFields) {
115+
return;
116+
}
117+
118+
if (!dataSource) {
119+
setError(new Error('DataSource is required for fetching record data (inline data not provided)'));
120+
setLoading(false);
88121
return;
89122
}
90123

@@ -100,13 +133,20 @@ export const ObjectForm: React.FC<ObjectFormProps> = ({
100133
}
101134
};
102135

103-
if (objectSchema) {
136+
if (objectSchema && !hasInlineFields) {
104137
fetchInitialData();
105138
}
106-
}, [schema.objectName, schema.recordId, schema.mode, schema.initialValues, dataSource, objectSchema]);
139+
}, [schema.objectName, schema.recordId, schema.mode, schema.initialValues, schema.initialData, dataSource, objectSchema, hasInlineFields]);
107140

108-
// Generate form fields from object schema
141+
// Generate form fields from object schema or inline fields
109142
useEffect(() => {
143+
// For inline fields, use them directly
144+
if (hasInlineFields && schema.customFields) {
145+
setFormFields(schema.customFields);
146+
setLoading(false);
147+
return;
148+
}
149+
110150
if (!objectSchema) return;
111151

112152
const generatedFields: FormField[] = [];
@@ -207,10 +247,22 @@ export const ObjectForm: React.FC<ObjectFormProps> = ({
207247

208248
setFormFields(generatedFields);
209249
setLoading(false);
210-
}, [objectSchema, schema.fields, schema.customFields, schema.readOnly, schema.mode]);
250+
}, [objectSchema, schema.fields, schema.customFields, schema.readOnly, schema.mode, hasInlineFields]);
211251

212252
// Handle form submission
213253
const handleSubmit = useCallback(async (formData: any) => {
254+
// For inline fields without a dataSource, just call the success callback
255+
if (hasInlineFields && !dataSource) {
256+
if (schema.onSuccess) {
257+
await schema.onSuccess(formData);
258+
}
259+
return formData;
260+
}
261+
262+
if (!dataSource) {
263+
throw new Error('DataSource is required for form submission (inline mode not configured)');
264+
}
265+
214266
try {
215267
let result;
216268

@@ -238,7 +290,7 @@ export const ObjectForm: React.FC<ObjectFormProps> = ({
238290

239291
throw err;
240292
}
241-
}, [schema, dataSource]);
293+
}, [schema, dataSource, hasInlineFields]);
242294

243295
// Handle form cancellation
244296
const handleCancel = useCallback(() => {

packages/types/src/objectql.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,18 @@ export interface ObjectFormSchema extends BaseSchema {
203203

204204
/**
205205
* Custom field configurations
206-
* Overrides auto-generated fields for specific fields
206+
* Overrides auto-generated fields for specific fields.
207+
* When used with inline field definitions (without dataSource), this becomes the primary field source.
207208
*/
208209
customFields?: FormField[];
209210

211+
/**
212+
* Inline initial data for demo/static forms
213+
* When provided along with customFields (or inline field definitions), the form can work without a data source.
214+
* Useful for documentation examples and prototyping.
215+
*/
216+
initialData?: Record<string, any>;
217+
210218
/**
211219
* Field groups for organized layout
212220
*/

0 commit comments

Comments
 (0)