hotwire_combobox 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +26 -42
- data/app/helpers/hotwire_combobox/helper.rb +11 -6
- data/lib/hotwire_combobox/version.rb +1 -1
- data/lib/hotwire_combobox.rb +13 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9cb2abe7ffd7dc81c39b774fb0028f0f9650182978735f3d21a3d375a32a948e
|
4
|
+
data.tar.gz: a038bd3ce8baff5a47e281be380cc0c4c0b77433cab3161174958a0f135fab08
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 52b14433be746e89f6e0612f42d3a84ed323ebb2931a93d535b90bb9eb1fbde555ea3bf0a3496774b9eb91c30ba0035576e6c3fab7da69e97e7228e927767e69
|
7
|
+
data.tar.gz: '09662cb44deb6854dca5d7a6919ae6170dff3ebcbbae9cab7ea7b1b368ec85e0db82f823a3471886cb55b9e4a44da6bd7e141bdc0e5948266b118ed96b2873bc'
|
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
|
-
|
|
53
|
-
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
|
-
|
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 `
|
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
|
-
|
103
|
+
### Naming Conflicts
|
112
104
|
|
113
|
-
|
114
|
-
```bash
|
115
|
-
$ bin/setup
|
116
|
-
```
|
105
|
+
If your application has naming conflicts with this gem, the following config will turn:
|
117
106
|
|
118
|
-
|
119
|
-
|
120
|
-
$ bundle exec rake app:test
|
121
|
-
```
|
107
|
+
* `#combobox_tag` into `#hw_combobox_tag`
|
108
|
+
* `#combobox_options` into `#hw_combobox_options`
|
122
109
|
|
123
|
-
```
|
124
|
-
|
125
|
-
```
|
110
|
+
```ruby
|
111
|
+
# config/initializers/hotwire_combobox.rb
|
126
112
|
|
127
|
-
|
128
|
-
|
129
|
-
|
113
|
+
HotwireCombobox.setup do |config|
|
114
|
+
config.bypass_convenience_methods = true
|
115
|
+
end
|
130
116
|
```
|
131
117
|
|
132
|
-
|
118
|
+
## Contributing
|
133
119
|
|
134
|
-
|
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).
|
@@ -1,11 +1,7 @@
|
|
1
1
|
module HotwireCombobox
|
2
2
|
module Helper
|
3
|
-
def
|
4
|
-
|
5
|
-
end
|
6
|
-
|
7
|
-
def hwbox_options(options)
|
8
|
-
options.map { |option| hwbox_option(**option) }
|
3
|
+
def hw_combobox_options(options)
|
4
|
+
options.map { |option| hw_combobox_option(**option) }
|
9
5
|
end
|
10
6
|
|
11
7
|
def hw_combobox_tag(name, value = nil, form: nil, options: [], data: {}, input: {}, **attrs)
|
@@ -28,6 +24,11 @@ module HotwireCombobox
|
|
28
24
|
parent_data: default_hw_combobox_parent_data(attrs, data)
|
29
25
|
end
|
30
26
|
|
27
|
+
unless HotwireCombobox.bypass_convenience_methods?
|
28
|
+
alias_method :combobox_options, :hw_combobox_options
|
29
|
+
alias_method :combobox_tag, :hw_combobox_tag
|
30
|
+
end
|
31
|
+
|
31
32
|
def hw_listbox_option_id(option)
|
32
33
|
option.try(:id)
|
33
34
|
end
|
@@ -49,6 +50,10 @@ module HotwireCombobox
|
|
49
50
|
end
|
50
51
|
|
51
52
|
private
|
53
|
+
def hw_combobox_option(...)
|
54
|
+
HotwireCombobox::Option.new(...)
|
55
|
+
end
|
56
|
+
|
52
57
|
def default_hw_combobox_value_field_id(attrs, form, name)
|
53
58
|
attrs.delete(:id) || form&.field_id(name)
|
54
59
|
end
|
data/lib/hotwire_combobox.rb
CHANGED
@@ -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.
|
4
|
+
version: 0.1.8
|
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-
|
11
|
+
date: 2023-12-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|