-
Notifications
You must be signed in to change notification settings - Fork 2
Add FormRenderer component for @objectstack/spec schema-driven forms #179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e45692d
Initial plan
Copilot 49c7374
feat: Add FormRenderer component with basic field support
Copilot 6978390
test: Add comprehensive tests and documentation for FormRenderer
Copilot d71affb
refactor: Address code review feedback - remove unused imports and do…
Copilot 90a9a56
perf: Optimize component performance - move constants outside render …
Copilot 4a0e82d
chore: Remove unused dependencies and update documentation
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,231 @@ | ||||||||||
| /** | ||||||||||
| * ObjectUI | ||||||||||
| * Copyright (c) 2024-present ObjectStack Inc. | ||||||||||
| * | ||||||||||
| * This source code is licensed under the MIT license found in the | ||||||||||
| * LICENSE file in the root directory of this source tree. | ||||||||||
| */ | ||||||||||
|
|
||||||||||
| import React from 'react'; | ||||||||||
| import { UseFormReturn } from 'react-hook-form'; | ||||||||||
| import type { FormField } from '@objectstack/spec/ui'; | ||||||||||
|
|
||||||||||
| export interface FieldFactoryProps { | ||||||||||
| /** | ||||||||||
| * Field configuration from FormFieldSchema | ||||||||||
| */ | ||||||||||
| field: FormField; | ||||||||||
| /** | ||||||||||
| * React Hook Form methods | ||||||||||
| */ | ||||||||||
| methods: UseFormReturn<any>; | ||||||||||
| /** | ||||||||||
| * Whether the field is disabled | ||||||||||
| */ | ||||||||||
| disabled?: boolean; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * FieldFactory component that renders different input types based on | ||||||||||
| * the widget property or field type | ||||||||||
| */ | ||||||||||
| export const FieldFactory: React.FC<FieldFactoryProps> = ({ | ||||||||||
| field, | ||||||||||
| methods, | ||||||||||
| disabled = false, | ||||||||||
| }) => { | ||||||||||
| const { register, formState: { errors } } = methods; | ||||||||||
|
|
||||||||||
| // Determine the widget type | ||||||||||
| const widgetType = field.widget || 'text'; | ||||||||||
| const fieldName = field.field; | ||||||||||
| const error = errors[fieldName]; | ||||||||||
|
|
||||||||||
| // Handle conditional visibility | ||||||||||
| // Note: visibleOn expression evaluation is not yet implemented | ||||||||||
| // Fields are always visible unless explicitly hidden | ||||||||||
| // Skip if explicitly hidden | ||||||||||
| if (field.hidden) { | ||||||||||
| return null; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Common field wrapper | ||||||||||
| const renderField = (input: React.ReactNode) => ( | ||||||||||
| <div className="space-y-2"> | ||||||||||
| {field.label && ( | ||||||||||
| <label htmlFor={fieldName} className="block text-sm font-medium text-gray-700"> | ||||||||||
| {field.label} | ||||||||||
| {field.required && <span className="text-red-500 ml-1">*</span>} | ||||||||||
| </label> | ||||||||||
| )} | ||||||||||
| {input} | ||||||||||
| {field.helpText && ( | ||||||||||
| <p className="text-sm text-gray-500">{field.helpText}</p> | ||||||||||
| )} | ||||||||||
| {error && ( | ||||||||||
| <p className="text-sm text-red-600">{error.message as string}</p> | ||||||||||
| )} | ||||||||||
| </div> | ||||||||||
| ); | ||||||||||
|
|
||||||||||
| // Render based on widget type | ||||||||||
| switch (widgetType.toLowerCase()) { | ||||||||||
| case 'text': | ||||||||||
| case 'string': | ||||||||||
| case 'email': | ||||||||||
| case 'password': | ||||||||||
| case 'url': | ||||||||||
| case 'tel': | ||||||||||
| return renderField( | ||||||||||
| <input | ||||||||||
| id={fieldName} | ||||||||||
| type={widgetType === 'string' ? 'text' : widgetType} | ||||||||||
| placeholder={field.placeholder} | ||||||||||
| disabled={disabled} | ||||||||||
| className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 disabled:bg-gray-100 disabled:cursor-not-allowed" | ||||||||||
| {...register(fieldName, { | ||||||||||
| required: field.required ? `${field.label || fieldName} is required` : false, | ||||||||||
| })} | ||||||||||
| /> | ||||||||||
| ); | ||||||||||
|
|
||||||||||
| case 'number': | ||||||||||
| case 'integer': | ||||||||||
| case 'float': | ||||||||||
| return renderField( | ||||||||||
| <input | ||||||||||
| id={fieldName} | ||||||||||
| type="number" | ||||||||||
| placeholder={field.placeholder} | ||||||||||
| disabled={disabled} | ||||||||||
| step={widgetType === 'integer' ? '1' : 'any'} | ||||||||||
| className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 disabled:bg-gray-100 disabled:cursor-not-allowed" | ||||||||||
| {...register(fieldName, { | ||||||||||
| required: field.required ? `${field.label || fieldName} is required` : false, | ||||||||||
| valueAsNumber: true, | ||||||||||
| })} | ||||||||||
| /> | ||||||||||
| ); | ||||||||||
|
|
||||||||||
| case 'checkbox': | ||||||||||
| case 'boolean': | ||||||||||
| return ( | ||||||||||
| <div className="flex items-start space-x-2"> | ||||||||||
| <input | ||||||||||
| id={fieldName} | ||||||||||
| type="checkbox" | ||||||||||
| disabled={disabled} | ||||||||||
| className="mt-1 h-4 w-4 text-blue-600 border-gray-300 rounded focus:ring-2 focus:ring-blue-500 disabled:opacity-50" | ||||||||||
| {...register(fieldName)} | ||||||||||
|
||||||||||
| {...register(fieldName)} | |
| {...register(fieldName, { | |
| required: field.required ? `${field.label || fieldName} is required` : false, | |
| })} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
widgetTypeis compared in a lowercased switch, but the rendered<input type=...>uses the originalwidgetTypevalue. If the schema provides a mixed/upper-cased widget (e.g. "Email"), the switch will match but thetypeattribute will become invalid. Use the normalized (lowercased) widget value when setting the inputtype.