Skip to content

Show no option in a native select without a stored value - #131

Closed
Contributolo wants to merge 1 commit into
inline0:mainfrom
Contributolo:fix/select-without-default
Closed

Contributolo wants to merge 1 commit into
inline0:mainfrom
Contributolo:fix/select-without-default

Conversation

@Contributolo

Copy link
Copy Markdown
Collaborator

A select field without default and without allowNull shows its first option in the inspector while nothing is stored. The block attribute stays "", Block::transform_attributes() turns it into false, and the template gets false. The editor suggests a choice the frontend never receives. A browser fires no change event when the user picks the option that is already selected, so users cannot store it without first picking another option. Conditions on that field see the empty value too.

Cause: Select passes value="" (or undefined) to SelectControl, no option has that value, and the browser selects the first <option>. The stylised select (ComboboxControl) and radio fields already show an empty value as nothing selected.

Reproduction on main (ec7950d), test block blockstudio/type-select-empty with options one/two and no default, in WordPress Playground:

Step Inspector shows Block attributes Editor render (PHP) Saved markup Frontend
untouched (main) one / "One" {"choice":""} "choice":false <!-- wp:blockstudio/type-select-empty /--> "choice":false
untouched (this PR) empty {"choice":""} "choice":false same "choice":false
pick two, then one (both) the picked option {"value":"one","label":"One"} "choice":"one" stored "choice":"one"

Change: while the value is empty and no option has the value "", the native select gets a disabled empty option in front, the placeholder pattern from the SelectControl README. It disappears once a value is stored, so the field still cannot be cleared without allowNull. Fields with allowNull already have a "" option and are unchanged. Stored values, defaults, the PHP side and the stylised and multiple selects are not touched. Existing posts keep their stored data; an untouched field now shows as empty instead of showing the first option.

An alternative would be to store the first option when the block is inserted (what a submitted HTML form would do). I did not do that because it changes stored data and render output for existing blocks, for example select/InnerBlocks expects "layout":false until a layout is picked. Happy to switch if you prefer that behavior.

Tests:

  • New tests/e2e/types/select/empty.ts with test block types/select/empty: the default renders "choice":false, the inspector value is "", then picking two and one renders and shows each value, at top level and inside InnerBlocks. On main it fails at "shows no option while nothing is stored" (received "one"), with this change 12/12 pass.
  • Unchanged results on main and with this change for types/select/innerBlocks.ts, types/options/select.ts, select-plain-string.ts; also passing with this change: types/conditions-default.ts, types/repeater. types/select/fetch.ts fails at "add test entry" on main and with this change alike in my local setup (port differs from wp-env).
  • npm run check passes.

Run against WordPress Playground instead of wp-env (no Docker here). The bundle in includes/admin/assets is not rebuilt in this branch, so the browser test needs npm run build first. No docs change: the select section is generated from the schema and documents no behavior for this case.

A native select without default and allowNull showed its first option
while nothing was stored, so the template received false although the
editor suggested a choice. Add a disabled empty placeholder while the
value is empty, as the SelectControl docs suggest, so the editor shows
what is stored. Stored values and rendering are unchanged.
dnnsjsk added a commit that referenced this pull request Oct 16, 2026
Address #132 with the WordPress ConfirmDialog, retaining textRemove overrides, immediate removal, minimum-row restrictions and nested-row isolation. Prevent portal keyboard events from reordering ancestor rows and cover cancellation, focus, keyboard confirmation and saved deletions in CI browser tests.

Integrate reviewed PR #131 with client regression tests and direct first-option selection coverage. Update schemas, generated docs and types, translations, LLM context, changelog and the production editor bundle. Local checks pass: 2048 PHP tests / 6067 assertions, 41 client tests, lint, TypeScript, PHPCS, docs, schemas, translations and build. No local E2E execution.

Co-authored-by: Contributolo <[email protected]>
@dnnsjsk

dnnsjsk commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

Reviewed and incorporated into the unreleased 7.7.4 preparation branch in d1d9ef8, without merging this PR yet. Kept the disabled empty-option policy, added client coverage for empty/null/zero and existing nullable options, and extended the browser regression to choose the first option directly from an untouched empty field. Docs and changelog are included. Local checks pass; the combined branch is going through full [all] CI. Thank you for the focused fix and reproduction.

@dnnsjsk

dnnsjsk commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Included in Blockstudio 7.7.4.

The native-select empty state has shipped, including direct selection of the first option and outer/InnerBlocks browser coverage.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants