Skip to content

Commit b31779b

Browse files
authored
add bind:inputEl to textarea in TextField (#531)
* add bind:inputEl to textarea * correct bind syntax * this shouldn't be this hard for me * use a read only binding to avoid type conflicts * don't rely on svelte 5 feature * fix lint errors * add example of bind:inputEl on TextField
1 parent 255f4f5 commit b31779b

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

packages/svelte-ux/src/lib/components/TextField.svelte

+9-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,13 @@
6161
? (node) => [autoFocus(node, typeof autofocus === 'object' ? autofocus : undefined)]
6262
: undefined;
6363
export let operators: { label: string; value: string }[] | undefined = undefined;
64-
export let inputEl: HTMLInputElement | null = null;
64+
export let inputEl: HTMLInputElement | HTMLTextAreaElement | null = null;
65+
// this is a workaround because Input only accepts an HTMLInputElement, not a TextAreaElement
66+
const inputHolder = {
67+
set input(value: HTMLInputElement | null) {
68+
inputEl = value;
69+
},
70+
};
6571
export let debounceChange: boolean | number = false;
6672
export let classes: {
6773
root?: string;
@@ -327,6 +333,7 @@
327333
{disabled}
328334
value={inputValue}
329335
{autocapitalize}
336+
bind:this={inputEl}
330337
on:input={handleInput}
331338
on:focus
332339
on:blur
@@ -366,7 +373,7 @@
366373
{max}
367374
{step}
368375
{actions}
369-
bind:inputEl
376+
bind:inputEl={inputHolder.input}
370377
on:input={handleInput}
371378
on:focus
372379
on:blur

packages/svelte-ux/src/routes/docs/components/TextField/+page.svelte

+15
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
2121
import Preview from '$lib/components/Preview.svelte';
2222
import Blockquote from '$docs/Blockquote.svelte';
23+
import Toggle from '$lib/components/Toggle.svelte';
2324
2425
const numberOperators = [
2526
{ label: '=', value: 'equal' },
@@ -40,6 +41,8 @@
4041
let value = '';
4142
let numberValue = 1;
4243
let multilineValue = 'one\ntwo\nthree';
44+
45+
let inputEl: HTMLInputElement | HTMLTextAreaElement | null = null;
4346
</script>
4447

4548
<h1>Examples</h1>
@@ -225,6 +228,18 @@
225228
/>
226229
</Preview>
227230

231+
<h2>bind:inputEl</h2>
232+
233+
<Preview>
234+
<div class="grid gap-2 justify-start">
235+
<Button on:click={() => inputEl?.focus()}>Manually Focus</Button>
236+
<Toggle let:on={multiline} let:toggle>
237+
<Button on:click={toggle}>{multiline ? 'To single line' : 'To multiline'}</Button>
238+
<TextField label="Name" {multiline} bind:inputEl />
239+
</Toggle>
240+
</div>
241+
</Preview>
242+
228243
<SectionDivider>Type</SectionDivider>
229244

230245
<h2>Input types</h2>

0 commit comments

Comments
 (0)