All files / packages/sds-recipes/src/modules/c/radioGroup radioGroup.js

0% Statements 0/83
0% Branches 0/25
0% Functions 0/19
0% Lines 0/82

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                                                                                                                                                                                                                                                                                                                                                                                                                 
import { LightningElement, api } from 'lwc';
import { reflectAttribute, normalizeBoolean } from 'sds/utils';
 
export default class RadioGroup extends LightningElement {
  // Render component in native shadow mode
  static shadowSupportMode = 'native';
  static get formAssociated() {
    return true;
  }
 
  constructor() {
    super();
    this.internals = this.attachInternals();
  }
 
  _name;
  _required;
  _disabled;
  _value;
  _invalid;
  _radios;
 
  get radios() {
    if (!this._radios) {
      this._radios = this.querySelectorAll('sds-input-radio');
    }
    return this._radios;
  }
  set radios(newRadios) {
    this._radios = newRadios;
    this.enforceStateOnRadios();
  }
 
  @api helpMessage;
 
  @api get validity() {
    this.handleValidity();
    return this.internals.validity;
  }
 
  @api get invalid() {
    return this._invalid;
  }
  set invalid(isInvalid) {
    this._invalid = normalizeBoolean(isInvalid);
    reflectAttribute(this, 'invalid', this._invalid);
    this.enforceStateOnRadios();
  }
 
  @api get required() {
    return this._required;
  }
  set required(isRequired) {
    this._required = normalizeBoolean(isRequired);
    this.internals.ariaRequired = this._required;
    reflectAttribute(this, 'required', this._required);
    this.enforceStateOnRadios();
  }
 
  @api get disabled() {
    return this._disabled;
  }
  set disabled(isDisabled) {
    this._disabled = normalizeBoolean(isDisabled);
    reflectAttribute(this, 'disabled', this.disabled);
    this.enforceStateOnRadios();
  }
 
  @api get name() {
    return this._name;
  }
  set name(newName) {
    this._name = newName;
    reflectAttribute(this, 'name', this._name);
  }
 
  @api get value() {
    return this._value;
  }
  set value(newVal) {
    this._value = newVal;
    this.internals.setFormValue(newVal);
  }
 
  enforceStateOnRadios() {
    // impose group requirements on slotted radios
    this.radios.forEach((radio) => {
      radio.disabled = this.disabled;
      radio.required = this.required;
      radio.invalid = this.invalid;
      radio.helpMessage = this.helpMessage;
    });
  }
 
  handleValidity() {
    const { value } = this;
    if (!value && this.required) {
      this.internals.setValidity(
        {
          valueMissing: true,
        },
        'This field is required',
      );
    } else {
      this.internals.setValidity({});
    }
  }
 
  handleKeyDown = (event) => {
    let flag = false;
 
    switch (event.key) {
      case ' ':
      case 'Enter':
        this.setChecked(event.target);
        flag = true;
        break;
 
      case 'Up':
      case 'ArrowUp':
      case 'Left':
      case 'ArrowLeft':
        this.setCheckedByIndex(this.getCheckedRadioIndex() - 1);
        flag = true;
        break;
 
      case 'Down':
      case 'ArrowDown':
      case 'Right':
      case 'ArrowRight':
        this.setCheckedByIndex(this.getCheckedRadioIndex() + 1);
        flag = true;
        break;
 
      default:
        break;
    }
 
    if (flag) {
      event.stopPropagation();
      event.preventDefault();
    }
  };
 
  setChecked(radioToCheck) {
    // ensure the radio is known before acting
    if (![...this.radios].includes(radioToCheck)) {
      return;
    }
    this.radios.forEach((radio) => {
      if (radio !== radioToCheck) {
        radio.checked = false;
        radio.tabIndex = '-1';
      } else {
        this.value = radio.value;
        radio.checked = true;
        radio.tabIndex = '0';
        radio.focus();
        this.dispatchEvent(new CustomEvent('change'));
      }
    });
  }
 
  setCheckedByIndex = (indexToCheck) => {
    // wrap to end
    if (indexToCheck < 0) {
      indexToCheck = this.radios.length - 1;
    }
    // wrap to front
    if (indexToCheck > this.radios.length - 1) {
      indexToCheck = 0;
    }
    this.setChecked(this.radios[indexToCheck]);
  };
 
  getCheckedRadioIndex = () => {
    return [...this.radios].findIndex((radioNode) => radioNode.checked);
  };
 
  handleRadioChange = ({ target }) => {
    this.setChecked(target);
  };
 
  handleSlotChange = () => {
    const radios = this.querySelectorAll('sds-input-radio');
    this.radios = radios;
 
    // initialize tab targeting for kb support
    // tab target is either checked radio or first radio
    const checkedIndex = this.getCheckedRadioIndex();
    const tabTargetIndex = checkedIndex === -1 ? 0 : checkedIndex;
    this.radios.forEach((radioNode, i) => {
      radioNode.tabIndex = tabTargetIndex === i ? '0' : '-1';
    });
 
    radios.forEach((radio) => {
      radio.addEventListener('change', this.handleRadioChange);
    });
  };
}