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

Commit acfbe2d

Browse files
fix(text-field): Update character counter to update when value is set. (#4663)
1 parent 5a52847 commit acfbe2d

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

packages/mdc-textfield/foundation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,8 @@ 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);
293292
}
293+
this.setCharacterCounter_(value.length);
294294
const isValid = this.isValid();
295295
this.styleValidity_(isValid);
296296
if (this.adapter_.hasLabel()) {

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,22 @@ test('#setValue updates character counter when present', () => {
222222
td.verify(characterCounter.setCounterValue(2, 4), {times: 1});
223223
});
224224

225+
test('#setValue forces the character counter to update if value was updated independently', () => {
226+
const {foundation, mockAdapter, characterCounter} = setupTest({useCharacterCounter: true});
227+
const nativeInput = {
228+
type: 'text',
229+
value: '',
230+
maxLength: 4,
231+
validity: {
232+
valid: true,
233+
},
234+
};
235+
td.when(mockAdapter.getNativeInput()).thenReturn(nativeInput);
236+
nativeInput.value = 'ok';
237+
foundation.setValue('ok');
238+
td.verify(characterCounter.setCounterValue(2, 4), {times: 1});
239+
});
240+
225241
test('#isValid for native validation', () => {
226242
const {foundation, nativeInput} = setupValueTest({value: '', optIsValid: true});
227243
assert.isOk(foundation.isValid());

0 commit comments

Comments
 (0)