tam_select 1.2.4 → 1.4.1

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.
@@ -1,72 +1,140 @@
1
+ require "digest"
2
+ require "json"
1
3
  require "rails/generators"
4
+ require "tam_select/version"
2
5
 
3
6
  module TamSelect
4
7
  module Generators
8
+ # Sets an app up to load Tam Select from the gem (importmap) or the npm
9
+ # package (jsbundling), so updating is `bundle update tam_select` or
10
+ # `npm update tam-select`. Safe to re-run; it also upgrades apps installed
11
+ # by tam_select 1.3 and earlier, which copied every file into the app.
5
12
  class InstallGenerator < Rails::Generators::Base
6
- source_root File.expand_path("../../..", __dir__)
13
+ source_root File.expand_path("templates", __dir__)
7
14
 
8
- desc "Install tam_select JavaScript, Stimulus, Simple Form and Rails integration"
15
+ desc "Set up Tam Select so it updates with `bundle update tam_select` (importmap) or `npm update tam-select` (jsbundling)"
9
16
 
10
- def copy_javascript
11
- copy_file "src/tam-select.js", "app/javascript/tam_select/tam_select.js"
12
- copy_file "lib/generators/tam_select/templates/tam_select_controller.js", "app/javascript/controllers/tam_select_controller.js"
17
+ LEGACY_LIBRARY = "app/javascript/tam_select/tam_select.js".freeze
18
+ LEGACY_PIN = /^\s*pin\s+["']tam_select["'],\s*to:\s*["']tam_select\/tam_select\.js["'].*\n?/
19
+ LEGACY_ALIAS = / ?--alias:tam_select=\.\/app\/javascript\/tam_select\/tam_select\.js/
20
+ CONTROLLER = "app/javascript/controllers/tam_select_controller.js".freeze
21
+ TAILWIND_CSS = "app/assets/tailwind/application.css".freeze
22
+ TAILWIND_IMPORT = '@import "../builds/tailwind/tam_select";'.freeze
23
+
24
+ def remove_legacy_copies
25
+ @kept_legacy_files = []
26
+ legacy_digests.each do |path, digests|
27
+ next unless File.exist?(destination_path(path))
28
+
29
+ if digests.include?(Digest::SHA256.file(destination_path(path)).hexdigest)
30
+ remove_file path
31
+ else
32
+ @kept_legacy_files << path
33
+ end
34
+ end
35
+ empty_directory_removed("app/javascript/tam_select")
36
+ empty_directory_removed("app/inputs")
13
37
  end
14
38
 
15
- def configure_importmap
16
- importmap = "config/importmap.rb"
17
- unless File.exist?(destination_path(importmap))
18
- say "Importmap was not detected. For jsbundling/esbuild, alias tam_select to app/javascript/tam_select/tam_select.js.", :yellow
19
- return
39
+ def configure_javascript
40
+ if importmap?
41
+ configure_importmap
42
+ elsif File.exist?(destination_path("package.json"))
43
+ configure_jsbundling
44
+ else
45
+ say "Neither config/importmap.rb nor package.json was found; import Tam Select from the tam-select npm package.", :yellow
20
46
  end
47
+ end
48
+
49
+ def configure_tailwind
50
+ return unless importmap?
21
51
 
22
- pin = 'pin "tam_select", to: "tam_select/tam_select.js"'
23
- contents = File.read(destination_path(importmap))
24
- existing_pin = /^\s*pin\s+["']tam_select["'].*$/
52
+ if File.exist?(destination_path(TAILWIND_CSS))
53
+ css = File.read(destination_path(TAILWIND_CSS))
54
+ return if css.include?(TAILWIND_IMPORT)
25
55
 
26
- if contents.match?(existing_pin)
27
- gsub_file importmap, existing_pin, pin unless contents.match?(/^#{Regexp.escape(pin)}$/)
56
+ if css.match?(/^@import\s+["']tailwindcss["'];?[ \t]*$/)
57
+ inject_into_file TAILWIND_CSS, "#{TAILWIND_IMPORT}\n", after: /^@import\s+["']tailwindcss["'];?[ \t]*\n/
58
+ else
59
+ append_to_file TAILWIND_CSS, "\n#{TAILWIND_IMPORT}\n"
60
+ end
28
61
  else
29
- append_to_file importmap, "\n#{pin}\n"
62
+ say "Add Tam Select's classes to your Tailwind build. With tailwindcss-rails 4.4+, add", :yellow
63
+ say " #{TAILWIND_IMPORT}"
64
+ say "to your Tailwind CSS entry point."
30
65
  end
31
66
  end
32
67
 
33
- def copy_simple_form_input
34
- if simple_form_available?
35
- copy_file "lib/generators/tam_select/templates/tam_select_input.rb", "app/inputs/tam_select_input.rb"
68
+ def copy_initializer
69
+ copy_file "tam_select.rb", "config/initializers/tam_select.rb", skip: true
70
+ end
71
+
72
+ def copy_remote_controller
73
+ copy_file "tam_select_remote_controller.rb", "app/controllers/tam_select_remote_controller.rb", skip: true
74
+ end
75
+
76
+ def report
77
+ say "\nTam Select is set up.", :green
78
+ if importmap?
79
+ say "The library, Stimulus controller, helper, Simple Form input and concerns load from the gem."
80
+ say "Update with: bundle update tam_select"
36
81
  else
37
- say "Simple Form is not installed; skipped app/inputs/tam_select_input.rb. Standard Rails form helpers remain available.", :yellow
82
+ say "Run your package manager's install (for example `npm install`), then update with:"
83
+ say " bundle update tam_select && npm update tam-select"
38
84
  end
85
+
86
+ return if @kept_legacy_files.empty?
87
+
88
+ say "\nThese files differ from every released Tam Select version, so they were kept.", :yellow
89
+ say "They override the gem's copy and will not update. Delete them to use the gem's version:", :yellow
90
+ @kept_legacy_files.each { |path| say " #{path}" }
39
91
  end
40
92
 
41
- def copy_controller_concern
42
- copy_file "lib/generators/tam_select/templates/tam_select_paginatable.rb", "app/controllers/concerns/tam_select_paginatable.rb"
43
- copy_file "lib/generators/tam_select/templates/tam_select_remote.rb", "app/controllers/concerns/tam_select_remote.rb"
93
+ private
94
+
95
+ def configure_importmap
96
+ importmap = "config/importmap.rb"
97
+ # The gem pins tam_select itself. Drop the pin to the old copy once
98
+ # that copy is gone; a kept, customized copy keeps its pin.
99
+ return if File.exist?(destination_path(LEGACY_LIBRARY))
100
+ return unless File.read(destination_path(importmap)).match?(LEGACY_PIN)
101
+
102
+ gsub_file importmap, LEGACY_PIN, ""
44
103
  end
45
104
 
46
- def copy_remote_controller
47
- copy_file "lib/generators/tam_select/templates/tam_select_remote_controller.rb", "app/controllers/tam_select_remote_controller.rb"
105
+ def configure_jsbundling
106
+ package_json = "package.json"
107
+ unless File.exist?(destination_path(LEGACY_LIBRARY))
108
+ gsub_file package_json, LEGACY_ALIAS, "" if File.read(destination_path(package_json)).match?(LEGACY_ALIAS)
109
+ end
110
+
111
+ package = JSON.parse(File.read(destination_path(package_json)))
112
+ unless package.dig("dependencies", "tam-select") || package.dig("devDependencies", "tam-select")
113
+ package["dependencies"] = (package["dependencies"] || {}).merge("tam-select" => "^#{TamSelect::VERSION}")
114
+ create_file package_json, "#{JSON.pretty_generate(package)}\n", force: true
115
+ end
116
+
117
+ copy_file "tam_select_controller.js", CONTROLLER, skip: true
118
+ say "Register the controller if your app uses a controllers manifest: bin/rails stimulus:manifest:update", :yellow
119
+ say "Add Tam Select's classes to Tailwind: @source \"../../../node_modules/tam-select/src\"; (adjust the path to your CSS entry point)", :yellow
48
120
  end
49
121
 
50
- def copy_helper
51
- copy_file "lib/generators/tam_select/templates/tam_select_helper.rb", "app/helpers/tam_select_helper.rb"
122
+ def importmap?
123
+ File.exist?(destination_path("config/importmap.rb"))
52
124
  end
53
125
 
54
- def show_tailwind_instruction
55
- say "\ntam_select installed.", :green
56
- say "Tailwind CSS 4 scans app/javascript automatically in most Rails setups."
57
- say "If your setup uses explicit sources, add:"
58
- say '@source "../../javascript/tam_select/**/*.js";'
126
+ def legacy_digests
127
+ JSON.parse(File.read(File.expand_path("legacy_copies.json", __dir__)))
59
128
  end
60
129
 
61
- private
130
+ def empty_directory_removed(path)
131
+ directory = destination_path(path)
132
+ Dir.rmdir(directory) if Dir.exist?(directory) && Dir.empty?(directory)
133
+ end
62
134
 
63
135
  def destination_path(relative_path)
64
136
  File.join(destination_root, relative_path)
65
137
  end
66
-
67
- def simple_form_available?
68
- Gem.loaded_specs.key?("simple_form")
69
- end
70
138
  end
71
139
  end
72
140
  end
@@ -0,0 +1,48 @@
1
+ {
2
+ "app/javascript/tam_select/tam_select.js": [
3
+ "00e7ce9f1caa03d8ee874769efeb1033e34ebc247aed0b6b8f4e53027b898d22",
4
+ "074f711538c61364e968b2de357d400078c33738ed9e86a6f25df365eabac405",
5
+ "1ce4b16bd92e941d38f8975602159ae443a93e3cf446224d81797ca927599806",
6
+ "37f121f6d307c91f915182dc6b0f8230ec20edd58d65d0a3c5cfb1a65aa1661f",
7
+ "3a125cffc95fc2ac58b2f3858f8245c7d9099443db73655470aefea19bc0f773",
8
+ "50e8dc60488fe80f663a3460fae0b309ec20bbe6c71baa92db22d44fae5d849f",
9
+ "59dc7a6b7660a2ee16024c0f39aba5fa3f06ab18d74378c1546a6b5d0a9bb1f4",
10
+ "70042736e698758f6872c1649e9a28571aebb43d2eba2bf343832a1c6deaa23f",
11
+ "7789e424ef8b529ee7eb84d3dfc14016b6ece04f6aefaed237634239bc197df2",
12
+ "908573e5836f7f22a45f9ba11588111eb3d75da040a4eb2febe9610b33f3137e",
13
+ "984056b5585b238d834402628304bc1250b4f99879913fcce1056fc6229e404e",
14
+ "a952da4ddaa707e30517461fcd360579304a1f57c3049aa7660c23a1e27b5e57",
15
+ "d5a7717161c1f2086907c6e35ebdedd6242307b4643e414621e98c4d210a3c0e",
16
+ "d6f6eaae36899689d1f140b77dcc1c624f3969405e66b5ddd788a9ba1dd395fc",
17
+ "e5b5a0409d0c5f293b89aea38328da849c541389fba17c167cfba55d1ca68547",
18
+ "fee115d2f7fa3936f89c099199426e6bde8b1177adb8a0f9878daa5f542d213d"
19
+ ],
20
+ "app/javascript/controllers/tam_select_controller.js": [
21
+ "53d5b3a447c9f4539f58d25348db24fe16ba971e264eaa8a74e2b9f2f4a70bc2",
22
+ "68b7fdc9a4cfec978944d9c92679443324d2fe61063cd7e9645d716ecb8ec257",
23
+ "9b64a8859889a9859b0485d109dfd6268ccb4974bc9c282cb8ed87638dc3ba55",
24
+ "a6bdab20d6a32c68caa186fa2614c7364195421175f68496069d53313c7af338",
25
+ "a762210c2efa47cd4a31339cddeed88925ffe6f63484530aec28f2074472f798"
26
+ ],
27
+ "app/helpers/tam_select_helper.rb": [
28
+ "1489ba8ac87ce944f407f6969c10ab2f059d930004c1ad0b5ded2048a5f4a93c",
29
+ "167075228500b54202b754a0e5104e2704b806b710f0d9b3d869d3a38a5ccefb",
30
+ "1a927962e5ddb9d469e5f5588e42361d836e173f7df98bd7546e6405f3b7c0d8",
31
+ "5c54110384d4b34150990205b25ece79227a7320d12c79065d8c7ef7c994b4db"
32
+ ],
33
+ "app/inputs/tam_select_input.rb": [
34
+ "6f0a1800350731cd3be2429af6e38a6d8e117190fbfc35d494db178030ffd1a1",
35
+ "92584709ba5c1d1e0d8195c33294da8c3bcb9ee59708650f091108e6b034acbd",
36
+ "abe0b7b1f36c9ef457b11979b66f16e1b9fb1f11d2cd67c75fb7cd9927f02d3c",
37
+ "ca214390affa864b1653e525214b2c238f9cfb0185e1c2c71d4ae42bfeb6a030"
38
+ ],
39
+ "app/controllers/concerns/tam_select_paginatable.rb": [
40
+ "59b4a4e7b6954123f065a465c34a8a65c7f80ed5798f6f54b0eaef5e63e0b354",
41
+ "735b1d05144b5ee9991e9050bfc266a3eb031ed5a427774532cb06beb71f6383",
42
+ "b5c258c31c96589409d253187b7fe1e87d33d81dd07e62915a077525977a2d24"
43
+ ],
44
+ "app/controllers/concerns/tam_select_remote.rb": [
45
+ "91fff90c5e7cb31a23402569b217bfd43f6e3250238fc51308d3de5fd4008881",
46
+ "c4d317349fc4978c3bce3dfd207adf5ef277bd1d0503d27a36bad74ab609d566"
47
+ ]
48
+ }
@@ -0,0 +1,8 @@
1
+ # Tam Select settings. Generated by `bin/rails generate tam_select:install`.
2
+ Rails.application.configure do
3
+ # Render every select as a Tam Select: Simple Form's `f.input :x, collection:`
4
+ # and `as: :select`, and Rails' select_tag, f.select, collection_select and
5
+ # grouped_collection_select. Single selects get a blank option so they can be
6
+ # cleared. Opt a field out with `data: { tam_select: false }`.
7
+ config.tam_select.default_select = true
8
+ end
@@ -1,26 +1,3 @@
1
- import { Controller } from "@hotwired/stimulus"
2
- import TamSelect from "tam_select"
3
-
4
- export default class extends Controller {
5
- static values = {
6
- options: { type: Object, default: {} }
7
- }
8
-
9
- connect() {
10
- this.instance = TamSelect.getInstance(this.element) || new TamSelect(this.element, this.optionsValue)
11
- }
12
-
13
- disconnect() {
14
- this.instance?.destroy()
15
- this.instance = null
16
- }
17
-
18
- optionsValueChanged() {
19
- if (!this.instance) return
20
- this.instance.destroy()
21
- this.instance = new TamSelect(this.element, this.optionsValue)
22
- }
23
-
24
- refresh() { this.instance?.refresh() }
25
- clear() { this.instance?.clear() }
26
- }
1
+ // Tam Select's Stimulus controller ships in the tam-select npm package.
2
+ // Update it with your package manager, for example: npm update tam-select
3
+ export { default } from "tam-select/stimulus"
@@ -0,0 +1,13 @@
1
+ require "generators/tam_select/install_generator"
2
+
3
+ module TamSelect
4
+ module Generators
5
+ # Upgrades an app installed by tam_select 1.3 or earlier: removes the
6
+ # copied files it never changed so the gem's versions load instead.
7
+ # After that, `bundle update tam_select` is the whole update.
8
+ class UpdateGenerator < InstallGenerator
9
+ desc "Switch an app installed by tam_select 1.3 or earlier to the gem-served files"
10
+ source_root File.expand_path("templates", __dir__)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,108 @@
1
+ require "json"
2
+
3
+ module TamSelect
4
+ # Makes Tam Select the default for every select rendered with Rails' own
5
+ # helpers: select_tag, select, collection_select and grouped_collection_select,
6
+ # and so form builders' f.select and friends. Enabled by
7
+ # `config.tam_select.default_select = true`; the engine then prepends this
8
+ # module to ActionView::Base and maps Simple Form's :select type to
9
+ # TamSelectInput. Opt a field out with `data: { tam_select: false }`.
10
+ module DefaultSelect
11
+ def select_tag(name, option_tags = nil, options = {})
12
+ options = DefaultSelect.html_options(options, template: self, option_tags: option_tags)
13
+ super(name, option_tags, options)
14
+ end
15
+
16
+ def select(object, method, choices = nil, options = {}, html_options = {}, &block)
17
+ html_options = DefaultSelect.html_options(html_options, template: self)
18
+ options = DefaultSelect.select_options(options, html_options, choices: choices) unless DefaultSelect.opted_out?(html_options)
19
+ super(object, method, choices, options, html_options, &block)
20
+ end
21
+
22
+ def collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
23
+ html_options = DefaultSelect.html_options(html_options, template: self)
24
+ options = DefaultSelect.select_options(options, html_options) unless DefaultSelect.opted_out?(html_options)
25
+ super(object, method, collection, value_method, text_method, options, html_options)
26
+ end
27
+
28
+ def grouped_collection_select(object, method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {})
29
+ html_options = DefaultSelect.html_options(html_options, template: self)
30
+ options = DefaultSelect.select_options(options, html_options) unless DefaultSelect.opted_out?(html_options)
31
+ super(object, method, collection, group_method, group_label_method, option_key_method, option_value_method, options, html_options)
32
+ end
33
+
34
+ class << self
35
+ # Adds the tam-select controller (and its default options) to a select's
36
+ # HTML options. For select_tag the same hash also carries include_blank.
37
+ def html_options(html, template:, option_tags: nil)
38
+ html = (html || {}).to_h.transform_keys(&:to_sym)
39
+ return html if opted_out?(html)
40
+
41
+ data = (html[:data] || {}).to_h.transform_keys(&:to_sym)
42
+ controllers = data[:controller].to_s.split
43
+ unless controllers.include?("tam-select")
44
+ data[:controller] = (controllers + ["tam-select"]).join(" ")
45
+ data[:tam_select_options_value] ||= defaults(template).to_json
46
+ end
47
+ html[:data] = data
48
+ html[:include_blank] = true if option_tags && add_blank?(html, html, option_tags: option_tags)
49
+ html
50
+ end
51
+
52
+ # Adds include_blank to the options hash of select/collection_select.
53
+ def select_options(options, html, choices: nil)
54
+ options = prompt_as_blank((options || {}).to_h.transform_keys(&:to_sym), html)
55
+ return options unless add_blank?(options, html, choices: choices)
56
+
57
+ options.merge(include_blank: true)
58
+ end
59
+
60
+ # Rails renders `prompt:` only while the field has no value, so once a
61
+ # value is chosen and the form re-renders the blank option is gone and
62
+ # the select can no longer be cleared. For single selects, render the
63
+ # prompt as an always-present blank option with the same text instead.
64
+ def prompt_as_blank(options, html)
65
+ return options if html[:multiple] || options.key?(:include_blank) || !options[:prompt]
66
+
67
+ prompt = options[:prompt]
68
+ options.except(:prompt).merge(include_blank: prompt.is_a?(String) ? prompt : true)
69
+ end
70
+
71
+ # A single select needs a blank option to be cleared and to show its
72
+ # placeholder. Add one unless the caller handles it or the choices
73
+ # already contain one.
74
+ def add_blank?(options, html, option_tags: nil, choices: nil)
75
+ return false if html[:multiple] || options.key?(:include_blank) || options.key?(:prompt)
76
+
77
+ !blank_present?(option_tags || choices)
78
+ end
79
+
80
+ def opted_out?(html)
81
+ data = html[:data] || html["data"]
82
+ return false unless data.respond_to?(:key?)
83
+
84
+ value = data.key?(:tam_select) ? data[:tam_select] : data["tam_select"]
85
+ value == false || value.to_s == "false"
86
+ end
87
+
88
+ private
89
+
90
+ def defaults(template)
91
+ template.respond_to?(:tam_select_options, true) ? template.tam_select_options : {}
92
+ end
93
+
94
+ def blank_present?(choices)
95
+ case choices
96
+ when String then choices.match?(/<option\b[^>]*\bvalue=(""|'')/)
97
+ when Hash then choices.values.any? { |value| value.to_s.empty? }
98
+ when Array
99
+ choices.any? do |choice|
100
+ value = choice.is_a?(Array) && choice.size > 1 ? choice[1] : choice
101
+ value.to_s.empty?
102
+ end
103
+ else false
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
@@ -1,6 +1,56 @@
1
1
  require "rails/engine"
2
2
 
3
3
  module TamSelect
4
+ # Serves the JavaScript, Stimulus controller, helper, Simple Form input and
5
+ # controller concerns straight from the gem, so `bundle update tam_select`
6
+ # updates every app. An app file with the same path overrides the gem's.
4
7
  class Engine < ::Rails::Engine
8
+ # tailwindcss-rails finds app/assets/tailwind/<engine_name>/engine.css by
9
+ # this name; Rails would otherwise call it "tam_select_engine".
10
+ engine_name "tam_select"
11
+
12
+ # config.tam_select.default_select = true renders every select as a Tam
13
+ # Select (Rails helpers and Simple Form). See TamSelect::DefaultSelect.
14
+ config.tam_select = ActiveSupport::OrderedOptions.new
15
+ config.tam_select.default_select = false
16
+
17
+ initializer "tam_select.default_select" do |app|
18
+ next unless app.config.tam_select.default_select
19
+
20
+ ActiveSupport.on_load(:action_view) { prepend TamSelect::DefaultSelect }
21
+ if defined?(::SimpleForm::FormBuilder)
22
+ app.config.to_prepare { ::SimpleForm::FormBuilder.map_type :select, to: ::TamSelectInput }
23
+ end
24
+ end
25
+
26
+ # The Rails integration lives in rails/app; the npm package ships the same
27
+ # files from there.
28
+ paths.add "rails/app", eager_load: true, glob: "{*,*/concerns}", exclude: %w[javascript inputs]
29
+ paths["app/helpers"] = "rails/app/helpers"
30
+
31
+ # The Simple Form input subclasses a Simple Form class, so only load it
32
+ # when the app uses Simple Form.
33
+ paths.add "rails/app/inputs", eager_load: true if Gem.loaded_specs.key?("simple_form")
34
+
35
+ initializer "tam_select.importmap", before: "importmap" do |app|
36
+ if app.config.respond_to?(:importmap)
37
+ app.config.importmap.paths << root.join("config/importmap.rb")
38
+ # Refresh the cached importmap when the gem's JavaScript changes, for
39
+ # example while developing against a local checkout of the gem.
40
+ app.config.importmap.cache_sweepers << root.join("src") << root.join("rails/app/javascript")
41
+ end
42
+ end
43
+
44
+ # Runs after importmap-rails adds app/javascript, so an app's own
45
+ # controllers/tam_select_controller.js still wins over the gem's.
46
+ initializer "tam_select.assets", after: "importmap.assets" do |app|
47
+ if app.config.respond_to?(:assets) && app.config.respond_to?(:importmap)
48
+ app.config.assets.paths << root.join("src").to_s
49
+ app.config.assets.paths << root.join("rails/app/javascript").to_s
50
+ if defined?(::Sprockets)
51
+ app.config.assets.precompile += %w[tam-select.js controllers/tam_select_controller.js]
52
+ end
53
+ end
54
+ end
5
55
  end
6
56
  end
@@ -1,3 +1,3 @@
1
1
  module TamSelect
2
- VERSION = "1.2.4"
2
+ VERSION = "1.4.1"
3
3
  end
data/lib/tam_select.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "tam_select/version"
2
+ require "tam_select/default_select"
2
3
  require "tam_select/engine"
3
4
 
4
5
  module TamSelect
data/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tam-select",
3
- "version": "1.2.3",
3
+ "version": "1.4.1",
4
4
  "description": "Accessible, searchable select component for Ruby on Rails, Simple Form, Stimulus, Turbo, and Tailwind CSS",
5
5
  "homepage": "https://github.com/tamiru/tam_select",
6
6
  "repository": {
@@ -9,9 +9,23 @@
9
9
  },
10
10
  "type": "module",
11
11
  "main": "./src/tam-select.js",
12
+ "types": "./src/tam-select.d.ts",
12
13
  "exports": {
13
- ".": "./src/tam-select.js",
14
- "./rails-controller": "./rails/app/javascript/controllers/tam_select_controller.js"
14
+ ".": {
15
+ "types": "./src/tam-select.d.ts",
16
+ "default": "./src/tam-select.js"
17
+ },
18
+ "./rails-controller": {
19
+ "types": "./rails/app/javascript/controllers/tam_select_controller.d.ts",
20
+ "default": "./rails/app/javascript/controllers/tam_select_controller.js"
21
+ },
22
+ "./stimulus": {
23
+ "types": "./rails/app/javascript/controllers/tam_select_controller.d.ts",
24
+ "default": "./rails/app/javascript/controllers/tam_select_controller.js"
25
+ }
26
+ },
27
+ "publishConfig": {
28
+ "access": "public"
15
29
  },
16
30
  "files": [
17
31
  "src",
@@ -21,7 +35,8 @@
21
35
  ],
22
36
  "scripts": {
23
37
  "test": "node --test test/*.test.js",
24
- "check": "node --check src/tam-select.js && node --check rails/app/javascript/controllers/tam_select_controller.js"
38
+ "check": "node --check src/tam-select.js && node --check rails/app/javascript/controllers/tam_select_controller.js",
39
+ "typecheck": "tsc -p tsconfig.json"
25
40
  },
26
41
  "keywords": [
27
42
  "select",
@@ -43,6 +58,7 @@
43
58
  "devDependencies": {
44
59
  "@hotwired/stimulus": "^3.2.2",
45
60
  "esbuild": "^0.28.1",
46
- "jsdom": "^29.1.1"
61
+ "jsdom": "^29.1.1",
62
+ "typescript": "^7.0.2"
47
63
  }
48
64
  }
@@ -13,9 +13,9 @@ module TamSelectPaginatable
13
13
  {
14
14
  value: tam_select_record_value(record, value).to_s,
15
15
  label: tam_select_record_value(record, label).to_s,
16
- detail: detail && tam_select_record_value(record, detail).to_s,
17
- meta: meta && tam_select_record_value(record, meta).to_s,
18
- image: image && tam_select_record_value(record, image).to_s
16
+ detail: tam_select_optional_value(record, detail),
17
+ meta: tam_select_optional_value(record, meta),
18
+ image: tam_select_optional_value(record, image)
19
19
  }.compact
20
20
  end,
21
21
  pagination: {
@@ -26,6 +26,12 @@ module TamSelectPaginatable
26
26
  }
27
27
  end
28
28
 
29
+ # Blank optional fields are omitted so the client never renders an empty
30
+ # detail line, meta badge, or an <img> with an empty src.
31
+ def tam_select_optional_value(record, resolver)
32
+ resolver && tam_select_record_value(record, resolver).to_s.presence
33
+ end
34
+
29
35
  def tam_select_record_value(record, resolver)
30
36
  resolver.respond_to?(:call) ? resolver.call(record) : record.public_send(resolver)
31
37
  end
@@ -2,16 +2,27 @@ module TamSelectHelper
2
2
  def tam_select_tag(name, option_tags = nil, options: {}, html_options: {}, &block)
3
3
  data = (html_options[:data] ||= {})
4
4
  data[:controller] = (data[:controller].to_s.split + ["tam-select"]).uniq.join(" ")
5
- data[:tam_select_options_value] = options.to_json
5
+ data[:tam_select_options_value] = tam_select_options.merge(options.to_h.symbolize_keys).to_json
6
+ # A single select needs a blank option to be cleared and to show its
7
+ # placeholder, so add one unless the caller handles it.
8
+ html_options[:include_blank] = true if tam_select_add_blank?(option_tags, html_options)
6
9
  select_tag(name, option_tags, html_options, &block)
7
10
  end
8
11
 
9
12
  def tam_select_options(**overrides)
13
+ # `searchable` is left to the JavaScript default (true) unless given, so
14
+ # an app's Stimulus controller can decide it, e.g. by option count.
10
15
  {
11
- searchable: true,
12
16
  clearable: true,
13
17
  creatable: false,
18
+ theme: "auto",
14
19
  placeholder: "Select…"
15
20
  }.merge(overrides)
16
21
  end
22
+
23
+ private
24
+
25
+ def tam_select_add_blank?(option_tags, html_options)
26
+ TamSelect::DefaultSelect.add_blank?(html_options, html_options, option_tags: option_tags.to_s)
27
+ end
17
28
  end
@@ -1,30 +1,45 @@
1
1
  class TamSelectInput < SimpleForm::Inputs::CollectionSelectInput
2
2
  def input(wrapper_options = nil)
3
3
  attributes = merge_wrapper_options(input_html_options, wrapper_options)
4
+ # With config.tam_select.default_select this input renders every select;
5
+ # `input_html: { data: { tam_select: false } }` keeps one native.
6
+ return super if TamSelect::DefaultSelect.opted_out?(attributes)
7
+
4
8
  label_method, value_method = detect_collection_methods
5
9
  tam_options = attributes.delete(:tam_options) || {}
6
10
  data = attributes[:data] ||= {}
7
11
  data[:controller] = (data[:controller].to_s.split + ["tam-select"]).uniq.join(" ")
8
- data[:tam_select_options_value] = defaults.merge(tam_options).to_json
12
+ data[:tam_select_options_value] = defaults.merge(tam_options.to_h.symbolize_keys).to_json
9
13
 
10
14
  @builder.collection_select(
11
15
  attribute_name,
12
16
  collection,
13
17
  value_method,
14
18
  label_method,
15
- input_options,
19
+ TamSelect::DefaultSelect.prompt_as_blank(input_options, attributes),
16
20
  attributes
17
21
  )
18
22
  end
19
23
 
20
24
  private
21
25
 
26
+ # Simple Form adds a blank option to single selects unless `prompt`,
27
+ # `include_blank`, `selected` or `default` is given. Keep it for `selected`
28
+ # and `default` too: the blank option is what lets the field be cleared and
29
+ # show its placeholder.
30
+ def skip_include_blank?
31
+ (options.keys & %i[prompt include_blank]).any? || multiple?
32
+ end
33
+
22
34
  def defaults
23
35
  {
24
- searchable: options.fetch(:searchable, true),
25
36
  creatable: options.fetch(:creatable, false),
26
37
  clearable: options.fetch(:clearable, true),
38
+ theme: options.fetch(:theme, "auto"),
27
39
  placeholder: options[:placeholder] || options[:prompt] || "Select…"
28
- }
40
+ }.tap do |defaults|
41
+ # Left to the JavaScript default (true) unless given.
42
+ defaults[:searchable] = options[:searchable] if options.key?(:searchable)
43
+ end
29
44
  end
30
45
  end
@@ -0,0 +1,21 @@
1
+ import { Controller } from "@hotwired/stimulus"
2
+ import TamSelect from "tam-select"
3
+
4
+ export default class TamSelectController extends Controller<HTMLSelectElement> {
5
+ instance: TamSelect | null
6
+ readonly optionsValue: Record<string, unknown>
7
+ /** Options passed to TamSelect. Override in a subclass to add app defaults. */
8
+ readonly tamSelectOptions: import("tam-select").TamSelectOptions | Record<string, unknown>
9
+
10
+ connect(): void
11
+ disconnect(): void
12
+ optionsValueChanged(): void
13
+ refresh(): void
14
+ open(): void
15
+ close(): void
16
+ focus(): void
17
+ clear(): void
18
+ selectAll(): void
19
+ deselectAll(): void
20
+ setValue(event: { params: { value: string | string[] | null } }): void
21
+ }