Class: JapanPrefecturesComponent

Inherits:
Funicular::Component
  • Object
show all
Defined in:
lib/components/japan_prefectures_component.rb

Overview

Prefecture dropdown over the bundled JIS X 0401 list.

component Funicular::Plugins::JapanPrefectures::Component,
        value: prefecture_id,          # optional preselected JIS code
        select_id: "address-prefecture",
        select_class: "...",
        prompt: "選択してください",     # blank option label
        on_change: ->(id) { ... }      # Integer JIS code, nil for blank

Controlled component: the parent owns the value and re-renders with a new one after on_change. The option list is fixed and lives in lib/japan_prefectures/data.rb.

Instance Method Summary collapse

Instance Method Details

#handle_change(_event) ⇒ Object



14
15
16
17
18
19
# File 'lib/components/japan_prefectures_component.rb', line 14

def handle_change(_event)
  node = @refs[:select]
  raw = node ? node[:value].to_s : ""
  callback = props[:on_change]
  callback.call(raw.empty? ? nil : raw.to_i) if callback
end

#renderObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/components/japan_prefectures_component.rb', line 21

def render
  value = props[:value].to_s
  select(
    ref: :select,
    id: props[:select_id],
    class: "funicular-japan-prefectures__select #{props[:select_class]}",
    onchange: :handle_change
  ) do
    option(value: "", selected: value.empty?) { props[:prompt] || "都道府県" }
    prefectures = Funicular::Plugins::JapanPrefectures::PREFECTURES
    i = 0
    size = prefectures.size
    while i < size
      pref = prefectures[i]
      pref_id = pref["id"].to_s
      option(value: pref_id, key: pref_id, selected: pref_id == value) do
        pref["name"]
      end
      i += 1
    end
  end
end