Skip to content

Commit 55e3143

Browse files
authored
Merge pull request #255 from ieedan/select-quick-find
Improved `<Select/>`
2 parents 26e0f5a + fceb083 commit 55e3143

File tree

3 files changed

+50
-13
lines changed

3 files changed

+50
-13
lines changed

package-lock.json

+10-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "geist-ui-svelte",
3-
"version": "0.7.6",
3+
"version": "0.7.7",
44
"author": {
55
"name": "Aidan Bleser",
66
"url": "https://aidanbleser.com"
@@ -354,8 +354,8 @@
354354
"@fontsource/geist-mono": "^5.0.3",
355355
"@fontsource/geist-sans": "^5.0.3",
356356
"@sveltejs/kit": "^2.5.10",
357-
"@vercel/analytics": "^1.3.0",
358-
"@vercel/speed-insights": "^1.0.10",
357+
"@vercel/analytics": "^1.3.1",
358+
"@vercel/speed-insights": "^1.0.11",
359359
"autoprefixer": "^10.4.19",
360360
"class-variance-authority": "^0.7.0",
361361
"clsx": "^2.1.1",

src/lib/select/Select.svelte

+37
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,43 @@
278278
e.preventDefault();
279279
searchEnter();
280280
}
281+
282+
if (show && /^[a-zA-Z]{1,1}$/.test(e.key)) {
283+
findByCharacter(e.key);
284+
}
285+
};
286+
287+
const findByCharacter = (char: string) => {
288+
const options = Array.from(dropDownRef.children);
289+
290+
let hasSelected = false;
291+
292+
for (let i = 0; i < options.length; i++) {
293+
const option = options[i] as HTMLElement;
294+
if (option.tagName == "BUTTON" && option.hasAttribute("data-value")) {
295+
const child = findChild(option, (a) =>
296+
a.hasAttribute("data-html"),
297+
) as HTMLDivElement;
298+
if (child.innerText[0].toLowerCase() == char.toLowerCase() && !hasSelected) {
299+
selectedIndex = i;
300+
hasSelected = true;
301+
option.setAttribute("data-focused", "true");
302+
const top = dropDownRef.offsetHeight + dropDownRef.scrollTop;
303+
const elementBottom = option.offsetHeight + option.offsetTop;
304+
if (top < elementBottom) {
305+
const scrollTop = elementBottom + 8 - dropDownRef.offsetHeight;
306+
dropDownRef.scrollTop = scrollTop;
307+
}
308+
309+
if (top - option.offsetTop + 110 > dropDownRef.offsetHeight) {
310+
const scrollTop = option.offsetTop - dropDownRef.offsetHeight / 2;
311+
dropDownRef.scrollTop = scrollTop;
312+
}
313+
} else {
314+
option.setAttribute("data-focused", "false");
315+
}
316+
}
317+
}
281318
};
282319
283320
let selectedIndex = 0;

0 commit comments

Comments
 (0)