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

Select option defining whether it is possible to have an empty value in option #532

Open
wants to merge 1 commit into
base: main
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
7 changes: 6 additions & 1 deletion src/plugins/select/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class HSSelect extends HSBasePlugin<ISelectOptions> 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;
Expand Down Expand Up @@ -208,6 +209,10 @@ class HSSelect extends HSBasePlugin<ISelectOptions> 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;
Expand Down Expand Up @@ -307,7 +312,7 @@ class HSSelect extends HSBasePlugin<ISelectOptions> 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');

Expand Down
1 change: 1 addition & 0 deletions src/plugins/select/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export interface ISelectOptions {
searchNoResultText?: string | null;
searchNoResultClasses?: string | null;

optionAllowEmptyOption?: boolean
optionTemplate?: string;
optionTag?: string;
optionClasses?: string;
Expand Down