Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(textfield): reportValidity() doesn't work #760

Merged
merged 2 commits into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
37 changes: 25 additions & 12 deletions components/textfield/src/vwc-textfield.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,28 @@ export class VWCTextField extends MWCTextField {
}
}

function setAttributeByValue(
attributeName: string,
value: unknown,
target: HTMLInputElement,
asEmpty = false
): void {
if (value) {
target.setAttribute(attributeName, asEmpty ? '' : String(value));
} else {
target.removeAttribute(attributeName);
}
}
const setAttributeByValue = (function () {
const NOT_ASSIGNED = Symbol('NotAssigned');
return function (
attributeName: string,
value: unknown,
target: HTMLInputElement,
asEmpty = false
): void {
const newValue:unknown = value
? (() => { return asEmpty ? '' : String(value); })()
: NOT_ASSIGNED;

const currentValue:unknown = target.hasAttribute(attributeName)
? target.getAttribute(attributeName)
: NOT_ASSIGNED;

if (newValue !== currentValue) {
if (newValue === NOT_ASSIGNED) {
target.removeAttribute(attributeName);
} else {
target.setAttribute(attributeName, newValue as string);
}
}
};
}());
8 changes: 4 additions & 4 deletions components/textfield/test/textfield.autovalidate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ describe('textfield validation', () => {

function assertValidityState(texfield, valid, assertHelper = false) {
// TODO: unmark the lines below when the invlalid class issue settled
// const labelHint = texfield.shadowRoot.querySelector('.mdc-text-field--invalid');
// if ((valid && labelHint) || (!valid && !labelHint)) {
// throw new Error(`expected textfield validity to be '${valid}', but the invalid label state ${labelHint ? '' : 'NOT'} found`);
// }
const labelHint = texfield.shadowRoot.querySelector('.mdc-text-field--invalid');
if ((valid && labelHint) || (!valid && !labelHint)) {
throw new Error(`expected textfield validity to be '${valid}', but the invalid label state ${labelHint ? '' : 'NOT'} found`);
}
if (assertHelper) {
const helperHint = texfield.shadowRoot.querySelector('vwc-helper-message[is-error]');
if ((valid && helperHint) || (!valid && !helperHint)) {
Expand Down