-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
84966d3
commit c1e3548
Showing
11 changed files
with
247 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import React from 'react'; | ||
import { useForm } from '../hooks/useForm'; | ||
import { JsonSchema } from '../interfaces/models'; | ||
import { TextInput } from './form/TextInput'; | ||
import { SelectInput } from './form/SelectInput'; | ||
|
||
interface DynamicFormBuilderProps { | ||
jsonSchema: JsonSchema; | ||
} | ||
|
||
const DynamicFormBuilder: React.FC<DynamicFormBuilderProps> = ({ jsonSchema }) => { | ||
const { formData, handleChange } = useForm(jsonSchema); | ||
|
||
const handleSubmit = (event: React.FormEvent) => { | ||
event.preventDefault(); | ||
console.log('Submitted Data:', formData); | ||
// Add submission logic here | ||
}; | ||
|
||
return ( | ||
<form onSubmit={handleSubmit} className='grid grid-cols-1 gap-9 sm:grid-cols-2 p-6.5'> | ||
{Object.entries(jsonSchema.properties).map(([key, property]) => ( | ||
<div key={key}> | ||
<label htmlFor={key}>{property.title}</label> | ||
{property.enum ? ( | ||
<SelectInput | ||
id={key} | ||
value={formData[key]} | ||
options={property.enum} | ||
onChange={(value) => handleChange(key, value)} | ||
/> | ||
) : ( | ||
<TextInput | ||
id={key} | ||
value={formData[key]} | ||
placeholder={property.description || ''} | ||
onChange={(value) => handleChange(key, value)} | ||
/> | ||
)} | ||
</div> | ||
))} | ||
<button | ||
type='submit' | ||
className='rounded-md px-3.5 py-2.5 text-sm bg-airt-primary text-airt-font-base hover:bg-opacity-85 shadow-sm focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600' | ||
> | ||
Submit | ||
</button> | ||
</form> | ||
); | ||
}; | ||
|
||
export default DynamicFormBuilder; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// components/ModelFormContainer.tsx | ||
import React from 'react'; | ||
import { ModelSchema } from '../interfaces/models'; | ||
|
||
interface ModelFormContainerProps { | ||
modelsSchema: ModelSchema; | ||
onModelChange: (selectedModel: string) => void; | ||
} | ||
|
||
const ModelFormContainer: React.FC<ModelFormContainerProps> = ({ modelsSchema, onModelChange }) => { | ||
return ( | ||
<div className='flex flex-col gap-9'> | ||
<div className='flex flex-col gap-5.5 p-6.5'> | ||
<label className='mb-3 block text-black dark:text-white'>Select Model</label> | ||
<div className='relative z-20 bg-white dark:bg-form-input'> | ||
<select | ||
className='w-full rounded border bg-transparent py-3 px-12 outline-none focus:border-primary' | ||
onChange={(e) => onModelChange(e.target.value)} | ||
> | ||
{modelsSchema.schemas.map((schema) => ( | ||
<option key={schema.name} value={schema.name}> | ||
{schema.name} | ||
</option> | ||
))} | ||
</select> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ModelFormContainer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import React from 'react'; | ||
|
||
interface SelectInputProps { | ||
id: string; | ||
value: string; | ||
options: string[]; | ||
onChange: (value: string) => void; | ||
} | ||
|
||
export const SelectInput: React.FC<SelectInputProps> = ({ id, value, options, onChange }) => ( | ||
<select value={value} onChange={(e) => onChange(e.target.value)} className='my-2 p-2 border rounded' id={id}> | ||
{options.map((option) => ( | ||
<option key={option} value={option}> | ||
{option} | ||
</option> | ||
))} | ||
</select> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import React from 'react'; | ||
|
||
interface TextInputProps { | ||
id: string; | ||
value: string; | ||
placeholder: string; | ||
onChange: (value: string) => void; | ||
} | ||
|
||
export const TextInput: React.FC<TextInputProps> = ({ id, value, placeholder, onChange }) => ( | ||
<input | ||
type='text' | ||
value={value} | ||
placeholder={placeholder} | ||
onChange={(e) => onChange(e.target.value)} | ||
className='my-2 p-2 border rounded' | ||
id={id} | ||
/> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// hooks/useForm.ts | ||
import { useState, useEffect } from 'react'; | ||
import { JsonSchema } from '../interfaces/models'; | ||
|
||
export const useForm = (jsonSchema: JsonSchema) => { | ||
const [formData, setFormData] = useState<{ [key: string]: any }>({}); | ||
|
||
useEffect(() => { | ||
const initialValues: { [key: string]: any } = {}; | ||
Object.keys(jsonSchema.properties).forEach((key) => { | ||
// Ensure every field starts with a defined value, use an empty string as a fallback | ||
initialValues[key] = jsonSchema.properties[key].default ?? ''; | ||
}); | ||
setFormData(initialValues); | ||
}, [jsonSchema]); | ||
|
||
const handleChange = (key: string, value: any) => { | ||
setFormData((prev) => ({ | ||
...prev, | ||
[key]: value, | ||
})); | ||
}; | ||
|
||
return { | ||
formData, | ||
handleChange, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// interfaces/models.ts | ||
export interface JsonSchemaProperty { | ||
default?: string; | ||
description: string; | ||
enum?: string[]; | ||
title: string; | ||
type: string; | ||
const?: string; | ||
format?: string; | ||
maxLength?: number; | ||
minLength?: number; | ||
} | ||
|
||
export interface JsonSchema { | ||
properties: Record<string, JsonSchemaProperty>; | ||
required: string[]; | ||
title: string; | ||
type: string; | ||
} | ||
|
||
export interface Schema { | ||
name: string; | ||
json_schema: JsonSchema; | ||
} | ||
|
||
export interface ModelSchema { | ||
schemas: Schema[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// services/modelService.ts | ||
import { getModels as apiGetModels } from 'wasp/client/operations'; | ||
|
||
export const getModels = async () => { | ||
try { | ||
const response = await apiGetModels(); | ||
return response; | ||
} catch (error) { | ||
console.error('Failed to fetch models:', error); | ||
throw error; | ||
} | ||
}; |