Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit bce2e63

Browse files
authored
fix(text-field): Set character counter in setValue (#4572)
1 parent 975bae2 commit bce2e63

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

packages/mdc-textfield/foundation.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ export class MDCTextFieldFoundation extends MDCFoundation<MDCTextFieldAdapter> {
289289
// Prevent Safari from moving the caret to the end of the input when the value has not changed.
290290
if (this.getValue() !== value) {
291291
this.getNativeInput_().value = value;
292+
this.setCharacterCounter_(value.length);
292293
}
293294
const isValid = this.isValid();
294295
this.styleValidity_(isValid);

test/unit/mdc-textfield/foundation.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,22 @@ test('#setValue does not affect disabled state', () => {
206206
td.verify(mockAdapter.removeClass(cssClasses.INVALID), {times: 1});
207207
});
208208

209+
test('#setValue updates character counter when present', () => {
210+
const {foundation, mockAdapter, characterCounter} = setupTest({useCharacterCounter: true});
211+
const nativeInput = {
212+
type: 'text',
213+
value: '',
214+
maxLength: 4,
215+
validity: {
216+
valid: true,
217+
},
218+
};
219+
td.when(mockAdapter.getNativeInput()).thenReturn(nativeInput);
220+
221+
foundation.setValue('ok');
222+
td.verify(characterCounter.setCounterValue(2, 4), {times: 1});
223+
});
224+
209225
test('#isValid for native validation', () => {
210226
const {foundation, nativeInput} = setupValueTest({value: '', optIsValid: true});
211227
assert.isOk(foundation.isValid());

0 commit comments

Comments
 (0)