foundation.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. /**
  2. * @license
  3. * Copyright 2016 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, __values } from "tslib";
  24. import { MDCFoundation } from '@material/base/foundation';
  25. import { ALWAYS_FLOAT_TYPES, cssClasses, numbers, strings, VALIDATION_ATTR_WHITELIST } from './constants';
  26. var POINTERDOWN_EVENTS = ['mousedown', 'touchstart'];
  27. var INTERACTION_EVENTS = ['click', 'keydown'];
  28. /** MDC Text Field Foundation */
  29. var MDCTextFieldFoundation = /** @class */ (function (_super) {
  30. __extends(MDCTextFieldFoundation, _super);
  31. /**
  32. * @param adapter
  33. * @param foundationMap Map from subcomponent names to their subfoundations.
  34. */
  35. function MDCTextFieldFoundation(adapter, foundationMap) {
  36. if (foundationMap === void 0) { foundationMap = {}; }
  37. var _this = _super.call(this, __assign(__assign({}, MDCTextFieldFoundation.defaultAdapter), adapter)) || this;
  38. _this.isFocused = false;
  39. _this.receivedUserInput = false;
  40. _this.useNativeValidation = true;
  41. _this.validateOnValueChange = true;
  42. _this.helperText = foundationMap.helperText;
  43. _this.characterCounter = foundationMap.characterCounter;
  44. _this.leadingIcon = foundationMap.leadingIcon;
  45. _this.trailingIcon = foundationMap.trailingIcon;
  46. _this.valid =
  47. !_this.adapter.hasClass(MDCTextFieldFoundation.cssClasses.INVALID);
  48. _this.inputFocusHandler = function () {
  49. _this.activateFocus();
  50. };
  51. _this.inputBlurHandler = function () {
  52. _this.deactivateFocus();
  53. };
  54. _this.inputInputHandler = function () {
  55. _this.handleInput();
  56. };
  57. _this.setPointerXOffset = function (evt) {
  58. _this.setTransformOrigin(evt);
  59. };
  60. _this.textFieldInteractionHandler = function () {
  61. _this.handleTextFieldInteraction();
  62. };
  63. _this.validationAttributeChangeHandler = function (attributesList) {
  64. _this.handleValidationAttributeChange(attributesList);
  65. };
  66. return _this;
  67. }
  68. Object.defineProperty(MDCTextFieldFoundation, "cssClasses", {
  69. get: function () {
  70. return cssClasses;
  71. },
  72. enumerable: false,
  73. configurable: true
  74. });
  75. Object.defineProperty(MDCTextFieldFoundation, "strings", {
  76. get: function () {
  77. return strings;
  78. },
  79. enumerable: false,
  80. configurable: true
  81. });
  82. Object.defineProperty(MDCTextFieldFoundation, "numbers", {
  83. get: function () {
  84. return numbers;
  85. },
  86. enumerable: false,
  87. configurable: true
  88. });
  89. Object.defineProperty(MDCTextFieldFoundation.prototype, "shouldAlwaysFloat", {
  90. get: function () {
  91. var type = this.getNativeInput().type;
  92. return ALWAYS_FLOAT_TYPES.indexOf(type) >= 0;
  93. },
  94. enumerable: false,
  95. configurable: true
  96. });
  97. Object.defineProperty(MDCTextFieldFoundation.prototype, "shouldFloat", {
  98. get: function () {
  99. return this.shouldAlwaysFloat || this.isFocused || !!this.getValue() ||
  100. this.isBadInput();
  101. },
  102. enumerable: false,
  103. configurable: true
  104. });
  105. Object.defineProperty(MDCTextFieldFoundation.prototype, "shouldShake", {
  106. get: function () {
  107. return !this.isFocused && !this.isValid() && !!this.getValue();
  108. },
  109. enumerable: false,
  110. configurable: true
  111. });
  112. Object.defineProperty(MDCTextFieldFoundation, "defaultAdapter", {
  113. /**
  114. * See {@link MDCTextFieldAdapter} for typing information on parameters and
  115. * return types.
  116. */
  117. get: function () {
  118. // tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
  119. return {
  120. addClass: function () { return undefined; },
  121. removeClass: function () { return undefined; },
  122. hasClass: function () { return true; },
  123. setInputAttr: function () { return undefined; },
  124. removeInputAttr: function () { return undefined; },
  125. registerTextFieldInteractionHandler: function () { return undefined; },
  126. deregisterTextFieldInteractionHandler: function () { return undefined; },
  127. registerInputInteractionHandler: function () { return undefined; },
  128. deregisterInputInteractionHandler: function () { return undefined; },
  129. registerValidationAttributeChangeHandler: function () {
  130. return new MutationObserver(function () { return undefined; });
  131. },
  132. deregisterValidationAttributeChangeHandler: function () { return undefined; },
  133. getNativeInput: function () { return null; },
  134. isFocused: function () { return false; },
  135. activateLineRipple: function () { return undefined; },
  136. deactivateLineRipple: function () { return undefined; },
  137. setLineRippleTransformOrigin: function () { return undefined; },
  138. shakeLabel: function () { return undefined; },
  139. floatLabel: function () { return undefined; },
  140. setLabelRequired: function () { return undefined; },
  141. hasLabel: function () { return false; },
  142. getLabelWidth: function () { return 0; },
  143. hasOutline: function () { return false; },
  144. notchOutline: function () { return undefined; },
  145. closeOutline: function () { return undefined; },
  146. };
  147. // tslint:enable:object-literal-sort-keys
  148. },
  149. enumerable: false,
  150. configurable: true
  151. });
  152. MDCTextFieldFoundation.prototype.init = function () {
  153. var e_1, _a, e_2, _b;
  154. if (this.adapter.hasLabel() && this.getNativeInput().required) {
  155. this.adapter.setLabelRequired(true);
  156. }
  157. if (this.adapter.isFocused()) {
  158. this.inputFocusHandler();
  159. }
  160. else if (this.adapter.hasLabel() && this.shouldFloat) {
  161. this.notchOutline(true);
  162. this.adapter.floatLabel(true);
  163. this.styleFloating(true);
  164. }
  165. this.adapter.registerInputInteractionHandler('focus', this.inputFocusHandler);
  166. this.adapter.registerInputInteractionHandler('blur', this.inputBlurHandler);
  167. this.adapter.registerInputInteractionHandler('input', this.inputInputHandler);
  168. try {
  169. for (var POINTERDOWN_EVENTS_1 = __values(POINTERDOWN_EVENTS), POINTERDOWN_EVENTS_1_1 = POINTERDOWN_EVENTS_1.next(); !POINTERDOWN_EVENTS_1_1.done; POINTERDOWN_EVENTS_1_1 = POINTERDOWN_EVENTS_1.next()) {
  170. var evtType = POINTERDOWN_EVENTS_1_1.value;
  171. this.adapter.registerInputInteractionHandler(evtType, this.setPointerXOffset);
  172. }
  173. }
  174. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  175. finally {
  176. try {
  177. if (POINTERDOWN_EVENTS_1_1 && !POINTERDOWN_EVENTS_1_1.done && (_a = POINTERDOWN_EVENTS_1.return)) _a.call(POINTERDOWN_EVENTS_1);
  178. }
  179. finally { if (e_1) throw e_1.error; }
  180. }
  181. try {
  182. for (var INTERACTION_EVENTS_1 = __values(INTERACTION_EVENTS), INTERACTION_EVENTS_1_1 = INTERACTION_EVENTS_1.next(); !INTERACTION_EVENTS_1_1.done; INTERACTION_EVENTS_1_1 = INTERACTION_EVENTS_1.next()) {
  183. var evtType = INTERACTION_EVENTS_1_1.value;
  184. this.adapter.registerTextFieldInteractionHandler(evtType, this.textFieldInteractionHandler);
  185. }
  186. }
  187. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  188. finally {
  189. try {
  190. if (INTERACTION_EVENTS_1_1 && !INTERACTION_EVENTS_1_1.done && (_b = INTERACTION_EVENTS_1.return)) _b.call(INTERACTION_EVENTS_1);
  191. }
  192. finally { if (e_2) throw e_2.error; }
  193. }
  194. this.validationObserver =
  195. this.adapter.registerValidationAttributeChangeHandler(this.validationAttributeChangeHandler);
  196. this.setcharacterCounter(this.getValue().length);
  197. };
  198. MDCTextFieldFoundation.prototype.destroy = function () {
  199. var e_3, _a, e_4, _b;
  200. this.adapter.deregisterInputInteractionHandler('focus', this.inputFocusHandler);
  201. this.adapter.deregisterInputInteractionHandler('blur', this.inputBlurHandler);
  202. this.adapter.deregisterInputInteractionHandler('input', this.inputInputHandler);
  203. try {
  204. for (var POINTERDOWN_EVENTS_2 = __values(POINTERDOWN_EVENTS), POINTERDOWN_EVENTS_2_1 = POINTERDOWN_EVENTS_2.next(); !POINTERDOWN_EVENTS_2_1.done; POINTERDOWN_EVENTS_2_1 = POINTERDOWN_EVENTS_2.next()) {
  205. var evtType = POINTERDOWN_EVENTS_2_1.value;
  206. this.adapter.deregisterInputInteractionHandler(evtType, this.setPointerXOffset);
  207. }
  208. }
  209. catch (e_3_1) { e_3 = { error: e_3_1 }; }
  210. finally {
  211. try {
  212. if (POINTERDOWN_EVENTS_2_1 && !POINTERDOWN_EVENTS_2_1.done && (_a = POINTERDOWN_EVENTS_2.return)) _a.call(POINTERDOWN_EVENTS_2);
  213. }
  214. finally { if (e_3) throw e_3.error; }
  215. }
  216. try {
  217. for (var INTERACTION_EVENTS_2 = __values(INTERACTION_EVENTS), INTERACTION_EVENTS_2_1 = INTERACTION_EVENTS_2.next(); !INTERACTION_EVENTS_2_1.done; INTERACTION_EVENTS_2_1 = INTERACTION_EVENTS_2.next()) {
  218. var evtType = INTERACTION_EVENTS_2_1.value;
  219. this.adapter.deregisterTextFieldInteractionHandler(evtType, this.textFieldInteractionHandler);
  220. }
  221. }
  222. catch (e_4_1) { e_4 = { error: e_4_1 }; }
  223. finally {
  224. try {
  225. if (INTERACTION_EVENTS_2_1 && !INTERACTION_EVENTS_2_1.done && (_b = INTERACTION_EVENTS_2.return)) _b.call(INTERACTION_EVENTS_2);
  226. }
  227. finally { if (e_4) throw e_4.error; }
  228. }
  229. this.adapter.deregisterValidationAttributeChangeHandler(this.validationObserver);
  230. };
  231. /**
  232. * Handles user interactions with the Text Field.
  233. */
  234. MDCTextFieldFoundation.prototype.handleTextFieldInteraction = function () {
  235. var nativeInput = this.adapter.getNativeInput();
  236. if (nativeInput && nativeInput.disabled) {
  237. return;
  238. }
  239. this.receivedUserInput = true;
  240. };
  241. /**
  242. * Handles validation attribute changes
  243. */
  244. MDCTextFieldFoundation.prototype.handleValidationAttributeChange = function (attributesList) {
  245. var _this = this;
  246. attributesList.some(function (attributeName) {
  247. if (VALIDATION_ATTR_WHITELIST.indexOf(attributeName) > -1) {
  248. _this.styleValidity(true);
  249. _this.adapter.setLabelRequired(_this.getNativeInput().required);
  250. return true;
  251. }
  252. return false;
  253. });
  254. if (attributesList.indexOf('maxlength') > -1) {
  255. this.setcharacterCounter(this.getValue().length);
  256. }
  257. };
  258. /**
  259. * Opens/closes the notched outline.
  260. */
  261. MDCTextFieldFoundation.prototype.notchOutline = function (openNotch) {
  262. if (!this.adapter.hasOutline() || !this.adapter.hasLabel()) {
  263. return;
  264. }
  265. if (openNotch) {
  266. var labelWidth = this.adapter.getLabelWidth() * numbers.LABEL_SCALE;
  267. this.adapter.notchOutline(labelWidth);
  268. }
  269. else {
  270. this.adapter.closeOutline();
  271. }
  272. };
  273. /**
  274. * Activates the text field focus state.
  275. */
  276. MDCTextFieldFoundation.prototype.activateFocus = function () {
  277. this.isFocused = true;
  278. this.styleFocused(this.isFocused);
  279. this.adapter.activateLineRipple();
  280. if (this.adapter.hasLabel()) {
  281. this.notchOutline(this.shouldFloat);
  282. this.adapter.floatLabel(this.shouldFloat);
  283. this.styleFloating(this.shouldFloat);
  284. this.adapter.shakeLabel(this.shouldShake);
  285. }
  286. if (this.helperText &&
  287. (this.helperText.isPersistent() || !this.helperText.isValidation() ||
  288. !this.valid)) {
  289. this.helperText.showToScreenReader();
  290. }
  291. };
  292. /**
  293. * Sets the line ripple's transform origin, so that the line ripple activate
  294. * animation will animate out from the user's click location.
  295. */
  296. MDCTextFieldFoundation.prototype.setTransformOrigin = function (evt) {
  297. if (this.isDisabled() || this.adapter.hasOutline()) {
  298. return;
  299. }
  300. var touches = evt.touches;
  301. var targetEvent = touches ? touches[0] : evt;
  302. var targetClientRect = targetEvent.target.getBoundingClientRect();
  303. var normalizedX = targetEvent.clientX - targetClientRect.left;
  304. this.adapter.setLineRippleTransformOrigin(normalizedX);
  305. };
  306. /**
  307. * Handles input change of text input and text area.
  308. */
  309. MDCTextFieldFoundation.prototype.handleInput = function () {
  310. this.autoCompleteFocus();
  311. this.setcharacterCounter(this.getValue().length);
  312. };
  313. /**
  314. * Activates the Text Field's focus state in cases when the input value
  315. * changes without user input (e.g. programmatically).
  316. */
  317. MDCTextFieldFoundation.prototype.autoCompleteFocus = function () {
  318. if (!this.receivedUserInput) {
  319. this.activateFocus();
  320. }
  321. };
  322. /**
  323. * Deactivates the Text Field's focus state.
  324. */
  325. MDCTextFieldFoundation.prototype.deactivateFocus = function () {
  326. this.isFocused = false;
  327. this.adapter.deactivateLineRipple();
  328. var isValid = this.isValid();
  329. this.styleValidity(isValid);
  330. this.styleFocused(this.isFocused);
  331. if (this.adapter.hasLabel()) {
  332. this.notchOutline(this.shouldFloat);
  333. this.adapter.floatLabel(this.shouldFloat);
  334. this.styleFloating(this.shouldFloat);
  335. this.adapter.shakeLabel(this.shouldShake);
  336. }
  337. if (!this.shouldFloat) {
  338. this.receivedUserInput = false;
  339. }
  340. };
  341. MDCTextFieldFoundation.prototype.getValue = function () {
  342. return this.getNativeInput().value;
  343. };
  344. /**
  345. * @param value The value to set on the input Element.
  346. */
  347. MDCTextFieldFoundation.prototype.setValue = function (value) {
  348. // Prevent Safari from moving the caret to the end of the input when the
  349. // value has not changed.
  350. if (this.getValue() !== value) {
  351. this.getNativeInput().value = value;
  352. }
  353. this.setcharacterCounter(value.length);
  354. if (this.validateOnValueChange) {
  355. var isValid = this.isValid();
  356. this.styleValidity(isValid);
  357. }
  358. if (this.adapter.hasLabel()) {
  359. this.notchOutline(this.shouldFloat);
  360. this.adapter.floatLabel(this.shouldFloat);
  361. this.styleFloating(this.shouldFloat);
  362. if (this.validateOnValueChange) {
  363. this.adapter.shakeLabel(this.shouldShake);
  364. }
  365. }
  366. };
  367. /**
  368. * @return The custom validity state, if set; otherwise, the result of a
  369. * native validity check.
  370. */
  371. MDCTextFieldFoundation.prototype.isValid = function () {
  372. return this.useNativeValidation ? this.isNativeInputValid() : this.valid;
  373. };
  374. /**
  375. * @param isValid Sets the custom validity state of the Text Field.
  376. */
  377. MDCTextFieldFoundation.prototype.setValid = function (isValid) {
  378. this.valid = isValid;
  379. this.styleValidity(isValid);
  380. var shouldShake = !isValid && !this.isFocused && !!this.getValue();
  381. if (this.adapter.hasLabel()) {
  382. this.adapter.shakeLabel(shouldShake);
  383. }
  384. };
  385. /**
  386. * @param shouldValidate Whether or not validity should be updated on
  387. * value change.
  388. */
  389. MDCTextFieldFoundation.prototype.setValidateOnValueChange = function (shouldValidate) {
  390. this.validateOnValueChange = shouldValidate;
  391. };
  392. /**
  393. * @return Whether or not validity should be updated on value change. `true`
  394. * by default.
  395. */
  396. MDCTextFieldFoundation.prototype.getValidateOnValueChange = function () {
  397. return this.validateOnValueChange;
  398. };
  399. /**
  400. * Enables or disables the use of native validation. Use this for custom
  401. * validation.
  402. * @param useNativeValidation Set this to false to ignore native input
  403. * validation.
  404. */
  405. MDCTextFieldFoundation.prototype.setUseNativeValidation = function (useNativeValidation) {
  406. this.useNativeValidation = useNativeValidation;
  407. };
  408. MDCTextFieldFoundation.prototype.isDisabled = function () {
  409. return this.getNativeInput().disabled;
  410. };
  411. /**
  412. * @param disabled Sets the text-field disabled or enabled.
  413. */
  414. MDCTextFieldFoundation.prototype.setDisabled = function (disabled) {
  415. this.getNativeInput().disabled = disabled;
  416. this.styleDisabled(disabled);
  417. };
  418. /**
  419. * @param content Sets the content of the helper text.
  420. */
  421. MDCTextFieldFoundation.prototype.setHelperTextContent = function (content) {
  422. if (this.helperText) {
  423. this.helperText.setContent(content);
  424. }
  425. };
  426. /**
  427. * Sets the aria label of the leading icon.
  428. */
  429. MDCTextFieldFoundation.prototype.setLeadingIconAriaLabel = function (label) {
  430. if (this.leadingIcon) {
  431. this.leadingIcon.setAriaLabel(label);
  432. }
  433. };
  434. /**
  435. * Sets the text content of the leading icon.
  436. */
  437. MDCTextFieldFoundation.prototype.setLeadingIconContent = function (content) {
  438. if (this.leadingIcon) {
  439. this.leadingIcon.setContent(content);
  440. }
  441. };
  442. /**
  443. * Sets the aria label of the trailing icon.
  444. */
  445. MDCTextFieldFoundation.prototype.setTrailingIconAriaLabel = function (label) {
  446. if (this.trailingIcon) {
  447. this.trailingIcon.setAriaLabel(label);
  448. }
  449. };
  450. /**
  451. * Sets the text content of the trailing icon.
  452. */
  453. MDCTextFieldFoundation.prototype.setTrailingIconContent = function (content) {
  454. if (this.trailingIcon) {
  455. this.trailingIcon.setContent(content);
  456. }
  457. };
  458. /**
  459. * Sets character counter values that shows characters used and the total
  460. * character limit.
  461. */
  462. MDCTextFieldFoundation.prototype.setcharacterCounter = function (currentLength) {
  463. if (!this.characterCounter) {
  464. return;
  465. }
  466. var maxLength = this.getNativeInput().maxLength;
  467. if (maxLength === -1) {
  468. throw new Error('MDCTextFieldFoundation: Expected maxlength html property on text input or textarea.');
  469. }
  470. this.characterCounter.setCounterValue(currentLength, maxLength);
  471. };
  472. /**
  473. * @return True if the Text Field input fails in converting the user-supplied
  474. * value.
  475. */
  476. MDCTextFieldFoundation.prototype.isBadInput = function () {
  477. // The badInput property is not supported in IE 11 💩.
  478. return this.getNativeInput().validity.badInput || false;
  479. };
  480. /**
  481. * @return The result of native validity checking (ValidityState.valid).
  482. */
  483. MDCTextFieldFoundation.prototype.isNativeInputValid = function () {
  484. return this.getNativeInput().validity.valid;
  485. };
  486. /**
  487. * Styles the component based on the validity state.
  488. */
  489. MDCTextFieldFoundation.prototype.styleValidity = function (isValid) {
  490. var INVALID = MDCTextFieldFoundation.cssClasses.INVALID;
  491. if (isValid) {
  492. this.adapter.removeClass(INVALID);
  493. }
  494. else {
  495. this.adapter.addClass(INVALID);
  496. }
  497. if (this.helperText) {
  498. this.helperText.setValidity(isValid);
  499. // We dynamically set or unset aria-describedby for validation helper text
  500. // only, based on whether the field is valid
  501. var helperTextValidation = this.helperText.isValidation();
  502. if (!helperTextValidation) {
  503. return;
  504. }
  505. var helperTextVisible = this.helperText.isVisible();
  506. var helperTextId = this.helperText.getId();
  507. if (helperTextVisible && helperTextId) {
  508. this.adapter.setInputAttr(strings.ARIA_DESCRIBEDBY, helperTextId);
  509. }
  510. else {
  511. this.adapter.removeInputAttr(strings.ARIA_DESCRIBEDBY);
  512. }
  513. }
  514. };
  515. /**
  516. * Styles the component based on the focused state.
  517. */
  518. MDCTextFieldFoundation.prototype.styleFocused = function (isFocused) {
  519. var FOCUSED = MDCTextFieldFoundation.cssClasses.FOCUSED;
  520. if (isFocused) {
  521. this.adapter.addClass(FOCUSED);
  522. }
  523. else {
  524. this.adapter.removeClass(FOCUSED);
  525. }
  526. };
  527. /**
  528. * Styles the component based on the disabled state.
  529. */
  530. MDCTextFieldFoundation.prototype.styleDisabled = function (isDisabled) {
  531. var _a = MDCTextFieldFoundation.cssClasses, DISABLED = _a.DISABLED, INVALID = _a.INVALID;
  532. if (isDisabled) {
  533. this.adapter.addClass(DISABLED);
  534. this.adapter.removeClass(INVALID);
  535. }
  536. else {
  537. this.adapter.removeClass(DISABLED);
  538. }
  539. if (this.leadingIcon) {
  540. this.leadingIcon.setDisabled(isDisabled);
  541. }
  542. if (this.trailingIcon) {
  543. this.trailingIcon.setDisabled(isDisabled);
  544. }
  545. };
  546. /**
  547. * Styles the component based on the label floating state.
  548. */
  549. MDCTextFieldFoundation.prototype.styleFloating = function (isFloating) {
  550. var LABEL_FLOATING = MDCTextFieldFoundation.cssClasses.LABEL_FLOATING;
  551. if (isFloating) {
  552. this.adapter.addClass(LABEL_FLOATING);
  553. }
  554. else {
  555. this.adapter.removeClass(LABEL_FLOATING);
  556. }
  557. };
  558. /**
  559. * @return The native text input element from the host environment, or an
  560. * object with the same shape for unit tests.
  561. */
  562. MDCTextFieldFoundation.prototype.getNativeInput = function () {
  563. // this.adapter may be undefined in foundation unit tests. This happens when
  564. // testdouble is creating a mock object and invokes the
  565. // shouldShake/shouldFloat getters (which in turn call getValue(), which
  566. // calls this method) before init() has been called from the MDCTextField
  567. // constructor. To work around that issue, we return a dummy object.
  568. var nativeInput = this.adapter ? this.adapter.getNativeInput() : null;
  569. return nativeInput || {
  570. disabled: false,
  571. maxLength: -1,
  572. required: false,
  573. type: 'input',
  574. validity: {
  575. badInput: false,
  576. valid: true,
  577. },
  578. value: '',
  579. };
  580. };
  581. return MDCTextFieldFoundation;
  582. }(MDCFoundation));
  583. export { MDCTextFieldFoundation };
  584. // tslint:disable-next-line:no-default-export Needed for backward compatibility with MDC Web v0.44.0 and earlier.
  585. export default MDCTextFieldFoundation;
  586. //# sourceMappingURL=foundation.js.map