Selection of already selected options is not respected

Created on 20 February 2025, 4 months ago

Problem/Motivation

Selected values are not kept if field formatter is set to select.

Steps to reproduce

Create a dependent field that uses a select fromatter and allow multiple values. Select options in both fields, then change selected values in the main/parent one. The dependent field will be updated, yet the selected options that are also present in new values are not marked as selected. It seems that there is a code that does that for other formatters like radios and checkboxes.

Proposed resolution

There is a need to extract the selected values before the update occurs and make any newly added options selected where the value matches. Most of the code is already present for other formatters.

if (element !== null) {
  // XXX CUSTOM START
  optionsArray = Array.prototype.slice.call(element.options)
  currentSelection = optionsArray
    .filter(option => option.selected)
    .map(option => option.value)
  // XXX CUSTOM END
  element.options.length = 0
  for (var i = 0; i <= options.length; i++) {
    if (options.hasOwnProperty(i)) {
      element.options.add(new Option(options[i].value, options[i].key))
      // XXX CUSTOM START
      if (currentSelection.includes(options[i].key.toString())) {
        element.options[i].setAttribute('selected', 'selected')
      }
      // XXX CUSTOM END
    }
  }
}

Remaining tasks

Make a patch and commit it to the merge request branch.

User interface changes

The matching selected options will still remain selected as they are expected to.

API changes

None.

Data model changes

None.

🐛 Bug report
Status

Active

Version

1.0

Component

Code

Created by

🇪🇪Estonia pjotr.savitski

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024