-
Notifications
You must be signed in to change notification settings - Fork 451
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
feat(core): add useDocumentForm
hook
#8557
base: next
Are you sure you want to change the base?
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
2 Skipped Deployments
|
No changes to documentation |
Component Testing Report Updated Feb 21, 2025 10:27 AM (UTC) ❌ Failed Tests (6) -- expand for details
|
⚡️ Editor Performance ReportUpdated Fri, 21 Feb 2025 10:22:14 GMT
Detailed information🏠 Reference resultThe performance result of
🧪 Experiment resultThe performance result of this branch
📚 Glossary
|
f35716c
to
193846b
Compare
193846b
to
5a4dff2
Compare
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.
Very happy to some decoupling of the document pane provider. Wondering if there are some improvements to be made on naming, but otherwise this looks good to me.
* | ||
* Use this as a base point to create your own form. | ||
*/ | ||
export function useCreateForm(options: CreateFormOptions): CreateFormValue { |
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.
Unsure about the naming here. Why "create" form? Could it be named "useDocumentForm" instead?
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.
Yeah, not married with the name.
I was thinking on create
because it creates the handlers and states necessary for the <FormBuilder>
create ~ build.
We are exposing another hook that is useFormBuilder
which gives you the values back from the formBuilder. Which when I read that name it gives me the idea that I can create a new form using the useFormBuilder
, but not, it gives me the values from the provider.
Btw, the comment is incorrect, it should say:
Hook for creating a form state and combine it with the
FormProviderFormBuilder.
documentId: string | ||
documentType: string | ||
}) { | ||
const {setParams: setPaneParams} = usePaneRouter() |
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.
Are the passed params
also coming from usePaneRouter()
? Wondering if setParams could then be passed in as well? Or if we could get params from the hook
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.
Makes sense, I'll pass it.
In the first implementation it was using them from the usePaneRouter
but then it requires to do here the to use the useUnique
which ends up creating a new state for the params, which we reuse in multiple places, so it was better to pass it.
collapsedPaths, | ||
schemaType, | ||
value, | ||
} = useCreateForm({documentId, documentType: 'tasks.task', initialValue}) |
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.
Very nice that you can now do this 🎉
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.
This could have save me a lot of work last year , hope it saves some time for us in the future
useCreateForm
hookuseDocumentForm
hook
const handleChange = useCallback((event: PatchEvent) => patchRef.current(event), []) | ||
|
||
useInsertionEffect(() => { | ||
if (readOnly) { |
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.
This now needs the change from #8691
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.
Rebasesd the branch and added here, thank you for the reminder.
*/ | ||
getFormDocumentValue?: (value: SanityDocumentLike) => SanityDocumentLike | ||
} | ||
interface CreateFormValue { |
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.
rename to DocumentFormProps?
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.
Thanks for catching.
Why Props
and not Value
?
dee23b7
to
52233ce
Compare
dbc1f9e
to
f391abb
Compare
Description
This PR implements a new hook
useDocumentForm
which handles the necessary logic to build a form and pass the values to the<FormProvider>
It also simplifies the logic inside the
<DocumentPaneProvider>
to make it easier to digest:useDocumentPaneInitialValue
useDocumentPaneInspector
Further steps:
forcedVersion
inDocumentPaneProvider
inspect
legacy dialog into the new inspectors logic suggestionWhy we need this?
Currently if you want to render a form using the patch mechanism, have presence, validation and compare values you would need to reuse the
<DocumentPaneProvider>
which bring with it some unnecessary things likehistory
,actions
,perspective
etc, or you needed to write it in your own like in useTasksFormBuilder. This is not ideal and it takes a lot of effort from the developer.By implementing this hook you can now do use this minimal implementation to get all the necessary form values and handlers. (see example in code)
This should also be useful for:
<DocumentPaneProvider>
from hereWhat to review
Please help me review in detail this PR as it touches a critical surface which handles a lot of logic, my recommendation is going commit per commit to see how the changes build on top of each other.
Is the naming of the new
useDocumentForm
hook good?Testing
Manual testing has been done to the PR, focusing on history, versions and tasks which are not covered by e2e tests.
Existing tests should cover the rest of the functionality
Notes for release
adds
useDocumentForm
hook