-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): introduce new completion system (#354)
BREAKING CHANGE
- Loading branch information
1 parent
b8ff178
commit 25099e8
Showing
12 changed files
with
29 additions
and
132 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains 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 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 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 |
---|---|---|
@@ -1,49 +1,18 @@ | ||
import { InternalAutocompleteOptions, AutocompleteState } from './types'; | ||
import { AutocompleteState } from './types'; | ||
import { getSelectedItem } from './utils'; | ||
|
||
interface GetCompletionProps<TItem> { | ||
state: AutocompleteState<TItem>; | ||
props: InternalAutocompleteOptions<TItem>; | ||
} | ||
|
||
export function getCompletion<TItem>({ | ||
state, | ||
props, | ||
}: GetCompletionProps<TItem>): string | null { | ||
if ( | ||
props.enableCompletion === false || | ||
state.isOpen === false || | ||
state.selectedItemId === null || | ||
state.status === 'stalled' | ||
) { | ||
if (state.selectedItemId === null) { | ||
return null; | ||
} | ||
|
||
const { itemInputValue } = getSelectedItem({ state })!; | ||
|
||
// The completion should appear only if the _first_ characters of the query | ||
// match with the item. | ||
if ( | ||
state.query.length > 0 && | ||
itemInputValue | ||
.toLocaleLowerCase() | ||
.indexOf(state.query.toLocaleLowerCase()) === 0 | ||
) { | ||
// If the query typed has a different case than the item, we want | ||
// to show the completion matching the case of the query. This makes both | ||
// strings overlap correctly. | ||
// Example: | ||
// - query: 'Gui' | ||
// - item: 'guitar' | ||
// => completion: 'Guitar' | ||
const completion = state.query + itemInputValue.slice(state.query.length); | ||
|
||
if (completion === state.query) { | ||
return null; | ||
} | ||
|
||
return completion; | ||
} | ||
|
||
return null; | ||
return itemInputValue || null; | ||
} |
This file contains 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 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 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 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 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 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 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 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