Skip to content

Commit

Permalink
Fixes setRangeText() in <sl-input> and <sl-textarea> (#1752)
Browse files Browse the repository at this point in the history
* fix setSelectionRange(); fixes #1746

* remove comment

* remove console.log
  • Loading branch information
claviska authored Dec 1, 2023
1 parent e2b7327 commit 4864ab8
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 9 deletions.
1 change: 1 addition & 0 deletions docs/pages/resources/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ New versions of Shoelace are released as-needed and generally occur when a criti
## Next

- Added the `hover-bridge` feature to `<sl-popup>` to support better tooltip accessibility [#1734]
- Fixed a bug in `<sl-input>` and `<sl-textarea>` that made it work differently from `<input>` and `<textarea>` when using defaults [#1746]
- Improved the accessibility of `<sl-tooltip>` so they persist when hovering over the tooltip and dismiss when pressing [[Esc]] [#1734]

## 2.12.0
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
12 changes: 12 additions & 0 deletions src/components/input/input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,5 +545,17 @@ 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;
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
12 changes: 12 additions & 0 deletions src/components/textarea/textarea.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,5 +295,17 @@ 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;
expect(el.value).to.equal('tboomt'); // cspell:disable-line
});
});

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

0 comments on commit 4864ab8

Please sign in to comment.