rubycentral/cfp-app

View on GitHub
app/javascript/controllers/sub_nav_controller.js

Summary

Maintainability
A
0 mins
Test Coverage
import { Controller } from 'stimulus'

export default class extends Controller {
  static targets = ['hideable']
  static values = {
    displayedId: String,
  }

  connect() {
    this.hideExceptDisplayedId()
  }

  hideExceptDisplayedId() {
    this.hideableTargets.forEach(target => {
      if(target.id != this.displayedIdValue) {
        target.classList.add('hidden');
      } else {
        target.classList.remove('hidden')
      }
    })
  }

  updateDisplay(e) {
    e.preventDefault()
    document.querySelector('.selected')?.classList.remove('selected')
    e.target.classList.add('selected')
    this.displayedIdValue = e.target.dataset.displayedid
  }

  displayedIdValueChanged() {
    this.hideExceptDisplayedId()
  }
}