calcite-input.cjs.entry.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /*!
  2. * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
  4. * v1.0.0-beta.82
  5. */
  6. 'use strict';
  7. Object.defineProperty(exports, '__esModule', { value: true });
  8. const index = require('./index-5c65e149.js');
  9. const dom = require('./dom-9ac0341c.js');
  10. const label = require('./label-165fc611.js');
  11. const form = require('./form-11926121.js');
  12. const locale = require('./locale-f2ebe4fa.js');
  13. const number = require('./number-364babbc.js');
  14. const math = require('./math-9d6de52c.js');
  15. const observers = require('./observers-d9fdf006.js');
  16. const interactive = require('./interactive-e294111f.js');
  17. require('./guid-8b6d6cb4.js');
  18. const CSS = {
  19. loader: "loader",
  20. clearButton: "clear-button",
  21. editingEnabled: "editing-enabled",
  22. inlineChild: "inline-child",
  23. inputIcon: "icon",
  24. prefix: "prefix",
  25. suffix: "suffix",
  26. numberButtonWrapper: "number-button-wrapper",
  27. buttonItemHorizontal: "number-button-item--horizontal",
  28. wrapper: "element-wrapper",
  29. inputWrapper: "wrapper",
  30. actionWrapper: "action-wrapper",
  31. resizeIconWrapper: "resize-icon-wrapper",
  32. numberButtonItem: "number-button-item"
  33. };
  34. const INPUT_TYPE_ICONS = {
  35. tel: "phone",
  36. password: "lock",
  37. email: "email-address",
  38. date: "calendar",
  39. time: "clock",
  40. search: "search"
  41. };
  42. const SLOTS = {
  43. action: "action"
  44. };
  45. const TEXT = {
  46. clear: "Clear value"
  47. };
  48. const inputCss = "@-webkit-keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in{0%{opacity:0}100%{opacity:1}}@-webkit-keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}@keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}:root{--calcite-animation-timing:calc(150ms * var(--calcite-internal-duration-factor));--calcite-internal-duration-factor:var(--calcite-duration-factor, 1);--calcite-internal-animation-timing-fast:calc(100ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-medium:calc(200ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-slow:calc(300ms * var(--calcite-internal-duration-factor))}.calcite-animate{opacity:0;-webkit-animation-fill-mode:both;animation-fill-mode:both;-webkit-animation-duration:var(--calcite-animation-timing);animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{-webkit-animation-name:in;animation-name:in}.calcite-animate__in-down{-webkit-animation-name:in-down;animation-name:in-down}.calcite-animate__in-up{-webkit-animation-name:in-up;animation-name:in-up}.calcite-animate__in-scale{-webkit-animation-name:in-scale;animation-name:in-scale}:root{--calcite-popper-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}:host{display:block}:host([scale=s]) input,:host([scale=s]) .prefix,:host([scale=s]) .suffix{height:1.5rem;padding:0.5rem;font-size:var(--calcite-font-size--2);line-height:1rem}:host([scale=s]) textarea{height:1.5rem;min-height:1.5rem}:host([scale=s]) .number-button-wrapper,:host([scale=s]) .action-wrapper calcite-button,:host([scale=s]) .action-wrapper calcite-button button{height:1.5rem}:host([scale=s]) input[type=file]{height:1.5rem}:host([scale=s]) .clear-button{min-height:1.5rem;min-width:1.5rem}:host([scale=s]) textarea{height:auto;padding-top:0.25rem;padding-bottom:0.25rem;padding-left:0.5rem;padding-right:0.5rem;font-size:var(--calcite-font-size--2);line-height:1rem}:host([scale=m]) input,:host([scale=m]) .prefix,:host([scale=m]) .suffix{height:2rem;padding:0.75rem;font-size:var(--calcite-font-size--1);line-height:1rem}:host([scale=m]) textarea{min-height:2rem}:host([scale=m]) .number-button-wrapper,:host([scale=m]) .action-wrapper calcite-button,:host([scale=m]) .action-wrapper calcite-button button{height:2rem}:host([scale=m]) input[type=file]{height:2rem}:host([scale=m]) .clear-button{min-height:2rem;min-width:2rem}:host([scale=m]) textarea{height:auto;padding-top:0.5rem;padding-bottom:0.5rem;padding-left:0.75rem;padding-right:0.75rem;font-size:var(--calcite-font-size--1);line-height:1rem}:host([scale=l]) input,:host([scale=l]) .prefix,:host([scale=l]) .suffix{height:2.75rem;padding:1rem;font-size:var(--calcite-font-size-0);line-height:1.25rem}:host([scale=l]) textarea{min-height:2.75rem}:host([scale=l]) .number-button-wrapper,:host([scale=l]) .action-wrapper calcite-button,:host([scale=l]) .action-wrapper calcite-button button{height:2.75rem}:host([scale=l]) input[type=file]{height:2.75rem}:host([scale=l]) .clear-button{min-height:2.75rem;min-width:2.75rem}:host([scale=l]) textarea{height:auto;padding-top:0.75rem;padding-bottom:0.75rem;padding-left:1rem;padding-right:1rem;font-size:var(--calcite-font-size-0);line-height:1.25rem}:host([disabled]){pointer-events:none;cursor:default;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;opacity:var(--calcite-ui-opacity-disabled)}:host([disabled]) textarea{resize:none}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}:host textarea,:host input{-webkit-transition:var(--calcite-animation-timing), height 0s;transition:var(--calcite-animation-timing), height 0s;-webkit-appearance:none;position:relative;margin:0px;-webkit-box-sizing:border-box;box-sizing:border-box;display:-ms-flexbox;display:flex;max-height:100%;width:100%;max-width:100%;-ms-flex:1 1 0%;flex:1 1 0%;border-radius:0px;background-color:var(--calcite-ui-foreground-1);font-family:inherit;font-weight:var(--calcite-font-weight-normal);color:var(--calcite-ui-text-1)}:host input[type=search]::-webkit-search-decoration{-webkit-appearance:none}:host input,:host textarea{border-width:1px;border-style:solid;border-color:var(--calcite-ui-border-input);color:var(--calcite-ui-text-1)}:host input:-ms-input-placeholder,:host textarea:-ms-input-placeholder{font-weight:var(--calcite-font-weight-normal);color:var(--calcite-ui-text-3)}:host input::-ms-input-placeholder,:host textarea::-ms-input-placeholder{font-weight:var(--calcite-font-weight-normal);color:var(--calcite-ui-text-3)}:host input::placeholder,:host input:-ms-input-placeholder,:host input::-ms-input-placeholder,:host textarea::placeholder,:host textarea:-ms-input-placeholder,:host textarea::-ms-input-placeholder{font-weight:var(--calcite-font-weight-normal);color:var(--calcite-ui-text-3)}:host input:focus,:host textarea:focus{border-color:var(--calcite-ui-brand);color:var(--calcite-ui-text-1)}:host input[readonly],:host textarea[readonly]{background-color:var(--calcite-ui-background);font-weight:var(--calcite-font-weight-medium)}:host input[readonly]:focus,:host textarea[readonly]:focus{color:var(--calcite-ui-text-1)}:host calcite-icon{color:var(--calcite-ui-text-3)}:host textarea,:host input{outline-offset:0;outline-color:transparent;-webkit-transition:outline-offset 100ms ease-in-out, outline-color 100ms ease-in-out;transition:outline-offset 100ms ease-in-out, outline-color 100ms ease-in-out}:host textarea:focus,:host input:focus{outline:2px solid var(--calcite-ui-brand);outline-offset:-2px}:host([status=invalid]) input,:host([status=invalid]) textarea{border-color:var(--calcite-ui-danger)}:host([status=invalid]) input:focus,:host([status=invalid]) textarea:focus{outline:2px solid var(--calcite-ui-danger);outline-offset:-2px}:host([scale=s]) .icon{inset-inline-start:0.5rem}:host([scale=m]) .icon{inset-inline-start:0.75rem}:host([scale=l]) .icon{inset-inline-start:1rem}:host([icon][scale=s]) input{-webkit-padding-start:2rem;padding-inline-start:2rem}:host([icon][scale=m]) input{-webkit-padding-start:2.5rem;padding-inline-start:2.5rem}:host([icon][scale=l]) input{-webkit-padding-start:3rem;padding-inline-start:3rem}.element-wrapper{position:relative;-ms-flex-order:3;order:3;display:-ms-inline-flexbox;display:inline-flex;-ms-flex:1 1 0%;flex:1 1 0%;-ms-flex-align:center;align-items:center}.icon{pointer-events:none;position:absolute;z-index:10;display:block;-webkit-transition-property:all;transition-property:all;-webkit-transition-duration:var(--calcite-animation-timing);transition-duration:var(--calcite-animation-timing);-webkit-transition-timing-function:ease-in-out;transition-timing-function:ease-in-out;-webkit-transition-delay:0s;transition-delay:0s}input[type=text]::-ms-clear,input[type=text]::-ms-reveal{display:none;height:0px;width:0px}input[type=search]::-webkit-search-decoration,input[type=search]::-webkit-search-cancel-button,input[type=search]::-webkit-search-results-button,input[type=search]::-webkit-search-results-decoration,input[type=date]::-webkit-clear-button,input[type=time]::-webkit-clear-button{display:none}.clear-button{pointer-events:initial;-ms-flex-order:4;order:4;margin:0px;-webkit-box-sizing:border-box;box-sizing:border-box;display:-ms-flexbox;display:flex;min-height:100%;cursor:pointer;-ms-flex-align:center;align-items:center;-ms-flex-pack:center;justify-content:center;-ms-flex-item-align:stretch;align-self:stretch;border-width:1px;border-style:solid;border-color:var(--calcite-ui-border-input);background-color:var(--calcite-ui-foreground-1);outline-offset:0;outline-color:transparent;-webkit-transition:outline-offset 100ms ease-in-out, outline-color 100ms ease-in-out;transition:outline-offset 100ms ease-in-out, outline-color 100ms ease-in-out;border-inline-start-width:0px}.clear-button:hover{background-color:var(--calcite-ui-foreground-2);-webkit-transition-property:all;transition-property:all;-webkit-transition-duration:var(--calcite-animation-timing);transition-duration:var(--calcite-animation-timing);-webkit-transition-timing-function:ease-in-out;transition-timing-function:ease-in-out;-webkit-transition-delay:0s;transition-delay:0s}.clear-button:hover calcite-icon{color:var(--calcite-ui-text-1);-webkit-transition-property:all;transition-property:all;-webkit-transition-duration:var(--calcite-animation-timing);transition-duration:var(--calcite-animation-timing);-webkit-transition-timing-function:ease-in-out;transition-timing-function:ease-in-out;-webkit-transition-delay:0s;transition-delay:0s}.clear-button:active{background-color:var(--calcite-ui-foreground-3)}.clear-button:active calcite-icon{color:var(--calcite-ui-text-1)}.clear-button:focus{outline:2px solid var(--calcite-ui-brand);outline-offset:-2px}.clear-button:disabled{opacity:var(--calcite-ui-opacity-disabled)}.loader{top:1px;left:1px;right:1px;pointer-events:none;position:absolute;display:block}.action-wrapper{-ms-flex-order:7;order:7;display:-ms-flexbox;display:flex}.prefix,.suffix{-webkit-box-sizing:border-box;box-sizing:border-box;display:-ms-flexbox;display:flex;height:auto;min-height:100%;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;-ms-flex-line-pack:center;align-content:center;-ms-flex-align:center;align-items:center;overflow-wrap:break-word;border-width:1px;border-style:solid;border-color:var(--calcite-ui-border-input);background-color:var(--calcite-ui-background);font-weight:var(--calcite-font-weight-medium);line-height:1;color:var(--calcite-ui-text-2)}.prefix{-ms-flex-order:2;order:2;border-inline-end-width:0px}.suffix{-ms-flex-order:5;order:5;border-inline-start-width:0px}:host([alignment=start]) textarea,:host([alignment=start]) input{text-align:start}:host([alignment=end]) textarea,:host([alignment=end]) input{text-align:end}:host input[type=number]{-moz-appearance:textfield}:host input[type=number]::-webkit-inner-spin-button,:host input[type=number]::-webkit-outer-spin-button{-webkit-appearance:none;-moz-appearance:textfield;margin:0px}.number-button-wrapper{pointer-events:none;-ms-flex-order:6;order:6;-webkit-box-sizing:border-box;box-sizing:border-box;display:-ms-flexbox;display:flex;-ms-flex-direction:column;flex-direction:column;-webkit-transition-property:all;transition-property:all;-webkit-transition-duration:var(--calcite-animation-timing);transition-duration:var(--calcite-animation-timing);-webkit-transition-timing-function:ease-in-out;transition-timing-function:ease-in-out;-webkit-transition-delay:0s;transition-delay:0s}:host([number-button-type=vertical]) .wrapper{-ms-flex-direction:row;flex-direction:row;display:-ms-flexbox;display:flex}:host([number-button-type=vertical]) input,:host([number-button-type=vertical]) textarea{-ms-flex-order:2;order:2}:host([number-button-type=horizontal]) .calcite--rtl .number-button-item[data-adjustment=down] calcite-icon{-webkit-transform:rotate(-90deg);transform:rotate(-90deg)}:host([number-button-type=horizontal]) .calcite--rtl .number-button-item[data-adjustment=up] calcite-icon{-webkit-transform:rotate(-90deg);transform:rotate(-90deg)}.number-button-item.number-button-item--horizontal[data-adjustment=down],.number-button-item.number-button-item--horizontal[data-adjustment=up]{-ms-flex-order:1;order:1;max-height:100%;min-height:100%;-ms-flex-item-align:stretch;align-self:stretch}.number-button-item.number-button-item--horizontal[data-adjustment=down] calcite-icon,.number-button-item.number-button-item--horizontal[data-adjustment=up] calcite-icon{-webkit-transform:rotate(90deg);transform:rotate(90deg)}.number-button-item.number-button-item--horizontal[data-adjustment=down]{border-width:1px;border-style:solid;border-color:var(--calcite-ui-border-input);border-inline-end-width:0px}.number-button-item.number-button-item--horizontal[data-adjustment=down]:hover{background-color:var(--calcite-ui-foreground-2)}.number-button-item.number-button-item--horizontal[data-adjustment=down]:hover calcite-icon{color:var(--calcite-ui-text-1)}.number-button-item.number-button-item--horizontal[data-adjustment=up]{-ms-flex-order:5;order:5}.number-button-item.number-button-item--horizontal[data-adjustment=up]:hover{background-color:var(--calcite-ui-foreground-2)}.number-button-item.number-button-item--horizontal[data-adjustment=up]:hover calcite-icon{color:var(--calcite-ui-text-1)}:host([number-button-type=vertical]) .number-button-item[data-adjustment=down]:hover{background-color:var(--calcite-ui-foreground-2)}:host([number-button-type=vertical]) .number-button-item[data-adjustment=down]:hover calcite-icon{color:var(--calcite-ui-text-1)}:host([number-button-type=vertical]) .number-button-item[data-adjustment=up]:hover{background-color:var(--calcite-ui-foreground-2)}:host([number-button-type=vertical]) .number-button-item[data-adjustment=up]:hover calcite-icon{color:var(--calcite-ui-text-1)}:host([number-button-type=vertical]) .number-button-item[data-adjustment=down]{border-top-width:0px}.number-button-item{max-height:50%;min-height:50%;pointer-events:initial;margin:0px;-webkit-box-sizing:border-box;box-sizing:border-box;display:-ms-flexbox;display:flex;cursor:pointer;-ms-flex-align:center;align-items:center;-ms-flex-item-align:center;align-self:center;border-width:1px;border-style:solid;border-color:var(--calcite-ui-border-input);background-color:var(--calcite-ui-foreground-1);padding-top:0px;padding-bottom:0px;padding-left:0.5rem;padding-right:0.5rem;-webkit-transition-property:all;transition-property:all;-webkit-transition-duration:var(--calcite-animation-timing);transition-duration:var(--calcite-animation-timing);-webkit-transition-timing-function:ease-in-out;transition-timing-function:ease-in-out;-webkit-transition-delay:0s;transition-delay:0s;border-inline-start-width:0px}.number-button-item calcite-icon{pointer-events:none;-webkit-transition-property:all;transition-property:all;-webkit-transition-duration:var(--calcite-animation-timing);transition-duration:var(--calcite-animation-timing);-webkit-transition-timing-function:ease-in-out;transition-timing-function:ease-in-out;-webkit-transition-delay:0s;transition-delay:0s}.number-button-item:focus{background-color:var(--calcite-ui-foreground-2)}.number-button-item:focus calcite-icon{color:var(--calcite-ui-text-1)}.number-button-item:active{background-color:var(--calcite-ui-foreground-3)}.wrapper{position:relative;display:-ms-flexbox;display:flex;-ms-flex-direction:row;flex-direction:row;-ms-flex-align:center;align-items:center}:host input::-webkit-calendar-picker-indicator{display:none}:host input[type=date]::-webkit-input-placeholder{visibility:hidden !important}:host textarea::-webkit-resizer{position:absolute;bottom:0px;-webkit-box-sizing:border-box;box-sizing:border-box;padding-top:0px;padding-bottom:0px;padding-left:0.25rem;padding-right:0.25rem;inset-inline-end:0}.resize-icon-wrapper{bottom:2px;inset-inline-end:2px;pointer-events:none;position:absolute;z-index:10;height:0.75rem;width:0.75rem;background-color:var(--calcite-ui-foreground-1);color:var(--calcite-ui-text-3)}.resize-icon-wrapper calcite-icon{bottom:0.25rem;inset-inline-end:0.25rem;-webkit-transform:rotate(-45deg);transform:rotate(-45deg)}.calcite--rtl .resize-icon-wrapper calcite-icon{-webkit-transform:rotate(45deg);transform:rotate(45deg)}:host([type=color]) input{padding:0.25rem}:host([type=file]) input{cursor:pointer;border-width:1px;border-style:dashed;border-color:var(--calcite-ui-border-input);background-color:var(--calcite-ui-foreground-1);text-align:center}:host([type=file][scale=s]) input{padding-top:1px;padding-bottom:1px;padding-left:0.5rem;padding-right:0.5rem}:host([type=file][scale=m]) input{padding-top:0.25rem;padding-bottom:0.25rem;padding-left:0.75rem;padding-right:0.75rem}:host([type=file][scale=l]) input{padding-top:0.5rem;padding-bottom:0.5rem;padding-left:1rem;padding-right:1rem}:host(.no-bottom-border) input{border-bottom-width:0px}:host(.border-top-color-one) input{border-top-color:var(--calcite-ui-border-1)}:host .inline-child{background-color:transparent;-webkit-transition-property:all;transition-property:all;-webkit-transition-duration:var(--calcite-animation-timing);transition-duration:var(--calcite-animation-timing);-webkit-transition-timing-function:ease-in-out;transition-timing-function:ease-in-out;-webkit-transition-delay:0s;transition-delay:0s}:host .inline-child .editing-enabled{background-color:inherit}:host .inline-child:not(.editing-enabled){display:-ms-flexbox;display:flex;cursor:pointer;border-color:transparent;-webkit-padding-start:0;padding-inline-start:0}::slotted(input[slot=hidden-form-input]){bottom:0 !important;left:0 !important;margin:0 !important;opacity:0 !important;outline:none !important;padding:0 !important;position:absolute !important;right:0 !important;top:0 !important;-webkit-transform:none !important;transform:none !important;-webkit-appearance:none !important;z-index:-1 !important}";
  49. const Input = class {
  50. constructor(hostRef) {
  51. index.registerInstance(this, hostRef);
  52. this.calciteInputFocus = index.createEvent(this, "calciteInputFocus", 7);
  53. this.calciteInputBlur = index.createEvent(this, "calciteInputBlur", 7);
  54. this.calciteInputInput = index.createEvent(this, "calciteInputInput", 7);
  55. this.calciteInputChange = index.createEvent(this, "calciteInputChange", 7);
  56. //--------------------------------------------------------------------------
  57. //
  58. // Properties
  59. //
  60. //--------------------------------------------------------------------------
  61. /** specify the alignment of the value of the input */
  62. this.alignment = "start";
  63. /** should the input autofocus */
  64. this.autofocus = false;
  65. /** optionally display a clear button that displays when field has a value
  66. * shows by default for search, time, date
  67. * will not display for type="textarea" */
  68. this.clearable = false;
  69. /** is the input disabled */
  70. this.disabled = false;
  71. /** for number values, displays the locale's group separator */
  72. this.groupSeparator = false;
  73. /** when true, the component will not be visible */
  74. this.hidden = false;
  75. /**
  76. * string to override English loading text
  77. * @default "Loading"
  78. */
  79. this.intlLoading = dom.TEXT.loading;
  80. /** flip the icon in rtl */
  81. this.iconFlipRtl = false;
  82. /** specify if the input is in loading state */
  83. this.loading = false;
  84. /** BCP 47 language tag for desired language and country format */
  85. this.locale = document.documentElement.lang || "en";
  86. /**
  87. * Toggles locale formatting for numbers.
  88. * @internal
  89. */
  90. this.localeFormat = false;
  91. /** specify the placement of the number buttons */
  92. this.numberButtonType = "vertical";
  93. /** When true, a field cannot be modified. */
  94. this.readOnly = false;
  95. /** is the input required */
  96. this.required = false;
  97. /** specify the scale of the input, defaults to m */
  98. this.scale = "m";
  99. /** specify the status of the input field, determines message and icons */
  100. this.status = "idle";
  101. /** @internal adds inline styles for text input when slotted in calcite-inline-editable */
  102. this.editingEnabled = false;
  103. /**
  104. * specify the input type
  105. *
  106. * Note that the following types add type-specific icons by default: `date`, `email`, `password`, `search`, `tel`, `time`
  107. */
  108. this.type = "text";
  109. /** input value */
  110. this.value = "";
  111. /** keep track of the rendered child type */
  112. this.childElType = "input";
  113. this.previousValueOrigin = "initial";
  114. this.mutationObserver = observers.createObserver("mutation", () => this.setDisabledAction());
  115. this.userChangedValue = false;
  116. //--------------------------------------------------------------------------
  117. //
  118. // Private Methods
  119. //
  120. //--------------------------------------------------------------------------
  121. this.keyDownHandler = (event) => {
  122. if (this.readOnly || this.disabled) {
  123. return;
  124. }
  125. if (this.isClearable && event.key === "Escape") {
  126. this.clearInputValue(event);
  127. event.preventDefault();
  128. }
  129. if (event.key === "Enter" && !event.defaultPrevented) {
  130. form.submitForm(this);
  131. }
  132. };
  133. this.clearInputValue = (nativeEvent) => {
  134. this.setValue({
  135. committing: true,
  136. nativeEvent,
  137. origin: "user",
  138. value: ""
  139. });
  140. };
  141. this.emitChangeIfUserModified = () => {
  142. if (this.previousValueOrigin === "user" && this.value !== this.previousEmittedValue) {
  143. this.calciteInputChange.emit();
  144. }
  145. this.previousEmittedValue = this.value;
  146. };
  147. this.inputBlurHandler = () => {
  148. this.calciteInputBlur.emit({
  149. element: this.childEl,
  150. value: this.value
  151. });
  152. this.emitChangeIfUserModified();
  153. };
  154. this.inputFocusHandler = (event) => {
  155. const slottedActionEl = dom.getSlotted(this.el, "action");
  156. if (event.target !== slottedActionEl) {
  157. this.setFocus();
  158. }
  159. this.calciteInputFocus.emit({
  160. element: this.childEl,
  161. value: this.value
  162. });
  163. };
  164. this.inputInputHandler = (nativeEvent) => {
  165. if (this.disabled || this.readOnly) {
  166. return;
  167. }
  168. this.setValue({
  169. nativeEvent,
  170. origin: "user",
  171. value: nativeEvent.target.value
  172. });
  173. };
  174. this.inputKeyDownHandler = (event) => {
  175. if (this.disabled || this.readOnly) {
  176. return;
  177. }
  178. if (event.key === "Enter") {
  179. this.emitChangeIfUserModified();
  180. }
  181. };
  182. this.inputNumberInputHandler = (nativeEvent) => {
  183. if (this.disabled || this.readOnly) {
  184. return;
  185. }
  186. const value = nativeEvent.target.value;
  187. const delocalizedValue = locale.delocalizeNumberString(value, this.locale);
  188. if (nativeEvent.inputType === "insertFromPaste") {
  189. if (!number.isValidNumber(delocalizedValue)) {
  190. nativeEvent.preventDefault();
  191. }
  192. this.setValue({
  193. nativeEvent,
  194. origin: "user",
  195. value: number.parseNumberString(delocalizedValue)
  196. });
  197. this.childNumberEl.value = this.localizedValue;
  198. }
  199. else {
  200. this.setValue({
  201. nativeEvent,
  202. origin: "user",
  203. value: delocalizedValue
  204. });
  205. }
  206. };
  207. this.inputNumberKeyDownHandler = (event) => {
  208. if (this.type !== "number" || this.disabled || this.readOnly) {
  209. return;
  210. }
  211. if (event.key === "ArrowUp") {
  212. /* prevent default behavior of moving cursor to the beginning of the input when holding down ArrowUp */
  213. event.preventDefault();
  214. this.nudgeNumberValue("up", event);
  215. return;
  216. }
  217. if (event.key === "ArrowDown") {
  218. this.nudgeNumberValue("down", event);
  219. return;
  220. }
  221. const supportedKeys = [
  222. ...number.numberKeys,
  223. "ArrowLeft",
  224. "ArrowRight",
  225. "Backspace",
  226. "Delete",
  227. "Enter",
  228. "Escape",
  229. "Tab"
  230. ];
  231. if (event.altKey || event.ctrlKey || event.metaKey) {
  232. return;
  233. }
  234. const isShiftTabEvent = event.shiftKey && event.key === "Tab";
  235. if (supportedKeys.includes(event.key) && (!event.shiftKey || isShiftTabEvent)) {
  236. if (event.key === "Enter") {
  237. this.emitChangeIfUserModified();
  238. }
  239. return;
  240. }
  241. const decimalSeparator = locale.getDecimalSeparator(this.locale);
  242. if (event.key === decimalSeparator) {
  243. if (!this.value && !this.childNumberEl.value) {
  244. return;
  245. }
  246. if (this.value && this.childNumberEl.value.indexOf(decimalSeparator) === -1) {
  247. return;
  248. }
  249. }
  250. if (/[eE]/.test(event.key)) {
  251. if (!this.value && !this.childNumberEl.value) {
  252. return;
  253. }
  254. if (this.value && !/[eE]/.test(this.childNumberEl.value)) {
  255. return;
  256. }
  257. }
  258. if (event.key === "-") {
  259. if (!this.value && !this.childNumberEl.value) {
  260. return;
  261. }
  262. if (this.value && this.childNumberEl.value.split("-").length <= 2) {
  263. return;
  264. }
  265. }
  266. event.preventDefault();
  267. };
  268. this.nudgeNumberValue = (direction, nativeEvent) => {
  269. if ((nativeEvent instanceof KeyboardEvent && nativeEvent.repeat) || this.type !== "number") {
  270. return;
  271. }
  272. const inputMax = this.maxString ? parseFloat(this.maxString) : null;
  273. const inputMin = this.minString ? parseFloat(this.minString) : null;
  274. const valueNudgeDelayInMs = 100;
  275. this.incrementOrDecrementNumberValue(direction, inputMax, inputMin, nativeEvent);
  276. if (this.nudgeNumberValueIntervalId) {
  277. window.clearInterval(this.nudgeNumberValueIntervalId);
  278. }
  279. let firstValueNudge = true;
  280. this.nudgeNumberValueIntervalId = window.setInterval(() => {
  281. if (firstValueNudge) {
  282. firstValueNudge = false;
  283. return;
  284. }
  285. this.incrementOrDecrementNumberValue(direction, inputMax, inputMin, nativeEvent);
  286. }, valueNudgeDelayInMs);
  287. };
  288. this.numberButtonMouseUpAndMouseOutHandler = () => {
  289. window.clearInterval(this.nudgeNumberValueIntervalId);
  290. };
  291. this.numberButtonMouseDownHandler = (event) => {
  292. event.preventDefault();
  293. const direction = event.target.dataset.adjustment;
  294. this.nudgeNumberValue(direction, event);
  295. };
  296. this.setChildElRef = (el) => {
  297. this.childEl = el;
  298. };
  299. this.setChildNumberElRef = (el) => {
  300. this.childNumberEl = el;
  301. };
  302. this.setInputValue = (newInputValue) => {
  303. if (this.type === "text" && !this.childEl) {
  304. return;
  305. }
  306. if (this.type === "number" && !this.childNumberEl) {
  307. return;
  308. }
  309. this[`child${this.type === "number" ? "Number" : ""}El`].value = newInputValue;
  310. };
  311. this.setPreviousEmittedValue = (newPreviousEmittedValue) => {
  312. this.previousEmittedValue =
  313. this.type === "number"
  314. ? number.isValidNumber(newPreviousEmittedValue)
  315. ? newPreviousEmittedValue
  316. : ""
  317. : newPreviousEmittedValue;
  318. };
  319. this.setPreviousValue = (newPreviousValue) => {
  320. this.previousValue =
  321. this.type === "number"
  322. ? number.isValidNumber(newPreviousValue)
  323. ? newPreviousValue
  324. : ""
  325. : newPreviousValue;
  326. };
  327. this.setValue = ({ committing = false, nativeEvent, origin, previousValue, value }) => {
  328. const previousLocalizedValue = this.type === "number"
  329. ? locale.localizeNumberString(this.previousValue, this.locale, this.groupSeparator)
  330. : "";
  331. const sanitizedValue = this.type === "number" ? number.sanitizeNumberString(value) : value;
  332. const newValue = this.type === "number" && value && !sanitizedValue
  333. ? number.isValidNumber(this.previousValue)
  334. ? this.previousValue
  335. : ""
  336. : sanitizedValue;
  337. const newLocalizedValue = this.type === "number"
  338. ? locale.localizeNumberString(newValue, this.locale, this.groupSeparator)
  339. : "";
  340. this.setPreviousValue(previousValue || this.value);
  341. this.previousValueOrigin = origin;
  342. this.userChangedValue = origin === "user" && this.value !== newValue;
  343. this.value = newValue;
  344. if (this.type === "number") {
  345. this.localizedValue = newLocalizedValue;
  346. }
  347. if (origin === "direct") {
  348. this.setInputValue(this.type === "number" ? newLocalizedValue : newValue);
  349. }
  350. if (nativeEvent) {
  351. const calciteInputInputEvent = this.calciteInputInput.emit({
  352. element: this.childEl,
  353. nativeEvent,
  354. value: this.value
  355. });
  356. if (calciteInputInputEvent.defaultPrevented) {
  357. this.value = this.previousValue;
  358. this.localizedValue = previousLocalizedValue;
  359. }
  360. else if (committing) {
  361. this.emitChangeIfUserModified();
  362. }
  363. }
  364. };
  365. this.inputKeyUpHandler = () => {
  366. window.clearInterval(this.nudgeNumberValueIntervalId);
  367. };
  368. }
  369. disabledWatcher() {
  370. this.setDisabledAction();
  371. }
  372. /** watcher to update number-to-string for max */
  373. maxWatcher() {
  374. var _a;
  375. this.maxString = ((_a = this.max) === null || _a === void 0 ? void 0 : _a.toString()) || null;
  376. }
  377. /** watcher to update number-to-string for min */
  378. minWatcher() {
  379. var _a;
  380. this.minString = ((_a = this.min) === null || _a === void 0 ? void 0 : _a.toString()) || null;
  381. }
  382. valueWatcher(newValue, previousValue) {
  383. if (!this.userChangedValue) {
  384. this.setValue({
  385. origin: "direct",
  386. previousValue,
  387. value: newValue == null || newValue == ""
  388. ? ""
  389. : this.type === "number"
  390. ? number.isValidNumber(newValue)
  391. ? newValue
  392. : this.previousValue || ""
  393. : newValue
  394. });
  395. this.warnAboutInvalidNumberValue(newValue);
  396. }
  397. this.userChangedValue = false;
  398. }
  399. updateRequestedIcon() {
  400. this.requestedIcon = dom.setRequestedIcon(INPUT_TYPE_ICONS, this.icon, this.type);
  401. }
  402. get isClearable() {
  403. return !this.isTextarea && (this.clearable || this.type === "search") && this.value.length > 0;
  404. }
  405. get isTextarea() {
  406. return this.childElType === "textarea";
  407. }
  408. //--------------------------------------------------------------------------
  409. //
  410. // Lifecycle
  411. //
  412. //--------------------------------------------------------------------------
  413. connectedCallback() {
  414. var _a;
  415. this.scale = dom.getElementProp(this.el, "scale", this.scale);
  416. this.status = dom.getElementProp(this.el, "status", this.status);
  417. this.inlineEditableEl = this.el.closest("calcite-inline-editable");
  418. if (this.inlineEditableEl) {
  419. this.editingEnabled = this.inlineEditableEl.editingEnabled || false;
  420. }
  421. this.setPreviousEmittedValue(this.value);
  422. this.setPreviousValue(this.value);
  423. if (this.type === "number") {
  424. this.warnAboutInvalidNumberValue(this.value);
  425. this.setValue({
  426. origin: "connected",
  427. value: number.isValidNumber(this.value) ? this.value : ""
  428. });
  429. }
  430. label.connectLabel(this);
  431. form.connectForm(this);
  432. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.observe(this.el, { childList: true });
  433. this.setDisabledAction();
  434. }
  435. disconnectedCallback() {
  436. var _a;
  437. label.disconnectLabel(this);
  438. form.disconnectForm(this);
  439. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
  440. }
  441. componentWillLoad() {
  442. var _a, _b;
  443. this.childElType = this.type === "textarea" ? "textarea" : "input";
  444. this.maxString = (_a = this.max) === null || _a === void 0 ? void 0 : _a.toString();
  445. this.minString = (_b = this.min) === null || _b === void 0 ? void 0 : _b.toString();
  446. this.requestedIcon = dom.setRequestedIcon(INPUT_TYPE_ICONS, this.icon, this.type);
  447. }
  448. componentShouldUpdate(newValue, oldValue, property) {
  449. if (this.type === "number" && property === "value" && newValue && !number.isValidNumber(newValue)) {
  450. this.setValue({
  451. origin: "reset",
  452. value: oldValue
  453. });
  454. return false;
  455. }
  456. return true;
  457. }
  458. componentDidRender() {
  459. interactive.updateHostInteraction(this);
  460. }
  461. //--------------------------------------------------------------------------
  462. //
  463. // Public Methods
  464. //
  465. //--------------------------------------------------------------------------
  466. /** Sets focus on the component. */
  467. async setFocus() {
  468. var _a, _b;
  469. if (this.type === "number") {
  470. (_a = this.childNumberEl) === null || _a === void 0 ? void 0 : _a.focus();
  471. }
  472. else {
  473. (_b = this.childEl) === null || _b === void 0 ? void 0 : _b.focus();
  474. }
  475. }
  476. onLabelClick() {
  477. this.setFocus();
  478. }
  479. incrementOrDecrementNumberValue(direction, inputMax, inputMin, nativeEvent) {
  480. const { value } = this;
  481. const inputStep = this.step === "any" ? 1 : Math.abs(this.step || 1);
  482. const inputVal = value && value !== "" ? parseFloat(value) : 0;
  483. const adjustment = direction === "up" ? 1 : -1;
  484. const nudgedValue = inputVal + inputStep * adjustment;
  485. const finalValue = (typeof inputMin === "number" && !isNaN(inputMin) && nudgedValue < inputMin) ||
  486. (typeof inputMax === "number" && !isNaN(inputMax) && nudgedValue > inputMax)
  487. ? inputVal
  488. : nudgedValue;
  489. const inputValPlaces = math.decimalPlaces(inputVal);
  490. const inputStepPlaces = math.decimalPlaces(inputStep);
  491. this.setValue({
  492. committing: true,
  493. nativeEvent,
  494. origin: "user",
  495. value: finalValue.toFixed(Math.max(inputValPlaces, inputStepPlaces))
  496. });
  497. }
  498. onFormReset() {
  499. this.setValue({
  500. origin: "reset",
  501. value: this.defaultValue
  502. });
  503. }
  504. syncHiddenFormInput(input) {
  505. var _a, _b, _c, _d;
  506. if (this.type === "number") {
  507. input.type = "number";
  508. input.min = (_b = (_a = this.min) === null || _a === void 0 ? void 0 : _a.toString(10)) !== null && _b !== void 0 ? _b : "";
  509. input.max = (_d = (_c = this.max) === null || _c === void 0 ? void 0 : _c.toString(10)) !== null && _d !== void 0 ? _d : "";
  510. }
  511. else if (this.type === "text") {
  512. input.type = "text";
  513. if (this.minLength != null) {
  514. input.minLength = this.minLength;
  515. }
  516. if (this.maxLength != null) {
  517. input.maxLength = this.maxLength;
  518. }
  519. }
  520. else if (this.type === "password") {
  521. input.type = "password";
  522. }
  523. }
  524. setDisabledAction() {
  525. const slottedActionEl = dom.getSlotted(this.el, "action");
  526. if (!slottedActionEl) {
  527. return;
  528. }
  529. this.disabled
  530. ? slottedActionEl.setAttribute("disabled", "")
  531. : slottedActionEl.removeAttribute("disabled");
  532. }
  533. warnAboutInvalidNumberValue(value) {
  534. if (this.type === "number" && value && !number.isValidNumber(value)) {
  535. console.warn(`The specified value "${value}" cannot be parsed, or is out of range.`);
  536. }
  537. }
  538. // --------------------------------------------------------------------------
  539. //
  540. // Render Methods
  541. //
  542. // --------------------------------------------------------------------------
  543. render() {
  544. const dir = dom.getElementDir(this.el);
  545. const loader = (index.h("div", { class: CSS.loader }, index.h("calcite-progress", { label: this.intlLoading, type: "indeterminate" })));
  546. const inputClearButton = (index.h("button", { "aria-label": this.intlClear || TEXT.clear, class: CSS.clearButton, disabled: this.disabled || this.readOnly, onClick: this.clearInputValue, tabIndex: this.disabled ? -1 : 0, type: "button" }, index.h("calcite-icon", { icon: "x", scale: "s" })));
  547. const iconEl = (index.h("calcite-icon", { class: CSS.inputIcon, flipRtl: this.iconFlipRtl, icon: this.requestedIcon, scale: "s" }));
  548. const isHorizontalNumberButton = this.numberButtonType === "horizontal";
  549. const numberButtonsHorizontalUp = (index.h("button", { class: {
  550. [CSS.numberButtonItem]: true,
  551. [CSS.buttonItemHorizontal]: isHorizontalNumberButton
  552. }, "data-adjustment": "up", disabled: this.disabled || this.readOnly, onMouseDown: this.numberButtonMouseDownHandler, onMouseOut: this.numberButtonMouseUpAndMouseOutHandler, onMouseUp: this.numberButtonMouseUpAndMouseOutHandler, tabIndex: -1, type: "button" }, index.h("calcite-icon", { icon: "chevron-up", scale: "s" })));
  553. const numberButtonsHorizontalDown = (index.h("button", { class: {
  554. [CSS.numberButtonItem]: true,
  555. [CSS.buttonItemHorizontal]: isHorizontalNumberButton
  556. }, "data-adjustment": "down", disabled: this.disabled || this.readOnly, onMouseDown: this.numberButtonMouseDownHandler, onMouseOut: this.numberButtonMouseUpAndMouseOutHandler, onMouseUp: this.numberButtonMouseUpAndMouseOutHandler, tabIndex: -1, type: "button" }, index.h("calcite-icon", { icon: "chevron-down", scale: "s" })));
  557. const numberButtonsVertical = (index.h("div", { class: CSS.numberButtonWrapper }, numberButtonsHorizontalUp, numberButtonsHorizontalDown));
  558. const prefixText = index.h("div", { class: CSS.prefix }, this.prefixText);
  559. const suffixText = index.h("div", { class: CSS.suffix }, this.suffixText);
  560. const localeNumberInput = this.type === "number" ? (index.h("input", { "aria-label": label.getLabelText(this), autofocus: this.autofocus ? true : null, defaultValue: this.defaultValue, disabled: this.disabled ? true : null, enterKeyHint: this.el.enterKeyHint, inputMode: this.el.inputMode, key: "localized-input", maxLength: this.maxLength, minLength: this.minLength, name: undefined, onBlur: this.inputBlurHandler, onFocus: this.inputFocusHandler, onInput: this.inputNumberInputHandler, onKeyDown: this.inputNumberKeyDownHandler, onKeyUp: this.inputKeyUpHandler, placeholder: this.placeholder || "", readOnly: this.readOnly, ref: this.setChildNumberElRef, type: "text", value: this.localizedValue })) : null;
  561. const childEl = this.type !== "number"
  562. ? [
  563. index.h(this.childElType, { "aria-label": label.getLabelText(this), autofocus: this.autofocus ? true : null, class: {
  564. [CSS.editingEnabled]: this.editingEnabled,
  565. [CSS.inlineChild]: !!this.inlineEditableEl
  566. }, defaultValue: this.defaultValue, disabled: this.disabled ? true : null, enterKeyHint: this.el.enterKeyHint, inputMode: this.el.inputMode, max: this.maxString, maxLength: this.maxLength, min: this.minString, minLength: this.minLength, name: this.name, onBlur: this.inputBlurHandler, onFocus: this.inputFocusHandler, onInput: this.inputInputHandler, onKeyDown: this.inputKeyDownHandler, onKeyUp: this.inputKeyUpHandler, placeholder: this.placeholder || "", readOnly: this.readOnly, ref: this.setChildElRef, required: this.required ? true : null, step: this.step, tabIndex: this.disabled || (this.inlineEditableEl && !this.editingEnabled) ? -1 : null, type: this.type, value: this.value }),
  567. this.isTextarea ? (index.h("div", { class: CSS.resizeIconWrapper }, index.h("calcite-icon", { icon: "chevron-down", scale: "s" }))) : null
  568. ]
  569. : null;
  570. return (index.h(index.Host, { onClick: this.inputFocusHandler, onKeyDown: this.keyDownHandler }, index.h("div", { class: { [CSS.inputWrapper]: true, [dom.CSS_UTILITY.rtl]: dir === "rtl" } }, this.type === "number" && this.numberButtonType === "horizontal" && !this.readOnly
  571. ? numberButtonsHorizontalDown
  572. : null, this.prefixText ? prefixText : null, index.h("div", { class: CSS.wrapper }, localeNumberInput, childEl, this.isClearable ? inputClearButton : null, this.requestedIcon ? iconEl : null, this.loading ? loader : null), index.h("div", { class: CSS.actionWrapper }, index.h("slot", { name: SLOTS.action })), this.type === "number" && this.numberButtonType === "vertical" && !this.readOnly
  573. ? numberButtonsVertical
  574. : null, this.suffixText ? suffixText : null, this.type === "number" && this.numberButtonType === "horizontal" && !this.readOnly
  575. ? numberButtonsHorizontalUp
  576. : null, index.h(form.HiddenFormInputSlot, { component: this }))));
  577. }
  578. get el() { return index.getElement(this); }
  579. static get watchers() { return {
  580. "disabled": ["disabledWatcher"],
  581. "max": ["maxWatcher"],
  582. "min": ["minWatcher"],
  583. "value": ["valueWatcher"],
  584. "icon": ["updateRequestedIcon"],
  585. "type": ["updateRequestedIcon"]
  586. }; }
  587. };
  588. Input.style = inputCss;
  589. exports.calcite_input = Input;