fix: issue with controlled input selection ranges

This commit is contained in:
dpiercey 2025-04-02 12:26:55 -07:00 committed by Dylan Piercey
parent 43c31babcd
commit 4524bc4598
4 changed files with 18 additions and 16 deletions

View File

@ -0,0 +1,5 @@
---
"@marko/runtime-tags": patch
---
Fix issue with controlled input selection ranges.

View File

@ -7,8 +7,8 @@
{
"name": "*",
"total": {
"min": 18907,
"brotli": 7216
"min": 18901,
"brotli": 7208
}
},
{

View File

@ -1,4 +1,4 @@
// size: 18907 (min) 7216 (brotli)
// size: 18901 (min) 7208 (brotli)
var empty = [],
rest = Symbol();
function attrTag(attrs2) {
@ -361,14 +361,12 @@ function controllable_input_value_effect(scope, nodeAccessor) {
isResuming && (scope["g" + nodeAccessor] = el.defaultValue),
syncControllable(el, "input", hasValueChanged, (ev) => {
let valueChange = scope["e" + nodeAccessor];
if (valueChange) {
let newValue = el.value;
(inputType = ev?.inputType),
setValueAndUpdateSelection(el, scope["g" + nodeAccessor]),
valueChange(newValue),
run(),
(inputType = "");
}
valueChange &&
((inputType = ev?.inputType),
valueChange(el.value),
run(),
setValueAndUpdateSelection(el, scope["g" + nodeAccessor]),
(inputType = ""));
});
}
function controllable_select_value(scope, nodeAccessor, value2, valueChange) {
@ -482,7 +480,7 @@ function setValueAndUpdateSelection(el, value2) {
el.value,
(el.value = value2),
);
~updatedPosition && (el.selectionStart = updatedPosition);
~updatedPosition && el.setSelectionRange(updatedPosition, updatedPosition);
}
}
function setCheckboxValue(scope, nodeAccessor, type, checked, checkedChange) {

View File

@ -137,14 +137,13 @@ export function controllable_input_value_effect(
AccessorPrefix.ControlledHandler + nodeAccessor
] as undefined | ((value: unknown) => unknown);
if (valueChange) {
const newValue = el.value;
inputType = (ev as InputEvent)?.inputType;
valueChange(el.value);
run();
setValueAndUpdateSelection(
el,
scope[AccessorPrefix.ControlledValue + nodeAccessor],
);
valueChange(newValue);
run();
inputType = "";
}
});
@ -306,7 +305,7 @@ function setValueAndUpdateSelection(el: HTMLInputElement, value: string) {
(el.value = value),
);
if (~updatedPosition) {
el.selectionStart = updatedPosition;
el.setSelectionRange(updatedPosition, updatedPosition);
}
}
}