Sha256: 914ed6536e8d8d61167545818971fa604a6db7a9def4a22f33259a1bca29d828

Contents?: true

Size: 1.13 KB

Versions: 4

Compression:

Stored size: 1.13 KB

Contents

import Combobox from "hw_combobox/models/combobox/base"

Combobox.Validity = Base => class extends Base {
  _markValid() {
    if (this._valueIsInvalid) return

    this._forAllComboboxes(combobox => {
      if (this.hasInvalidClass) {
        combobox.classList.remove(this.invalidClass)
      }

      combobox.removeAttribute("aria-invalid")
      combobox.removeAttribute("aria-errormessage")
    })
  }

  _markInvalid() {
    if (this._valueIsValid) return

    this._forAllComboboxes(combobox => {
      if (this.hasInvalidClass) {
        combobox.classList.add(this.invalidClass)
      }

      combobox.setAttribute("aria-invalid", true)
      combobox.setAttribute("aria-errormessage", `Please select a valid option for ${combobox.name}`)
    })
  }

  get _valueIsValid() {
    return !this._valueIsInvalid
  }

  // +_valueIsInvalid+ only checks if `comboboxTarget` (and not `_actingCombobox`) is required
  // because the `required` attribute is only forwarded to the `comboboxTarget` element
  get _valueIsInvalid() {
    const isRequiredAndEmpty = this.comboboxTarget.required && !this.hiddenFieldTarget.value
    return isRequiredAndEmpty
  }
}

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hotwire_combobox-0.1.42 app/assets/javascripts/hw_combobox/models/combobox/validity.js
hotwire_combobox-0.1.41 app/assets/javascripts/hw_combobox/models/combobox/validity.js
hotwire_combobox-0.1.40 app/assets/javascripts/hw_combobox/models/combobox/validity.js
hotwire_combobox-0.1.39 app/assets/javascripts/hw_combobox/models/combobox/validity.js