Skip to content

Commit

Permalink
fix setSelectionRange(); fixes #1746
Browse files Browse the repository at this point in the history
  • Loading branch information
claviska committed Dec 1, 2023
1 parent bfa7c4c commit 5b3d914
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 9 deletions.
4 changes: 4 additions & 0 deletions docs/pages/resources/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ Components with the <sl-badge variant="warning" pill>Experimental</sl-badge> bad

New versions of Shoelace are released as-needed and generally occur when a critical mass of changes have accumulated. At any time, you can see what's coming in the next release by visiting [next.shoelace.style](https://next.shoelace.style).

## Next

- Fixed a bug in `<sl-input>` and `<sl-textarea>` that made it work differently from `<input>` and `<textarea>` when using defaults [#1746]

## 2.12.0

- Added the Italian translation [#1727]
Expand Down
8 changes: 5 additions & 3 deletions src/components/input/input.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,12 @@ export default class SlInput extends ShoelaceElement implements ShoelaceFormCont
replacement: string,
start?: number,
end?: number,
selectMode?: 'select' | 'start' | 'end' | 'preserve'
selectMode: 'select' | 'start' | 'end' | 'preserve' = 'preserve'
) {
// @ts-expect-error - start, end, and selectMode are optional
this.input.setRangeText(replacement, start, end, selectMode);
const selectionStart = start ?? this.input.selectionStart!;
const selectionEnd = end ?? this.input.selectionEnd!;

this.input.setRangeText(replacement, selectionStart, selectionEnd, selectMode);

if (this.value !== this.input.value) {
this.value = this.input.value;
Expand Down
13 changes: 13 additions & 0 deletions src/components/input/input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,5 +545,18 @@ describe('<sl-input>', () => {
});
});

describe('when using the setRangeText() function', () => {
it('should set replacement text in the correct location', async () => {
const el = await fixture<SlInput>(html` <sl-input value="test"></sl-input> `);

el.focus();
el.setSelectionRange(1, 3);
el.setRangeText('boom');
await el.updateComplete;
console.log(el.value);
expect(el.value).to.equal('tboomt'); // cspell:disable-line
});
});

runFormControlBaseTests('sl-input');
});
10 changes: 4 additions & 6 deletions src/components/textarea/textarea.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,14 +260,12 @@ export default class SlTextarea extends ShoelaceElement implements ShoelaceFormC
replacement: string,
start?: number,
end?: number,
selectMode?: 'select' | 'start' | 'end' | 'preserve'
selectMode: 'select' | 'start' | 'end' | 'preserve' = 'preserve'
) {
// @ts-expect-error - start, end, and selectMode are optional
this.input.setRangeText(replacement, start, end, selectMode);
const selectionStart = start; // ?? this.input.selectionStart;
const selectionEnd = end; // ?? this.input.selectionEnd;

if (this.value !== this.input.value) {
this.value = this.input.value;
}
this.input.setRangeText(replacement, selectionStart, selectionEnd, selectMode);

if (this.value !== this.input.value) {
this.value = this.input.value;
Expand Down
13 changes: 13 additions & 0 deletions src/components/textarea/textarea.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,5 +295,18 @@ describe('<sl-textarea>', () => {
});
});

describe('when using the setRangeText() function', () => {
it('should set replacement text in the correct location', async () => {
const el = await fixture<SlTextarea>(html` <sl-textarea value="test"></sl-textarea> `);

el.focus();
el.setSelectionRange(1, 3);
el.setRangeText('boom');
await el.updateComplete;
console.log(el.value);
expect(el.value).to.equal('tboomt'); // cspell:disable-line
});
});

runFormControlBaseTests('sl-textarea');
});

0 comments on commit 5b3d914

Please sign in to comment.