-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
feat(lit-query): add render method to query controllers #10791
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
Open
EskiMojo14
wants to merge
10
commits into
TanStack:main
Choose a base branch
from
EskiMojo14:query-render
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+609
−0
Open
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f8bb89e
feat(lit-query): add renderResult utility for rendering query results…
EskiMojo14 5307fcd
feat(lit-query): add render method to query, mutation, and infinite q…
EskiMojo14 87d2d82
feat(lit-query): export renderResult separately
EskiMojo14 41a0609
tests(lit-query): add test for renderResult to check type inference a…
EskiMojo14 6f02132
docs(lit-query): update documentation for render method in guides
EskiMojo14 c3c7fe1
tests(lit-query): add tests for render method in controllers
EskiMojo14 37e010e
fix(lit-query): return undefined when no renderer matches current status
EskiMojo14 4d2c97e
chore: add changeset
EskiMojo14 97e932a
fix(lit-query): use type imports
EskiMojo14 65cc829
chore(lit-query): use optional chaining instead of ternary in renderR…
EskiMojo14 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@tanstack/lit-query': minor | ||
| --- | ||
|
|
||
| Add render method to controllers based on Tasks API |
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
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
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
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,50 @@ | ||
| export type ResultRenderers<TResult extends { status: string }> = { | ||
| [K in TResult['status']]?: ( | ||
| result: Extract<TResult, { status: K }>, | ||
| ) => unknown | ||
| } | ||
|
|
||
| export type RendererResult< | ||
| TResult extends { status: string }, | ||
| TRenderers extends ResultRenderers<TResult>, | ||
| > = { | ||
| [K in TResult['status']]: TRenderers[K] extends ( | ||
| result: Extract<TResult, { status: K }>, | ||
| ) => infer R | ||
| ? R | ||
| : undefined | ||
| }[TResult['status']] | ||
|
|
||
| /** | ||
| * Based on the `status` property of the given `result`, renders the appropriate content using the provided `renderers`. If no renderer is found for the | ||
| * current status, renders nothing. | ||
| * | ||
| * This function is useful for rendering the state of a query result, such as loading, error, or success states, in a declarative way. | ||
| * @param result - The result object containing a `status` property that indicates the current state of the query. | ||
| * @param renderers - An object mapping possible `status` values to their corresponding rendering functions. Each function receives the result object as an argument and returns the content to be rendered for that status. | ||
| * @returns The content returned by the appropriate renderer based on the `status` of the result, or nothing if no renderer is found for that status. | ||
| * | ||
| * @example | ||
| * class TodosView extends LitElement { | ||
| * private readonly todos = createQueryController(this, { | ||
| * queryKey: ['todos'], | ||
| * queryFn: async () => fetch('/api/todos').then((r) => r.json()), | ||
| * }) | ||
| * | ||
| * render() { | ||
| * const query = this.todos() | ||
| * return renderResult(query, { | ||
| * pending: () => html`Loading...`, | ||
| * error: ({ error }) => html`Error: ${error.message}`, | ||
| * success: ({ data }) => html`<ul>${data.map((todo) => html`<li>${todo.title}</li>`)}</ul>`, | ||
| * }) | ||
| * } | ||
| * } | ||
| */ | ||
| export function renderResult< | ||
| TResult extends { status: string }, | ||
| TRenderers extends ResultRenderers<TResult>, | ||
| >(result: TResult, renderers: TRenderers): RendererResult<TResult, TRenderers> { | ||
| const renderer = renderers[result.status as TResult['status']] | ||
| return renderer ? (renderer(result as any) as any) : (undefined as any) | ||
| } |
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.
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.
Uh oh!
There was an error while loading. Please reload this page.