Skip to content

Commit 15fc15b

Browse files
committed
feat: automatically focus input in select and allow forward also for radio/chcekbox
1 parent 18bf035 commit 15fc15b

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

src/django_smartbase_admin/static/sb_admin/src/js/autocomplete.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ export default class Autocomplete {
133133
if(wrapperElButton){
134134
// filter in dropdown
135135
wrapperElButton.addEventListener('show.bs.dropdown', initLoad)
136+
wrapperElButton.addEventListener('shown.bs.dropdown', () => {
137+
choicesJS.input.element.focus()
138+
})
136139
} else {
137140
choicesJS.input.element.addEventListener('focus', initLoad)
138141
}
@@ -209,6 +212,8 @@ export default class Autocomplete {
209212
if (searchOn) {
210213
choicesJS.containerOuter.element.classList.add('search-on')
211214
choicesJS.containerOuter.element.classList.remove('search-off')
215+
// Focus the search input once it becomes visible (first open only).
216+
choicesJS.input.element.focus()
212217
} else {
213218
choicesJS.containerOuter.element.classList.remove('search-on')
214219
choicesJS.containerOuter.element.classList.add('search-off')
@@ -236,7 +241,26 @@ export default class Autocomplete {
236241
autocompleteData.forward.forEach(fieldToForward => {
237242
// replace current field id for forward field id keeping the view_id or inline_id prefixes intact
238243
const fieldToForwardInputId = inputEl.id.replace(new RegExp(autocompleteData.field_name + '$'), fieldToForward)
239-
autocompleteForwardData[fieldToForward] = document.getElementById(fieldToForwardInputId).value
244+
const fieldToForwardEl = document.getElementById(fieldToForwardInputId)
245+
let forwardValue
246+
if (fieldToForwardEl && fieldToForwardEl.value !== undefined) {
247+
// Standard input / select — use .value directly.
248+
forwardValue = fieldToForwardEl.value
249+
} else {
250+
// Radio / checkbox widgets render as <ul id="id_..."> with no .value.
251+
// Derive the input name from the element id (strip leading "id_") so that
252+
// nested-form prefixes (e.g. "form-0-state") are preserved correctly.
253+
const fieldName = fieldToForwardInputId.replace(/^id_/, '')
254+
const checkedInputs = Array.from(document.querySelectorAll(`[name="${fieldName}"]:checked`))
255+
if (checkedInputs.length > 1) {
256+
// Checkbox group — forward all checked values as an array.
257+
forwardValue = checkedInputs.map(el => el.value)
258+
} else {
259+
// Radio group (or nothing checked) — forward single value or empty string.
260+
forwardValue = checkedInputs.length === 1 ? checkedInputs[0].value : ''
261+
}
262+
}
263+
autocompleteForwardData[fieldToForward] = forwardValue
240264
})
241265
}
242266
autocompleteRequestData.set(autocompleteData.constants.autocomplete_forward, JSON.stringify(autocompleteForwardData))

0 commit comments

Comments
 (0)