1.4.0 • Published 5 years ago

fly-select v1.4.0

Weekly downloads
9
License
MIT
Repository
github
Last release
5 years ago

Fly Select

Fly Select is a customizable select component for Angular 6+. Used for displaying options and groups of options with default minimalistic design.

N|Solid

Demo

Contents

Installing

Install the package from npm:

npm i fly-select --save

Simple usage

Add the FlySelectModule in the imports array in AppModule:

import {FlySelectModule} from 'fly-select';

Add the fly-select component in your component:

<fly-select
    [data]="data">
</fly-select>

Add the data array in you component class:

const data = [
    {
        label: 'Tony Stark',
        value: 'ironman'
    },
    {
        label: 'Bruce Wayne',
        value: 'batman'
    },
    {
        label: 'Clark Kent',
        value: 'superman'
    }
];

The basic data structure (obecjts in the array must have label and value properites) looks like the above example, but you can use a different structure. If your data don't have label nad value properties, there is no need to transform the data to the previous structure. In this case you specify two additioanl inputs (labelProperty, valueProperty) on the component:

<fly-select
    [data]="data"
    [labelProperty]="'name'"
    [valueProperty]="'id'">
</fly-select>
const data = [
    {
        id: 1,
        name: 'Tony Stark',
        alias: 'Ironman',
        company: 'Stark Industries',
        avatar: 'https://cdn0.iconfinder.com/data/icons/superhero-2/256/Ironman-512.png'
    }
];

Custom Option Templates

You can customize the options by adding template inside the fly-select component with the directive flySelectItemBody, you can use the data inside the template if you create option (which will contain the original data under the property original) varaible:

<fly-select
    [data]="data"
    [labelProperty]="'name'"
    [valueProperty]="'id'">
    <ng-template let-option="option" flySelectItemBody>
        <div class="hero-card">
            <div class="image" [style.backgroundImage]="'url(' + option.original.avatar + ')'"></div>
        
            <div class="info">
                <div class="alias">{{option.original.alias}}</div>
                <div class="name-copmany">{{option.original.name}}, {{option.original.company}}</div>
            </div>
        </div>
    </ng-template>
</fly-select>

Groups

Fly Select support option groups, the data structure is different from previous examples. Group objects must contain groupLabel and groupOptions properties or two additional inputs (groupLabelProperty, groupOptionsProperty) to be specified on the component.

groups = [
    {
        groupLabel: 'Marvel',
        avatar: 'https://cdn3.iconfinder.com/data/icons/movie-company/129/MARVEL.png',
        groupOptions: [
            {
                  id: 1,
                  name: 'Tony Stark',
                  alias: 'Ironman',
                  gender: 'Male',
                  company: 'Stark Industries',
                  avatar: 'https://cdn0.iconfinder.com/data/icons/superhero-2/256/Ironman-512.png'
            }
        ]
    }
];

You can also change the body of the group label by adding template inside fly-select component with directive flySelectGroupBody. You can access the group data by creating group variable in the template, if your data objects have more properties they will be moved in the groupOriginal.

<form [formGroup]="formOne">
    <fly-select
      [data]="groups"
      [labelProperty]="'name'"
      [valueProperty]="'id'">
        <ng-template let-group="group" flySelectGroupBody>
            <div class="universe-card">
                <div class="image" [style.backgroundImage]="'url(' + group.groupOriginal.avatar + ')'"></div>
                
                <div class="info">{{group.groupLabel}}</div>
            </div>
        </ng-template>
        
        <ng-template let-option="option" flySelectItemBody>...</ng-template>
    </fly-select>
</form>

Reactive Forms

You can use the component with reactive forms:

form = new FormGroup({
    hero: new FormControl('', [Validators.required])
});
<fly-select
    [data]="data"
    [formControlName]="'hero'">
</fly-select>

If you want to set a default value, lets say you want Superman to be selected on component init:

ngOnInit() {
  this.form.get('hero').setValue(this.data[2]);
}

Styling

You can style the select to your need, just set the input style to false on the component and you are good to go. Useful css classes for stying:

  • .fly-select-head-wrapper (have :hover)
  • .fly-select-body-wrapper
  • .fly-select-option-wrapper
  • .fly-select-group-wrapper
  • .fly-select-group-label

Added classes after some event:

  • fly-select.host-focus
  • .fly-select-wrapper.open
  • .fly-select-head-wrapper.disabled
  • .fly-select-option-wrapper.highlight

Inputs

InputTypeDescription
dataArrayData of the options or the groups
placeholderstringCustom text to show when no option is selected
labelPropertystringProperty to get the option label from
valuePropertystringProperty to get the option value from
groupLabelPropertystringProperty to get the group label value from
groupOptionsPropertystringProperty to get the group options from
stylebooleanSwitch component styling

Outputs

OutputTypeDescription
selectedEventEmitterEmitted when option is selected

Directives

Directive NameDescription
flySelectGroupBodyUse it for customizing the groups template
flySelectItemBodyUse it for customizing the options template

Keyboard events

Keyboard events are available for esc, enter, arrowup, arrowdown.

KeyDescription
EscClose the select if opened
EnterSelect option from the select if highlighted
ArrowUp/ArrowDownNavigate through the options
1.4.0

5 years ago

1.3.4

6 years ago

1.3.3

6 years ago

1.3.2

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago