foundation.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /**
  2. * @license
  3. * Copyright 2018 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 Select Helper Text Foundation */
  27. var MDCSelectHelperTextFoundation = /** @class */ (function (_super) {
  28. __extends(MDCSelectHelperTextFoundation, _super);
  29. function MDCSelectHelperTextFoundation(adapter) {
  30. return _super.call(this, __assign(__assign({}, MDCSelectHelperTextFoundation.defaultAdapter), adapter)) || this;
  31. }
  32. Object.defineProperty(MDCSelectHelperTextFoundation, "cssClasses", {
  33. get: function () {
  34. return cssClasses;
  35. },
  36. enumerable: false,
  37. configurable: true
  38. });
  39. Object.defineProperty(MDCSelectHelperTextFoundation, "strings", {
  40. get: function () {
  41. return strings;
  42. },
  43. enumerable: false,
  44. configurable: true
  45. });
  46. Object.defineProperty(MDCSelectHelperTextFoundation, "defaultAdapter", {
  47. /**
  48. * See {@link MDCSelectHelperTextAdapter} for typing information on parameters
  49. * 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. setAttr: function () { return undefined; },
  58. getAttr: function () { return null; },
  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. /**
  68. * @return The ID of the helper text, or null if none is set.
  69. */
  70. MDCSelectHelperTextFoundation.prototype.getId = function () {
  71. return this.adapter.getAttr('id');
  72. };
  73. /**
  74. * @return Whether the helper text is currently visible.
  75. */
  76. MDCSelectHelperTextFoundation.prototype.isVisible = function () {
  77. return this.adapter.getAttr(strings.ARIA_HIDDEN) !== 'true';
  78. };
  79. /**
  80. * Sets the content of the helper text field.
  81. */
  82. MDCSelectHelperTextFoundation.prototype.setContent = function (content) {
  83. this.adapter.setContent(content);
  84. };
  85. /**
  86. * Sets the helper text to act as a validation message.
  87. * By default, validation messages are hidden when the select is valid and
  88. * visible when the select is invalid.
  89. *
  90. * @param isValidation True to make the helper text act as an error validation
  91. * message.
  92. */
  93. MDCSelectHelperTextFoundation.prototype.setValidation = function (isValidation) {
  94. if (isValidation) {
  95. this.adapter.addClass(cssClasses.HELPER_TEXT_VALIDATION_MSG);
  96. }
  97. else {
  98. this.adapter.removeClass(cssClasses.HELPER_TEXT_VALIDATION_MSG);
  99. }
  100. };
  101. /**
  102. * Sets the persistency of the validation helper text.
  103. * This keeps the validation message visible even if the select is valid,
  104. * though it will be displayed in the normal (grey) color.
  105. */
  106. MDCSelectHelperTextFoundation.prototype.setValidationMsgPersistent = function (isPersistent) {
  107. if (isPersistent) {
  108. this.adapter.addClass(cssClasses.HELPER_TEXT_VALIDATION_MSG_PERSISTENT);
  109. }
  110. else {
  111. this.adapter.removeClass(cssClasses.HELPER_TEXT_VALIDATION_MSG_PERSISTENT);
  112. }
  113. };
  114. /**
  115. * @return Whether the helper text acts as a validation message.
  116. * By default, validation messages are hidden when the select is valid and
  117. * visible when the select is invalid.
  118. */
  119. MDCSelectHelperTextFoundation.prototype.getIsValidation = function () {
  120. return this.adapter.hasClass(cssClasses.HELPER_TEXT_VALIDATION_MSG);
  121. };
  122. /**
  123. * @return Whether the validation helper text persists even if the input is
  124. * valid. If it is, it will be displayed in the normal (grey) color.
  125. */
  126. MDCSelectHelperTextFoundation.prototype.getIsValidationMsgPersistent = function () {
  127. return this.adapter.hasClass(cssClasses.HELPER_TEXT_VALIDATION_MSG_PERSISTENT);
  128. };
  129. /**
  130. * When acting as a validation message, shows/hides the helper text and
  131. * triggers alerts as necessary based on the select's validity.
  132. */
  133. MDCSelectHelperTextFoundation.prototype.setValidity = function (selectIsValid) {
  134. var isValidationMsg = this.adapter.hasClass(cssClasses.HELPER_TEXT_VALIDATION_MSG);
  135. if (!isValidationMsg) {
  136. // Non-validating helper-text is always displayed and does not participate
  137. // in validation logic.
  138. return;
  139. }
  140. var isPersistentValidationMsg = this.adapter.hasClass(cssClasses.HELPER_TEXT_VALIDATION_MSG_PERSISTENT);
  141. // Validating helper text is displayed if select is invalid, unless it is
  142. // set as persistent, in which case it always displays.
  143. var msgShouldDisplay = !selectIsValid || isPersistentValidationMsg;
  144. if (msgShouldDisplay) {
  145. this.showToScreenReader();
  146. // In addition to displaying, also trigger an alert if the select
  147. // has become invalid.
  148. if (!selectIsValid) {
  149. this.adapter.setAttr(strings.ROLE, 'alert');
  150. }
  151. else {
  152. this.adapter.removeAttr(strings.ROLE);
  153. }
  154. return;
  155. }
  156. // Hide everything.
  157. this.adapter.removeAttr(strings.ROLE);
  158. this.hide();
  159. };
  160. /**
  161. * Makes the helper text visible to screen readers.
  162. */
  163. MDCSelectHelperTextFoundation.prototype.showToScreenReader = function () {
  164. this.adapter.removeAttr(strings.ARIA_HIDDEN);
  165. };
  166. /**
  167. * Hides the help text from screen readers.
  168. */
  169. MDCSelectHelperTextFoundation.prototype.hide = function () {
  170. this.adapter.setAttr(strings.ARIA_HIDDEN, 'true');
  171. };
  172. return MDCSelectHelperTextFoundation;
  173. }(MDCFoundation));
  174. export { MDCSelectHelperTextFoundation };
  175. // tslint:disable-next-line:no-default-export Needed for backward compatibility with MDC Web v0.44.0 and earlier.
  176. export default MDCSelectHelperTextFoundation;
  177. //# sourceMappingURL=foundation.js.map