-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
ref(forms): Migrate highlights settings #115778
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
priscilawebdev
merged 8 commits into
master
from
priscilawebdev/ref/de-1251-highlights-settings-form-migration
May 21, 2026
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cc24014
ref(forms): Migrate highlights settings
priscilawebdev f7c6b8c
ref(forms): submit parsed autosave values
priscilawebdev 249df1e
ref(forms): parse highlights context via schema transform
priscilawebdev 4db2943
ref(forms): revert formatter changes in autoSaveForm.mdx
priscilawebdev 9579857
ref(forms): mention passthrough for arbitrary keys in autosave docs
priscilawebdev 710fe6e
ref(forms): use z.looseObject for choice_mapper and update autosave docs
priscilawebdev e7cf6ed
ref(forms): restore type-flow test in autoSaveForm.spec
priscilawebdev dd29f65
Merge branch 'master' into priscilawebdev/ref/de-1251-highlights-sett…
priscilawebdev 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 |
|---|---|---|
|
|
@@ -26,28 +26,29 @@ For traditional submit-based forms, see [Form](./form.mdx). | |
| </Storybook.Demo> | ||
|
|
||
| ```jsx | ||
| import {z} from 'zod'; | ||
| import { z } from "zod"; | ||
|
|
||
| import {AutoSaveForm} from '@sentry/scraps/form'; | ||
| import { AutoSaveForm } from "@sentry/scraps/form"; | ||
|
|
||
| import {fetchMutation} from 'sentry/utils/queryClient'; | ||
| import { fetchMutation } from "sentry/utils/queryClient"; | ||
|
|
||
| const schema = z.object({ | ||
| displayName: z.string().min(1, 'Display name is required'), | ||
| displayName: z.string().min(1, "Display name is required"), | ||
| }); | ||
|
|
||
| <AutoSaveForm | ||
| name="displayName" | ||
| schema={schema} | ||
| initialValue={user.displayName} | ||
| mutationOptions={{ | ||
| mutationFn: (data: Partial<User>) => fetchMutation({url: '/user/', method: 'PUT', data}), | ||
| onSuccess: data => { | ||
| queryClient.setQueryData(['user'], old => ({...old, ...data})); | ||
| mutationFn: (data: Partial<User>) => | ||
| fetchMutation({ url: "/user/", method: "PUT", data }), | ||
| onSuccess: (data) => { | ||
| queryClient.setQueryData(["user"], (old) => ({ ...old, ...data })); | ||
| }, | ||
| }} | ||
| > | ||
| {field => ( | ||
| {(field) => ( | ||
| <field.Layout.Row label="Display Name"> | ||
| <field.Input value={field.state.value} onChange={field.handleChange} /> | ||
| </field.Layout.Row> | ||
|
|
@@ -88,6 +89,35 @@ const schema = z.object({ | |
| > [!NOTE] | ||
| > Do NOT use toasts to communicate auto-save status. The built-in inline indicators are the correct feedback mechanism. Toasts are noisy and disruptive for fields that save frequently. | ||
|
|
||
| ## Transformed Submit Values | ||
|
|
||
| `AutoSaveForm` keeps field state typed as the schema input, but submits the schema's parsed output to the mutation. This matches `useScrapsForm` and lets you use transforms or narrower output types without extra parsing in `mutationFn`. | ||
|
|
||
| > [!WARNING] | ||
| > The schema is applied to the form value on submit, so unknown keys are stripped per Zod's default behavior. For map-like fields with arbitrary keys, use `z.record(z.string(), …)` — not `z.object({})`, which declares zero keys and will strip everything at submit time. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can also use
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| ```jsx | ||
| const schema = z.object({ | ||
| highlightContext: z.string().transform((value) => JSON.parse(value)), | ||
| }); | ||
|
|
||
| <AutoSaveForm | ||
| name="highlightContext" | ||
| schema={schema} | ||
| initialValue="{}" | ||
| mutationOptions={{ | ||
| mutationFn: (data: { highlightContext: Record<string, string[]> }) => | ||
| fetchMutation({ url: "/project/", method: "PUT", data }), | ||
| }} | ||
| > | ||
| {(field) => ( | ||
| <field.Layout.Row label="Highlight Context"> | ||
| <field.TextArea value={field.state.value} onChange={field.handleChange} /> | ||
| </field.Layout.Row> | ||
| )} | ||
| </AutoSaveForm>; | ||
| ``` | ||
|
|
||
| ## Confirmation Dialogs | ||
|
|
||
| For dangerous operations (security settings, permissions), use the `confirm` prop to show a confirmation modal before saving. It accepts a string (always shown) or a function (conditionally shown). | ||
|
|
@@ -126,11 +156,11 @@ Type the `mutationFn` with the API's data type, **not** the zod schema type. The | |
| ```jsx | ||
| // ❌ Don't tie mutation type to the zod schema | ||
| mutationFn: (data: Partial<z.infer<typeof schema>>) => | ||
| fetchMutation({url: '/user/', method: 'PUT', data}) | ||
| fetchMutation({ url: "/user/", method: "PUT", data }); | ||
|
|
||
| // ✅ Use the API's data type | ||
| mutationFn: (data: Partial<UserDetails>) => | ||
| fetchMutation({url: '/user/', method: 'PUT', data}) | ||
| fetchMutation({ url: "/user/", method: "PUT", data }); | ||
| ``` | ||
|
|
||
| Make sure the zod schema's types are compatible with the API type. For example, if the API expects a string union like `'off' | 'low' | 'high'`, use `z.enum(['off', 'low', 'high'])` instead of `z.string()`. | ||
|
|
||
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
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.
lots of unrelated formatting changes here
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.
yes... I was expecting these to be auto fixed in this PR 😅
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.
4db2943