Skip to content

Commit 232c34a

Browse files
authored
Add live input handling for episode search (#135)
1 parent 75eb2c8 commit 232c34a

2 files changed

Lines changed: 29 additions & 12 deletions

File tree

src/ui/PodcastView/EpisodeList.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
<div class="episode-list-search">
4040
<Text
4141
bind:value={searchInputQuery}
42-
on:change={forwardSearchInput}
42+
on:input={forwardSearchInput}
4343
placeholder="Search episodes"
4444
style={{
4545
width: "100%",

src/ui/obsidian/Text.svelte

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,47 @@
1717
1818
let text: TextComponent;
1919
let styles: CSSObject = {};
20-
let textChangeHandler: ((event: Event) => void) | null = null;
20+
let inputHandler: ((event: Event) => void) | null = null;
21+
let changeHandler: ((value: string) => void) | null = null;
2122
2223
onMount(() => {
2324
text = new TextComponent(textRef);
24-
25-
textChangeHandler = (event: Event) => {
26-
const newValue = (event.target as HTMLInputElement).value;
27-
value = newValue;
28-
dispatch("change", { value: newValue });
29-
};
30-
31-
text.inputEl.addEventListener("input", textChangeHandler);
3225
});
3326
3427
onDestroy(() => {
35-
if (text?.inputEl && textChangeHandler) {
36-
text.inputEl.removeEventListener("input", textChangeHandler);
28+
if (text?.inputEl && inputHandler) {
29+
text.inputEl.removeEventListener("input", inputHandler);
3730
}
3831
});
3932
4033
$: if (text) {
34+
attachEventListeners(text);
4135
updateTextComponentAttributes(text, value, disabled, placeholder, type, styles);
4236
}
4337
38+
function handleInput(event: Event) {
39+
const input = event.target as HTMLInputElement | null;
40+
const newValue = input?.value ?? "";
41+
42+
value = newValue;
43+
dispatch("input", { value: newValue });
44+
}
45+
46+
function handleChange(newValue: string) {
47+
value = newValue;
48+
dispatch("change", { value: newValue });
49+
}
50+
51+
function attachEventListeners(component: TextComponent) {
52+
if (!component?.inputEl || inputHandler) return;
53+
54+
changeHandler = handleChange;
55+
component.onChange(changeHandler);
56+
57+
inputHandler = handleInput;
58+
component.inputEl.addEventListener("input", inputHandler);
59+
}
60+
4461
function updateTextComponentAttributes(
4562
component: TextComponent,
4663
currentValue: string,

0 commit comments

Comments
 (0)