From 1b4af98d5de2d4d153fe3f8e46700c0dd404d5aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20G=C3=B3rnisiewicz?= Date: Wed, 18 Dec 2024 11:35:53 +0100 Subject: [PATCH] select option defining whether it is possible to have an empty value in option --- src/plugins/select/index.ts | 7 ++++++- src/plugins/select/interfaces.ts | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/plugins/select/index.ts b/src/plugins/select/index.ts index c063adf..92bfe6e 100644 --- a/src/plugins/select/index.ts +++ b/src/plugins/select/index.ts @@ -89,6 +89,7 @@ class HSSelect extends HSBasePlugin implements ISelect { private readonly searchNoResultTemplate: string | null; private readonly searchNoResultText: string | null; private readonly searchNoResultClasses: string | null; + private readonly optionAllowEmptyOption: boolean; private readonly optionTag: string | null; private readonly optionTemplate: string | null; private readonly optionClasses: string | null; @@ -208,6 +209,10 @@ class HSSelect extends HSBasePlugin implements ISelect { this.searchNoResultClasses = concatOptions?.searchNoResultClasses || 'px-4 text-sm text-gray-800 dark:text-neutral-200'; + this.optionAllowEmptyOption = + typeof concatOptions?.optionAllowEmptyOption !== 'undefined' + ? concatOptions?.optionAllowEmptyOption + : false; this.optionTemplate = concatOptions?.optionTemplate || null; this.optionTag = concatOptions?.optionTag || null; this.optionClasses = concatOptions?.optionClasses || null; @@ -307,7 +312,7 @@ class HSSelect extends HSBasePlugin implements ISelect { if (this.el.children) { Array.from(this.el.children) - .filter((el: HTMLOptionElement) => el.value && el.value !== '') + .filter((el: HTMLOptionElement) => this.optionAllowEmptyOption || (!this.optionAllowEmptyOption && el.value && el.value !== '')) .forEach((el: HTMLOptionElement) => { const data = el.getAttribute('data-hs-select-option'); diff --git a/src/plugins/select/interfaces.ts b/src/plugins/select/interfaces.ts index e520c00..b421b9d 100644 --- a/src/plugins/select/interfaces.ts +++ b/src/plugins/select/interfaces.ts @@ -85,6 +85,7 @@ export interface ISelectOptions { searchNoResultText?: string | null; searchNoResultClasses?: string | null; + optionAllowEmptyOption?: boolean optionTemplate?: string; optionTag?: string; optionClasses?: string;