Skip to content

Commit

Permalink
fix(autocomplete): add function
Browse files Browse the repository at this point in the history
  • Loading branch information
koory1st committed Mar 11, 2024
1 parent 1228675 commit f840997
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/autocomplete/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"@svelement-ui/scrollbar": "workspace:^",
"@svelement-ui/theme-chalk": "workspace:^",
"@svelement-ui/tooltip": "workspace:^",
"@svelement-ui/util-array-2-class-string": "workspace:^"
"@svelement-ui/util-array-2-class-string": "workspace:^",
"@svelement-ui/utils": "workspace:^"
},
"bundledDependencies": [
"@svelement-ui/util-array-2-class-string"
Expand Down
27 changes: 27 additions & 0 deletions packages/autocomplete/src/lib/autocomplete.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import SvelTooltip from '@svelement-ui/tooltip';
import SvelScrollbar from '@svelement-ui/scrollbar';
import { getContext } from 'svelte';
import { isArray } from '@svelement-ui/utils';
export let value;
export let fetchSuggestions;
export let triggerOnFocus = true;
export let highlightFirstItem = false;
let suggestions = [];
let dropdownWidth = '';
Expand All @@ -16,8 +18,33 @@
let loading = false;
let activated = false;
let tooltipVisible = false;
let highlightedIndex = -1;
$: dark = dark || getContext('svel-dark');
const getData = async (queryString) => {
if (suggestionDisabled) return;
const cb = (suggestionList) => {
loading = false;
if (suggestionDisabled) return;
if (isArray(suggestionList)) {
suggestions = suggestionList;
highlightedIndex = highlightFirstItem ? 0 : -1;
} else {
throw Error('autocomplete suggestions must be an array');
}
};
loading.value = true;
if (isArray(fetchSuggestions)) {
cb(fetchSuggestions);
} else {
const result = await fetchSuggestions(queryString, cb);
if (isArray(result)) cb(result);
}
};
function handleFocus() {
tooltipVisible = true;
}
Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { isNull } from './is_null.js';
export { isNumber } from './is_number.js';
export { isUndefined } from './is_undefined.js';
export { isString } from './is_string.js';
export { isArray } from './is_array.js';
3 changes: 3 additions & 0 deletions packages/utils/src/is_array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isArray(input) {
return Array.isArray(input);
}
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f840997

Please sign in to comment.