fix: pass useUpload hook at F0Form level instead of field-level#3752
fix: pass useUpload hook at F0Form level instead of field-level#3752sauldom102 wants to merge 1 commit intomainfrom
Conversation
✅ No New Circular DependenciesNo new circular dependencies detected. Current count: 0 |
📦 Alpha Package Version PublishedUse Use |
🔍 Visual review for your branch is published 🔍Here are the links to: |
There was a problem hiding this comment.
Pull request overview
This PR refactors F0Form’s file upload configuration by moving useUpload from per-field config to a single, form-level prop that is exposed to file fields through F0FormContext.
Changes:
- Adds
useUpload?: UseFileUploadtoF0Formprops and threads it intoF0FormContext(single schema, per-section, and definition adapters). - Removes
useUploadfrom file field config/runtime field types and removesuseUpload-based file type inference. - Updates file field rendering/tests/stories to source upload behavior from the form-level
useUpload.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/react/src/components/F0Form/useSchemaDefinition.ts | Stops mapping useUpload from schema definition config into runtime fields. |
| packages/react/src/components/F0Form/types.ts | Adds useUpload?: UseFileUpload to all F0Form prop interfaces. |
| packages/react/src/components/F0Form/fields/file/types.ts | Removes useUpload from F0FileConfig/F0FileField types. |
| packages/react/src/components/F0Form/fields/file/tests/FileFieldRenderer.test.tsx | Removes some field-level useUpload usage (but still needs further updates). |
| packages/react/src/components/F0Form/fields/file/FileFieldRenderer.tsx | Switches upload hook resolution from field.useUpload to context?.useUpload. |
| packages/react/src/components/F0Form/f0Schema.ts | Removes inferring fieldType: "file" based on useUpload. |
| packages/react/src/components/F0Form/context.ts | Adds useUpload?: UseFileUpload to F0FormContextValue. |
| packages/react/src/components/F0Form/components/F0FormSection.tsx | Accepts/forwards useUpload into section-level context provider value. |
| packages/react/src/components/F0Form/stories/F0Form.stories.tsx | Updates file-field stories to pass useUpload at the <F0Form> level. |
| packages/react/src/components/F0Form/F0Form.tsx | Threads useUpload through all <F0Form> render paths into context. |
Comments suppressed due to low confidence (1)
packages/react/src/components/F0Form/fields/file/tests/FileFieldRenderer.test.tsx:94
- This test suite still has upload scenarios that don't provide a form-level
useUploadprop, so selecting a file will never trigger an upload (and assertions like expectingsigned_*.pdfwill fail). Also, there is still at least one remaininguseUploadusage inside af0FormFieldconfig later in this file, which no longer matches the new API/type. Update these tests to passuseUpload={createMockUploadHook(...)}to<F0Form>and remove any remaining field-leveluseUploadconfig usage.
describe("FileFieldRenderer", () => {
it("renders a dropzone with default text", () => {
const schema = z.object({
file: f0FormField(z.string().optional(), {
label: "Document",
fieldType: "file",
}),
})
render(
<F0Form
name="test-file"
schema={schema}
defaultValues={{ file: "" }}
onSubmit={async () => ({ success: true })}
/>
)
| @@ -183,8 +181,6 @@ export type F0FileField = F0BaseField & { | |||
| multiple?: boolean | |||
| /** Dropzone description text */ | |||
| description?: string | |||
There was a problem hiding this comment.
useUpload was removed from F0FileField, which makes standalone file fields (e.g. F0FormField where there is no F0FormContext.Provider) unable to upload at all. With the current FileFieldRenderer implementation relying on context, consumers have no way to supply an upload hook for standalone rendering. Consider either keeping useUpload on the runtime F0FileField for non-form usage, or adding a useUpload prop to F0FormField (and/or a way to inject it into context) so file uploads still work outside <F0Form>.
| description?: string | |
| description?: string | |
| /** | |
| * Consumer-provided upload hook. | |
| * | |
| * This is primarily used for standalone file fields rendered outside of | |
| * `<F0Form>` / `F0FormContext.Provider`, where the upload hook cannot be | |
| * supplied via context. | |
| */ | |
| useUpload?: UseFileUpload |
| <FileUploadItem | ||
| key={entry.key} | ||
| entry={entry} | ||
| useUpload={entry.file ? field.useUpload : undefined} | ||
| useUpload={entry.file ? context?.useUpload : undefined} | ||
| onUploadComplete={(value) => |
There was a problem hiding this comment.
File uploads are now sourced from context?.useUpload, but when useUpload isn't provided the UI still lets users pick/drop files and then uploads never start (because FileUploadItem no-ops without an upload hook). Please add an explicit guard (e.g. hide/disable the dropzone when no useUpload is configured, or surface a clear error/throw when a new file is added) so misconfiguration doesn't fail silently.
Move
useUploadfrom file field config to F0Form levelWhat
Lifts the
useUploadhook from individual file field configurations (f0FormField) to a form-level prop on<F0Form>. All file fields in a form now share the same upload hook provided once at the form level.Why
Previously, every file field required its own
useUploadin the field config, leading to repetitive boilerplate. Since all file fields in a form typically use the same upload mechanism, it makes more sense to declare it once at the form level.Changes
fields/file/types.ts— RemoveduseUploadfromF0FileConfigandF0FileFieldtypes.ts— AddeduseUpload?: UseFileUploadto all 4 form prop interfaces (F0FormPropsWithSingleSchema,F0FormPropsWithPerSectionSchema,F0FormPropsWithSingleSchemaDefinition,F0FormPropsWithPerSectionDefinition)context.ts— AddeduseUploadtoF0FormContextValueso file fields can access it via contextF0Form.tsx— ThreadsuseUploadthrough all code paths (single schema, per-section, definition adapters) into the form contextF0FormSection.tsx— Accepts and forwardsuseUploadinto section-level contextFileFieldRenderer.tsx— ResolvesuseUploadfrom context instead offield.useUploadf0Schema.ts— RemoveduseUpload-based file field type inference (file fields must usefieldType: "file"explicitly)useSchemaDefinition.ts— RemoveduseUploadmapping in the file field caseuseUploadat the<F0Form>levelUsage