Skip to content

Commit

Permalink
fix: delete chars from value at maxlength (#909)
Browse files Browse the repository at this point in the history
  • Loading branch information
ph-fritsche authored Apr 11, 2022
1 parent 627a5cf commit f5049c4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/utils/edit/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function editInputElement(
) {
let dataToInsert = data
const spaceUntilMaxLength = getSpaceUntilMaxLength(element)
if (spaceUntilMaxLength !== undefined) {
if (spaceUntilMaxLength !== undefined && data.length > 0) {
if (spaceUntilMaxLength > 0) {
dataToInsert = data.substring(0, spaceUntilMaxLength)
} else {
Expand Down
10 changes: 8 additions & 2 deletions tests/utils/edit/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,10 @@ test('prevent input on `beforeinput` event', () => {

cases(
'maxlength',
({html, data, expectedValue}) => {
({html, data, inputType, expectedValue}) => {
const {element, eventWasFired} = render(html)

input(createConfig(), element, data)
input(createConfig(), element, data, inputType)

expect(element).toHaveValue(expectedValue)
expect(eventWasFired('beforeinput')).toBe(true)
Expand Down Expand Up @@ -270,6 +270,12 @@ cases(
data: '',
expectedValue: '',
},
'delete data when maxlength is reached': {
html: `<input maxlength="3" value="foo"/>`,
data: '',
inputType: 'deleteContentForward',
expectedValue: 'oo',
},
},
)

Expand Down

0 comments on commit f5049c4

Please sign in to comment.