Skip to content

Fix input collapses for multi select & no placeholder & no initial selection - #1374

Open
muendlein wants to merge 1 commit into
Choices-js:mainfrom
muendlein:fix-initial-setWidth
Open

Fix input collapses for multi select & no placeholder & no initial selection#1374
muendlein wants to merge 1 commit into
Choices-js:mainfrom
muendlein:fix-initial-setWidth

Conversation

@muendlein

Copy link
Copy Markdown

Description

Fixes #1368

Types of changes

  • Chore (tooling change or documentation change)
  • Refactor (non-breaking change which maintains existing functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist

  • My code follows the code style of this project.
  • I have added new tests for the bug I fixed/the new feature I added.
  • I have modified existing tests for the bug I fixed/the new feature I added.

@Xon Xon changed the title avoid setWidth without any items Fix input collapses for multi select & no placeholder & no initial selection Jan 31, 2026
@Xon Xon self-assigned this Jan 31, 2026
@Xon Xon added the bugfix Pull request that fixes an existing bug label Jan 31, 2026
Comment thread src/scripts/choices.ts
}
this.input.setWidth();

if (this._placeholderValue || this._store.items.length > 0) {

@Xon Xon Jan 31, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to not simplify this check to just?
else if (this._store.items.length) {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if I fully understand your suggestion but if (this._store.items.length) should always evaluate to true as items are initialized as [].

@Xon Xon Feb 2, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In javascript, [].length === 0. but if length is a positive value, then it is truthy. However the real fix probably should be in setWidth

  setWidth(): void {
    // Resize input to contents or placeholder
    const { element } = this;
    element.style.minWidth = `${element.placeholder.length + 1}ch`;
    element.style.width = `${element.value.length + 1}ch`;
  }

This should have had a minimum length of 1, but obviously something isn't working as expected; likely for element.value.length term.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The this._store.items aren't populated when _createStructure() is called, so this check doesn't work as expected

@Xon

Xon commented Feb 2, 2026

Copy link
Copy Markdown
Collaborator

Looking into this more, removing the logic which reduces the input to 1ch causes reflow issues when multiple items are selected on a narrow enough display port

@Xon

Xon commented Feb 7, 2026

Copy link
Copy Markdown
Collaborator

Ah, after some tinkering this is a more general issue with no placeholders or no items causing the total height of the entire choices.js widget to dramatically shrink.

This change doesn't fix that, and just bandaids the issue

@Xon Xon added the changes required Pull request requires changes before it can be merged label Jul 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Pull request that fixes an existing bug changes required Pull request requires changes before it can be merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Input collapses for multi select & no placeholder & no initial selection

2 participants