This commit is contained in:
Iliyan Angelov
2025-09-14 23:24:25 +03:00
commit c67067a2a4
71311 changed files with 6800714 additions and 0 deletions

View File

@@ -0,0 +1,2 @@
import { keyboardState } from '../types';
export declare function carryValue(element: Element, state: keyboardState, newValue: string): void;

View File

@@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.carryValue = carryValue;
var _utils = require("../../utils");
function carryValue(element, state, newValue) {
const value = (0, _utils.getValue)(element);
state.carryValue = value !== newValue && value === '' && (0, _utils.hasUnreliableEmptyValue)(element) ? newValue : undefined;
}

View File

@@ -0,0 +1,3 @@
export declare function fireChangeForInputTimeIfValid(el: HTMLInputElement & {
type: 'time';
}, prevValue: unknown, timeNewEntry: string): void;

View File

@@ -0,0 +1,20 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.fireChangeForInputTimeIfValid = fireChangeForInputTimeIfValid;
var _dom = require("@testing-library/dom");
var _utils = require("../../utils");
function fireChangeForInputTimeIfValid(el, prevValue, timeNewEntry) {
if ((0, _utils.isValidInputTimeValue)(el, timeNewEntry) && prevValue !== timeNewEntry) {
_dom.fireEvent.change(el, {
target: {
value: timeNewEntry
}
});
}
}

View File

@@ -0,0 +1,17 @@
import { fireEvent } from '@testing-library/dom';
export declare function fireInputEvent(element: HTMLElement, { newValue, newSelectionStart, eventOverrides, }: {
newValue: string;
newSelectionStart: number;
eventOverrides: Partial<Parameters<typeof fireEvent>[1]> & {
[k: string]: unknown;
};
}): void;
declare const initial: unique symbol;
declare const onBlur: unique symbol;
declare global {
interface Element {
[initial]?: string;
[onBlur]?: EventListener;
}
}
export {};

View File

@@ -0,0 +1,110 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.fireInputEvent = fireInputEvent;
var _dom = require("@testing-library/dom");
var _utils = require("../../utils");
function fireInputEvent(element, {
newValue,
newSelectionStart,
eventOverrides
}) {
// apply the changes before firing the input event, so that input handlers can access the altered dom and selection
if ((0, _utils.isContentEditable)(element)) {
applyNative(element, 'textContent', newValue);
} else
/* istanbul ignore else */
if ((0, _utils.isElementType)(element, ['input', 'textarea'])) {
applyNative(element, 'value', newValue);
} else {
// TODO: properly type guard
throw new Error('Invalid Element');
}
setSelectionRangeAfterInput(element, newSelectionStart);
_dom.fireEvent.input(element, { ...eventOverrides
});
setSelectionRangeAfterInputHandler(element, newValue, newSelectionStart);
}
function setSelectionRangeAfterInput(element, newSelectionStart) {
(0, _utils.setSelectionRange)(element, newSelectionStart, newSelectionStart);
}
function setSelectionRangeAfterInputHandler(element, newValue, newSelectionStart) {
const value = (0, _utils.getValue)(element); // don't apply this workaround on elements that don't necessarily report the visible value - e.g. number
// TODO: this could probably be only applied when there is keyboardState.carryValue
const isUnreliableValue = value === '' && (0, _utils.hasUnreliableEmptyValue)(element);
if (!isUnreliableValue && value === newValue) {
const {
selectionStart
} = (0, _utils.getSelectionRange)(element);
if (selectionStart === value.length) {
// The value was changed as expected, but the cursor was moved to the end
// TODO: this could probably be only applied when we work around a framework setter on the element in applyNative
(0, _utils.setSelectionRange)(element, newSelectionStart, newSelectionStart);
}
}
}
const initial = Symbol('initial input value/textContent');
const onBlur = Symbol('onBlur');
/**
* React tracks the changes on element properties.
* This workaround tries to alter the DOM element without React noticing,
* so that it later picks up the change.
*
* @see https://github.com/facebook/react/blob/148f8e497c7d37a3c7ab99f01dec2692427272b1/packages/react-dom/src/client/inputValueTracking.js#L51-L104
*/
function applyNative(element, propName, propValue) {
const descriptor = Object.getOwnPropertyDescriptor(element, propName);
const nativeDescriptor = Object.getOwnPropertyDescriptor(element.constructor.prototype, propName);
if (descriptor && nativeDescriptor) {
Object.defineProperty(element, propName, nativeDescriptor);
} // Keep track of the initial value to determine if a change event should be dispatched.
// CONSTRAINT: We can not determine what happened between focus event and our first API call.
if (element[initial] === undefined) {
element[initial] = String(element[propName]);
}
element[propName] = propValue; // Add an event listener for the blur event to the capture phase on the window.
// CONSTRAINT: Currently there is no cross-platform solution to unshift the event handler stack.
// Our change event might occur after other event handlers on the blur event have been processed.
if (!element[onBlur]) {
var _element$ownerDocumen;
(_element$ownerDocumen = element.ownerDocument.defaultView) == null ? void 0 : _element$ownerDocumen.addEventListener('blur', element[onBlur] = () => {
const initV = element[initial]; // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete element[onBlur]; // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete element[initial];
if (String(element[propName]) !== initV) {
_dom.fireEvent.change(element);
}
}, {
capture: true,
once: true
});
}
if (descriptor) {
Object.defineProperty(element, propName, descriptor);
}
}

View File

@@ -0,0 +1,3 @@
export * from './carryValue';
export * from './fireChangeForInputTimeIfValid';
export * from './fireInputEvent';

View File

@@ -0,0 +1,44 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _carryValue = require("./carryValue");
Object.keys(_carryValue).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (key in exports && exports[key] === _carryValue[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function () {
return _carryValue[key];
}
});
});
var _fireChangeForInputTimeIfValid = require("./fireChangeForInputTimeIfValid");
Object.keys(_fireChangeForInputTimeIfValid).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (key in exports && exports[key] === _fireChangeForInputTimeIfValid[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function () {
return _fireChangeForInputTimeIfValid[key];
}
});
});
var _fireInputEvent = require("./fireInputEvent");
Object.keys(_fireInputEvent).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (key in exports && exports[key] === _fireInputEvent[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function () {
return _fireInputEvent[key];
}
});
});