Module: UI::CalendarBehavior
- Included in:
- Calendar, CalendarComponent
- Defined in:
- app/behaviors/ui/calendar_behavior.rb
Overview
UI::CalendarBehavior
Constant Summary collapse
- WEEKDAYS =
%w[Su Mo Tu We Th Fr Sa].freeze
Instance Method Summary collapse
-
#aria_label_text ⇒ Object
Generate aria-label based on mode.
-
#calendar_classes ⇒ Object
Default calendar classes.
-
#calendar_data_attributes ⇒ Object
Generate Stimulus controller data attributes.
-
#calendar_html_attributes ⇒ Object
Build complete HTML attributes hash for calendar container.
-
#disabled_dates_json ⇒ Object
Convert disabled dates to JSON array.
-
#ordered_weekdays ⇒ Object
Get weekdays ordered by week_starts_on.
-
#selected_json ⇒ Object
Convert selected dates to JSON array.
-
#selected_value ⇒ Object
Convert selected dates for form value.
- #use_native_select? ⇒ Boolean
Instance Method Details
#aria_label_text ⇒ Object
Generate aria-label based on mode
96 97 98 99 100 101 102 103 104 105 |
# File 'app/behaviors/ui/calendar_behavior.rb', line 96 def aria_label_text case @mode.to_s when "range" "Date range picker" when "multiple" "Multiple date picker" else "Date picker" end end |
#calendar_classes ⇒ Object
Default calendar classes
108 109 110 111 112 113 114 115 |
# File 'app/behaviors/ui/calendar_behavior.rb', line 108 def calendar_classes base = "p-3" if @number_of_months && @number_of_months > 1 "#{base} flex flex-col sm:flex-row space-y-4 sm:space-x-4 sm:space-y-0 #{@classes}".strip else "#{base} #{@classes}".strip end end |
#calendar_data_attributes ⇒ Object
Generate Stimulus controller data attributes
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'app/behaviors/ui/calendar_behavior.rb', line 36 def calendar_data_attributes attrs = { controller: "ui--calendar", ui__calendar_mode_value: @mode.to_s, ui__calendar_selected_value: selected_json, ui__calendar_month_value: @month.to_s, ui__calendar_number_of_months_value: @number_of_months, ui__calendar_week_starts_on_value: @week_starts_on, ui__calendar_locale_value: @locale, ui__calendar_min_date_value: @min_date&.to_s, ui__calendar_max_date_value: @max_date&.to_s, ui__calendar_disabled_value: disabled_dates_json, ui__calendar_show_outside_days_value: @show_outside_days, ui__calendar_fixed_weeks_value: @fixed_weeks, ui__calendar_year_range_value: @year_range, # Range constraints ui__calendar_min_range_days_value: @min_range_days, ui__calendar_max_range_days_value: @max_range_days, ui__calendar_exclude_disabled_value: @exclude_disabled, action: "keydown->ui--calendar#handleKeydown" } # Add UI Select controller if using custom select if @show_dropdowns && !use_native_select? attrs[:controller] = "ui--calendar ui--select-calendar-sync" end attrs.compact end |
#calendar_html_attributes ⇒ Object
Build complete HTML attributes hash for calendar container
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'app/behaviors/ui/calendar_behavior.rb', line 71 def calendar_html_attributes base_attrs = @attributes&.except(:data) || {} user_data = @attributes&.fetch(:data, {}) || {} # Merge data attributes, concatenating action values merged_data = calendar_data_attributes.merge(user_data) do |key, calendar_val, user_val| if key == :action # Concatenate Stimulus actions with space separator [calendar_val, user_val].compact.join(" ") else user_val end end base_attrs.merge( class: calendar_classes, role: "application", aria: { label: aria_label_text }, data: merged_data ) end |
#disabled_dates_json ⇒ Object
Convert disabled dates to JSON array
138 139 140 |
# File 'app/behaviors/ui/calendar_behavior.rb', line 138 def disabled_dates_json (@disabled_dates || []).map(&:to_s).to_json end |
#ordered_weekdays ⇒ Object
Get weekdays ordered by week_starts_on
143 144 145 |
# File 'app/behaviors/ui/calendar_behavior.rb', line 143 def ordered_weekdays WEEKDAYS.rotate(@week_starts_on || 0) end |
#selected_json ⇒ Object
Convert selected dates to JSON array
118 119 120 121 122 123 124 125 |
# File 'app/behaviors/ui/calendar_behavior.rb', line 118 def selected_json case @selected when Date then [@selected.to_s].to_json when Range then [@selected.begin.to_s, @selected.end.to_s].to_json when Array then @selected.map(&:to_s).to_json else [].to_json end end |
#selected_value ⇒ Object
Convert selected dates for form value
128 129 130 131 132 133 134 135 |
# File 'app/behaviors/ui/calendar_behavior.rb', line 128 def selected_value case @selected when Date then @selected.to_s when Range then "#{@selected.begin},#{@selected.end}" when Array then @selected.map(&:to_s).join(",") else "" end end |
#use_native_select? ⇒ Boolean
66 67 68 |
# File 'app/behaviors/ui/calendar_behavior.rb', line 66 def use_native_select? @use_native_select.nil? || @use_native_select end |