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

Commit 875e393

Browse files
patrickrodeebonniezhou
authored andcommitted
feat(textfield): helperTextContent setter (#1569)
BREAKING CHANGE: Adds adapter method to set helper text content.
1 parent 67354d0 commit 875e393

File tree

6 files changed

+66
-0
lines changed

6 files changed

+66
-0
lines changed

packages/mdc-textfield/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,10 @@ being used as the text field's "helper text". It defaults to `null`. If the text
373373
contains an `aria-controls` attribute on instantiation of the component, it will look for an element
374374
with the corresponding id within the document and automatically assign it to this property.
375375

376+
##### MDCTextField.helperTextContent
377+
378+
String setter. Proxies to the foundation's `setHelperTextContent` method when set.
379+
376380
##### MDCTextField.disabled
377381

378382
Boolean. Proxies to the foundation's `isDisabled/setDisabled` methods when retrieved/set
@@ -413,6 +417,7 @@ complicated.
413417
| deregisterBottomLineEventHandler(evtType: string, handler: EventListener) => void | Deregisters an event listener on the bottom line element for a given event |
414418
| setHelperTextAttr(name: string, value: string) => void | Sets an attribute with a given value on the helper text element |
415419
| removeHelperTextAttr(name: string) => void | Removes an attribute from the helper text element |
420+
| setHelperTextContent(content: string) => void | Sets the content of the helper text element |
416421
| getNativeInput() => {value: string, disabled: boolean, badInput: boolean, checkValidity: () => boolean}? | Returns an object representing the native text input element, with a similar API shape. The object returned should include the `value`, `disabled` and `badInput` properties, as well as the `checkValidity()` function. We _never_ alter the value within our code, however we _do_ update the disabled property, so if you choose to duck-type the return value for this method in your implementation it's important to keep this in mind. Also note that this method can return null, which the foundation will handle gracefully. |
417422
| getBottomLineFoundation() => MDCTextFieldBottomLineFoundation | Returns the instance of the bottom line element's foundation |
418423

packages/mdc-textfield/adapter.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ class MDCTextFieldAdapter {
164164
*/
165165
removeHelperTextAttr(name) {}
166166

167+
/**
168+
* Sets the text content for the help text element
169+
* @param {string} content
170+
*/
171+
setHelperTextContent(content) {}
172+
167173
/**
168174
* Returns an object representing the native text input element, with a
169175
* similar API shape. The object returned should include the value, disabled

packages/mdc-textfield/foundation.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class MDCTextFieldFoundation extends MDCFoundation {
6161
deregisterBottomLineEventHandler: () => {},
6262
setHelperTextAttr: () => {},
6363
removeHelperTextAttr: () => {},
64+
setHelperTextContent: () => {},
6465
getNativeInput: () => {},
6566
getBottomLineFoundation: () => {},
6667
});
@@ -311,6 +312,13 @@ class MDCTextFieldFoundation extends MDCFoundation {
311312
}
312313
}
313314

315+
/**
316+
* @param {string} content Sets the content of the helper text field
317+
*/
318+
setHelperTextContent(content) {
319+
this.adapter_.setHelperTextContent(content);
320+
}
321+
314322
/**
315323
* @return {!Element|!NativeInputType} The native text input from the
316324
* host environment, or a dummy if none exists.

packages/mdc-textfield/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ class MDCTextField extends MDCComponent {
124124
this.foundation_.setValid(valid);
125125
}
126126

127+
/**
128+
* @param {string} content Sets the Helper Text element textContent.
129+
*/
130+
set helperTextContent(content) {
131+
this.foundation_.setHelperTextContent(content);
132+
}
133+
127134
/**
128135
* @return {!MDCTextFieldFoundation}
129136
*/
@@ -236,6 +243,11 @@ class MDCTextField extends MDCComponent {
236243
this.helperTextElement.removeAttribute(name);
237244
}
238245
},
246+
setHelperTextContent: (content) => {
247+
if (this.helperTextElement) {
248+
this.helperTextElement.textContent = content;
249+
}
250+
},
239251
};
240252
}
241253
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ test('defaultAdapter returns a complete adapter implementation', () => {
4343
'registerInputInteractionHandler', 'deregisterInputInteractionHandler',
4444
'registerBottomLineEventHandler', 'deregisterBottomLineEventHandler',
4545
'setHelperTextAttr', 'removeHelperTextAttr', 'getNativeInput', 'getBottomLineFoundation',
46+
'setHelperTextContent',
4647
]);
4748
});
4849

@@ -181,6 +182,12 @@ test('#init does not add mdc-text-field__label--float-above class if the input d
181182
td.verify(mockAdapter.addClassToLabel(cssClasses.LABEL_FLOAT_ABOVE), {times: 0});
182183
});
183184

185+
test('#setHelperTextContent sets the content of the helper text element', () => {
186+
const {foundation, mockAdapter} = setupTest();
187+
foundation.setHelperTextContent('foo');
188+
td.verify(mockAdapter.setHelperTextContent('foo'));
189+
});
190+
184191
test('on input focuses if input event occurs without any other events', () => {
185192
const {foundation, mockAdapter} = setupTest();
186193
let input;

test/unit/mdc-textfield/mdc-text-field.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,21 @@ test('set valid updates the component styles', () => {
138138
assert.isNotOk(root.classList.contains(cssClasses.INVALID));
139139
});
140140

141+
test('set helperTextContent updates the helper text element content', () => {
142+
const {component} = setupTest();
143+
const helptext = getHelperText();
144+
component.helperTextElement = helptext;
145+
component.helperTextContent = 'foo';
146+
assert.equal(helptext.textContent, 'foo');
147+
});
148+
149+
test('set helperTextContent has no effect when no helper text element is present', () => {
150+
const {component} = setupTest();
151+
assert.isNull(component.helperTextElement);
152+
component.helperTextContent = 'foo';
153+
assert.isNull(component.helperTextElement);
154+
});
155+
141156
test('#adapter.setIconAttr sets a given attribute to a given value to the icon element', () => {
142157
const {icon, component} = setupTest();
143158

@@ -311,6 +326,19 @@ test('#adapter.removeHelperTextAttr removes an attribute on the helper text elem
311326
assert.isNotOk(helperText.hasAttribute('aria-label'));
312327
});
313328

329+
test('#adapter.setHelperTextContent does nothing if no help text element present', () => {
330+
const {component} = setupTest();
331+
assert.doesNotThrow(() => component.getDefaultFoundation().adapter_.setHelperTextContent('foo'));
332+
});
333+
334+
test('#adapter.setHelperTextContent updates the text content of the help text element', () => {
335+
const {component} = setupTest();
336+
const helptext = getHelperText();
337+
component.helperTextElement = helptext;
338+
component.getDefaultFoundation().adapter_.setHelperTextContent('foo');
339+
assert.equal(helptext.textContent, 'foo');
340+
});
341+
314342
test(`#adapter.notifyIconAction emits ${strings.ICON_EVENT}`, () => {
315343
const {component} = setupTest();
316344
const handler = td.func('leadingHandler');

0 commit comments

Comments
 (0)