|
12 | 12 | const type = $derived(VALUE_TYPES.includes(valueType) ? valueType : "string") |
13 | 13 |
|
14 | 14 | $effect(() => { |
| 15 | + editValue // track editValue so this re-runs on input changes |
15 | 16 | if (isEditing && inputElement && measureElement) { |
16 | 17 | tick().then(() => updateWidth()) |
17 | 18 | } |
18 | 19 | }) |
19 | 20 |
|
20 | | - $effect(() => { |
21 | | - if (editValue !== undefined && isEditing) { |
22 | | - updateWidth() |
23 | | - } |
24 | | - }) |
25 | | -
|
26 | 21 | function updateWidth() { |
27 | 22 | if (!inputElement || !measureElement) return |
28 | 23 |
|
29 | 24 | const value = editValue?.toString() || inputElement.placeholder || "W" |
30 | 25 | measureElement.textContent = value |
31 | | - const width = measureElement.offsetWidth |
32 | | -
|
33 | | - const wrapper = inputElement.closest(".input-wrapper") |
34 | | - const maxWidth = wrapper?.parentElement?.offsetWidth || Infinity |
35 | | -
|
36 | | - const finalWidth = Math.min(width, maxWidth - 40) // -40px for buttons |
37 | | - inputElement.style.width = `${finalWidth}px` |
38 | | -
|
39 | | - if (wrapper) { |
40 | | - wrapper.style.width = `${finalWidth}px` |
41 | | - } |
| 26 | + const width = Math.ceil(measureElement.getBoundingClientRect().width) + 12 |
| 27 | + inputElement.style.width = `${width}px` |
42 | 28 | } |
43 | 29 |
|
44 | 30 | const handleSubmit = (e) => { |
|
64 | 50 | } |
65 | 51 |
|
66 | 52 | const handleInput = (e) => { |
67 | | - editValue = e.target.value |
| 53 | + editValue = type === "number" ? e.target.valueAsNumber : e.target.value |
68 | 54 | } |
69 | 55 | </script> |
70 | 56 |
|
71 | 57 | {#if isEditing} |
72 | | - <form class="d-flex gap-2 w-100" onsubmit={handleSubmit}> |
| 58 | + <form class="d-flex gap-2" onsubmit={handleSubmit}> |
73 | 59 | <div class="input-wrapper"> |
74 | 60 | <input bind:this={inputElement} class="auto-width-input" value={editValue} type={type === "null" || type === "undefined" ? "text" : type} oninput={handleInput} /> |
75 | 61 | <span bind:this={measureElement} class="size-measure"></span> |
|
106 | 92 | min-width: 2.5rem; |
107 | 93 | max-width: 100%; |
108 | 94 | width: auto; |
109 | | - transition: all 0.15s ease; |
| 95 | + transition: border-color 0.15s ease, box-shadow 0.15s ease; |
110 | 96 | line-height: 1.4; |
111 | 97 | height: 1.2rem; |
112 | 98 | } |
|
0 commit comments