Skip to content

Commit a3f5f65

Browse files
authored
Merge pull request #658 from AnthonyANI/improve-label-click-handler
Improve label click handler to handle clicks on other label child elements
2 parents f6d239c + d313b8d commit a3f5f65

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

src/docs/pages/frameworks/complex.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ export interface FieldOption {
1111
export default defineComponent({
1212
name: 'ComplexFieldExample',
1313
components: {
14-
ShikiStyle, SlimSelect },
14+
ShikiStyle,
15+
SlimSelect
16+
},
1517
props: {
1618
modelValue: {
1719
type: Array as PropType<string[]>,

src/slim-select/index.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,15 +1278,15 @@ describe('SlimSelect Module', () => {
12781278
document.getElementById('test-select')!.innerHTML = `
12791279
<option value="opt1">Option 1 updated</option>
12801280
<option value="opt2" selected>Option 2 updated</option>
1281-
`;
1281+
`
12821282

12831283
// Wait for all mutations to process
12841284
await new Promise((r) => setTimeout(r, 200))
12851285

12861286
// Verify options are still there
12871287
expect(selectElement.options.length).toBe(2)
1288-
expect(selectElement.options[0].textContent).toBe("Option 1 updated")
1289-
expect(selectElement.options[1].textContent).toBe("Option 2 updated")
1288+
expect(selectElement.options[0].textContent).toBe('Option 1 updated')
1289+
expect(selectElement.options[1].textContent).toBe('Option 2 updated')
12901290
expect(selectElement.value).toBe('opt2')
12911291

12921292
// Verify SlimSelect also has the options
@@ -1295,7 +1295,7 @@ describe('SlimSelect Module', () => {
12951295

12961296
// Verify selected value is empty
12971297
const selected = slim.getSelected()
1298-
expect(selected).toEqual(["opt2"])
1298+
expect(selected).toEqual(['opt2'])
12991299

13001300
slim.destroy()
13011301
})

src/slim-select/select.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { kebabCase } from './helpers'
1+
import { kebabCase, hasClassInTree } from './helpers'
22
import { Optgroup, Option } from './store'
33

44
export default class Select {
@@ -167,7 +167,7 @@ export default class Select {
167167
let classChanged = false
168168
let disabledChanged = false
169169
let optgroupOptionChanged = false
170-
let selectionChanged = false;
170+
let selectionChanged = false
171171

172172
// Loop through mutations and check various things
173173
for (const m of mutations) {
@@ -187,7 +187,7 @@ export default class Select {
187187
for (const n of Array.from(m.addedNodes)) {
188188
if (n.nodeName === 'OPTION' && (<HTMLOptionElement>n).value === this.select.value) {
189189
// we added a new option that's now the select value
190-
selectionChanged = true;
190+
selectionChanged = true
191191
break
192192
}
193193
}
@@ -228,7 +228,7 @@ export default class Select {
228228
this.pendingOptionsChange = currentData
229229
}
230230
}
231-
if(selectionChanged) {
231+
if (selectionChanged) {
232232
this.select.dispatchEvent(new Event('change'))
233233
}
234234
return
@@ -239,7 +239,7 @@ export default class Select {
239239
this.changeListen(true)
240240
}
241241

242-
if(selectionChanged) {
242+
if (selectionChanged) {
243243
this.select.dispatchEvent(new Event('change'))
244244
}
245245
}
@@ -566,14 +566,18 @@ export default class Select {
566566
const labelClickHandler = (e: MouseEvent) => {
567567
const target = e.target as HTMLElement
568568

569+
// Check if click is on SlimSelect UI elements (main div or content)
570+
const isSlimSelectElement = hasClassInTree(target, this.select.dataset.id!)
571+
569572
// Prevent default label behavior (focusing the select)
570573
// This needs to happen for all clicks on the label or its children
571574
// to prevent the browser from focusing the hidden select
572575
e.preventDefault()
573576
// e.stopPropagation() // dont stop propagation
574577

575-
// Only handle the click if it's directly on the label
576-
if (target === label && this.onLabelClick) {
578+
// Only trigger the toggle if the click is NOT on SlimSelect elements
579+
// This allows clicking label text/children to toggle, while wrapped SlimSelect handles its own clicks
580+
if (!isSlimSelectElement && this.onLabelClick) {
577581
this.onLabelClick()
578582
}
579583
}

0 commit comments

Comments
 (0)