GithubHelp home page GithubHelp logo

ihabsalem / ngxmattypeahead Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mrsan22/ngxmattypeahead

0.0 2.0 0.0 218 KB

A typeahead directive to be used with Angular Material input and matAutocomplete component.

Home Page: https://www.npmjs.com/package/ngx-mat-typeahead

License: MIT License

JavaScript 10.80% TypeScript 80.66% HTML 8.11% CSS 0.43%

ngxmattypeahead's Introduction

NgxMatTypeahead

  • A simple typeahead directive to be used with Angular Material input and matAutocomplete component.
  • This directives enhances the funtionality of Angular Material matAutocomplete component and is recommended that it is used with it.
  • However, this directive can be used with any other autocomplete component.
  • It is developed using Angular >=6.0.0 and its newly introduced ng g library schematics.
  • This library is part of MatTypeahead project and it is generated with Angular CLI version 6.0.3.
  • Library location: projects/ngx-mat-typeahead directory of this repository.

Examples/Demo

  • A simple Example can be found under src/app directory of this repository. It uses json-server to have a url and filter functionality.

Installation

npm i ngx-mat-typeahead

API

import { NgxMatTypeaheadModule } from 'ngx-mat-typeahead'
selector: NgxMatTypeahead

@Inputs()

Input Type Required Description
apiURL string YES the url of a remote server that supports http/jsonp calls.
delayTime number Optional, default: 300 the debounce time for this request.
urlParams object Optional, default: {} { key: string, value: any} object as additional parameters
urlQueryParam string Optional, default: 'query' a string value which is used a query parameter in the url. Ex: http://localhost:3000/countries?query='c
apiMethod string Optional, default: 'get' the http/jsonp method to be used.
apiType string Optional, default: 'http' http or jsonp method types.
callbackFuncName string Optional a string value for the callback query parameter.
allowEmptyString boolean Optional, default: true if true, it allows empty strings to pass and invoke search

@Outputs()

Output Type Required Description
filteredDataList Array YES emits filtered data list depending on the search term.

Usage

  1. Register the NgxMatTypeaheadModule in your app module.

import { NgxMatTypeaheadModule } from 'ngx-mat-typeahead'

import { HttpClientModule } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MatAutocompleteModule, MatInputModule } from '@angular/material';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { NgxMatTypeaheadModule } from 'ngx-mat-typeahead';
import { AppComponent } from './app.component';

@NgModule({
 declarations: [AppComponent],
 imports: [
   BrowserModule,
   BrowserAnimationsModule,
   FormsModule,
   ReactiveFormsModule,
   MatInputModule,
   MatAutocompleteModule,
   HttpClientModule,
   NgxMatTypeaheadModule
 ],
 providers: [],
 bootstrap: [AppComponent]
})
export class AppModule {}
  1. Use the directive (NgxMatTypeahead) in your component.
import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import { AppService } from './app.service';
@Component({
  selector: 'mat-ta-root',
  template: `<h3>NgxMatTypeahead demo app using Angular Material</h3>
<div [formGroup]="testFormGroup">
  <mat-form-field>
    <input matInput NgxMatTypeahead [apiURL]="url" [urlQueryParam]="queryParam" (filteredDataList)="getFilteredSuggestions($event)"
      formControlName="country" [matAutocomplete]="countryAuto" placeholder="Choose Country">
    <mat-autocomplete #countryAuto="matAutocomplete">
      <mat-option *ngFor="let country of countries" [value]="country">
        {{country}}
      </mat-option>
    </mat-autocomplete>
  </mat-form-field>
</div>
`,
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  // Paramteres for the input type are defined below. The url is generated using `json-server`.
  // Please run your own instance of the json-server to use the the below url.
  queryParam = 'q';
  url = 'http://localhost:3000/countries';

  constructor(private appService: AppService) {}

  testFormGroup: FormGroup = new FormGroup({ country: new FormControl('') });
  countries: Array<string> = [];

  ngOnInit() {
    this.countries = ["United States", "United Kingdom", "China", "Japan", "India", "Russia", "Canada", "Brazil"];
  }

  getFilteredSuggestions(filteredDataLst: Array<any>) {
    this.countries = [...filteredDataLst];
  }
}

Running the example in local env

  • npm i
  • Run ng serve for a dev server and running the demo app. Navigate to http://localhost:4200/. The app will automatically reload if you change any of the source files.
  • The demo app uses json-server module for enabling the url and filter funtionality.

Build the NgxMatTypeahead module

Run ng build NgxMatTypeahead to build the library. The build artifacts will be stored in the dist/ngx-mat-typeahead directory. Use the --prod flag for a production build.

Running unit tests

Run ng test NgxMatTypeahead to execute the unit tests via Karma.

Credits

This project is based on ngx-typeahead. I want to thank Oren Farhi from Orizens for open sourcing his project as it helped me to write my first simple Angular library. Also want to thanks entire Angular team for creating this awesome framework.

ngxmattypeahead's People

Contributors

mrsan22 avatar

Watchers

James Cloos avatar Ihab Salem avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.