Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Autocomplete] Only activate clear button plugin when there is a placeholder option #2282

Open
wants to merge 2 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/Autocomplete/assets/dist/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise, SuppressedError, Symbol */
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */


function __classPrivateFieldGet(receiver, state, kind, f) {
Expand Down Expand Up @@ -227,7 +227,11 @@ _default_1_instances = new WeakSet(), _default_1_getCommonConfig = function _def
const plugins = {};
const isMultiple = !this.selectElement || this.selectElement.multiple;
if (!this.formElement.disabled && !isMultiple) {
plugins.clear_button = { title: '' };
const placeholderOptions = Array.from(this.selectElement.options)
.filter((option) => option.value === '');
Comment on lines +230 to +231
Copy link
Member

Choose a reason for hiding this comment

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

Maybe use Array.from(...).find() here ?
(especially as the placeholder will be first element in most of the cases)

Copy link
Member

Choose a reason for hiding this comment

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

Even .some() that will return you the boolean you need L232 ?

Copy link
Member

Choose a reason for hiding this comment

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

(yeah.. in the .ts file 😅 )

if (placeholderOptions.length) {
plugins.clear_button = { title: '' };
}
}
if (isMultiple) {
plugins.remove_button = { title: '' };
Expand Down
9 changes: 7 additions & 2 deletions src/Autocomplete/assets/src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,15 @@ export default class extends Controller {
#getCommonConfig(): Partial<TomSettings> {
const plugins: TPluginHash = {};

// multiple values excepted if this is NOT A select (i.e. input) or a multiple select
// Multiple values expected if this is NOT A select (i.e. input) or a multiple select
const isMultiple = !this.selectElement || this.selectElement.multiple;
if (!this.formElement.disabled && !isMultiple) {
plugins.clear_button = { title: '' };
// Only activate clear button plugin when there is a placeholder option
const placeholderOptions = Array.from(this.selectElement.options)
.filter((option) => option.value === '');
if (placeholderOptions.length) {
plugins.clear_button = { title: '' };
}
}

if (isMultiple) {
Expand Down
Loading