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

fix(select): select not rendering option wrapped in ng container #1280

Open
wants to merge 3 commits into
base: master
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@

<nb-card *nbPortal class="select" [ngClass]="[status, overlayPosition]" [style.width.px]="hostWidth">
<nb-card-body>
<ng-content select="nb-option, nb-option-group"></ng-content>
<ng-content select="nb-option, nb-option-group, ng-container"></ng-content>
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure if it's ok to select ng-container here. This way it would be possible to pass arbitrary content into select. What do you think @aefox? Probably there is something we could do to restrict ng-container content (e.g. more specific selector)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@yggg I totally see your point on possible to pass arbitrary content into select. That indeed is a problem I did not figure out, nice catch 👍

At the same time, please note the following hack/trick:

<nb-select placeholder="Select Showcase" [(selected)]="selectedItem">
  <ng-container *ngFor="let item of items" ngProjectAs="nb-option">
    <div>{{ item.label }}</div>
  </ng-container>
</nb-select>

This is currently possible to do and it works. Works here means it actually renders the divs correctly (they ofc do not work as an option, but that is expected).

I'm not sure if we should force the users of nebular to restrict them from using ng-container in this situation as probably some devs will prefer to use ng-container for loops and keep their nb-options cleaner.

Indeed doing this we're opening up the possibility for miss usage, but the same can happen for the pure html tags (select and option).

I'm basically opened to doing this any way you think it is best.

</nb-card-body>
</nb-card>
12 changes: 12 additions & 0 deletions src/framework/theme/components/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ const TEST_GROUPS = [
<nb-option-group *ngFor="let group of groups" [title]="group.title">
<nb-option *ngFor="let option of group.options" [value]="option.value">{{ option.title }}</nb-option>
</nb-option-group>
<ng-container *ngIf="groups">
<nb-option>ng-container option</nb-option>
</ng-container>
</nb-select>
</nb-layout-column>
</nb-layout>
Expand Down Expand Up @@ -221,4 +224,13 @@ describe('Component: NbSelectComponent', () => {
expect(button.textContent).toContain('1 noitpO');
})
});

it('should render option wrapped in ng-container', () => {
select.multiple = true;
setSelectedAndOpen([]);

const options = overlayContainer.querySelectorAll('nb-option');
const lastOption = options[options.length - 1];
expect(lastOption.textContent).toEqual('ng-container option');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@
<nb-option value="3">Option 3</nb-option>
<nb-option value="4">Option 4</nb-option>
</nb-select>