Repository navigation
Fix: accept a single value in multiple selects and checkboxes - #123
Contributolo wants to merge 4 commits into
Conversation
A select with `multiple: true` and a checkbox field store a list of
`{value, label}` options. Two other shapes occur in real content:
- a single `{value, label}` object, saved while the field was still a
single select, before `multiple` was enabled
- a plain string or number, written outside the editor by scripts, the
REST API or content tooling, for example `"language":"de"`
In the editor a plain value crashed the field: the multiple select
called `filter` and the checkbox called `map` on it ("is not a
function"). With a single object the checkbox showed no option as
checked, and choosing a second option in either field replaced the
stored option instead of adding to it.
On the server a plain value raised a `foreach()` warning and rendered
as `false`. A single object was iterated as two selections, its value
and its label, which duplicated it when both are equal.
Both sides now read the stored value through one helper each,
`normalizeOptionValues()` and `Block::multiple_option_values()`: a list
is kept as it is, a single option object becomes a one item list, a
plain value becomes one option (labelled from the field options in the
editor) and empty values become an empty list. Lists behave exactly as
before.
dnnsjsk
left a comment
There was a problem hiding this comment.
The normalization addresses the reported crashes, but I reproduced a remaining data-loss path in the numeric scalar case through the actual getValue() implementation: start with scalar 0 and numeric options 0/1, select 1, then uncheck 0. Selecting 1 correctly produces [0,1] with this PR, but unchecking 0 returns [] instead of preserving 1. Checkbox passes itemInner.value directly to change(), so the numeric 0 reaches the value ? toggleOption() : [] guard in Fields and is treated as a clear-all command.
Please treat zero as a valid option value in the edit path and add a regression covering that sequence. The pure normalization tests do not cover add/remove behavior in Fields. Browser regression coverage is also needed for scalar and single-object values in multiple selects/checkboxes: show the existing selection, add/remove without losing it or sibling values, and preserve stored shapes on an untouched save/reload. The current component SSR checks cannot verify those editor interactions. Leaving this open for the corrections rather than merging it into the tested 7.7.2 cut.
The checkbox passes the clicked option value straight to change(), and
Fields only toggled it when the value was truthy (`value ? toggleOption()
: []`). An option value of 0 was therefore read as "clear everything":
starting from a stored 0 with the options 0 and 1, selecting 1 gave
[0, 1], and unchecking 0 gave [] instead of [1].
The add and remove logic is now toggleOptionValue() in
src/blocks/utils/option-values.ts, and getValue() calls it directly, so
the unit tests run the same code as the editor. Every option value
toggles, including 0, "0", "" and false. Clearing all options keeps
working as before: the toggle-all checkbox and removing the last card
send a direct change, which never reaches getValue(), and undefined or
null still clear.
Option objects with a falsy value broke one step earlier. optionSetter()
and the option lookup in Fields read options with `e?.value || e`, so
`{ value: 0 }` turned into the whole object: the checkbox never showed it
as checked and toggling it stored a nested object. The multiple select
filtered its cards with `e?.value`, so a selected 0 was not shown and
removing a sibling card wrote the list without it. Option values and
labels are now read with getOptionValue() and getOptionLabel(), and
selections are compared by their string form, as the server does.
Labels reach the controls as text. An option 0 had no visible checkbox
label, and plain numeric options crashed the multiple select because
ComboboxControl calls string methods on labels.
A stored list of plain values, as scripts write it, is read as options
too, so the editor shows it and toggles it like any other list.
…kboxes transform_option_attribute() kept only truthy resolved values, so a selected option 0 or "0" disappeared from the template data, and transform_attributes() skipped a stored integer 0 as empty before it could be read as a selection. Only unresolved values (false and null) are dropped now, and an integer 0 counts as a stored value for checkboxes and multiple selects. Single selects and other fields keep their current handling of 0.
…n the editor Adds the test block blockstudio/type-option-values with checkboxes and multiple selects for numeric and string options, declared as objects and as plain values, each starting with 0 or "0" stored. For a stored scalar and for a stored single option object, the browser test checks that the editor shows the stored selection, that an untouched save and reload keeps the stored attributes exactly (compared with the saved post content), that adding a second option keeps the stored one, and that removing the stored option keeps the added one, including after save and reload. The rendered values are checked at every step.
|
Thanks for the thorough review, you were right. Changes are in three new commits (9fbe9d8, 8da67c4, 95f497a): Zero in the edit path. The checkbox passes Testing the other falsy values turned up four more spots on the same path, all fixed:
Regression for your sequence. Browser coverage. Verified locally in WordPress Playground (no Docker here): new E2E 6/6, related E2E suites unchanged (select-fetch needs its hardcoded :8888), BlockTest 61/61, Not changed here (separate from this PR, happy to follow up): 0 in single/stylised selects ( |
|
Included in Blockstudio 7.7.4. Support for imported scalar and single-option values in multiple selects and checkboxes has shipped, including numeric-zero and repeater regressions. Your changes were incorporated into the consolidated release branch with integration tests and release adjustments, so I am closing this PR as shipped rather than merging the original branch again. Full CI (14/14) and Distribution (5/5) passed on release commit e1d38df. Thank you for the contribution. |
Problem
A
selectwithmultiple: trueand acheckboxfield store a list of{value, label}options. Two other stored shapes occur in real content:{value, label}object, saved while the field was still a single select, beforemultiplewas enabled."language":"de".Symptoms:
(value || []).filter is not a functionin the multiple select,v.map is not a functionin the checkbox).foreach() argument must be of type array|objectand renders asfalse. A single object is iterated as two selections (its value and its label), which duplicates it when both are equal, for example with plain string options.Fix
One helper per side reads the stored value as a list:
normalizeOptionValues()insrc/blocks/utils/option-values.ts, used by the multiple select, the checkbox and the add/remove logic ingetValue().Block::multiple_option_values(), used bytransform_option_attribute(), including multiple selects withpopulate.type: "fetch".A list is returned unchanged, a single option object becomes a one-item list, a plain string or number becomes one option (labelled from the field options in the editor, falling back to the value), and empty values become an empty list. Lists behave exactly as before. Stored content is not rewritten on load; the field saves a list once the user changes it.
Tests
tests/unit/block/BlockTest.php: data-provider test fortransform_attributes()covering string, number, numeric string, object, list and empty values for multiple selects, checkboxes and fetch selects. 10 of the 19 cases fail onmainand all pass with this change.tests/client/option-values.test.ts: unit tests for the helper.Checked locally with the new tests outside wp-env,
tsc --noEmit, eslint, prettier, PHPCS and PHPStan level 3. The editor itself was checked by rendering the field components server side (multiple select and checkbox with a string, a number and a single object), not in a browser.