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

Add disabled options support in tp-multi-select #12

Merged
merged 2 commits into from
Dec 12, 2023
Merged
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
3 changes: 2 additions & 1 deletion src/multi-select/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<tp-multi-select-option value="varun" label="Varun">Varun</tp-multi-select-option>
<tp-multi-select-option value="john" label="John">John</tp-multi-select-option>
<tp-multi-select-option value="jane" label="Jane">Jane</tp-multi-select-option>
<tp-multi-select-option value="deepak" label="Deepak" disabled="yes">Deepak</tp-multi-select-option>
</tp-multi-select-options>
</tp-multi-select>

Expand Down Expand Up @@ -102,7 +103,7 @@
<tp-multi-select-option value="john" label="John">John</tp-multi-select-option>
<tp-multi-select-option value="jane" label="Jane">Jane</tp-multi-select-option>
<tp-multi-select-option value="jack" label="Jack">Jack</tp-multi-select-option>
<tp-multi-select-option value="jill" label="Jill">Jill</tp-multi-select-option>
<tp-multi-select-option value="jill" label="Jill" disabled="yes">Jill</tp-multi-select-option>
</tp-multi-select-options>
</tp-multi-select>
</main>
Expand Down
5 changes: 5 additions & 0 deletions src/multi-select/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ tp-multi-select-option {
&[hidden="yes"] {
display: none;
}

&[disabled="yes"] {
opacity: 0.5;
cursor: not-allowed;
}
}

tp-multi-select-placeholder {
Expand Down
2 changes: 1 addition & 1 deletion src/multi-select/tp-multi-select-select-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class TPMultiSelectSelectAllElement extends HTMLElement {
return;
}

if ( options.length === multiSelect.value.length ) {
if ( Array.from( options ).filter( ( optionNode ) => optionNode.getAttribute( 'disabled' ) !== 'yes' ).length === multiSelect.value.length ) {
this.setAttribute( 'selected', 'yes' );
this.innerHTML = this.getAttribute( 'unselect-text' ) ?? '';
} else {
Expand Down
8 changes: 6 additions & 2 deletions src/multi-select/tp-multi-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,9 @@ export class TPMultiSelectElement extends HTMLElement {
// Select all options.
const styledSelectedOptions: NodeListOf<TPMultiSelectOptionElement> | null = this.querySelectorAll( `tp-multi-select-option[value="${ value }"]` );
styledSelectedOptions?.forEach( ( option: TPMultiSelectOptionElement ): void => {
option.setAttribute( 'selected', 'yes' );
if ( 'yes' !== option.getAttribute( 'disabled' ) ) {
option.setAttribute( 'selected', 'yes' );
}
} );

// Search stuff.
Expand All @@ -271,7 +273,9 @@ export class TPMultiSelectElement extends HTMLElement {
selectAll(): void {
const styledOptions: NodeListOf<TPMultiSelectOptionElement> | null = this.querySelectorAll( 'tp-multi-select-option' );
styledOptions?.forEach( ( option: TPMultiSelectOptionElement ): void => {
option.setAttribute( 'selected', 'yes' );
if ( 'yes' !== option.getAttribute( 'disabled' ) ) {
option.setAttribute( 'selected', 'yes' );
}
} );

this.dispatchEvent( new CustomEvent( 'select-all', { bubbles: true } ) );
Expand Down