Skip to content
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

fix(suggestion): add isLoading state to suggestion popup #5542

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chilly-suits-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tiptap/suggestion": patch
---

Exposes an `isLoading` prop to the Suggestion popup, returning a boolean that indicates if the items callback is a promise awaiting resolution
17 changes: 14 additions & 3 deletions packages/suggestion/src/suggestion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ export interface SuggestionProps<I = any, TSelected = any> {
*/
items: I[]

/**
* The state of loading items.
*/
isLoading: boolean

/**
* A function that is called when a suggestion is selected.
* @param props The props object.
Expand Down Expand Up @@ -215,6 +220,7 @@ export function Suggestion<I = any, TSelected = any>({
query: state.query,
text: state.text,
items: [],
isLoading: false,
command: commandProps => {
return command({
editor,
Expand Down Expand Up @@ -247,9 +253,14 @@ export function Suggestion<I = any, TSelected = any>({
}

if (handleChange || handleStart) {
props.items = await items({
editor,
query: state.query,
props.isLoading = true
Promise.resolve(items({ editor, query: state.query })).then(result => {
if (props == null) {
return
}
props.isLoading = false
props.items = result
renderer?.onUpdate?.(props)
Comment on lines +256 to +263
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is doing more than just exposing the loading state, it should still be awaited, you are changing the behavior of how this works.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see apologies - this is kind of a naive question, why does it need to be awaited?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By awaiting the function, you are suspending it's execution of the lines after it. If the goal truly is to only expose an isLoading flag, then you should not be messing with the order of execution. By doing a Promise.resolve & not awaiting it, you are immediately executing the next lines synchronously.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense! Though wouldn't having await defeat the purpose of having an up-to-date isLoading value if the component is only going to be re-rendered after the promise returns?

I imagined it like this:

  • update isLoading -> rerender (i.e. immediately executing next lines)
  • when promise is resolved -> update isLoading -> rerender

and re: messing up the next lines: maybe something like this might work?

Might be missing a bit of context and need a bit of hand-holding here

})
}

Expand Down