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

feat(textfield): helperTextContent setter #1569

Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
3cfd6e9
feat(textfield): add API to get/set help text
patrickrodee Nov 8, 2017
ecdb071
fix(textfield): updated files to pass lint
patrickrodee Nov 8, 2017
a47b333
Merge branch 'master' into feat/textfield-helptext-accessor
patrickrodee Nov 8, 2017
71fff53
fix(textfield): changes according to review
patrickrodee Nov 16, 2017
76a58bf
fix(textfield): merged upstream changes
patrickrodee Nov 16, 2017
4f45cdb
Merge branch 'master' into feat/textfield-helptext-accessor
patrickrodee Nov 16, 2017
024fa61
fix(textfield): removed extraneous accessor
patrickrodee Nov 16, 2017
00b48f9
fix(textfield): removed extraneous methods
patrickrodee Nov 16, 2017
335a394
fix(textfield): removed more extranous methods
patrickrodee Nov 16, 2017
b605a4d
fix(textfield): removed extraneous tests
patrickrodee Nov 16, 2017
960c3fe
fix(textfield): resolved conflicts
patrickrodee Nov 16, 2017
d6af5c3
fix(textfield): resolved failing test issues
patrickrodee Nov 16, 2017
37c078d
fix(textfield): all tests finally pass and use new "helper text" name
patrickrodee Nov 16, 2017
af16d8d
fix(textfield): incorrect casing of "MDCTextField"
patrickrodee Nov 17, 2017
dde368a
Merge branch 'master' into feat/textfield-helptext-accessor
patrickrodee Nov 20, 2017
958a046
fix(textfield): removed helperTextContent getter
patrickrodee Nov 20, 2017
6a0d26d
Merge branch 'master' into feat/textfield-helptext-accessor
amsheehan Nov 21, 2017
f79f9b4
Merge branch 'master' into feat/textfield-helptext-accessor
patrickrodee Nov 21, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/mdc-textfield/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,10 @@ being used as the text field's "helper text". It defaults to `null`. If the text
contains an `aria-controls` attribute on instantiation of the component, it will look for an element
with the corresponding id within the document and automatically assign it to this property.

##### MDCTextField.helperTextContent

String setter. Proxies to the foundation's `setHelperTextContent` method when set.

##### MDCTextField.disabled

Boolean. Proxies to the foundation's `isDisabled/setDisabled` methods when retrieved/set
Expand Down Expand Up @@ -413,6 +417,7 @@ complicated.
| deregisterBottomLineEventHandler(evtType: string, handler: EventListener) => void | Deregisters an event listener on the bottom line element for a given event |
| setHelperTextAttr(name: string, value: string) => void | Sets an attribute with a given value on the helper text element |
| removeHelperTextAttr(name: string) => void | Removes an attribute from the helper text element |
| setHelperTextContent(content: string) => void | Sets the content of the helper text element |
| 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. |
| getBottomLineFoundation() => MDCTextFieldBottomLineFoundation | Returns the instance of the bottom line element's foundation |

Expand Down
6 changes: 6 additions & 0 deletions packages/mdc-textfield/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ class MDCTextFieldAdapter {
*/
removeHelperTextAttr(name) {}

/**
* Sets the text content for the help text element
* @param {string} content
*/
setHelperTextContent(content) {}

/**
* Returns an object representing the native text input element, with a
* similar API shape. The object returned should include the value, disabled
Expand Down
8 changes: 8 additions & 0 deletions packages/mdc-textfield/foundation.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class MDCTextFieldFoundation extends MDCFoundation {
deregisterBottomLineEventHandler: () => {},
setHelperTextAttr: () => {},
removeHelperTextAttr: () => {},
setHelperTextContent: () => {},
getNativeInput: () => {},
getBottomLineFoundation: () => {},
});
Expand Down Expand Up @@ -311,6 +312,13 @@ class MDCTextFieldFoundation extends MDCFoundation {
}
}

/**
* @param {string} content Sets the content of the helper text field
*/
setHelperTextContent(content) {
this.adapter_.setHelperTextContent(content);
}

/**
* @return {!Element|!NativeInputType} The native text input from the
* host environment, or a dummy if none exists.
Expand Down
12 changes: 12 additions & 0 deletions packages/mdc-textfield/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ class MDCTextField extends MDCComponent {
this.foundation_.setValid(valid);
}

/**
* @param {string} content Sets the Helper Text element textContent.
*/
set helperTextContent(content) {
this.foundation_.setHelperTextContent(content);
}

/**
* @return {!MDCTextFieldFoundation}
*/
Expand Down Expand Up @@ -236,6 +243,11 @@ class MDCTextField extends MDCComponent {
this.helperTextElement.removeAttribute(name);
}
},
setHelperTextContent: (content) => {
if (this.helperTextElement) {
this.helperTextElement.textContent = content;
}
},
};
}
}
Expand Down
7 changes: 7 additions & 0 deletions test/unit/mdc-textfield/foundation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ test('defaultAdapter returns a complete adapter implementation', () => {
'registerInputInteractionHandler', 'deregisterInputInteractionHandler',
'registerBottomLineEventHandler', 'deregisterBottomLineEventHandler',
'setHelperTextAttr', 'removeHelperTextAttr', 'getNativeInput', 'getBottomLineFoundation',
'setHelperTextContent',
]);
});

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

test('#setHelperTextContent sets the content of the helper text element', () => {
const {foundation, mockAdapter} = setupTest();
foundation.setHelperTextContent('foo');
td.verify(mockAdapter.setHelperTextContent('foo'));
});

test('on input focuses if input event occurs without any other events', () => {
const {foundation, mockAdapter} = setupTest();
let input;
Expand Down
28 changes: 28 additions & 0 deletions test/unit/mdc-textfield/mdc-text-field.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,21 @@ test('set valid updates the component styles', () => {
assert.isNotOk(root.classList.contains(cssClasses.INVALID));
});

test('set helperTextContent updates the helper text element content', () => {
const {component} = setupTest();
const helptext = getHelperText();
component.helperTextElement = helptext;
component.helperTextContent = 'foo';
assert.equal(helptext.textContent, 'foo');
});

test('set helperTextContent has no effect when no helper text element is present', () => {
const {component} = setupTest();
assert.isNull(component.helperTextElement);
component.helperTextContent = 'foo';
assert.isNull(component.helperTextElement);
});

test('#adapter.setIconAttr sets a given attribute to a given value to the icon element', () => {
const {icon, component} = setupTest();

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

test('#adapter.setHelperTextContent does nothing if no help text element present', () => {
const {component} = setupTest();
assert.doesNotThrow(() => component.getDefaultFoundation().adapter_.setHelperTextContent('foo'));
});

test('#adapter.setHelperTextContent updates the text content of the help text element', () => {
const {component} = setupTest();
const helptext = getHelperText();
component.helperTextElement = helptext;
component.getDefaultFoundation().adapter_.setHelperTextContent('foo');
assert.equal(helptext.textContent, 'foo');
});

test(`#adapter.notifyIconAction emits ${strings.ICON_EVENT}`, () => {
const {component} = setupTest();
const handler = td.func('leadingHandler');
Expand Down