foundation.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * @license
  3. * Copyright 2017 Google Inc.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. import { __assign, __extends } from "tslib";
  24. import { MDCFoundation } from '@material/base/foundation';
  25. import { cssClasses, strings } from './constants';
  26. /** MDC Text Field Helper Text Foundation */
  27. var MDCTextFieldHelperTextFoundation = /** @class */ (function (_super) {
  28. __extends(MDCTextFieldHelperTextFoundation, _super);
  29. function MDCTextFieldHelperTextFoundation(adapter) {
  30. return _super.call(this, __assign(__assign({}, MDCTextFieldHelperTextFoundation.defaultAdapter), adapter)) || this;
  31. }
  32. Object.defineProperty(MDCTextFieldHelperTextFoundation, "cssClasses", {
  33. get: function () {
  34. return cssClasses;
  35. },
  36. enumerable: false,
  37. configurable: true
  38. });
  39. Object.defineProperty(MDCTextFieldHelperTextFoundation, "strings", {
  40. get: function () {
  41. return strings;
  42. },
  43. enumerable: false,
  44. configurable: true
  45. });
  46. Object.defineProperty(MDCTextFieldHelperTextFoundation, "defaultAdapter", {
  47. /**
  48. * See {@link MDCTextFieldHelperTextAdapter} for typing information on
  49. * parameters and return types.
  50. */
  51. get: function () {
  52. // tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
  53. return {
  54. addClass: function () { return undefined; },
  55. removeClass: function () { return undefined; },
  56. hasClass: function () { return false; },
  57. getAttr: function () { return null; },
  58. setAttr: function () { return undefined; },
  59. removeAttr: function () { return undefined; },
  60. setContent: function () { return undefined; },
  61. };
  62. // tslint:enable:object-literal-sort-keys
  63. },
  64. enumerable: false,
  65. configurable: true
  66. });
  67. MDCTextFieldHelperTextFoundation.prototype.getId = function () {
  68. return this.adapter.getAttr('id');
  69. };
  70. MDCTextFieldHelperTextFoundation.prototype.isVisible = function () {
  71. return this.adapter.getAttr(strings.ARIA_HIDDEN) !== 'true';
  72. };
  73. /**
  74. * Sets the content of the helper text field.
  75. */
  76. MDCTextFieldHelperTextFoundation.prototype.setContent = function (content) {
  77. this.adapter.setContent(content);
  78. };
  79. MDCTextFieldHelperTextFoundation.prototype.isPersistent = function () {
  80. return this.adapter.hasClass(cssClasses.HELPER_TEXT_PERSISTENT);
  81. };
  82. /**
  83. * @param isPersistent Sets the persistency of the helper text.
  84. */
  85. MDCTextFieldHelperTextFoundation.prototype.setPersistent = function (isPersistent) {
  86. if (isPersistent) {
  87. this.adapter.addClass(cssClasses.HELPER_TEXT_PERSISTENT);
  88. }
  89. else {
  90. this.adapter.removeClass(cssClasses.HELPER_TEXT_PERSISTENT);
  91. }
  92. };
  93. /**
  94. * @return whether the helper text acts as an error validation message.
  95. */
  96. MDCTextFieldHelperTextFoundation.prototype.isValidation = function () {
  97. return this.adapter.hasClass(cssClasses.HELPER_TEXT_VALIDATION_MSG);
  98. };
  99. /**
  100. * @param isValidation True to make the helper text act as an error validation
  101. * message.
  102. */
  103. MDCTextFieldHelperTextFoundation.prototype.setValidation = function (isValidation) {
  104. if (isValidation) {
  105. this.adapter.addClass(cssClasses.HELPER_TEXT_VALIDATION_MSG);
  106. }
  107. else {
  108. this.adapter.removeClass(cssClasses.HELPER_TEXT_VALIDATION_MSG);
  109. }
  110. };
  111. /**
  112. * Makes the helper text visible to the screen reader.
  113. */
  114. MDCTextFieldHelperTextFoundation.prototype.showToScreenReader = function () {
  115. this.adapter.removeAttr(strings.ARIA_HIDDEN);
  116. };
  117. /**
  118. * Sets the validity of the helper text based on the input validity.
  119. */
  120. MDCTextFieldHelperTextFoundation.prototype.setValidity = function (inputIsValid) {
  121. var helperTextIsPersistent = this.adapter.hasClass(cssClasses.HELPER_TEXT_PERSISTENT);
  122. var helperTextIsValidationMsg = this.adapter.hasClass(cssClasses.HELPER_TEXT_VALIDATION_MSG);
  123. var validationMsgNeedsDisplay = helperTextIsValidationMsg && !inputIsValid;
  124. if (validationMsgNeedsDisplay) {
  125. this.showToScreenReader();
  126. // If role is already alert, refresh it to trigger another announcement
  127. // from screenreader.
  128. if (this.adapter.getAttr(strings.ROLE) === 'alert') {
  129. this.refreshAlertRole();
  130. }
  131. else {
  132. this.adapter.setAttr(strings.ROLE, 'alert');
  133. }
  134. }
  135. else {
  136. this.adapter.removeAttr(strings.ROLE);
  137. }
  138. if (!helperTextIsPersistent && !validationMsgNeedsDisplay) {
  139. this.hide();
  140. }
  141. };
  142. /**
  143. * Hides the help text from screen readers.
  144. */
  145. MDCTextFieldHelperTextFoundation.prototype.hide = function () {
  146. this.adapter.setAttr(strings.ARIA_HIDDEN, 'true');
  147. };
  148. MDCTextFieldHelperTextFoundation.prototype.refreshAlertRole = function () {
  149. var _this = this;
  150. this.adapter.removeAttr(strings.ROLE);
  151. requestAnimationFrame(function () {
  152. _this.adapter.setAttr(strings.ROLE, 'alert');
  153. });
  154. };
  155. return MDCTextFieldHelperTextFoundation;
  156. }(MDCFoundation));
  157. export { MDCTextFieldHelperTextFoundation };
  158. // tslint:disable-next-line:no-default-export Needed for backward compatibility with MDC Web v0.44.0 and earlier.
  159. export default MDCTextFieldHelperTextFoundation;
  160. //# sourceMappingURL=foundation.js.map