bard-tag_field 0.5.0 → 0.5.2

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.
@@ -0,0 +1,357 @@
1
+ import { expect } from '@esm-bundle/chai'
2
+ import '../src/input-tag.js'
3
+ import {
4
+ setupGlobalTestHooks,
5
+ setupInputTag,
6
+ waitForUpdate,
7
+ waitForBasicInitialization,
8
+ simulateInput,
9
+ simulateUserAddTag,
10
+ getTagElements,
11
+ getTagValues,
12
+ } from './lib/test-utils.js'
13
+
14
+ describe('Value/Label Separation', () => {
15
+ setupGlobalTestHooks()
16
+
17
+ describe('Basic Value/Label Behavior (Zero API Changes)', () => {
18
+ it('should work with value attribute different from display text', async () => {
19
+ const inputTag = await setupInputTag(`
20
+ <input-tag name="languages" multiple>
21
+ <tag-option value="js">JavaScript</tag-option>
22
+ <tag-option value="py">Python</tag-option>
23
+ </input-tag>
24
+ `)
25
+
26
+ // API works with values (unchanged behavior)
27
+ expect(inputTag.tags).to.deep.equal(['js', 'py'])
28
+ expect(getTagValues(inputTag)).to.deep.equal(['js', 'py'])
29
+
30
+ // But display shows labels
31
+ const tagElements = getTagElements(inputTag)
32
+ expect(tagElements[0].textContent.trim()).to.equal('JavaScript')
33
+ expect(tagElements[1].textContent.trim()).to.equal('Python')
34
+
35
+ // Values are still accessible
36
+ expect(tagElements[0].value).to.equal('js')
37
+ expect(tagElements[1].value).to.equal('py')
38
+ })
39
+
40
+ it('should fall back to text content when no value attribute', async () => {
41
+ const inputTag = await setupInputTag(`
42
+ <input-tag name="categories" multiple>
43
+ <tag-option>Frontend</tag-option>
44
+ <tag-option>Backend</tag-option>
45
+ </input-tag>
46
+ `)
47
+
48
+ // Both value and display are the same
49
+ expect(inputTag.tags).to.deep.equal(['Frontend', 'Backend'])
50
+ expect(getTagValues(inputTag)).to.deep.equal(['Frontend', 'Backend'])
51
+
52
+ const tagElements = getTagElements(inputTag)
53
+ expect(tagElements[0].textContent.trim()).to.equal('Frontend')
54
+ expect(tagElements[1].textContent.trim()).to.equal('Backend')
55
+ expect(tagElements[0].value).to.equal('Frontend')
56
+ expect(tagElements[1].value).to.equal('Backend')
57
+ })
58
+
59
+ it('should support mixed value/label and text-only tags', async () => {
60
+ const inputTag = await setupInputTag(`
61
+ <input-tag name="mixed" multiple>
62
+ <tag-option value="1">Label A</tag-option>
63
+ <tag-option>Text Only</tag-option>
64
+ <tag-option value="3">Label C</tag-option>
65
+ </input-tag>
66
+ `)
67
+
68
+ // Values array contains mix of explicit values and text
69
+ expect(inputTag.tags).to.deep.equal(['1', 'Text Only', '3'])
70
+
71
+ // Display shows appropriate text
72
+ const tagElements = getTagElements(inputTag)
73
+ expect(tagElements[0].textContent.trim()).to.equal('Label A')
74
+ expect(tagElements[1].textContent.trim()).to.equal('Text Only')
75
+ expect(tagElements[2].textContent.trim()).to.equal('Label C')
76
+
77
+ // Values are correct
78
+ expect(tagElements[0].value).to.equal('1')
79
+ expect(tagElements[1].value).to.equal('Text Only')
80
+ expect(tagElements[2].value).to.equal('3')
81
+ })
82
+ })
83
+
84
+ describe('API Methods Work with Values (Backward Compatible)', () => {
85
+ it('should add tags by value, display by label', async () => {
86
+ const inputTag = await setupInputTag(`
87
+ <input-tag name="languages" multiple>
88
+ <tag-option value="js">JavaScript</tag-option>
89
+ </input-tag>
90
+ `)
91
+
92
+ // Add by value (existing API)
93
+ inputTag.add('js')
94
+ await waitForUpdate()
95
+
96
+ // Should not create duplicate since value already exists
97
+ expect(inputTag.tags).to.deep.equal(['js'])
98
+ expect(getTagElements(inputTag)).to.have.length(1)
99
+
100
+ // Display still shows label
101
+ expect(getTagElements(inputTag)[0].textContent.trim()).to.equal('JavaScript')
102
+ })
103
+
104
+ it('should remove tags by value', async () => {
105
+ const inputTag = await setupInputTag(`
106
+ <input-tag name="languages" multiple>
107
+ <tag-option value="js">JavaScript</tag-option>
108
+ <tag-option value="py">Python</tag-option>
109
+ </input-tag>
110
+ `)
111
+
112
+ // Remove by value (existing API)
113
+ inputTag.remove('js')
114
+ await waitForUpdate()
115
+
116
+ expect(inputTag.tags).to.deep.equal(['py'])
117
+ expect(getTagElements(inputTag)).to.have.length(1)
118
+ expect(getTagElements(inputTag)[0].textContent.trim()).to.equal('Python')
119
+ })
120
+
121
+ it('should check tag existence by value', async () => {
122
+ const inputTag = await setupInputTag(`
123
+ <input-tag name="languages" multiple>
124
+ <tag-option value="js">JavaScript</tag-option>
125
+ </input-tag>
126
+ `)
127
+
128
+ // Check by value (existing API)
129
+ expect(inputTag.has('js')).to.be.true
130
+ expect(inputTag.has('JavaScript')).to.be.false // Label doesn't work for has()
131
+ expect(inputTag.has('nonexistent')).to.be.false
132
+ })
133
+ })
134
+
135
+ describe('User-Entered Tags (Mixed Values)', () => {
136
+ it('should handle user-typed tags alongside predefined value/label tags', async () => {
137
+ const inputTag = await setupInputTag(`
138
+ <input-tag name="mixed" multiple>
139
+ <tag-option value="1">Predefined A</tag-option>
140
+ <tag-option value="2">Predefined B</tag-option>
141
+ </input-tag>
142
+ `)
143
+
144
+ // User types custom tag
145
+ await simulateUserAddTag(inputTag, 'CustomTag')
146
+
147
+ // Values array contains mix of predefined values and user text
148
+ expect(inputTag.tags).to.deep.equal(['1', '2', 'CustomTag'])
149
+
150
+ // Display shows appropriate text
151
+ const tagElements = getTagElements(inputTag)
152
+ expect(tagElements[0].textContent.trim()).to.equal('Predefined A')
153
+ expect(tagElements[1].textContent.trim()).to.equal('Predefined B')
154
+ expect(tagElements[2].textContent.trim()).to.equal('CustomTag')
155
+
156
+ // User-entered tag has same value and label
157
+ expect(tagElements[2].value).to.equal('CustomTag')
158
+ })
159
+
160
+ it('should allow removing user-entered tags by their text value', async () => {
161
+ const inputTag = await setupInputTag(`
162
+ <input-tag name="mixed" multiple>
163
+ <tag-option value="1">Predefined</tag-option>
164
+ </input-tag>
165
+ `)
166
+
167
+ await simulateUserAddTag(inputTag, 'UserTag')
168
+ expect(inputTag.tags).to.deep.equal(['1', 'UserTag'])
169
+
170
+ // Remove user-entered tag by its value (which is the text)
171
+ inputTag.remove('UserTag')
172
+ await waitForUpdate()
173
+
174
+ expect(inputTag.tags).to.deep.equal(['1'])
175
+ expect(getTagElements(inputTag)).to.have.length(1)
176
+ })
177
+ })
178
+
179
+ describe('Form Integration', () => {
180
+ it('should submit values array to form, not labels', async () => {
181
+ const form = document.createElement('form')
182
+ form.innerHTML = `
183
+ <input-tag name="test" multiple>
184
+ <tag-option value="val1">Label 1</tag-option>
185
+ <tag-option value="val2">Label 2</tag-option>
186
+ </input-tag>
187
+ `
188
+ document.body.appendChild(form)
189
+
190
+ const inputTag = form.querySelector('input-tag')
191
+ await waitForUpdate()
192
+
193
+ // Wait for component initialization
194
+ while (!inputTag._taggle) {
195
+ await waitForUpdate()
196
+ }
197
+
198
+ // Add user-entered tag
199
+ await simulateUserAddTag(inputTag, 'UserInput')
200
+
201
+ // Form data should contain values, not labels
202
+ const formData = new FormData(form)
203
+ const values = formData.getAll('test')
204
+ expect(values).to.deep.equal(['val1', 'val2', 'UserInput'])
205
+
206
+ document.body.removeChild(form)
207
+ })
208
+ })
209
+
210
+ describe('TagOption Label Getter', () => {
211
+ it('should provide label getter that returns innerText', async () => {
212
+ const inputTag = await setupInputTag(`
213
+ <input-tag name="test" multiple>
214
+ <tag-option value="short">Long Display Name</tag-option>
215
+ <tag-option>Text Only</tag-option>
216
+ </input-tag>
217
+ `)
218
+
219
+ const tagElements = getTagElements(inputTag)
220
+
221
+ // First tag: value !== label
222
+ expect(tagElements[0].value).to.equal('short')
223
+ expect(tagElements[0].label).to.equal('Long Display Name')
224
+
225
+ // Second tag: value === label
226
+ expect(tagElements[1].value).to.equal('Text Only')
227
+ expect(tagElements[1].label).to.equal('Text Only')
228
+ })
229
+ })
230
+
231
+ describe('Backward Compatibility', () => {
232
+ it('should maintain exact same behavior for tags without value attributes', async () => {
233
+ const inputTag = await setupInputTag(`
234
+ <input-tag name="legacy" multiple>
235
+ <tag-option>JavaScript</tag-option>
236
+ <tag-option>Python</tag-option>
237
+ </input-tag>
238
+ `)
239
+
240
+ // Existing behavior unchanged
241
+ expect(inputTag.tags).to.deep.equal(['JavaScript', 'Python'])
242
+ expect(getTagValues(inputTag)).to.deep.equal(['JavaScript', 'Python'])
243
+
244
+ const tagElements = getTagElements(inputTag)
245
+ expect(tagElements[0].value).to.equal('JavaScript')
246
+ expect(tagElements[1].value).to.equal('Python')
247
+ expect(tagElements[0].textContent.trim()).to.equal('JavaScript')
248
+ expect(tagElements[1].textContent.trim()).to.equal('Python')
249
+ })
250
+
251
+ it('should work with existing add/remove/has API exactly as before', async () => {
252
+ const inputTag = await setupInputTag('<input-tag name="test" multiple></input-tag>')
253
+
254
+ // All existing API behavior unchanged
255
+ inputTag.add('test1')
256
+ inputTag.add(['test2', 'test3'])
257
+ expect(inputTag.tags).to.deep.equal(['test1', 'test2', 'test3'])
258
+
259
+ expect(inputTag.has('test1')).to.be.true
260
+ expect(inputTag.has('nonexistent')).to.be.false
261
+
262
+ inputTag.remove('test2')
263
+ expect(inputTag.tags).to.deep.equal(['test1', 'test3'])
264
+
265
+ inputTag.removeAll()
266
+ expect(inputTag.tags).to.deep.equal([])
267
+ })
268
+ })
269
+
270
+ describe('Autocomplete with Value/Label Separation', () => {
271
+ it('should create tag-option with correct value and label when selecting from autocomplete', async () => {
272
+ // Set up input-tag with datalist that has value/label separation
273
+ document.body.innerHTML = `
274
+ <input-tag name="frameworks" list="suggestions" multiple></input-tag>
275
+ <datalist id="suggestions">
276
+ <option value="vue">Vue.js Framework</option>
277
+ <option value="react">React Library</option>
278
+ </datalist>
279
+ `
280
+ const inputTag = document.querySelector('input-tag')
281
+ await waitForBasicInitialization(inputTag)
282
+
283
+ // Simulate typing "vue" to trigger autocomplete
284
+ const input = inputTag._taggleInputTarget
285
+ await simulateInput(input, 'vue')
286
+
287
+ // Verify autocomplete shows the label
288
+ expect(inputTag._autocompleteSuggestions).to.deep.equal(['Vue.js Framework'])
289
+
290
+ // Simulate clicking on the autocomplete suggestion
291
+ // This should add a tag with value="vue" but display "Vue.js Framework"
292
+ const autocompleteItems = inputTag.autocompleteContainerTarget.querySelectorAll('.ui-menu-item')
293
+ expect(autocompleteItems.length).to.equal(1)
294
+
295
+ // Click the autocomplete item
296
+ autocompleteItems[0].click()
297
+
298
+ // Wait for tag to be added
299
+ await new Promise(resolve => setTimeout(resolve, 10))
300
+
301
+ // The bug: Currently this creates <tag-option value="vue">vue</tag-option>
302
+ // But it should create <tag-option value="vue">Vue.js Framework</tag-option>
303
+
304
+ const tagElements = getTagElements(inputTag)
305
+ expect(tagElements.length).to.equal(1)
306
+
307
+ // Value should be the short form
308
+ expect(tagElements[0].value).to.equal('vue')
309
+
310
+
311
+ // BUT display text should be the full label
312
+ expect(tagElements[0].textContent.trim()).to.equal('Vue.js Framework')
313
+
314
+ // Form submission should use the value
315
+ expect(inputTag.tags).to.deep.equal(['vue'])
316
+ })
317
+
318
+ it('should work with mixed value/label and simple options', async () => {
319
+ document.body.innerHTML = `
320
+ <input-tag name="frameworks" list="suggestions" multiple></input-tag>
321
+ <datalist id="suggestions">
322
+ <option value="vue">Vue.js Framework</option>
323
+ <option value="simple">simple</option>
324
+ </datalist>
325
+ `
326
+ const inputTag = document.querySelector('input-tag')
327
+ await waitForBasicInitialization(inputTag)
328
+
329
+ const input = inputTag._taggleInputTarget
330
+
331
+ // Add Vue.js via autocomplete
332
+ await simulateInput(input, 'vue')
333
+ const autocompleteItems1 = inputTag.autocompleteContainerTarget.querySelectorAll('.ui-menu-item')
334
+ autocompleteItems1[0].click()
335
+ await new Promise(resolve => setTimeout(resolve, 10))
336
+
337
+ // Add simple via autocomplete
338
+ await simulateInput(input, 'simple')
339
+ const autocompleteItems2 = inputTag.autocompleteContainerTarget.querySelectorAll('.ui-menu-item')
340
+ autocompleteItems2[0].click()
341
+ await new Promise(resolve => setTimeout(resolve, 10))
342
+
343
+ const tagElements = getTagElements(inputTag)
344
+ expect(tagElements.length).to.equal(2)
345
+
346
+ // First tag: value/label separation
347
+ expect(tagElements[0].value).to.equal('vue')
348
+ expect(tagElements[0].textContent.trim()).to.equal('Vue.js Framework')
349
+
350
+ // Second tag: simple case where value equals label
351
+ expect(tagElements[1].value).to.equal('simple')
352
+ expect(tagElements[1].textContent.trim()).to.equal('simple')
353
+
354
+ expect(inputTag.tags).to.deep.equal(['vue', 'simple'])
355
+ })
356
+ })
357
+ })
@@ -0,0 +1,20 @@
1
+ import failOnly from './test/lib/fail-only.mjs'
2
+
3
+ export default {
4
+ files: 'test/**/*.test.js',
5
+ nodeResolve: true,
6
+ coverage: true,
7
+ coverageConfig: {
8
+ include: ['src/**/*']
9
+ },
10
+ testFramework: {
11
+ config: {
12
+ timeout: 10000 // Increased timeout for slower browsers
13
+ }
14
+ },
15
+ plugins: [failOnly],
16
+ // Browser timeout configurations for cross-browser compatibility
17
+ browserStartTimeout: 60000,
18
+ testsStartTimeout: 20000,
19
+ testsFinishTimeout: 120000
20
+ };
@@ -16,11 +16,22 @@ class Chop::Form::TagField < Chop::Form::Field
16
16
  end
17
17
 
18
18
  def set_value
19
- if field[:multiple]
19
+ values = if field[:multiple]
20
20
  value.to_s.split(", ").map(&:strip)
21
21
  else
22
- value.to_s.strip
22
+ [value.to_s.strip]
23
23
  end
24
+
25
+ # Resolve display labels to submit values using datalist
26
+ datalist_options = field.all("datalist option", visible: false)
27
+ if datalist_options.any?
28
+ label_to_value = datalist_options.each_with_object({}) do |opt, hash|
29
+ hash[opt[:innerText]] = opt[:value]
30
+ end
31
+ values = values.map { |v| label_to_value[v] || v }
32
+ end
33
+
34
+ field[:multiple] ? values : values.first
24
35
  end
25
36
 
26
37
  def fill_in!
@@ -2,10 +2,11 @@ module Bard
2
2
  module TagField
3
3
  class Field < ActionView::Helpers::Tags::TextField
4
4
  def render &block
5
+ @options = @options.dup.transform_keys(&:to_s)
5
6
  add_default_name_and_id(@options)
6
7
 
7
8
  # Remove choices from HTML options before rendering
8
- choices = @options.delete(:choices)
9
+ choices = @options.delete("choices")
9
10
 
10
11
  # Store choices for render_object_values method
11
12
  @choices = choices
@@ -14,7 +15,7 @@ module Bard
14
15
  content = block ? block.call(@options) : render_object_values
15
16
 
16
17
  # Add nested anonymous datalist if we have choices, no block, and no external list specified
17
- if choices&.any? && !block && !@options[:list]
18
+ if choices&.any? && !block && !@options["list"]
18
19
  content += render_datalist(nil, choices)
19
20
  end
20
21
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Bard
4
4
  module TagField
5
- VERSION = "0.5.0"
5
+ VERSION = "0.5.2"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bard-tag_field
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Micah Geisel
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-10-28 00:00:00.000000000 Z
11
+ date: 2026-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -66,6 +66,76 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: capybara
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: cuprite
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: chop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: cucumber-rails
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: puma
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
69
139
  description: form.tag_field using @botandrose/input-tag custom element
70
140
  email:
71
141
  - micah@botandrose.com
@@ -84,15 +154,35 @@ files:
84
154
  - bard-tag_field.gemspec
85
155
  - bin/console
86
156
  - bin/setup
157
+ - cucumber.yml
87
158
  - gemfiles/rails_7.1.gemfile
88
159
  - gemfiles/rails_7.2.gemfile
89
160
  - gemfiles/rails_8.0.gemfile
90
161
  - gemfiles/rails_8.1.gemfile
91
162
  - input-tag/.gitignore
92
- - input-tag/bun.lockb
93
- - input-tag/index.js
163
+ - input-tag/CLAUDE.md
164
+ - input-tag/LICENSE
165
+ - input-tag/README.md
166
+ - input-tag/TESTING.md
167
+ - input-tag/bun.lock
168
+ - input-tag/index.html
94
169
  - input-tag/package.json
95
170
  - input-tag/rollup.config.js
171
+ - input-tag/src/input-tag.js
172
+ - input-tag/src/taggle.js
173
+ - input-tag/test/api-methods.test.js
174
+ - input-tag/test/autocomplete.test.js
175
+ - input-tag/test/basic-functionality.test.js
176
+ - input-tag/test/dom-mutation.test.js
177
+ - input-tag/test/edge-cases.test.js
178
+ - input-tag/test/events.test.js
179
+ - input-tag/test/form-integration.test.js
180
+ - input-tag/test/input-tag.test.js
181
+ - input-tag/test/lib/fail-only.mjs
182
+ - input-tag/test/lib/test-utils.js
183
+ - input-tag/test/nested-datalist.test.js
184
+ - input-tag/test/value-label-separation.test.js
185
+ - input-tag/web-test-runner.config.mjs
96
186
  - lib/bard/tag_field.rb
97
187
  - lib/bard/tag_field/cucumber.rb
98
188
  - lib/bard/tag_field/field.rb
@@ -105,7 +195,7 @@ metadata:
105
195
  homepage_uri: https://github.com/botandrose/bard-tag_field
106
196
  source_code_uri: https://github.com/botandrose/bard-tag_field
107
197
  changelog_uri: https://github.com/botandrose/bard-tag_field/blob/master/CHANGELOG.md
108
- post_install_message:
198
+ post_install_message:
109
199
  rdoc_options: []
110
200
  require_paths:
111
201
  - lib
@@ -121,7 +211,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
211
  version: '0'
122
212
  requirements: []
123
213
  rubygems_version: 3.5.11
124
- signing_key:
214
+ signing_key:
125
215
  specification_version: 4
126
216
  summary: form.tag_field using @botandrose/input-tag custom element
127
217
  test_files: []