component.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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 } from "tslib";
  24. import { MDCComponent } from '@material/base/component';
  25. import { applyPassive } from '@material/dom/events';
  26. import * as ponyfill from '@material/dom/ponyfill';
  27. import { MDCFloatingLabel } from '@material/floating-label/component';
  28. import { MDCLineRipple } from '@material/line-ripple/component';
  29. import { MDCNotchedOutline } from '@material/notched-outline/component';
  30. import { MDCRipple } from '@material/ripple/component';
  31. import { MDCRippleFoundation } from '@material/ripple/foundation';
  32. import { MDCTextFieldCharacterCounter } from './character-counter/component';
  33. import { MDCTextFieldCharacterCounterFoundation } from './character-counter/foundation';
  34. import { cssClasses, strings } from './constants';
  35. import { MDCTextFieldFoundation } from './foundation';
  36. import { MDCTextFieldHelperText } from './helper-text/component';
  37. import { MDCTextFieldHelperTextFoundation } from './helper-text/foundation';
  38. import { MDCTextFieldIcon } from './icon/component';
  39. /** MDC Text Field */
  40. var MDCTextField = /** @class */ (function (_super) {
  41. __extends(MDCTextField, _super);
  42. function MDCTextField() {
  43. return _super !== null && _super.apply(this, arguments) || this;
  44. }
  45. MDCTextField.attachTo = function (root) {
  46. return new MDCTextField(root);
  47. };
  48. MDCTextField.prototype.initialize = function (rippleFactory, lineRippleFactory, helperTextFactory, characterCounterFactory, iconFactory, labelFactory, outlineFactory) {
  49. if (rippleFactory === void 0) { rippleFactory = function (el, foundation) { return new MDCRipple(el, foundation); }; }
  50. if (lineRippleFactory === void 0) { lineRippleFactory = function (el) { return new MDCLineRipple(el); }; }
  51. if (helperTextFactory === void 0) { helperTextFactory = function (el) {
  52. return new MDCTextFieldHelperText(el);
  53. }; }
  54. if (characterCounterFactory === void 0) { characterCounterFactory = function (el) {
  55. return new MDCTextFieldCharacterCounter(el);
  56. }; }
  57. if (iconFactory === void 0) { iconFactory = function (el) { return new MDCTextFieldIcon(el); }; }
  58. if (labelFactory === void 0) { labelFactory = function (el) { return new MDCFloatingLabel(el); }; }
  59. if (outlineFactory === void 0) { outlineFactory = function (el) { return new MDCNotchedOutline(el); }; }
  60. this.input =
  61. this.root.querySelector(strings.INPUT_SELECTOR);
  62. var labelElement = this.root.querySelector(strings.LABEL_SELECTOR);
  63. this.label = labelElement ? labelFactory(labelElement) : null;
  64. var lineRippleElement = this.root.querySelector(strings.LINE_RIPPLE_SELECTOR);
  65. this.lineRipple =
  66. lineRippleElement ? lineRippleFactory(lineRippleElement) : null;
  67. var outlineElement = this.root.querySelector(strings.OUTLINE_SELECTOR);
  68. this.outline = outlineElement ? outlineFactory(outlineElement) : null;
  69. // Helper text
  70. var helperTextStrings = MDCTextFieldHelperTextFoundation.strings;
  71. var nextElementSibling = this.root.nextElementSibling;
  72. var hasHelperLine = (nextElementSibling &&
  73. nextElementSibling.classList.contains(cssClasses.HELPER_LINE));
  74. var helperTextEl = hasHelperLine && nextElementSibling &&
  75. nextElementSibling.querySelector(helperTextStrings.ROOT_SELECTOR);
  76. this.helperText = helperTextEl ? helperTextFactory(helperTextEl) : null;
  77. // Character counter
  78. var characterCounterStrings = MDCTextFieldCharacterCounterFoundation.strings;
  79. var characterCounterEl = this.root.querySelector(characterCounterStrings.ROOT_SELECTOR);
  80. // If character counter is not found in root element search in sibling
  81. // element.
  82. if (!characterCounterEl && hasHelperLine && nextElementSibling) {
  83. characterCounterEl = nextElementSibling.querySelector(characterCounterStrings.ROOT_SELECTOR);
  84. }
  85. this.characterCounter =
  86. characterCounterEl ? characterCounterFactory(characterCounterEl) : null;
  87. // Leading icon
  88. var leadingIconEl = this.root.querySelector(strings.LEADING_ICON_SELECTOR);
  89. this.leadingIcon = leadingIconEl ? iconFactory(leadingIconEl) : null;
  90. // Trailing icon
  91. var trailingIconEl = this.root.querySelector(strings.TRAILING_ICON_SELECTOR);
  92. this.trailingIcon = trailingIconEl ? iconFactory(trailingIconEl) : null;
  93. // Prefix and Suffix
  94. this.prefix = this.root.querySelector(strings.PREFIX_SELECTOR);
  95. this.suffix = this.root.querySelector(strings.SUFFIX_SELECTOR);
  96. this.ripple = this.createRipple(rippleFactory);
  97. };
  98. MDCTextField.prototype.destroy = function () {
  99. if (this.ripple) {
  100. this.ripple.destroy();
  101. }
  102. if (this.lineRipple) {
  103. this.lineRipple.destroy();
  104. }
  105. if (this.helperText) {
  106. this.helperText.destroy();
  107. }
  108. if (this.characterCounter) {
  109. this.characterCounter.destroy();
  110. }
  111. if (this.leadingIcon) {
  112. this.leadingIcon.destroy();
  113. }
  114. if (this.trailingIcon) {
  115. this.trailingIcon.destroy();
  116. }
  117. if (this.label) {
  118. this.label.destroy();
  119. }
  120. if (this.outline) {
  121. this.outline.destroy();
  122. }
  123. _super.prototype.destroy.call(this);
  124. };
  125. /**
  126. * Initializes the Text Field's internal state based on the environment's
  127. * state.
  128. */
  129. MDCTextField.prototype.initialSyncWithDOM = function () {
  130. this.disabled = this.input.disabled;
  131. };
  132. Object.defineProperty(MDCTextField.prototype, "value", {
  133. get: function () {
  134. return this.foundation.getValue();
  135. },
  136. /**
  137. * @param value The value to set on the input.
  138. */
  139. set: function (value) {
  140. this.foundation.setValue(value);
  141. },
  142. enumerable: false,
  143. configurable: true
  144. });
  145. Object.defineProperty(MDCTextField.prototype, "disabled", {
  146. get: function () {
  147. return this.foundation.isDisabled();
  148. },
  149. /**
  150. * @param disabled Sets the Text Field disabled or enabled.
  151. */
  152. set: function (disabled) {
  153. this.foundation.setDisabled(disabled);
  154. },
  155. enumerable: false,
  156. configurable: true
  157. });
  158. Object.defineProperty(MDCTextField.prototype, "valid", {
  159. get: function () {
  160. return this.foundation.isValid();
  161. },
  162. /**
  163. * @param valid Sets the Text Field valid or invalid.
  164. */
  165. set: function (valid) {
  166. this.foundation.setValid(valid);
  167. },
  168. enumerable: false,
  169. configurable: true
  170. });
  171. Object.defineProperty(MDCTextField.prototype, "required", {
  172. get: function () {
  173. return this.input.required;
  174. },
  175. /**
  176. * @param required Sets the Text Field to required.
  177. */
  178. set: function (required) {
  179. this.input.required = required;
  180. },
  181. enumerable: false,
  182. configurable: true
  183. });
  184. Object.defineProperty(MDCTextField.prototype, "pattern", {
  185. get: function () {
  186. return this.input.pattern;
  187. },
  188. /**
  189. * @param pattern Sets the input element's validation pattern.
  190. */
  191. set: function (pattern) {
  192. this.input.pattern = pattern;
  193. },
  194. enumerable: false,
  195. configurable: true
  196. });
  197. Object.defineProperty(MDCTextField.prototype, "minLength", {
  198. get: function () {
  199. return this.input.minLength;
  200. },
  201. /**
  202. * @param minLength Sets the input element's minLength.
  203. */
  204. set: function (minLength) {
  205. this.input.minLength = minLength;
  206. },
  207. enumerable: false,
  208. configurable: true
  209. });
  210. Object.defineProperty(MDCTextField.prototype, "maxLength", {
  211. get: function () {
  212. return this.input.maxLength;
  213. },
  214. /**
  215. * @param maxLength Sets the input element's maxLength.
  216. */
  217. set: function (maxLength) {
  218. // Chrome throws exception if maxLength is set to a value less than zero
  219. if (maxLength < 0) {
  220. this.input.removeAttribute('maxLength');
  221. }
  222. else {
  223. this.input.maxLength = maxLength;
  224. }
  225. },
  226. enumerable: false,
  227. configurable: true
  228. });
  229. Object.defineProperty(MDCTextField.prototype, "min", {
  230. get: function () {
  231. return this.input.min;
  232. },
  233. /**
  234. * @param min Sets the input element's min.
  235. */
  236. set: function (min) {
  237. this.input.min = min;
  238. },
  239. enumerable: false,
  240. configurable: true
  241. });
  242. Object.defineProperty(MDCTextField.prototype, "max", {
  243. get: function () {
  244. return this.input.max;
  245. },
  246. /**
  247. * @param max Sets the input element's max.
  248. */
  249. set: function (max) {
  250. this.input.max = max;
  251. },
  252. enumerable: false,
  253. configurable: true
  254. });
  255. Object.defineProperty(MDCTextField.prototype, "step", {
  256. get: function () {
  257. return this.input.step;
  258. },
  259. /**
  260. * @param step Sets the input element's step.
  261. */
  262. set: function (step) {
  263. this.input.step = step;
  264. },
  265. enumerable: false,
  266. configurable: true
  267. });
  268. Object.defineProperty(MDCTextField.prototype, "helperTextContent", {
  269. /**
  270. * Sets the helper text element content.
  271. */
  272. set: function (content) {
  273. this.foundation.setHelperTextContent(content);
  274. },
  275. enumerable: false,
  276. configurable: true
  277. });
  278. Object.defineProperty(MDCTextField.prototype, "leadingIconAriaLabel", {
  279. /**
  280. * Sets the aria label of the leading icon.
  281. */
  282. set: function (label) {
  283. this.foundation.setLeadingIconAriaLabel(label);
  284. },
  285. enumerable: false,
  286. configurable: true
  287. });
  288. Object.defineProperty(MDCTextField.prototype, "leadingIconContent", {
  289. /**
  290. * Sets the text content of the leading icon.
  291. */
  292. set: function (content) {
  293. this.foundation.setLeadingIconContent(content);
  294. },
  295. enumerable: false,
  296. configurable: true
  297. });
  298. Object.defineProperty(MDCTextField.prototype, "trailingIconAriaLabel", {
  299. /**
  300. * Sets the aria label of the trailing icon.
  301. */
  302. set: function (label) {
  303. this.foundation.setTrailingIconAriaLabel(label);
  304. },
  305. enumerable: false,
  306. configurable: true
  307. });
  308. Object.defineProperty(MDCTextField.prototype, "trailingIconContent", {
  309. /**
  310. * Sets the text content of the trailing icon.
  311. */
  312. set: function (content) {
  313. this.foundation.setTrailingIconContent(content);
  314. },
  315. enumerable: false,
  316. configurable: true
  317. });
  318. Object.defineProperty(MDCTextField.prototype, "useNativeValidation", {
  319. /**
  320. * Enables or disables the use of native validation. Use this for custom
  321. * validation.
  322. * @param useNativeValidation Set this to false to ignore native input
  323. * validation.
  324. */
  325. set: function (useNativeValidation) {
  326. this.foundation.setUseNativeValidation(useNativeValidation);
  327. },
  328. enumerable: false,
  329. configurable: true
  330. });
  331. Object.defineProperty(MDCTextField.prototype, "prefixText", {
  332. /**
  333. * Gets the text content of the prefix, or null if it does not exist.
  334. */
  335. get: function () {
  336. return this.prefix ? this.prefix.textContent : null;
  337. },
  338. /**
  339. * Sets the text content of the prefix, if it exists.
  340. */
  341. set: function (prefixText) {
  342. if (this.prefix) {
  343. this.prefix.textContent = prefixText;
  344. }
  345. },
  346. enumerable: false,
  347. configurable: true
  348. });
  349. Object.defineProperty(MDCTextField.prototype, "suffixText", {
  350. /**
  351. * Gets the text content of the suffix, or null if it does not exist.
  352. */
  353. get: function () {
  354. return this.suffix ? this.suffix.textContent : null;
  355. },
  356. /**
  357. * Sets the text content of the suffix, if it exists.
  358. */
  359. set: function (suffixText) {
  360. if (this.suffix) {
  361. this.suffix.textContent = suffixText;
  362. }
  363. },
  364. enumerable: false,
  365. configurable: true
  366. });
  367. /**
  368. * Focuses the input element.
  369. */
  370. MDCTextField.prototype.focus = function () {
  371. this.input.focus();
  372. };
  373. /**
  374. * Recomputes the outline SVG path for the outline element.
  375. */
  376. MDCTextField.prototype.layout = function () {
  377. var openNotch = this.foundation.shouldFloat;
  378. this.foundation.notchOutline(openNotch);
  379. };
  380. MDCTextField.prototype.getDefaultFoundation = function () {
  381. // DO NOT INLINE this variable. For backward compatibility, foundations take
  382. // a Partial<MDCFooAdapter>. To ensure we don't accidentally omit any
  383. // methods, we need a separate, strongly typed adapter variable.
  384. // tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
  385. var adapter = __assign(__assign(__assign(__assign(__assign({}, this.getRootAdapterMethods()), this.getInputAdapterMethods()), this.getLabelAdapterMethods()), this.getLineRippleAdapterMethods()), this.getOutlineAdapterMethods());
  386. // tslint:enable:object-literal-sort-keys
  387. return new MDCTextFieldFoundation(adapter, this.getFoundationMap());
  388. };
  389. MDCTextField.prototype.getRootAdapterMethods = function () {
  390. var _this = this;
  391. // tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
  392. return {
  393. addClass: function (className) {
  394. _this.root.classList.add(className);
  395. },
  396. removeClass: function (className) {
  397. _this.root.classList.remove(className);
  398. },
  399. hasClass: function (className) { return _this.root.classList.contains(className); },
  400. registerTextFieldInteractionHandler: function (evtType, handler) {
  401. _this.listen(evtType, handler);
  402. },
  403. deregisterTextFieldInteractionHandler: function (evtType, handler) {
  404. _this.unlisten(evtType, handler);
  405. },
  406. registerValidationAttributeChangeHandler: function (handler) {
  407. var getAttributesList = function (mutationsList) {
  408. return mutationsList.map(function (mutation) { return mutation.attributeName; })
  409. .filter(function (attributeName) { return attributeName; });
  410. };
  411. var observer = new MutationObserver(function (mutationsList) {
  412. handler(getAttributesList(mutationsList));
  413. });
  414. var config = { attributes: true };
  415. observer.observe(_this.input, config);
  416. return observer;
  417. },
  418. deregisterValidationAttributeChangeHandler: function (observer) {
  419. observer.disconnect();
  420. },
  421. };
  422. // tslint:enable:object-literal-sort-keys
  423. };
  424. MDCTextField.prototype.getInputAdapterMethods = function () {
  425. var _this = this;
  426. // tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
  427. return {
  428. getNativeInput: function () { return _this.input; },
  429. setInputAttr: function (attr, value) {
  430. _this.safeSetAttribute(_this.input, attr, value);
  431. },
  432. removeInputAttr: function (attr) {
  433. _this.input.removeAttribute(attr);
  434. },
  435. isFocused: function () { return document.activeElement === _this.input; },
  436. registerInputInteractionHandler: function (evtType, handler) {
  437. _this.input.addEventListener(evtType, handler, applyPassive());
  438. },
  439. deregisterInputInteractionHandler: function (evtType, handler) {
  440. _this.input.removeEventListener(evtType, handler, applyPassive());
  441. },
  442. };
  443. // tslint:enable:object-literal-sort-keys
  444. };
  445. MDCTextField.prototype.getLabelAdapterMethods = function () {
  446. var _this = this;
  447. return {
  448. floatLabel: function (shouldFloat) {
  449. _this.label && _this.label.float(shouldFloat);
  450. },
  451. getLabelWidth: function () { return _this.label ? _this.label.getWidth() : 0; },
  452. hasLabel: function () { return Boolean(_this.label); },
  453. shakeLabel: function (shouldShake) {
  454. _this.label && _this.label.shake(shouldShake);
  455. },
  456. setLabelRequired: function (isRequired) {
  457. _this.label && _this.label.setRequired(isRequired);
  458. },
  459. };
  460. };
  461. MDCTextField.prototype.getLineRippleAdapterMethods = function () {
  462. var _this = this;
  463. return {
  464. activateLineRipple: function () {
  465. if (_this.lineRipple) {
  466. _this.lineRipple.activate();
  467. }
  468. },
  469. deactivateLineRipple: function () {
  470. if (_this.lineRipple) {
  471. _this.lineRipple.deactivate();
  472. }
  473. },
  474. setLineRippleTransformOrigin: function (normalizedX) {
  475. if (_this.lineRipple) {
  476. _this.lineRipple.setRippleCenter(normalizedX);
  477. }
  478. },
  479. };
  480. };
  481. MDCTextField.prototype.getOutlineAdapterMethods = function () {
  482. var _this = this;
  483. return {
  484. closeOutline: function () {
  485. _this.outline && _this.outline.closeNotch();
  486. },
  487. hasOutline: function () { return Boolean(_this.outline); },
  488. notchOutline: function (labelWidth) {
  489. _this.outline && _this.outline.notch(labelWidth);
  490. },
  491. };
  492. };
  493. /**
  494. * @return A map of all subcomponents to subfoundations.
  495. */
  496. MDCTextField.prototype.getFoundationMap = function () {
  497. return {
  498. characterCounter: this.characterCounter ?
  499. this.characterCounter.foundationForTextField :
  500. undefined,
  501. helperText: this.helperText ? this.helperText.foundationForTextField :
  502. undefined,
  503. leadingIcon: this.leadingIcon ? this.leadingIcon.foundationForTextField :
  504. undefined,
  505. trailingIcon: this.trailingIcon ?
  506. this.trailingIcon.foundationForTextField :
  507. undefined,
  508. };
  509. };
  510. MDCTextField.prototype.createRipple = function (rippleFactory) {
  511. var _this = this;
  512. var isTextArea = this.root.classList.contains(cssClasses.TEXTAREA);
  513. var isOutlined = this.root.classList.contains(cssClasses.OUTLINED);
  514. if (isTextArea || isOutlined) {
  515. return null;
  516. }
  517. // DO NOT INLINE this variable. For backward compatibility, foundations take
  518. // a Partial<MDCFooAdapter>. To ensure we don't accidentally omit any
  519. // methods, we need a separate, strongly typed adapter variable.
  520. // tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
  521. var adapter = __assign(__assign({}, MDCRipple.createAdapter(this)), { isSurfaceActive: function () { return ponyfill.matches(_this.input, ':active'); }, registerInteractionHandler: function (evtType, handler) {
  522. _this.input.addEventListener(evtType, handler, applyPassive());
  523. }, deregisterInteractionHandler: function (evtType, handler) {
  524. _this.input.removeEventListener(evtType, handler, applyPassive());
  525. } });
  526. // tslint:enable:object-literal-sort-keys
  527. return rippleFactory(this.root, new MDCRippleFoundation(adapter));
  528. };
  529. return MDCTextField;
  530. }(MDCComponent));
  531. export { MDCTextField };
  532. //# sourceMappingURL=component.js.map