hotwire_combobox 0.1.7 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 01a08bf7fdf486660daebfd29960a62c8435ad87939a8963d805967872ffca4d
4
- data.tar.gz: 2ca73e1f2693d9adf838463187bd2c79867b8086d59beea0bb699c12a49e8507
3
+ metadata.gz: 682b24c423ae14b503b0c018ef7299e89b25fb641057934a835bf615ffd9b999
4
+ data.tar.gz: d588e46d58c757534239d6ccbcd85165e6ba05bfab127a5e315c9f6eb96b5168
5
5
  SHA512:
6
- metadata.gz: d505fbfb95060cb730a5533dd4d850f4691391a94f4849fd12017f1ebca12df55a12871d29f784e55dccbe2a8512c007c7516f960225e1a13f6ab74d5d8a3431
7
- data.tar.gz: 6792bae8acf5f9051f2a5605f95e86d7e3b755d323e47e9dff65dc0a02eb27771fa145d2129b246518711e4b050cc2e3cba8b03e83b581ea519c31c30a2ad5d6
6
+ metadata.gz: b6b954b089943a78f4cdf1a3cc4833e8ddafcf2fd5f697556c2c0cbd7ba89cf9425d065146b2046b34464c653ba9ba4bc793b349f97b17ef774aca718e721df7
7
+ data.tar.gz: 9499ab225eb5b668361003e3776fa5972d3cfd2ae138b42b51138e1e976863a3aac5921e0a4f91f21a03e897e8a82ae25d2e05cc916c1a7c3b7ae012b8a6b5d7
data/README.md CHANGED
@@ -6,6 +6,7 @@ A combobox implementation for Ruby on Rails apps running on Hotwire.
6
6
  > This gem is pre-release software. It's not ready for production use yet and the API is subject to change.
7
7
 
8
8
  ## Installation
9
+
9
10
  Add this line to your application's Gemfile:
10
11
 
11
12
  ```ruby
@@ -49,34 +50,25 @@ Options are what you see when you open the combobox.
49
50
 
50
51
  The `options` argument takes an array of any objects which respond to:
51
52
 
52
- | Attribute | Description | Required? |
53
- |----------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------|-------------------|
54
- | `id` | Used as the option element's `id` attribute. Only required if `value` is not provided. | Required* |
55
- | `value` | Used to populate the input element's `value` attribute. Falls back to calling `id` on the object if not provided. | Optional |
56
- | `content` <br> **Supports HTML** | Used as the option element's content. Falls back to calling `display` on the object if not provided. | Optional |
57
- | `filterable_as` | Used to filter down the options when the user types into the input element. Falls back to calling `display` on the object if not provided. | Optional |
58
- | `autocompletable_as` | Used to autocomplete the input element when the user types into it. Falls back to calling `display` on the object if not provided. | Optional |
59
- | `display` | Used as a short-hand for other attributes. See the rest of the list for details. | Optional |
60
-
53
+ | Method | Description |
54
+ |--------------------|--------------------------------------------------------------------------------------------------------------------------------------------|
55
+ | id | Used as the option element's `id` attribute. Only required if `value` is not provided. |
56
+ | value | Used to populate the input element's `value` attribute. Falls back to calling `id` on the object if not provided. |
57
+ | content | **Supports HTML** <br> Used as the option element's content. Falls back to calling `display` on the object if not provided. |
58
+ | filterable_as | Used to filter down the options when the user types into the input element. Falls back to calling `display` on the object if not provided. |
59
+ | autocompletable_as | Used to autocomplete the input element when the user types into it. Falls back to calling `display` on the object if not provided. |
60
+ | display | Used as a short-hand for other attributes. See the rest of the list for details. |
61
61
 
62
62
  > [!NOTE]
63
63
  > The `id` attribute is required only if `value` is not provided.
64
64
 
65
-
66
- The gem provides a `HotwireCombobox::Option` class which you can use to create options:
67
-
68
- ```ruby
69
- @states = [
70
- HotwireCombobox::Option.new(value: "AL", display: "Alabama"),
71
- # ...
72
- ]
73
- ```
74
-
75
- If you feel `HotwireCombobox::Option` is too verbose, you can also use the `hwbox_options` helper. It will destructure the hashes you pass to it and create `HotwireCombobox::Option` instances for you:
65
+ You can use the `combobox_options` helper to create an array of option objects which respond to the above methods:
76
66
 
77
67
  ```ruby
78
- @states = hwbox_options [
68
+ combobox_options [
79
69
  { value: "AL", display: "Alabama" },
70
+ { value: "AK", display: "Alaska" },
71
+ { value: "AZ", display: "Arizona" },
80
72
  # ...
81
73
  ]
82
74
  ```
@@ -90,7 +82,7 @@ The combobox is completely unstyled by default. You can use the following CSS se
90
82
  * `.hw-combobox [role="listbox"]` targets the listbox which encloses all option elements.
91
83
  * `.hw-combobox [role="option"]` targets each option element inside the listbox.
92
84
 
93
- Additionally, you can pass the following [Stimulus class values](https://stimulus.hotwired.dev/reference/css-classes) to `hw_combobox_tag`:
85
+ Additionally, you can pass the following [Stimulus class values](https://stimulus.hotwired.dev/reference/css-classes) to `combobox_tag`:
94
86
 
95
87
  * `data-hw-combobox-selected-class`: The class to apply to the selected option while shown inside an open listbox.
96
88
  * `data-hw-combobox-invalid-class`: The class to apply to the input element when the current value is invalid.
@@ -108,33 +100,25 @@ The library will mark the element as invalid but this won't be noticeable in the
108
100
  > [!CAUTION]
109
101
  > Bad actors can still submit invalid values to the server. You should always validate the input on the server side.
110
102
 
111
- ## Contributing
103
+ ### Naming Conflicts
112
104
 
113
- ### Setup
114
- ```bash
115
- $ bin/setup
116
- ```
105
+ If your application has naming conflicts with this gem, the following config will turn:
117
106
 
118
- ### Running the tests
119
- ```bash
120
- $ bundle exec rake app:test
121
- ```
107
+ * `#combobox_tag` into `#hw_combobox_tag`
108
+ * `#combobox_options` into `#hw_combobox_options`
122
109
 
123
- ```bash
124
- $ bundle exec rake app:test:system
125
- ```
110
+ ```ruby
111
+ # config/initializers/hotwire_combobox.rb
126
112
 
127
- ### Running the dummy app
128
- ```bash
129
- $ bin/rails s
113
+ HotwireCombobox.setup do |config|
114
+ config.bypass_convenience_methods = true
115
+ end
130
116
  ```
131
117
 
132
- ### Releasing
118
+ ## Contributing
133
119
 
134
- 1. Bump the version in `lib/hotwire_combobox/version.rb` (e.g. `VERSION = "0.1.0"`)
135
- 2. Bump the version in `Gemfile.lock` (e.g. `hotwire_combobox (0.1.0)`)
136
- 3. Commit the change (e.g. `git commit -am "Bump to 0.1.0"`)
137
- 4. Run `bundle exec rake release`
120
+ Please read [CONTRIBUTING.md](./CONTRIBUTING.md).
138
121
 
139
122
  ## License
123
+
140
124
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -47,6 +47,7 @@ export default class extends Controller {
47
47
  if (this.element.contains(target)) return
48
48
 
49
49
  this.close()
50
+ target.focus()
50
51
  }
51
52
 
52
53
  closeOnFocusOutside({ target }) {
@@ -55,6 +56,7 @@ export default class extends Controller {
55
56
  if (target.matches("main")) return
56
57
 
57
58
  this.close()
59
+ target.focus()
58
60
  }
59
61
 
60
62
  // private
@@ -1,12 +1,13 @@
1
1
  module HotwireCombobox
2
2
  module Helper
3
- def hwbox_option(...)
4
- HotwireCombobox::Option.new(...)
3
+ class << self
4
+ delegate :bypass_convenience_methods?, to: :HotwireCombobox
5
5
  end
6
6
 
7
- def hwbox_options(options)
8
- options.map { |option| hwbox_option(**option) }
7
+ def hw_combobox_options(options)
8
+ options.map { |option| hw_combobox_option(**option) }
9
9
  end
10
+ alias_method :combobox_options, :hw_combobox_options unless bypass_convenience_methods?
10
11
 
11
12
  def hw_combobox_tag(name, value = nil, form: nil, options: [], data: {}, input: {}, **attrs)
12
13
  value_field_attrs = {}.tap do |h|
@@ -27,6 +28,7 @@ module HotwireCombobox
27
28
  listbox_id: hw_combobox_listbox_id(value_field_attrs[:id]),
28
29
  parent_data: default_hw_combobox_parent_data(attrs, data)
29
30
  end
31
+ alias_method :combobox_tag, :hw_combobox_tag unless bypass_convenience_methods?
30
32
 
31
33
  def hw_listbox_option_id(option)
32
34
  option.try(:id)
@@ -49,6 +51,10 @@ module HotwireCombobox
49
51
  end
50
52
 
51
53
  private
54
+ def hw_combobox_option(...)
55
+ HotwireCombobox::Option.new(...)
56
+ end
57
+
52
58
  def default_hw_combobox_value_field_id(attrs, form, name)
53
59
  attrs.delete(:id) || form&.field_id(name)
54
60
  end
@@ -1,3 +1,3 @@
1
1
  module HotwireCombobox
2
- VERSION = "0.1.7"
2
+ VERSION = "0.1.9"
3
3
  end
@@ -3,4 +3,17 @@ require "hotwire_combobox/engine"
3
3
 
4
4
  module HotwireCombobox
5
5
  Option = Struct.new(:id, :value, :display, :content, :filterable_as, :autocompletable_as)
6
+
7
+ mattr_accessor :bypass_convenience_methods
8
+ @@bypass_convenience_methods = false
9
+
10
+ class << self
11
+ def setup
12
+ yield self
13
+ end
14
+
15
+ def bypass_convenience_methods?
16
+ bypass_convenience_methods
17
+ end
18
+ end
6
19
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hotwire_combobox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jose Farias
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-12-10 00:00:00.000000000 Z
11
+ date: 2023-12-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails