ionic-framework icon indicating copy to clipboard operation
ionic-framework copied to clipboard

Feature Request: ionic-combobox

Open xlegs opened this issue 10 years ago • 8 comments

I'm looking for a combobox component in Ionic. It's essentially a form element that combines ion-input and ionic-searchbar. I've implemented it to a certain extent using ionic-searchbar, but I'm having difficulty making it look like ionic-input and making it a formControl.

Here's the HTML:

<ion-searchbar (ionInput)="search($event)" [(ngModel)]="searchString" debounce=1000></ion-searchbar>

<ion-list class="results" *ngIf="showResults">
  <ion-item *ngFor="let item of items" (click)="set(item)">
    <ion-thumbnail item-left>
      <img src="assets/logos/{{item.key.toLowerCase()}}.svg">
    </ion-thumbnail>
    {{item.key}} - {{item.value}}
  </ion-item>
</ion-list>

Here's the TypeScript:

export class AirlineSearch implements OnInit {

  data: any; 
  promise:any;
  public items: any;
  public searchString: string;
  public showResults: boolean;

  constructor(private nav: NavController, navParams: NavParams, private airlineCodes: AirlineCodes) {
        this.showResults = false;
  }

  ngOnInit(): void {
    this.airlineCodes.load().then(res => this.data = res);
  }

  set(item) {
    this.searchString = item.key + ' - ' + item.value;
    this.showResults = false;
  }

  search(ev: any) {
    // set val to the value of the searchbar
    let val = this.searchString;

    if (val && val.trim() == '') {
      this.showResults = false;
    }

    let input: string = val.trim();

    this.items = this.data.filter(function (row) {
        if (input.length < 2) {
            return;
        } 

        input = input.toLowerCase();
        let str: string = null;

        if (input.length == 2) {
            str = row.key.toLowerCase();
        }

        if (input.length > 2) {
            str = row.value.toLowerCase();
        }

        return str.includes(input);
    });

    if (this.items.length == 1) {
        this.set(this.items[0]);
    } 

    if (this.items.length > 1) {
        this.showResults = true;
    }

  }

}

I think it would be useful to have a component where one can just write:

<ion-combobox (ionInput)="search($event)" [(ngModel)]="searchString" debounce=1000 formControlName="combobox"></ion-combobox>

Since typing is faster than scrolling for large dropdowns (e selecting a country), a combobox is useful in a lot of cases, particularly for mobile users.

xlegs avatar Oct 04 '16 19:10 xlegs

I think the expected UX on mobile would be to open a modal with a search bar at the top similar to the Language & Region example here. Your form on the modal's parent would have the name of the field on the left & the selected (or none) value on the right.

WhatsThatItsPat avatar Oct 04 '16 20:10 WhatsThatItsPat

That makes sense, but how would I go about integrating the modal into the FormBuilder API?

xlegs avatar Oct 04 '16 21:10 xlegs

I only skimmed these, but it looks like you're looking for ControlValueAccessor.

WhatsThatItsPat avatar Oct 04 '16 22:10 WhatsThatItsPat

Thanks for the links, but since everything is so new, these docs are not that helpful yet. This use case is not that uncommon, so I hope the Ionic developers consider implementing this anyway. It would help Ionic differentiate itself from other frameworks.

Thanks .

xlegs avatar Oct 05 '16 18:10 xlegs

Hello all! While this is an awesome feature request it is not something that we plan on doing anytime soon. Because of this I am going to move this to our internal feature tracking repo for now as it is just "collecting dust" here. Once we decide to implement this I will move it back. Thanks everyone for using Ionic!

jgw96 avatar Feb 06 '17 21:02 jgw96

This issue was moved to driftyco/ionic-feature-requests#72

jgw96 avatar Feb 06 '17 21:02 jgw96

The link above does not lead anywhere? Is there a status on this issue? It would be of great use to me to have a combo box component. Is it possible at this point to such a feature added to Ionic?

lukejoliat avatar Jun 09 '17 02:06 lukejoliat

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.

ionitron-bot[bot] avatar Sep 02 '18 17:09 ionitron-bot[bot]