Skip to content

Commit 03a29c8

Browse files
authored
merge: pull request #229 from texonom/codex/fix-search-input-event-for-paste
fix search dialog paste handling
2 parents 9843a49 + 2974afd commit 03a29c8

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

packages/nreact/src/components/search-dialog.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import React from 'react'
2-
3-
import throttle from 'lodash.throttle'
42
import { getBlockParentPage, getBlockTitle } from '@texonom/nutils'
53

64
import { NotionContextConsumer, NotionContextProvider } from '../context'
@@ -12,6 +10,15 @@ import { PageTitle } from './page-title'
1210
import type { SearchParams, SearchResults, APIError } from '@texonom/ntypes'
1311
// TODO: modal.default.setAppElement('.notion-viewport')
1412

13+
// simple debounce utility so we only search after the user stops typing
14+
const debounce = (func: (...args: any[]) => void, wait: number) => {
15+
let timeout: ReturnType<typeof setTimeout> | undefined
16+
return (...args: any[]) => {
17+
if (timeout) clearTimeout(timeout)
18+
timeout = setTimeout(() => func(...args), wait)
19+
}
20+
}
21+
1522
export class SearchDialog extends React.Component<{
1623
isOpen: boolean
1724
rootBlockId: string
@@ -34,7 +41,8 @@ export class SearchDialog extends React.Component<{
3441
_search: any
3542

3643
componentDidMount() {
37-
this._search = throttle(this._searchImpl.bind(this), 1000)
44+
// debounce search calls so the expensive query only runs after typing stops
45+
this._search = debounce(this._searchImpl.bind(this), 500)
3846
this._warmupSearch()
3947
}
4048

@@ -66,7 +74,7 @@ export class SearchDialog extends React.Component<{
6674
placeholder='Search'
6775
value={query}
6876
ref={this._inputRef}
69-
onChange={this._onChangeQuery}
77+
onInput={this._onChangeQuery}
7078
/>
7179

7280
{query && (

0 commit comments

Comments
 (0)