rails_accordion 0.1.12.pre.beta → 0.2.0.pre.beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/app/assets/builds/rails_accordion.css +1 -1
- data/app/assets/builds/rails_accordion.js +41 -20
- data/app/assets/builds/rails_accordion.js.map +3 -3
- data/app/components/accordion_component.rb +5 -2
- data/app/javascript/controllers/accordion_controller.js +25 -1
- data/lib/generators/rails_accordion/templates/rails_accordion.tt +1 -0
- data/lib/rails_accordion/configuration.rb +2 -1
- data/lib/rails_accordion/version.rb +1 -1
- metadata +1 -1
@@ -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
|
-
|
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
|
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.
|
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,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
|
|