11.1.6 • Published 3 years ago

@bbortt/ngx-autocomplete v11.1.6

Weekly downloads
1
License
Apache-2.0
Repository
github
Last release
3 years ago

@bbortt/ngx-autocomplete

Angular V11 compatible autocomplete component to use with any style framework.

Travis CI npm Version Blazing Fast License: Apache 2

Highlights:

  • Fully flexible for any autocomplete object through propertyAccessor function
  • Scales perfectly to every screen size, even when resizing
  • Navigable using the mouse or keyboard (see Navigation)
  • Create your own filter method which matches the case best
  • Fits into the @angular/forms / FormGroup setup
  • Customizable styling using css classes

>> Life Samples.

Contents

Getting Started

Add the NgxAutocompleteModule to your app.module.ts like this:

[...]

import { NgxAutocompleteModule } from '@bbortt/ngx-autocomplete';

@NgModule({
  declarations: [ ... ],
  imports: [ ..., NgxAutocompleteModule ],
  bootstrap: [ ... ],
})
export class AppModule {
}

That's it. You can head into the code at this point! The component selector is named <ngx-autocomplete />.

API Reference

Inputs

InputTypeDescriptionDefault
idstringThe ID of the <input /> component. Mostly used to link any <label /> elements.
namestringThe <input /> element name. Makes the form accessible for screen readers.
placeholderstringThe typical default placeholder of the <input /> element. Displayed weak as long as no value present.'Search.'
optionsany[]An array of options which could be selected.[]
propertyAccessorstring or (option: T) => stringThe object autocomplete (and display) property name. Or an accessor function, given the option.
navigateInfinitebooleanIf true then the arrow key navigation will never end. Thus restart from the beginning / end.false
debounceTimenumberA debounce time in ms. This delays the filtering as long as the user types continuously.0

Outputs

InputTypeDescription
autocompleteChangesEventEmitter<string>Event will be emitted whenever the filter value changes. The user is responsible for filtering his data and providing the updated options.

Css Classes

ngx-autocomplete-input: The autocomplete text input.

ngx-autocomplete-container: <div /> with absolute position, holding the possible autocomplete options.

ngx-autocomplete-option: Every <option /> of the autocomplete dropdown.

ngx-autocomplete-option-active: A single active option (not yet selected).

Navigation

KeyMeaning
arrow up / arrow downNavigate through the options. May be infinite using navigateInfinite.
enterSelects the current active option from the dropdown.
escapeCancel the selection - this does not use the current active option.

Examples

All example autocompletes are deployed life: bbortt.github.io/ngx-autocomplete.

Simple

The easiest autocomplete is the pure string array.

autocomplete-sample.component.ts:

@Component({
  selector: 'app-autocomplete-sample',
  templateUrl: './autocomplete-sample.component.html'
})
export class AutocompleteSampleComponent implements OnInit {
  editForm = this.fb.group({
    country: ['', Validators.required]
  });

  private allOptions = ALL_COUNTRIES;
  options: string[] = [];

  constructor(private fb: FormBuilder) {}

  ngOnInit(): void {
    this.editForm
      .get('country')!
      .valueChanges.subscribe((value: string) => this.filter(value));
  }

  filter(value: string): void {
    this.options = this.allOptions.filter((option: string) =>
      option.startsWith(value)
    );
  }
}

autocomplete-sample.component.html

<ngx-autocomplete
  inputName="country"
  inputId="field-country"
  formControlName="country"
  [options]="options"
  (autocompleteChanges)="filter($event)"
></ngx-autocomplete>

Source.

Advanced

The advanced sample uses complex objects and the name property as an accessor. Take a look at the code in projects/bbortt/samples/src/app/sample-advanced.

Bootstrap

The bootstrap sample lives up to its name. Using a custom .scss styling, it extends the Bootstrap form styles. Take a look at the code in projects/bbortt/samples/src/app/sample-bootstrap.

License

This project is licensed under the terms of the Apache 2.0 License.

11.1.6

3 years ago

11.1.5

3 years ago

11.1.4

3 years ago

11.1.3

3 years ago

11.1.2

3 years ago

11.1.1

3 years ago

11.0.2

3 years ago

11.1.0

3 years ago

11.0.1

3 years ago

11.0.0

3 years ago