Skip to content

Commit

Permalink
feat: ✨ support objects in form field values and schema
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Dec 30, 2024
1 parent 70dbcf2 commit 8df0bac
Showing 1 changed file with 29 additions and 25 deletions.
54 changes: 29 additions & 25 deletions packages/web/src/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { marked } from "marked"

interface FormFieldProps {
field: JSONSchemaSimpleType
value: string | boolean | number
onChange: (value: string | boolean | number) => void
value: string | boolean | number | object
onChange: (value: string | boolean | number | object) => void
}

const FormField: React.FC<FormFieldProps> = ({ field, value, onChange }) => {
Expand Down Expand Up @@ -72,7 +72,11 @@ interface JsonSchemaFormProps {
}

export const JsonSchemaForm: React.FC<JsonSchemaFormProps> = ({ schema }) => {
const [formData, setFormData] = useState<FormData>({})
const properties = (schema.properties || {}) as Record<
string,
JSONSchemaSimpleType
>
const [formData, setFormData] = useState<PromptParameters>({})
const [markdown, setMarkdown] = useState<string>("")

const handleSubmit = (e: React.FormEvent) => {
Expand All @@ -85,7 +89,7 @@ export const JsonSchemaForm: React.FC<JsonSchemaFormProps> = ({ schema }) => {

const handleFieldChange = (
fieldName: string,
value: string | boolean | number
value: string | boolean | number | object
) => {
setFormData((prev) => ({
...prev,
Expand All @@ -96,33 +100,33 @@ export const JsonSchemaForm: React.FC<JsonSchemaFormProps> = ({ schema }) => {
return (
<div className="container">
<form onSubmit={handleSubmit}>
{Object.entries(schema.properties || {}).map(
([fieldName, field]) => (
<div key={fieldName} className="field-container">
<label>{fieldName}</label>
<FormField
field={field}
value={formData[fieldName] || ""}
onChange={(value) =>
handleFieldChange(fieldName, value)
}
/>
{field.description && (
<small className="description">
{field.description}
</small>
)}
</div>
)
)}
<VSCodeButton type="submit">Generate Markdown</VSCodeButton>
{Object.entries(properties).map(([fieldName, field]) => (
<div key={fieldName} className="field-container">
<label>{fieldName}</label>
<FormField
field={field}
value={formData[fieldName]}
onChange={(value) =>
handleFieldChange(fieldName, value)
}
/>
{field?.description && (
<small className="description">
{field.description}
</small>
)}
</div>
))}
<VscodeButton type="submit">Generate Markdown</VscodeButton>
</form>

{markdown && (
<div className="markdown-output">
<h2>Output:</h2>
<div
dangerouslySetInnerHTML={{ __html: marked(markdown) }}
dangerouslySetInnerHTML={{
__html: marked(markdown) as any,
}}
className="markdown-content"
/>
</div>
Expand Down

0 comments on commit 8df0bac

Please sign in to comment.