MCPcopy Create free account
hub / github.com/testing-library/user-event / editInputElement

Function editInputElement

src/event/input.ts:118–181  ·  view source on GitHub ↗
(
  instance: Instance,
  element: EditableInputOrTextarea,
  inputRange: UISelectionRange,
  data: string,
  inputType: string,
)

Source from the content-addressed store, hash-verified

116}
117
118function editInputElement(
119 instance: Instance,
120 element: EditableInputOrTextarea,
121 inputRange: UISelectionRange,
122 data: string,
123 inputType: string,
124) {
125 let dataToInsert = data
126 if (supportsMaxLength(element)) {
127 const maxLength = getMaxLength(element)
128 if (maxLength !== undefined && data.length > 0) {
129 const selectionLength = inputRange.endOffset - inputRange.startOffset
130 const spaceUntilMaxLength =
131 maxLength - element.value.length + selectionLength
132 if (spaceUntilMaxLength > 0) {
133 dataToInsert = data.substring(0, spaceUntilMaxLength)
134 } else {
135 return
136 }
137 }
138 }
139
140 const {newValue, newOffset, oldValue} = calculateNewValue(
141 dataToInsert,
142 element,
143 inputRange,
144 inputType,
145 )
146
147 if (
148 newValue === oldValue &&
149 newOffset === inputRange.startOffset &&
150 newOffset === inputRange.endOffset
151 ) {
152 return
153 }
154
155 if (
156 isElementType(element, 'input', {type: 'number'}) &&
157 !isValidNumberInput(newValue)
158 ) {
159 return
160 }
161
162 setUIValue(element, newValue)
163 setSelection({
164 focusNode: element,
165 anchorOffset: newOffset,
166 focusOffset: newOffset,
167 })
168
169 if (isDateOrTime(element)) {
170 if (isValidDateOrTimeValue(element, newValue)) {
171 commitInput(instance, element, newOffset, {})
172 instance.dispatchUIEvent(element, 'change')
173 clearInitialValue(element)
174 }
175 } else {

Callers 1

inputFunction · 0.85

Calls 11

supportsMaxLengthFunction · 0.90
getMaxLengthFunction · 0.90
isElementTypeFunction · 0.90
setUIValueFunction · 0.90
setSelectionFunction · 0.90
isValidDateOrTimeValueFunction · 0.90
clearInitialValueFunction · 0.90
calculateNewValueFunction · 0.85
isValidNumberInputFunction · 0.85
isDateOrTimeFunction · 0.85
commitInputFunction · 0.85

Tested by

no test coverage detected