All files / packages/sds-components/src/sds/progress progress.js

100% Statements 37/37
100% Branches 7/7
100% Functions 14/14
100% Lines 37/37

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              18x 18x 18x 18x 18x 18x 18x               1x     1x 1x                 34x     1x                 34x     32x                 68x     5x                 68x     5x                 68x     4x                       34x 34x 1x 1x   33x   1x 1x   32x                       10x 10x 10x                     34x 34x 2x     10x   22x         1x      
import { LightningElement, api } from 'lwc';
import { normalizeBoolean, normalizeInput, reflectAttribute } from 'sds/utils';
import 'sds/privateThemeProvider';
 
export default class Progress extends LightningElement {
  static shadowSupportMode = 'native';
 
  _id = 'progress-id';
  _labelId = 'label-id';
  _labelHidden = false;
  _max = '100';
  _min = '0';
  _indicatorValue;
  _value = '0';
 
  /**
   * Visually hides the label.
   * @type {boolean}
   */
  @api
  get labelHidden() {
    return this._labelHidden;
  }
  set labelHidden(value) {
    this._labelHidden = normalizeBoolean(value);
    reflectAttribute(this, 'label-hidden', this._labelHidden);
  }
 
  /**
   * The id of the progress bar. Associates the label with the progress.
   *
   * @type {string}
   */
  @api
  get id() {
    return this._id;
  }
  set id(value) {
    this._id = normalizeInput(value);
  }
 
  /**
   * The interpolated value converted to a CSS property and percentage.
   *
   * @type {string}
   */
  get indicatorValue() {
    return this._indicatorValue;
  }
  set indicatorValue(value) {
    this._indicatorValue = `inline-size: ${normalizeInput(value)}%`;
  }
 
  /**
   * The maximum value of the progress range.
   *
   * @type {string}
   */
  @api
  get max() {
    return this._max;
  }
  set max(value) {
    this._max = normalizeInput(value);
  }
 
  /**
   * The minimum value of the progress range.
   *
   * @type {string}
   */
  @api
  get min() {
    return this._min;
  }
  set min(value) {
    this._min = normalizeInput(value);
  }
 
  /**
   * The current value of the progress.
   *
   * @type {string}
   */
  @api
  get value() {
    return this._value;
  }
  set value(value) {
    this._value = normalizeInput(value);
  }
 
  /**
   * Validates the max and min values and if the value is between them.
   *
   * @param {number} max
   * @param {number} min
   * @param {number} value
   * @returns {boolean}
   */
  _validateRange(max, min, value) {
    const isSameValue = max === min;
    if (isSameValue) {
      console.warn(`Max and Min cannot be the same value: max is ${max} and min is ${min}`);
      return false;
    }
 
    const isOutOfRange = Number(value) < Number(min) || Number(value) > Number(max);
    if (isOutOfRange) {
      console.warn(`Value must be between ${min} and ${max}`);
      return false;
    }
 
    return true;
  }
 
  /**
   * Interpolates the value between the max and min values.
   *
   * @param {number} max
   * @param {number} min
   * @param {number} value
   * @returns {number}
   */
  _interpolateRange(max, min, value) {
    const range = max - min;
    const adjustedValue = value - min;
 
    return (adjustedValue / range) * 100;
  }
 
  /**
   * Lifecycle call after every render of the component.
   */
  renderedCallback() {
    const { _max: max, _min: min, _value: value } = this;
 
    if (!this._validateRange(max, min, value)) {
      return;
    }
    if (max !== '100' || min !== '0') {
      this.indicatorValue = this._interpolateRange(max, min, value);
    } else {
      this.indicatorValue = value;
    }
  }
}