calcite-input.entry.js 41 KB

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