hotwire_combobox 0.1.4 → 0.1.5

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: d8eaa849ad87e3edaaced39ab2cddddd2de2c4b57c112024c13706055df674eb
4
- data.tar.gz: 9668cf95d8b4b6e994acd852ba6fbac8db3c491959ce40ed520479074d2d326a
3
+ metadata.gz: 99cf62ece0c5bf6182ab759cbab18c4f561c22215babfdf00eed7329f5797eb9
4
+ data.tar.gz: dcca7ddceca4e3b7f840716bf789b641d62ffaea589b9bf86da39e4445c6a9cf
5
5
  SHA512:
6
- metadata.gz: f4f143a0aeafc3fcc7b6f76447ff464342cbffe40f59d55502cc619bcda27bcaba066cf4927a87cf1e551d563c84f0b1ef0bba90ab5d596c9d0e2b3bcfb9a37c
7
- data.tar.gz: f0adeb1d14897243d9b87daa9a67f779765a1c59a9862113928f9b521959a824fafa955d9fe7580e0acef0c841c821ea377c4507208f6dc698adb1f53976a1d2
6
+ metadata.gz: fb5f896deae2dde8b36d91d7eaee8ec27abfd00b07a94d64c094d8da865e4cd2723c6349a9fe49296f8e20cde933ed698f2f64b1e65909ed1fca889517213773
7
+ data.tar.gz: 31ce6c44d91feb634fd16349ed0f89ab58975a3a6744f36c6f05f6ea207d6caa1317267b1892ae602b48daf5f4182a3908681c7746e05bd19ae9b489bcf9696b
@@ -1,7 +1,7 @@
1
1
  import { Controller } from "@hotwired/stimulus"
2
2
 
3
3
  export default class extends Controller {
4
- static classes = [ "selected" ]
4
+ static classes = [ "selected", "invalid" ]
5
5
  static targets = [ "combobox", "listbox", "valueField" ]
6
6
  static values = { expanded: Boolean, filterableAttribute: String, autocompletableAttribute: String }
7
7
 
@@ -16,12 +16,13 @@ export default class extends Controller {
16
16
  }
17
17
 
18
18
  close() {
19
+ this.commitSelection()
19
20
  this.expandedValue = false
20
21
  }
21
22
 
22
23
  selectOption(event) {
23
24
  this.select(event.currentTarget)
24
- this.commitSelection()
25
+ this.close()
25
26
  }
26
27
 
27
28
  filter(event) {
@@ -76,14 +77,13 @@ export default class extends Controller {
76
77
  cancel(event)
77
78
  },
78
79
  Enter(event) {
79
- this.commitSelection()
80
+ this.close()
80
81
  cancel(event)
81
82
  }
82
83
  }
83
84
 
84
85
  commitSelection() {
85
86
  this.select(this.selectedOptionElement, { force: true })
86
- this.close()
87
87
  }
88
88
 
89
89
  expandedValueChanged() {
@@ -109,8 +109,17 @@ export default class extends Controller {
109
109
 
110
110
  if (option) {
111
111
  if (this.hasSelectedClass) option.classList.add(this.selectedClass)
112
+ if (this.hasInvalidClass) this.comboboxTarget.classList.remove(this.invalidClass)
113
+
112
114
  this.maybeAutocompleteWith(option, { force })
113
115
  this.executeSelect(option, { selected: true })
116
+ } else {
117
+ if (this.valueIsInvalid) {
118
+ if (this.hasInvalidClass) this.comboboxTarget.classList.add(this.invalidClass)
119
+
120
+ this.comboboxTarget.setAttribute("aria-invalid", true)
121
+ this.comboboxTarget.setAttribute("aria-errormessage", `Please select a valid option for ${this.comboboxTarget.name}`)
122
+ }
114
123
  }
115
124
  }
116
125
 
@@ -176,6 +185,11 @@ export default class extends Controller {
176
185
  get isOpen() {
177
186
  return this.expandedValue
178
187
  }
188
+
189
+ get valueIsInvalid() {
190
+ const isRequiredAndEmpty = this.comboboxTarget.required && !this.valueFieldTarget.value
191
+ return isRequiredAndEmpty
192
+ }
179
193
  }
180
194
 
181
195
  function applyFilter(query, { matching }) {
@@ -1,3 +1,3 @@
1
1
  module Combobox
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  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
4
+ version: 0.1.5
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-09-14 00:00:00.000000000 Z
11
+ date: 2023-10-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -52,7 +52,7 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.2'
55
- description: A combobox implementation for Ruby on Rails.
55
+ description: A combobox implementation for Ruby on Rails apps running on Hotwire.
56
56
  email:
57
57
  - jose@farias.mx
58
58
  executables: []
@@ -62,7 +62,7 @@ files:
62
62
  - MIT-LICENSE
63
63
  - README.md
64
64
  - Rakefile
65
- - app/assets/javascripts/application.js
65
+ - app/assets/javascripts/combobox_application.js
66
66
  - app/assets/javascripts/controllers/application.js
67
67
  - app/assets/javascripts/controllers/combobox_controller.js
68
68
  - app/assets/javascripts/controllers/index.js
@@ -97,5 +97,5 @@ requirements: []
97
97
  rubygems_version: 3.4.18
98
98
  signing_key:
99
99
  specification_version: 4
100
- summary: A combobox implementation for Ruby on Rails
100
+ summary: A combobox implementation for Ruby on Rails apps running on Hotwire
101
101
  test_files: []