|
1 | 1 | <script lang="ts"> |
2 | 2 | import { writable } from 'svelte/store'; |
3 | 3 |
|
4 | | - import { afterUpdate } from 'svelte'; |
| 4 | + import { afterUpdate, onDestroy } from 'svelte'; |
5 | 5 | import { twMerge as merge } from 'tailwind-merge'; |
6 | 6 |
|
7 | 7 | import Chip from '$lib/holocene/chip.svelte'; |
|
19 | 19 | export let validator: (value: string) => boolean = () => true; |
20 | 20 | export let removeChipButtonLabel: string | ((chipValue: string) => string); |
21 | 21 | export let external = false; |
22 | | - export let maxLength = 0; |
23 | 22 |
|
24 | 23 | const values = writable<string[]>(chips); |
25 | 24 | let displayValue = ''; |
| 25 | + let shouldScrollToInput = false; |
26 | 26 | let inputContainer: HTMLDivElement; |
27 | 27 | let input: HTMLInputElement; |
28 | 28 |
|
|
32 | 32 | let className = ''; |
33 | 33 | export { className as class }; |
34 | 34 |
|
| 35 | + const scrollToInput = () => { |
| 36 | + let rect = input.getBoundingClientRect(); |
| 37 | + inputContainer.scrollTo(rect.x, rect.y); |
| 38 | + shouldScrollToInput = false; |
| 39 | + }; |
| 40 | +
|
| 41 | + const unsubscribe = values.subscribe((updatedChips) => { |
| 42 | + shouldScrollToInput = updatedChips.length > chips.length; |
| 43 | + chips = updatedChips; |
| 44 | + }); |
| 45 | +
|
35 | 46 | afterUpdate(() => { |
36 | | - input.scrollIntoView(); |
| 47 | + if (shouldScrollToInput) { |
| 48 | + scrollToInput(); |
| 49 | + } |
| 50 | + }); |
| 51 | +
|
| 52 | + onDestroy(() => { |
| 53 | + unsubscribe(); |
37 | 54 | }); |
38 | 55 |
|
39 | 56 | const handleKeydown = (e: KeyboardEvent) => { |
|
57 | 74 |
|
58 | 75 | const handlePaste = (e: ClipboardEvent) => { |
59 | 76 | e.preventDefault(); |
60 | | - if (maxLength && $values.length >= maxLength) return; |
61 | 77 | const clipboardContents = e.clipboardData.getData('text/plain'); |
62 | | - let newValues = clipboardContents |
63 | | - .split(',') |
64 | | - .map((content) => content.trim()); |
65 | | -
|
66 | | - if (maxLength) { |
67 | | - newValues = newValues.slice(0, maxLength - $values.length); |
68 | | - } |
69 | | -
|
70 | | - values.update((previous) => [...previous, ...newValues]); |
| 78 | + values.update((previous) => [ |
| 79 | + ...previous, |
| 80 | + ...clipboardContents.split(',').map((content) => content.trim()), |
| 81 | + ]); |
71 | 82 | }; |
72 | 83 |
|
73 | 84 | const handleBlur = () => { |
|
86 | 97 | }; |
87 | 98 | </script> |
88 | 99 |
|
89 | | -<div |
90 | | - class={merge( |
91 | | - 'group flex flex-col gap-1', |
92 | | - disabled && 'cursor-not-allowed', |
93 | | - className, |
94 | | - )} |
95 | | -> |
96 | | - <Label {required} {label} {disabled} hidden={labelHidden} for={id} /> |
| 100 | +<div class={merge(disabled && 'cursor-not-allowed', className)}> |
| 101 | + <Label |
| 102 | + class="pb-1" |
| 103 | + {required} |
| 104 | + {label} |
| 105 | + {disabled} |
| 106 | + hidden={labelHidden} |
| 107 | + for={id} |
| 108 | + /> |
97 | 109 | <div |
98 | 110 | bind:this={inputContainer} |
99 | 111 | class={merge( |
|
132 | 144 | on:blur={handleBlur} |
133 | 145 | on:keydown|stopPropagation={handleKeydown} |
134 | 146 | on:paste={handlePaste} |
135 | | - maxlength={maxLength && $values.length >= maxLength ? 0 : undefined} |
136 | 147 | /> |
137 | 148 | </div> |
138 | | - |
139 | | - {#if (invalid && hintText) || (maxLength && !disabled)} |
140 | | - <div class="flex justify-between gap-2"> |
141 | | - <div |
142 | | - class="error-msg" |
143 | | - class:min-width={maxLength} |
144 | | - aria-live={invalid ? 'assertive' : 'off'} |
145 | | - > |
146 | | - {#if invalid && hintText} |
147 | | - <p>{hintText}</p> |
148 | | - {/if} |
149 | | - </div> |
150 | | - {#if maxLength && !disabled} |
151 | | - <span class="count"> |
152 | | - <span |
153 | | - class="text-information" |
154 | | - class:warn={maxLength - $values?.length <= 5} |
155 | | - class:error={maxLength === $values?.length} |
156 | | - > |
157 | | - {$values?.length ?? 0} |
158 | | - </span> / {maxLength} |
159 | | - </span> |
160 | | - {/if} |
161 | | - </div> |
| 149 | + {#if invalid && hintText} |
| 150 | + <span class="hint"> |
| 151 | + {hintText} |
| 152 | + </span> |
162 | 153 | {/if} |
163 | | - |
164 | 154 | {#if $values.length > 0 && external} |
165 | | - <div class="flex flex-row flex-wrap gap-1"> |
| 155 | + <div class="mt-1 flex flex-row flex-wrap gap-1"> |
166 | 156 | {#each $values as chip, i (`${chip}-${i}`)} |
167 | 157 | {@const valid = validator(chip)} |
168 | 158 | <Chip |
|
184 | 174 | } |
185 | 175 |
|
186 | 176 | input { |
187 | | - @apply surface-primary inline-block grow focus:outline-none; |
188 | | - } |
189 | | -
|
190 | | - .error-msg { |
191 | | - @apply break-words text-sm text-danger; |
192 | | - } |
193 | | -
|
194 | | - .error-msg.min-width { |
195 | | - @apply w-[calc(100%-6rem)]; |
196 | | - } |
197 | | -
|
198 | | - .count { |
199 | | - @apply invisible text-right text-sm font-medium text-primary group-focus-within:visible; |
200 | | - } |
201 | | -
|
202 | | - .count > .warn { |
203 | | - @apply text-warning; |
| 177 | + @apply surface-primary inline-block w-full focus:outline-none; |
204 | 178 | } |
205 | 179 |
|
206 | | - .count > .error { |
207 | | - @apply text-danger; |
| 180 | + .hint { |
| 181 | + @apply text-xs text-danger; |
208 | 182 | } |
209 | 183 | </style> |
0 commit comments