rails_accordion 0.1.12.pre.beta → 0.2.0.pre.beta

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,10 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class AccordionComponent < ViewComponent::Base
4
+
4
5
  def initialize(**args)
5
6
  super
6
7
  @args = args.presence || {}
7
- set_controller
8
+ set_data_params
8
9
  end
9
10
 
10
11
  def call
@@ -13,8 +14,10 @@ class AccordionComponent < ViewComponent::Base
13
14
 
14
15
  private
15
16
 
16
- def set_controller
17
+ def set_data_params
17
18
  @args[:data] ||= {}
18
19
  @args[:data][:controller] = [@args[:data][:controller], "accordion"].compact.join(" ")
20
+ @args[:data][:multiple_open] = @args.delete(:multiple_open) || RailsAccordion.configuration.multiple_open
21
+ @args[:data][:default_state] = @args.delete(:default_state) || RailsAccordion.configuration.default_state
19
22
  end
20
23
  end
@@ -3,6 +3,24 @@ import { Controller } from "@hotwired/stimulus";
3
3
  export default class extends Controller {
4
4
  connect() {
5
5
  this.initAccordion();
6
+
7
+ this.initDefaultState()
8
+ }
9
+
10
+ initDefaultState() {
11
+ const items = this.element.querySelectorAll('.accordion_item');
12
+
13
+ switch($(this.element).data('default-state')) {
14
+ case 'all_closed':
15
+ this.hideAll(items);
16
+ break;
17
+ case 'all_opened':
18
+ this.showAll(items);
19
+ break;
20
+ case 'first_opened':
21
+ this.hideAll(items)
22
+ this.open(items[0].querySelector('.accordion_content'))
23
+ }
6
24
  }
7
25
 
8
26
  initAccordion() {
@@ -16,7 +34,9 @@ export default class extends Controller {
16
34
  if (content.classList.contains('accordion_active')) {
17
35
  this.hide(content);
18
36
  } else {
19
- this.hideAll(items);
37
+ if ($(this.element).data('multiple-open') != true) {
38
+ this.hideAll(items);
39
+ }
20
40
  this.open(content);
21
41
  }
22
42
  });
@@ -27,6 +47,10 @@ export default class extends Controller {
27
47
  items.forEach((item) => this.hide(item.querySelector('.accordion_content')));
28
48
  }
29
49
 
50
+ showAll(items) {
51
+ items.forEach((item) => this.open(item.querySelector('.accordion_content')));
52
+ }
53
+
30
54
  hide(item) {
31
55
  item.classList.remove("accordion_active")
32
56
  item.style.height = 0;
@@ -1,4 +1,5 @@
1
1
  RailsAccordion.configure do |config|
2
2
  config.default_state = :all_closed # :all_closed, :all_opened, :first_opened
3
3
  config.animation_duration = 300 # in ms
4
+ config.multiple_open = false
4
5
  end
@@ -1,10 +1,11 @@
1
1
  module RailsAccordion
2
2
  class Configuration
3
- attr_accessor :default_state, :animation_duration
3
+ attr_accessor :default_state, :animation_duration, :multiple_open
4
4
 
5
5
  def initialize
6
6
  @default_state = :closed
7
7
  @animation_duration = 300
8
+ @multiple_open = false
8
9
  end
9
10
  end
10
11
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsAccordion
4
- VERSION = "0.1.12-beta"
4
+ VERSION = "0.2.0-beta"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_accordion
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.12.pre.beta
4
+ version: 0.2.0.pre.beta
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ahmadshoh Nasrullozoda