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

0% Statements 0/89
0% Branches 0/32
0% Functions 0/38
0% Lines 0/88

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
import { LightningElement, api } from 'lwc';
import { reflectAttribute, normalizeBoolean, normalizeInput, hasSlotText } from 'sds/utils';
import 'sds/privateThemeProvider';
 
export default class InputCounter extends LightningElement {
  static shadowSupportMode = 'native';
 
  _ariaInvalid = false;
  _disabled = false;
  _form;
  _invalid = false;
  _max;
  _min;
  _name;
  _placeholder;
  _readonly = false;
  _required = false;
  _step = '1';
  _stepDownLabel;
  _stepUpLabel;
  _validityMessage;
  _value;
  _helpTextElement;
  _inputElement;
 
  @api id = 'input';
  @api label = 'Input Counter Label';
 
  /**
   * Indicates the entered value does not conform to the format expected.
   *
   * @type {boolean}
   * @default false
   */
  @api
  get ariaInvalid() {
    return this._ariaInvalid;
  }
  set ariaInvalid(value) {
    this._ariaInvalid = normalizeBoolean(value);
  }
 
  /**
   * Makes the element not mutable, interactable, or focusable.
   *
   * @type {boolean}
   * @default false
   */
  @api
  get disabled() {
    return this._disabled;
  }
  set disabled(value) {
    this._disabled = normalizeBoolean(value);
    reflectAttribute(this, 'disabled', this._disabled);
  }
 
  /**
   * Specifies the id of a form to associate the input with (that is, its form owner).
   * This attribute lets you place an input anywhere in the document but have it included with a form elsewhere in the document.
   * It can also override the form's action attribute.
   * Form attribute is removed from custom element and delegated to the input element
   *
   * @type {string}
   */
  @api
  get form() {
    return this._form;
  }
  set form(value) {
    this._form = normalizeInput(value);
  }
 
  /**
   * Returns the help text element, if it exists
   * Used to dynamically add/remove the associated ID to the input element if help text is present
   *
   * @returns {object} help text element
   * @private
   */
  get helpText() {
    this._helpTextElement = this._helpTextElement || this.template.querySelector('[part="help-text"]');
 
    return this._helpTextElement;
  }
 
  /**
   * Returns the form's input element
   *
   * @returns {object} input element
   * @private
   */
  get input() {
    this._inputElement = this._inputElement || this.template.querySelector('input');
 
    return this._inputElement;
  }
 
  /**
   * Allows customer to set invalid state on input
   * An example would be if the input is required and the value is empty on form submit,
   * since we are not using native form validation, we need to offer the ability set the invalid state on the input
   */
  @api
  get invalid() {
    return this._invalid;
  }
  set invalid(value) {
    this._invalid = normalizeBoolean(value);
    reflectAttribute(this, 'invalid', this._invalid);
  }
 
  /**
   * The maximum valid value.
   *
   * @type {string}
   */
  @api
  get max() {
    return this._max;
  }
  set max(value) {
    this._max = normalizeInput(value);
  }
 
  /**
   * The minimum valid value.
   *
   * @type {string}
   */
  @api
  get min() {
    return this._min;
  }
  set min(value) {
    this._min = normalizeInput(value);
  }
 
  /**
   * The name of the input element, used on form submit.
   * Name attribute is removed from custom element and delegated to the input element
   *
   * @type {string}
   */
  @api
  get name() {
    return this._name;
  }
  set name(value) {
    this._name = normalizeInput(value);
  }
 
  /**
   * Placeholder text of the input element.
   * Placeholder attribute is removed from custom element and delegated to the input element
   *
   * @type {string}
   */
  @api
  get placeholder() {
    return this._placeholder;
  }
  set placeholder(value) {
    this._placeholder = normalizeInput(value);
  }
 
  /**
   * Readonly is a Boolean attribute which, if present, indicates that the user cannot modify the value of the input.
   *
   * @type {boolean}
   * @default false
   */
  @api
  get readOnly() {
    return this._readonly;
  }
  set readOnly(value) {
    this._readonly = normalizeBoolean(value);
    reflectAttribute(this, 'readonly', this._readonly);
  }
 
  /**
   * Determines if the form control is required or not.
   *
   * @type {boolean}
   * @default false
   */
  @api
  get required() {
    return this._required;
  }
  set required(value) {
    this._required = normalizeBoolean(value);
    reflectAttribute(this, 'required', this._required);
  }
 
  /**
   * The granularity that the value must adhere to
   * @type {string}
   */
  @api
  get step() {
    return this._step;
  }
  set step(value) {
    this._step = normalizeInput(value);
  }
 
  /**
   * Label for the step down button
   * @type {string}
   */
  @api
  get stepDownLabel() {
    return this._stepDownLabel ? this._stepDownLabel : `Decrease value by ${this.step}`;
  }
  set stepDownLabel(value) {
    this._stepDownLabel = normalizeInput(value);
  }
 
  /**
   * Label for the step up button
   * @type {string}
   */
  @api
  get stepUpLabel() {
    return this._stepUpLabel ? this._stepUpLabel : `Increase value by ${this.step}`;
  }
  set stepUpLabel(value) {
    this._stepUpLabel = normalizeInput(value);
  }
 
  /**
   * Returns the validity object of the input element
   */
  @api
  get validity() {
    return Promise.resolve().then(() => this.input.validty);
  }
 
  /**
   * Returns the validity message of the input element
   */
  @api
  get validityMessage() {
    return this._validityMessage;
  }
 
  /**
   * The value of the input element
   * Value attribute is removed from custom element and delegated to the input element
   *
   * @type {string}
   */
  @api
  get value() {
    return this._value;
  }
  set value(value) {
    this._value = normalizeInput(value);
  }
 
  /**
   * If invalid, sets the aria-invalid attribute on the input element
   * and sets invalid attribute on custom element
   *
   * @private
   */
  isInvalid() {
    if (!this.input.validity.valid) {
      this.invalid = true;
      this._ariaInvalid = 'true';
    } else {
      this.invalid = false;
      this._ariaInvalid = 'false';
    }
  }
  /**
   * On blur event handler
   * Used to remove the "active" attribute from the custom element since the user is no longer interacting with it
   * Checks validity of the input element and sets the validity state
   *
   * @param {*} e - blur event
   * @returns {Object} - validity state
   */
  handleBlur(e) {
    this._value = e.target.value;
    this.hasAttribute('active') && this.removeAttribute('active');
    this.handleValidity();
    this.isInvalid();
    this.removeAttribute('focus');
  }
 
  /**
   * On click event handler
   * Used to focus the input element when the custom element is clicked
   *
   * @param {Object} e - click event
   */
  handleClick() {
    if (!this._disabled || !this._readonly) {
      this.input.focus();
      this.setAttribute('focus', '');
    }
  }
 
  /**
   * Slot change event on help text slot
   * Used to dynamically add/remove the associated ID to the input element if help text is present
   * Used to dynamically add "visible" part to the help text element if help text is found in the slot
   *
   * @param {*} e - slot change event
   */
  handleHelpTextSlotChange(e) {
    let childElements = e.target.assignedElements({ flatten: true });
    if (childElements.length > 0) {
      if (this.helpText) {
        this.helpText.id = 'id-help-text';
        this.input.setAttribute('aria-describedby', 'id-help-text');
        this.helpText.setAttribute('part', 'help-text visible');
      }
    }
  }
 
  /**
   * Slot change event on the label slot
   * Used to check if the label has a slotted value, if not, we throw an error that one must be included
   * @param {*} e - slot change event
   */
  handleLabelSlotChange(e) {
    !hasSlotText(e) && console.error('A label value must be included for all form elements.');
  }
 
  /**
   * On input event handler
   * Used to set the "active" attribute on the custom element when the user is interacting with it
   * Checks validity of the input element and sets the validity state via aria-invalid
   *
   * @param {Object} e - input event
   */
  handleInput() {
    if (!this._disabled || !this._readonly) {
      !this.hasAttribute('active') && this.setAttribute('active', '');
    }
  }
 
  /**
   * Set the validity message based on the validity state of the input element
   *
   * @private
   */
  handleValidity() {
    if (this.input.validity.valueMissing) {
      this._validityMessage = `This field is required.`;
    } else if (this.input.validity.rangeOverflow) {
      this._validityMessage = `Value exceeds maximum of ${this.max}.`;
    } else if (this.input.validity.rangeUnderflow) {
      this._validityMessage = `Value is less than minimum of ${this.min}.`;
    } else {
      this._validityMessage = null;
    }
  }
 
  /**
   * Handle the stepUp() on the input element
   */
  handleStepUp() {
    this.input.stepUp();
  }
 
  /**
   * Handle the stepDown() on the input element
   */
  handleStepDown() {
    this.input.stepDown();
  }
 
  renderedCallback() {
    // Check if rendered for the first time and if so, set the validity state
    if (!this._rendered) {
      this._rendered = true;
      this.handleValidity();
      this.isInvalid();
    }
  }
}