Skip to content

Commit 8a797cc

Browse files
committed
fix: #1625
1 parent 1d32804 commit 8a797cc

File tree

3 files changed

+54
-12
lines changed

3 files changed

+54
-12
lines changed

src/lib/forms/input-field/Input.svelte

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { CloseButton, type SizeType, type InputProps, type InputValue, cn } from "$lib";
44
import { input, clampSize } from ".";
55
6-
let { children, left, right, value = $bindable(), elementRef = $bindable(), clearable = false, size, color = "default", class: className, wrapperClass, leftClass, rightClass, divClass, clearableSvgClass, clearableColor = "none", clearableClass, clearableOnClick, data = [], maxSuggestions = 5, onSelect, comboClass, ...restProps }: InputProps<InputValue> = $props();
6+
let { children, left, right, value = $bindable(), elementRef = $bindable(), clearable = false, size, color = "default", class: className, wrapperClass, leftClass, rightClass, divClass, clearableSvgClass, clearableColor = "none", clearableClass, clearableOnClick, data = [], maxSuggestions = 5, onSelect, comboClass,onInput, onFocus, onBlur, onKeydown, ...restProps }: InputProps<InputValue> = $props();
77
88
// Automatically enable combobox when data is provided
99
const isCombobox = $derived(Array.isArray(data) && data.length > 0);
@@ -80,20 +80,21 @@
8080
}
8181
});
8282
83-
function handleInput() {
83+
// Default event handlers
84+
function defaultHandleInput(event: Event) {
8485
// Reset backspace flag if user starts typing again
8586
if ((value as string).length > 0) {
8687
backspaceUsed = false;
8788
}
8889
updateSuggestions();
8990
}
9091
91-
function handleFocus() {
92+
function defaultHandleFocus(event: FocusEvent) {
9293
isFocused = true;
9394
updateSuggestions();
9495
}
9596
96-
function handleBlur() {
97+
function defaultHandleBlur(event: FocusEvent) {
9798
// Small delay to allow click on suggestion to fire first
9899
setTimeout(() => {
99100
isFocused = false;
@@ -102,7 +103,7 @@
102103
}, 200);
103104
}
104105
105-
function handleKeydown(event: KeyboardEvent) {
106+
function defaultHandleKeydown(event: KeyboardEvent) {
106107
if (!isCombobox) return;
107108
108109
// Special handling for backspace/delete - track when it's used to clear the input
@@ -138,6 +139,35 @@
138139
}
139140
}
140141
142+
// Combined event handlers that call custom handlers first, then default behavior
143+
function handleInput(event: Event) {
144+
if (onInput) {
145+
onInput(event);
146+
}
147+
defaultHandleInput(event);
148+
}
149+
150+
function handleFocus(event: FocusEvent) {
151+
if (onFocus) {
152+
onFocus(event);
153+
}
154+
defaultHandleFocus(event);
155+
}
156+
157+
function handleBlur(event: FocusEvent) {
158+
if (onBlur) {
159+
onBlur(event);
160+
}
161+
defaultHandleBlur(event);
162+
}
163+
164+
function handleKeydown(event: KeyboardEvent) {
165+
if (onKeydown) {
166+
onKeydown(event);
167+
}
168+
defaultHandleKeydown(event);
169+
}
170+
141171
function selectItem(item: string) {
142172
value = item;
143173

src/lib/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,10 @@ export interface InputProps<T extends InputValue = string> extends Omit<HTMLInpu
772772
maxSuggestions?: number;
773773
onSelect?: (item: string) => void;
774774
comboClass?: ClassValue;
775+
onInput?: (event: Event) => void;
776+
onFocus?: (event: FocusEvent) => void;
777+
onBlur?: (event: FocusEvent) => void;
778+
onKeydown?: (event: KeyboardEvent) => void;
775779
}
776780

777781
// input-addon

src/routes/docs/forms/input-field.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ On this page you will find all of the input types based on multiple variants, st
2727
</script>
2828
```
2929

30-
## Input fields
30+
## Input Fields
3131

3232
Use this example as a generic form element which includes multiple input fields types such as text, email, password, number, URL, and phone number and use the grid layout to add multiple columns and rows.
3333

@@ -111,6 +111,14 @@ text with one click.
111111
/>
112112
```
113113

114+
115+
## Custom Event Handlers
116+
117+
118+
```svelte example
119+
hi
120+
```
121+
114122
## Accessing Input Element with elementRef
115123

116124
```svelte example class="flex justify-center" hideResponsiveButtons
@@ -168,7 +176,7 @@ text with one click.
168176
<Input data={carMakers} clearable placeholder="Clearable" />
169177
```
170178

171-
## Combobox with icon
179+
## Combobox with Icon
172180

173181
```svelte example class="h-80"
174182
<script>
@@ -184,7 +192,7 @@ text with one click.
184192
</Input>
185193
```
186194

187-
## Input sizes
195+
## Input Sizes
188196

189197
Use the following examples to apply a small, default or large size for the input fields.
190198

@@ -211,7 +219,7 @@ User the size prop to change the input size. Choose one from 'sm:text-md' | 'tex
211219
</Label>
212220
```
213221

214-
## Disabled state
222+
## Disabled State
215223

216224
Get started with this example if you want to apply the disabled state to an input field. Add the disabled to change the input to disabled.
217225

@@ -251,7 +259,7 @@ Use the following example to apply validation styles for success and error messa
251259
</div>
252260
```
253261

254-
## Input with icon
262+
## Input with Icon
255263

256264
```svelte example class="space-y-6"
257265
<script>
@@ -290,7 +298,7 @@ Use the following example to apply validation styles for success and error messa
290298
</Label>
291299
```
292300

293-
## Input group
301+
## Input Group
294302

295303
This example can be used to add a descriptive icon or additional text inside the input field.
296304

@@ -348,7 +356,7 @@ This example can be used to add a descriptive icon or additional text inside the
348356
</div>
349357
```
350358

351-
## Icon click handler
359+
## Icon Click Handler
352360

353361
This example shows how to add `onclick` event handler to the icon in `Input`. By clicking an icon, it toggles icon and `type`:
354362

0 commit comments

Comments
 (0)