All files / coral-component-numberinput/src/scripts NumberInput.js

97.19% Statements 242/249
91.39% Branches 138/151
96.36% Functions 53/55
97.19% Lines 242/249

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840                                                      2x   2x 2x 2x 2x   2x 52x   52x 22x 22x 22x     30x     2x     118x 118x 38x 80x 38x 42x 42x         118x 26x 26x 26x 26x 26x   26x 26x   26x   26x   92x     118x                           2x 2x   2x           270x   270x   270x                                       270x 270x           270x   270x 270x                   2x         10x             4x                     52x   52x   52x 14x   38x 38x                       50x   50x   50x 12x   38x 38x                         144x                     48x                     502x 502x                             46x 38x                                                         38x 18x     38x                       14x   14x 14x   14x   14x   14x     14x                       12x   12x 12x   12x   12x   12x     12x                     10x   10x 4x       6x   4x   4x 4x   4x     6x                     10x   10x 4x       6x   4x   4x 4x   4x     6x                   16x   16x 6x     10x                   16x   16x 6x     10x                     22x 10x       12x   12x 12x   12x 6x   6x       12x                     22x   22x     22x 22x 22x   22x                   36x   36x   36x   36x                   19x   19x     19x   19x   19x             485x   485x 148x               318x     318x   318x   318x 153x 153x 153x   153x 148x                       258x   258x   258x 238x       258x   258x       258x 258x   258x 258x   258x 40x   40x         40x       258x         1922x     318x   318x   318x   318x     318x 292x   292x   26x   26x         318x 166x     152x     318x 318x 318x                     142x   142x 4x     138x   138x 94x     44x     8x 8x 8x 8x                             716x     38x 38x   38x 4x   4x   4x   34x     34x   34x     38x 38x                             758x     42x 42x   42x 4x   4x   4x   38x     38x   38x     42x 42x                             348x     294x 288x   288x   288x                           18x     14x   14x                         1156x     486x   486x   486x 486x 486x   486x                         2x                                       914x     16x   16x   16x   16x                 54x     488x   488x  
/**
 * Copyright 2019 Adobe. All rights reserved.
 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy
 * of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
 * OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
 
import {BaseComponent} from '../../../coral-base-component';
import {BaseFormField} from '../../../coral-base-formfield';
import '../../../coral-component-button';
import '../../../coral-component-textfield';
import {Icon} from '../../../coral-component-icon';
import base from '../templates/base';
import {transform, commons, i18n} from '../../../coral-utils';
import {Decorator} from '../../../coral-decorator';
 
const CLASSNAME = '_coral-Stepper';
let clearLiveRegionTimeout;
const LIVEREGION_TIMEOUT_DELAY = 3000;
const MSPOINTER_TYPE_MOUSE = 0x00000004;
let flagTouchStart = false;
let flagStepButtonClick = false;
 
const exponentialToDecimalString = (value) => {
  const notation = value.toString();
  if (notation.indexOf('e') !== -1) {
    const negative = notation.indexOf('-') === 0;
    const exponent = parseInt(notation.split('-')[negative ? 2 : 1], 10);
    return value.toFixed(exponent).toString();
  }
 
  return value.toString();
};
 
const handleDecimalOperation = (operator, value1, value2) => {
  let result;
 
  const operation = (operator, value1, value2) => {
    if (operator === '+') {
      return value1 + value2;
    } else if (operator === '-') {
      return value1 - value2;
    } else if (operator === '%') {
      return value1 % value2;
    }
  };
 
  // Check if we have decimals
  if (value1 % 1 !== 0 || value2 % 1 !== 0) {
    const vEalue1Decimal = exponentialToDecimalString(value1).split('.');
    const value2Decimal = exponentialToDecimalString(value2).split('.');
    const value1DecimalLength = value1Decimal[1] && value1Decimal[1].length || 0;
    const value2DecimalLength = value2Decimal[1] && value2Decimal[1].length || 0;
    const multiplier = Math.pow(10, Math.max(value1DecimalLength, value2DecimalLength));
 
    // Transform the decimals to integers based on the multiplier
    value1 = Math.round(value1 * multiplier);
    value2 = Math.round(value2 * multiplier);
 
    // Perform the operation on integers values to make sure we don't get a fancy decimal value
    result = operation(operator, value1, value2);
 
    // Transform the integer result back to decimal
    result /= multiplier;
  } else {
    result = operation(operator, value1, value2);
  }
 
  return result;
};
 
/**
 @class Coral.NumberInput
 @classdesc A NumberInput component is a numeric control form field. It follows the ARIA specification for spinbutton.
 This means the following keys are valid for interacting with it: <code>up</code>, <code>down</code>, <code>left</code>,
 <code>right</code>, <code>pageup</code>, <code>pagedown</code>, <code>home</code>, <code>end</code> and the Mouse Wheel.
 @htmltag coral-numberinput
 @extends {HTMLElement}
 @extends {BaseComponent}
 @extends {BaseFormField}
 */
const NumberInput = Decorator(class extends BaseFormField(BaseComponent(HTMLElement)) {
  /** @ignore */
  constructor() {
    super();
 
    this._delegateEvents(commons.extend(this._events, {
      'key:up': '_onKeyUp',
      'key:pageup': '_onKeyUp',
      'key:down': '_onKeyDown',
      'key:pagedown': '_onKeyDown',
      'key:home': '_onKeyHome',
      'key:end': '_onKeyEnd',
      'touchstart [handle=stepUp], [handle=stepDown]': '_onTouchStart',
      'pointerdown [handle=stepUp], [handle=stepDown]': '_onTouchStart',
      'MSPointerDown [handle=stepUp], [handle=stepUp]': '_onTouchStart',
      'MSPointerDown [handle=stepDown], [handle=stepDown]': '_onTouchStart',
 
      'click [handle=stepUp]': '_onStepUpButtonClick',
      'click [handle=stepDown]': '_onStepDownButtonClick',
 
      'mousewheel [handle="input"]': '_onInputMouseWheel',
      'DOMMouseScroll [handle="input"]': '_onInputMouseWheel',
 
      'capture:focus': '_onFocus',
      'capture:blur': '_onBlur'
    }));
 
    // Prepare templates
    this._elements = {};
    base.call(this._elements, {i18n, commons, Icon});
 
    // Pre-define labellable element
    this._labellableElement = this._elements.input;
 
    // Default is null
    this._min = this._max = null;
  }
 
  /**
   This field's current value.
   @type {String}
   @default ""
   @htmlattribute value
   */
  get value() {
    return this._elements.input.value;
  }
 
  set value(value) {
    value = isNaN(value) ? '' : String(value);
 
    // sets the value immediately so it is picked up in form submits
    this._elements.input.value = value;
 
    // in order to keep the reset value in sync, we need to handle the "value" attribute of the inner input
    const valueAttribute = this.getAttribute('value');
    this._elements.input[valueAttribute ? 'setAttribute' : 'removeAttribute']('value', valueAttribute);
 
    // @a11y: aria-valuetext is used so that VoiceOver does not announce a percentage
    if (this.value) {
      this._elements.input.setAttribute('aria-valuenow', this.value);
      this._elements.input.setAttribute('aria-valuetext', this.value);
    } else {
      this._elements.input.removeAttribute('aria-valuenow');
      this._elements.input.removeAttribute('aria-valuetext');
    }
 
    // If the event triggering a value change is a click on a +/- button,
    // announce the new value using the live region.
    if (flagStepButtonClick || !!window.chrome) {
      this._updateLiveRegion(this.value);
      // Otherwise, clear the live region.
    } else {
      this._updateLiveRegion();
    }
 
    flagStepButtonClick = false;
 
    this.invalid = this.hasAttribute('invalid');
    this.disabled = this.hasAttribute('disabled');
  }
 
  /**
   The value returned as a Number. Value is <code>NaN</code> if conversion to Number is not possible.
 
   @type {Number}
   @default NaN
   */
  get valueAsNumber() {
    let valueAsNumber = this._valueAsNumber;
 
    if (typeof valueAsNumber !== 'undefined' && valueAsNumber !== null) {
      return valueAsNumber;
    }
 
    valueAsNumber = transform.number(this.value);
    if (valueAsNumber !== null) {
      return valueAsNumber;
    }
 
    return NaN;
  }
 
  set valueAsNumber(value) {
    this._valueAsNumber = transform.number(value);
 
    this.value = this._valueAsNumber;
    this.invalid = this.hasAttribute('invalid');
    this.disabled = this.hasAttribute('disabled');
  }
 
  /**
   The minimum value for the NumberInput. If a value below the minimum is set, the NumberInput will be marked as
   invalid but the value will be preserved. Stepping down the NumberInput via {@link Coral.NumberInput#stepDown}
   or the decrement button respects the minimum value. It reflects the <code>min</code> attribute to the DOM.
 
   @type {?Number}
   @default null
   @htmlattribute min
   @htmlattributereflected
   */
  get min() {
    return this._min;
  }
 
  set min(value) {
    value = transform.number(value);
 
    this._min = isNaN(value) ? null : value;
 
    if (this._min === null) {
      this._reflectAttribute('min', false);
 
      this._elements.input.removeAttribute('aria-valuemin');
      this._elements.input.removeAttribute('min');
    } else {
      this._reflectAttribute('min', this._min);
      // sets the min in the input so that keyboard handles this component
      this._elements.input.setAttribute('aria-valuemin', this._min);
      this._elements.input.min = this._min;
    }
 
    this.invalid = this.hasAttribute('invalid');
    this.disabled = this.hasAttribute('disabled');
  }
 
  /**
   The maximum value for the NumberInput. If a value above the maximum is set, the NumberInput will be marked as
   invalid but the value will be preserved. Stepping up the NumberInput via {@link Coral.NumberInput#stepUp} or
   the increment button respects the maximum value. It reflects the <code>max</code> attribute to the DOM.
 
   @type {?Number}
   @default null
   @htmlattribute max
   @htmlattributereflected
   */
  get max() {
    return this._max;
  }
 
  set max(value) {
    value = transform.number(value);
 
    this._max = isNaN(value) ? null : value;
 
    if (this.max === null) {
      this._reflectAttribute('max', false);
 
      this._elements.input.removeAttribute('aria-valuemax');
      this._elements.input.removeAttribute('max');
    } else {
      this._reflectAttribute('max', this._max);
      // sets the max in the input so that keyboard handles this component
      this._elements.input.setAttribute('aria-valuemax', this._max);
      this._elements.input.max = this._max;
    }

    this.invalid = this.hasAttribute('invalid');
    this.disabled = this.hasAttribute('disabled');
  }
 
  /**
   The amount to increment by when stepping up or down. It can be the string <code>any</code> or any positive
   floating point number. If this is not set to <code>any<code>, the control accepts only values at multiples of
   the step value greater than the minimum.
 
   @type {Number|String}
   @default 1
   @htmlattribute step
   @htmlattributereflected
   */
  get step() {
    return this._step || 1;
  }
 
  set step(value) {
    if (value !== null && (value > 0 || value === 'any')) {
      this._step = value === 'any' ? value : transform.number(value);
      this._reflectAttribute('step', this._step);
 
      this._elements.input.step = this._step;
    }
  }
 
  /**
   Name used to submit the data in a form.
   @type {String}
   @default ""
   @htmlattribute name
   @htmlattributereflected
   */
  get name() {
    return this._elements.input.name;
  }
 
  set name(value) {
    this._reflectAttribute('name', value);
 
    this._elements.input.name = value;
  }
 
  /**
   Whether this field is disabled or not.
   @type {Boolean}
   @default false
   @htmlattribute disabled
   @htmlattributereflected
   */
  get disabled() {
    return this._disabled || false;
  }
 
  set disabled(value) {
    this._disabled = transform.booleanAttr(value);
    this._reflectAttribute('disabled', this._disabled);
 
    this[this._disabled ? 'setAttribute' : 'removeAttribute']('aria-disabled', this._disabled);
    this.classList.toggle('is-disabled', this._disabled);
    this._elements.input.disabled = this._disabled;
 
    this._setButtonState();
  }
 
  /**
   Whether this field is required or not.
   @type {Boolean}
   @default false
   @htmlattribute required
   @htmlattributereflected
   */
  get required() {
    return this._required || false;
  }
 
  set required(value) {
    this._required = transform.booleanAttr(value);
    this._reflectAttribute('required', this._required);
 
    this._elements.input.required = this._required;
  }
 
  /**
   Whether this field is readOnly or not. Indicating that the user cannot modify the value of the control.
   @type {Boolean}
   @default false
   @htmlattribute readonly
   @htmlattributereflected
   */
  get readOnly() {
    return this._readOnly || false;
  }
 
  set readOnly(value) {
    this._readOnly = transform.booleanAttr(value);
    this._reflectAttribute('readonly', this._readOnly);
 
    this._elements.input.readOnly = this._readOnly;
    this._setButtonState();
  }
 
  /**
   Inherited from {@link BaseFormField#invalid}.
   */
  get invalid() {
    return super.invalid;
  }
 
  set invalid(value) {
    super.invalid = value;
    this._elements.input.invalid = this._invalid;
  }
 
  /**
   Inherited from {@link BaseFormField#labelledBy}.
   */
  get labelledBy() {
    return super.labelledBy;
  }
 
  set labelledBy(value) {
    super.labelledBy = value;
    // in case the user focuses the buttons, he will still get a notion of the usage of the component
    this[this.labelledBy ? 'setAttribute' : 'removeAttribute']('aria-labelledby', this.labelledBy);
  }
 
  /**
   Short hint that describes the expected value of the NumberInput. It is displayed when the NumberInput is empty.
 
   @type {String}
   @default ""
   @htmlattribute placeholder
   @htmlattributereflected
   */
  get placeholder() {
    return this._elements.input.placeholder || '';
  }
 
  set placeholder(value) {
    value = transform.string(value);
    this._reflectAttribute('placeholder', value);
 
    this._elements.input.placeholder = value;
  }
 
  // overrides the behavior from BaseFormField
  reset() {
    // since there is an internal value, this one handles the reset
    this._elements.input.reset();
  }
 
  // overrides the behavior from BaseFormField
  clear() {
    // since there is an internal value, this one handles the clear
    this._elements.input.clear();
  }
 
  /**
   Increments the value by <code>step</code>. If the current value is <code>null</code> or <code>''</code>, it is
   considered as 0. The new value will always respect the <code>min</code> and <code>max</code> values if available.
   */
  stepUp() {
    // uses the Number representation since it simplifies the calculations
    const value = this.valueAsNumber;
    const step = this._getActualStep();
    if (isNaN(value)) {
      this.value = this.max !== null ? Math.min(step, this.max) : step;
    } else {
      const newValue = handleDecimalOperation('+', value, step);
 
      this.value = this.max !== null ? Math.min(newValue, this.max) : newValue;
    }
  }
 
  /**
   Decrements the value by <code>step</code>. If the current value is <code>null</code> or <code>''</code>, it is
   considered as 0. The new value will always respect the <code>min</code> and <code>max</code> values if available.
   */
  stepDown() {
    // uses the Number representation since it simplifies the calculations
    const value = this.valueAsNumber;
    const step = this._getActualStep();
    if (isNaN(value)) {
      this.value = this.min !== null ? Math.max(-step, this.min) : -step;
    } else {
      const newValue = handleDecimalOperation('-', value, step);
 
      this.value = this.min !== null ? Math.max(newValue, this.min) : newValue;
    }
  }
 
  /**
   If the value is 'any' there is no allowed step, that means we incremenet with the default (as if the step was not
   defined).
 
   @returns {Number} the valid step according to the specs.
 
   @ignore
   */
  _getActualStep() {
    return this.step === 'any' ? 1 : this.step;
  }
 
  /**
   Checks if the current NumberInput is valid or not. This is done by checking that the current value is between the
   provided <code>min</code> and <code>max</code> values. This check is only performed on user interaction.
 
   @ignore
   */
  _validateInputValue() {
    this.invalid = this.value !== '' && (isNaN(Number(this.value)) ||
      (this.max !== null && this.value > this.max || this.min !== null && this.value < this.min) ||
      this.step !== 'any' && handleDecimalOperation('%', Number(this.value), this._getActualStep()) !== 0);
  }
 
  /**
   Sets the correct state of the buttons based on <code>disabled</code>, <code>min</code>, <code>max</code> and
   <code>readOnly</code> properties.
 
   @ignore
   */
  _setButtonState() {
    this._elements.stepUp.disabled = this.disabled || this.max !== null && this.value >= this.max || this.readOnly;
    this._elements.stepDown.disabled = this.disabled || this.min !== null && this.value <= this.min || this.readOnly;
  }
 
  /**
   Triggers a change event. This is only done if the provided values are different.
 
   @param {String} newValue
   The new value of the component.
   @param {String} oldValue
   The old value of the component.
 
   @private
   */
  _triggerChange(newValue, oldValue) {
    // if the underlaying value stayed the same, there no need to trigger an event
    if (newValue !== oldValue) {
      this.trigger('change');
    }
  }
 
  /**
   Flags a touchstart or pointer event so that we can determine if an event originates from a touch screen interaction
   or from a mouse interaction. An event originating from a mouse interaction should shift the focus to the input,
   while an event originating from a touch interaction should not change the focus. On a touch screen, if the user
   presses the increment or decrement button, focus should not shift to the input and open the software keyboard.
 
   @ignore
   */
  _onTouchStart(event) {
    if (event.type === 'touchstart' || event.pointerType !== 'mouse' && event.pointerType !== MSPOINTER_TYPE_MOUSE) {
      flagTouchStart = true;
    }
  }
 
  /**
   Per WAI-ARIA spinbutton design pattern, http://www.w3.org/TR/wai-aria-practices/#spinbutton, shift focus to the
   input if it does not currently have focus. We make an exception for touch devices, because a better user
   experience is for the focus to remain on an increment or decrement button without shifting focus and opening the
   soft keyboard.
 
   @ignore
   */
  _setFocusToInput() {
    if (!flagTouchStart && document.activeElement !== this._elements.input) {
      this._elements.input.focus();
    }
    flagTouchStart = false;
  }
 
  /**
   Handles the click on the step up button. It causes the NumberInput to step up its value and returns the focus back
   to the input. This way the clicked button does not get focus.
 
   @emits {change}
   @ignore
   */
  _onStepUpButtonClick(event) {
    event.preventDefault();
 
    // stores the old value before stepup
    const oldValue = this.value;
 
    flagStepButtonClick = event.type === 'click';
 
    this._setFocusToInput();
I
    this.stepUp();
 
    // we only do this on user interaction
    this._validateInputValue();
 
    // checks if we need to trigger a change event
    this._triggerChange(this.value, oldValue);
  }
 
  /**
   Handles the click on the step down button. It causes the NumberInput to step down its value and returns the focus
   back to the input. This way the clicked button does not get focus.
I
   @emits {change}
   @ignore
   */
  _onStepDownButtonClick(event) {
    event.preventDefault();
 
    // stores the old value before stepdown
    const oldValue = this.value;
 
    flagStepButtonClick = event.type === 'click';
 
    this._setFocusToInput();
 
    this.stepDown();
 
    // we only do this on user interaction
    this._validateInputValue();
 
    // checks if we need to trigger a change event
    this._triggerChange(this.value, oldValue);
  }
 
  /**
   Handles the home key press. If a max has been set, the value will be modified to match it, otherwise the key is
   ignored.
 
   @ignore
   */
  _onKeyHome(event) {
    event.preventDefault();
 
    // stops interaction if the numberinput is disabled or readonly
    if (this.disabled || this.readOnly) {
      return;
    }
 
    // sets the max value only if it exists
    if (this.max !== null) {
      // stores the old value before setting the max
      const oldValue = this.value;
 
      // When appropriate flagStepButtonClick will trigger a live region update.
      flagStepButtonClick = true;
 
      this.value = this.max;
 
      // checks if we need to trigger a change event
      this._triggerChange(this.value, oldValue);
    }
 
    this._setFocusToInput();
  }
 
  /**
   Handles the end key press. If a min has been set, the value will be modified to match it, otherwise the key is
   ignored.
 
   @ignore
   */
  _onKeyEnd(event) {
    event.preventDefault();
 
    // stops interaction if the numberinput is disabled or readonly
    if (this.disabled || this.readOnly) {
      return;
    }
 
    // sets the min value only if it exists
    if (this.min !== null) {
      // stores the old value before setting the min
      const oldValue = this.value;
 
      // When appropriate, flagStepButtonClick will trigger a live region update.
      flagStepButtonClick = true;
 
      this.value = this.min;
 
      // checks if we need to trigger a change event
      this._triggerChange(this.value, oldValue);
    }
 
    this._setFocusToInput();
  }
 
  /**
   Handles the up action by steping up the NumberInput. It prevents the default action.
 
   @ignore
   */
  _onKeyUp(event) {
    event.preventDefault();
 
    // stops interaction if the numberinput is disabled or readonly
    if (this.disabled || this.readOnly) {
      return;
    }
 
    this._onStepUpButtonClick(event);
  }
 
  /**
   Handles the down action by steping down the NumberInput. It prevents the default action.
 
   @ignore
   */
  _onKeyDown(event) {
    event.preventDefault();
 
    // stops interaction if the numberinput is disabled or readonly
    if (this.disabled || this.readOnly) {
      return;
    }
 
    this._onStepDownButtonClick(event);
  }
 
  /**
   Handles the Mousewheel to increment/decrement values.
 
   @ignore
   */
  _onInputMouseWheel(event) {
    // stops interaction if the numberinput is disabled or readonly or is not focused (this is the case where its hovered but not focused)
    if (this.disabled || this.readOnly || this._elements.input !== document.activeElement) {
      return;
    }
 
    // else we prevent the default event like user scrolling the page and handle the mouse wheel input
    event.preventDefault();
 
    // stores the old value to calculate the change
    const oldValue = this.value;
 
    const delta = Math.max(-1, Math.min(1, event.wheelDelta || -event.detail || event.deltaY));
    if (delta < 0) {
      this.stepDown();
    } else {
      this.stepUp();
    }
 
    // checks if we need to trigger a change event
    this._triggerChange(this.value, oldValue);
  }
 
  /**
   Overrides the method from formField to be able to add validation after the user has changed the value.
 
   @private
   */
  _onInputChange(event) {
    // stops the current event
    event.stopPropagation();
 
    // we only do this on user interaction
    this._validateInputValue();
 
    // we force the sync of the value,invalid and disabled properties
    this.value = this.value;
    this.invalid = this.invalid;
    this.disabled = this.disabled;
 
    // we always trigger a change since it came from user interaction
    this.trigger('change');
  }

  /**
   Handles focus event.
 
   @ignore
   */
  _onFocus() {
    this.classList.add('is-focused');
    this._elements.input.classList.add('is-focused');
    this._elements.liveregion.removeAttribute('role');
    this._elements.liveregion.removeAttribute('aria-hidden');
  }
 
  /**
   Handles blur event.
 
   @ignore
   */
  _onBlur() {
    this.classList.remove('is-focused');
    this._elements.input.classList.remove('is-focused');
 
    // clear liveregion
    this._elements.liveregion.setAttribute('role', 'presentation');
    this._elements.liveregion.setAttribute('aria-hidden', true);
    this._clearLiveRegion();
  }
 
  /** @ignore */
  _clearLiveRegion() {
    const liveregion = this._elements.liveregion;
    if (liveregion.firstChild) {
      liveregion.removeChild(liveregion.firstChild);
    }
  }
 
  /** @ignore */
  _updateLiveRegion(value) {
    let textNode;
 
    clearTimeout(clearLiveRegionTimeout);
    this._clearLiveRegion();
 
    if (value && value !== '') {
      textNode = document.createTextNode(value);
      window.requestAnimationFrame(() => {
        this._elements.liveregion.appendChild(textNode);
 
        clearLiveRegionTimeout = window.setTimeout(() => {
          this._clearLiveRegion();
        }, LIVEREGION_TIMEOUT_DELAY);
      });
    }
  }
 
  /** @ignore */
  static get observedAttributes() {
    return super.observedAttributes.concat([
      'min',
      'max',
      'step',
      'placeholder'
    ]);
  }
 
  /** @ignore */
  render() {
    super.render();
 
    this.classList.add(CLASSNAME);
 
    // Default reflected attributes
    if (!this._step) {
      this.step = 1;
    }
 
    // a11y
    this.setAttribute('role', 'group');
 
    if (this._elements.input.type === 'text') {
      this._elements.input.setAttribute('role', 'spinbutton');
    }
 
    const frag = document.createDocumentFragment();
 
    const templateHandleNames = ['presentation', 'input'];
 
    // Render main template
    frag.appendChild(this._elements.input);
    frag.appendChild(this._elements.presentation);
 
    while (this.firstChild) {
      const child = this.firstChild;
      if (child.nodeType === Node.TEXT_NODE ||
        child.nodeType === Node.ELEMENT_NODE && templateHandleNames.indexOf(child.getAttribute('handle')) === -1) {
        // Add non-template elements to the frag
        frag.appendChild(child);
      } else {
        // Remove anything else
        this.removeChild(child);
      }
    }
 
    this.appendChild(frag);
  }
});
 
export default NumberInput;