You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.8 KiB

3 years ago
  1. <urban-accordion>
  2. <div class="accordion">
  3. <div class="tabs mb-3">
  4. <ul>
  5. <li class={ getCurrentClass(index) } each={ (item, index) in state.items } onclick={ () => handleClick(event, index) }>
  6. <a>{ item }</a>
  7. </li>
  8. </ul>
  9. </div>
  10. </div>
  11. <script>
  12. /**
  13. *
  14. *
  15. *
  16. */
  17. export default {
  18. state: {
  19. items: [],
  20. index: 0
  21. },
  22. /**
  23. * getting innerHTML and remove it
  24. *
  25. */
  26. onBeforeMount()
  27. {
  28. this.content = this.root.innerHTML
  29. this.root.innerHTML = ''
  30. },
  31. /**
  32. * create wrapper for
  33. *
  34. * @param {object} props
  35. * @param {object} state
  36. *
  37. */
  38. onMounted(props, state) {
  39. const header = this.$('.tabs')
  40. state.wrapper = document.createElement('div')
  41. state.wrapper.innerHTML = this.content
  42. header.after(state.wrapper)
  43. // run through all items and add a css-class and current class to first element
  44. for (let i = 0; i < state.wrapper.children.length; i++) {
  45. state.items.push(state.wrapper.children[i].title)
  46. state.wrapper.children[i].classList.add('accordion__item')
  47. if (i === 0) {
  48. state.wrapper.children[i].classList.add('accordion__item--active')
  49. }
  50. }
  51. this.update()
  52. },
  53. /**
  54. * remove all active classes and add active-classes to clicked
  55. *
  56. * @param {object} event
  57. * @param {object} index
  58. *
  59. */
  60. handleClick(event, index) {
  61. for (let i = 0; i < this.state.wrapper.children.length; i++) {
  62. this.state.wrapper.children[i].classList.remove('accordion__item--active')
  63. if (i === index) {
  64. this.state.wrapper.children[i].classList.add('accordion__item--active')
  65. this.state.index = i
  66. }
  67. }
  68. this.update()
  69. },
  70. /**
  71. * getting class for active accordion in header
  72. *
  73. */
  74. getCurrentClass(index)
  75. {
  76. let classes = []
  77. if (index === this.state.index) {
  78. classes.push('is-active')
  79. }
  80. return classes.join(' ')
  81. }
  82. }
  83. </script>
  84. </urban-accordion>