|
19 | 19 | let styles: CSSObject = {}; |
20 | 20 | let inputHandler: ((event: Event) => void) | null = null; |
21 | 21 | let changeHandler: ((value: string) => void) | null = null; |
| 22 | + let isProgrammaticUpdate = false; |
22 | 23 |
|
23 | 24 | onMount(() => { |
24 | 25 | text = new TextComponent(textRef); |
|
36 | 37 | } |
37 | 38 |
|
38 | 39 | function handleInput(event: Event) { |
| 40 | + if (isProgrammaticUpdate) return; |
| 41 | +
|
39 | 42 | const input = event.target as HTMLInputElement | null; |
40 | 43 | const newValue = input?.value ?? ""; |
41 | 44 |
|
|
44 | 47 | } |
45 | 48 |
|
46 | 49 | function handleChange(newValue: string) { |
| 50 | + if (isProgrammaticUpdate) return; |
| 51 | +
|
47 | 52 | value = newValue; |
48 | 53 | dispatch("change", { value: newValue }); |
49 | 54 | } |
|
66 | 71 | currentType: "text" | "password" | "email" | "number" | "tel" | "url", |
67 | 72 | currentStyles: CSSObject |
68 | 73 | ) { |
69 | | - if (currentValue !== undefined) component.setValue(currentValue); |
70 | | - if (isDisabled) component.setDisabled(isDisabled); |
71 | | - if (currentPlaceholder) component.setPlaceholder(currentPlaceholder); |
72 | | - if (currentType) component.inputEl.type = currentType; |
73 | | - if (currentStyles) { |
74 | | - component.inputEl.setAttr("style", extractStylesFromObj(currentStyles)); |
| 74 | + const componentValue = |
| 75 | + typeof component.getValue === "function" |
| 76 | + ? component.getValue() |
| 77 | + : component.inputEl?.value; |
| 78 | +
|
| 79 | + if (currentValue !== undefined && componentValue !== currentValue) { |
| 80 | + isProgrammaticUpdate = true; |
| 81 | + component.setValue(currentValue); |
| 82 | + isProgrammaticUpdate = false; |
75 | 83 | } |
| 84 | +
|
76 | 85 | if (component?.inputEl) { |
| 86 | + if (component.inputEl.disabled !== isDisabled) { |
| 87 | + component.setDisabled(isDisabled); |
| 88 | + } |
| 89 | +
|
| 90 | + if ( |
| 91 | + currentPlaceholder && |
| 92 | + component.inputEl.placeholder !== currentPlaceholder |
| 93 | + ) { |
| 94 | + component.setPlaceholder(currentPlaceholder); |
| 95 | + } |
| 96 | +
|
| 97 | + if (component.inputEl.type !== currentType) { |
| 98 | + component.inputEl.type = currentType; |
| 99 | + } |
| 100 | +
|
| 101 | + if (currentStyles) { |
| 102 | + component.inputEl.setAttr( |
| 103 | + "style", |
| 104 | + extractStylesFromObj(currentStyles), |
| 105 | + ); |
| 106 | + } |
| 107 | +
|
77 | 108 | el = component.inputEl; |
78 | 109 | } |
79 | 110 | } |
|
0 commit comments