7.3.4 • Published 3 years ago

@niliur/ng-select2-component v7.3.4

Weekly downloads
-
License
-
Repository
github
Last release
3 years ago

npm version Downloads GitHub license

Select2

This Angular CLI module it's a fork of select2-component without Vue & React. For Vue or React, please use the original component.

Installation

npm i ng-select2-component --save

Requirements

  • Angular 7.0.0 and more

Demo

See a demo.

Features

  • select one
  • options or groups
  • scroll
  • local search
  • select by keyboard
  • disabled option
  • disabled component
  • hide search box
  • placeholder
  • multiple selection
  • material style
  • form binding
  • templating

Usage

example

import { Select2Module } from "ng-select2-component";

@NgModule({
    imports: [BrowserModule, FormsModule, Select2Module],
    declarations: [MainComponent],
    bootstrap: [MainComponent],
})
class MainModule { }
<select2 [data]="data"
    [value]="value"
    (update)="update($event)">
</select2>

properties and events of the component

nametypestatusdefaultdescription
dataSelect2Datarequiredthe data of the select2
valueSelect2Valueinitial value
disabledbooleanwhether the component is disabled
minCharForSearchnumber0start the search when the number of characters is reached (0 = unlimited)
minCountForSearchnumber6hide search box if options.length < minCountForSearch
displaySearchStatus'default' or 'hidden' or 'always''default'display the search box (default : is based on minCountForSearch)
placeholderstringthe placeholder string if nothing selected
customSearchEnabledbooleanwill trigger search event, and disable inside filter
multiple"" or true or 'true'select multiple options
resettableadd a button to reset value (not with multiple)
limitSelectionnumber0to limit multiple selection (0 = unlimited)
hideSelectedItemsbooleanfor multiple, remove selected values
resultMaxHeightstringchange the height size of results
listPosition'below' or 'above''below'the position for the dropdown list
material"" or true or 'true'enable material style
nostyle"" or true or 'true'remove border and background color
templatesTemplateRef or {option?: TemplateRef, group?: TemplateRef} or {templateId1: TemplateRef, ...}use templates for formatting content (see Templating)
editPattern(str: string) => stringuse it for change the pattern of the filter search
ngModelidrequireddisabledreadonlytabIndexjust like a select control
(update)(event:Select2UpdateEvent) => voideventtriggered when user select an option
(open)(event: Select2) => voideventtriggered when user open the options
(close)(event: Select2) => voideventtriggered when user close the options
(focus)(event: Select2) => voideventtriggered when user enters the component
(blur)(event: Select2) => voideventtriggered when user leaves the component
(search)(event:Select2SearchEvent) => voideventtriggered when search text changed

select2 data structure

type Select2Data = (Select2Group | Select2Option)[];

export interface Select2Group {
    /** label of group */
    label: string;
    /** options list */
    options: Select2Option[];
    /** add classes  */
    classes?: string;
    /** template id  */
    templateId?: string;
    /** template data  */
    data?: any;
}

export interface Select2Option {
    /** value  */
    value: Select2Value;
    /** label of option */
    label: string;
    /** no selectable is disabled */
    disabled?: boolean;
    /** for identification */
    id?: string;
    /** add classes  */
    classes?: string;
    /** template id  */
    templateId?: string;
    /** template data  */
    data?: any;
}

type Select2Value = string | number | boolean;

type Select2UpdateValue = Select2Value | Select2Value[];

export interface Select2UpdateEvent<U extends Select2UpdateValue = Select2Value> {
    component: Select2;
    value: U;
    options: Select2Option[];
}

export interface Select2SearchEvent<U extends Select2UpdateValue = Select2Value> {
    component: Select2;
    value: U;
    search: string;
}

Templating

Unique template

<select2 [data]="data"
         [templates]="template">
    <ng-template #template let-data="data"><strong>{{data?.color}}</strong>: {{data?.name}}</ng-template>
</select2>
const data: Select2Data = [
    {
        value: 'heliotrope', 
        label: 'Heliotrope', 
        data: { color: 'white', name: 'Heliotrope' }
    },
    {
        value: 'hibiscus', 
        label: 'Hibiscus', 
        data: { color: 'red', name: 'Hibiscus' }
    }
]

Template group & option

<select2 [data]="data"
         [templates]="{option : option, group: group}">
    <ng-template #option let-data="data">{{data?.name}}</ng-template>
    <ng-template #group let-label="label">Group: {{label}}</ng-template>
</select2>

No difference in data structure. The template is defined by its type, option or group, automatically.

Template by templateId

<select2 [data]="data"
         [templates]="{template1 : template1, template2: template2">
    <ng-template #template1 let-data="data">{{data?.name}}</ng-template>
    <ng-template #template2 let-label="label" let-data="data">{{label}} : {{data?.color}}</ng-template>
</select2>
const data: Select2Data = [
    {
        value: 'heliotrope', 
        label: 'Heliotrope', 
        data: { color: 'white', name: 'Heliotrope' }, 
        templateId: 'template1'
    },
    {
        value: 'hibiscus', 
        label: 'Hibiscus', 
        data: { color: 'red', name: 'Hibiscus'},
        templateId: 'template2' 
    }
]

CSS variables (doesn't work on IE11)

It's possible to change different colors (and more) with CSS variables without having to modify them with ::ng-deep or other CSS rules :

:root {
    /* label */
    --select2-label-text-color: #000;
    --select2-required-color: red;

    /* selection */
    --select2-selection-border-radius: 4px;
    --select2-selection-background: #fff;
    --select2-selection-disabled-background: #eee;
    --select2-selection-border-color: #aaa;
    --select2-selection-focus-border-color: #000;
    --select2-selection-text-color: #444;

    /* selection: choice item (multiple) */
    --select2-selection-choice-background: #e4e4e4;
    --select2-selection-choice-text-color: #000;
    --select2-selection-choice-border-color: #aaa;
    --select2-selection-choice-close-color: #999;
    --select2-selection-choice-hover-close-color: #333;

     /* placeholder */
    --select2-placeholder-color: #999;

    /* reset */
    --select2-reset-color: #999;

    /* arrow */
    --select2-arrow-color: #888;

    /* dropdown panel */
    --select2-dropdown-background: #fff;
    --select2-dropdown-border-color: #aaa;

    /* search field */
    --select2-search-border-color: #aaa;
    --select2-search-background: #fff;
    --select2-search-border-radius: 0px;

    /* dropdown option */
    --select2-option-text-color: #000;
    --select2-option-disabled-text-color: #999;
    --select2-option-disabled-background: transparent;
    --select2-option-selected-text-color: #000;
    --select2-option-selected-background: #ddd;
    --select2-option-highlighted-text-color: #fff;
    --select2-option-highlighted-background: #5897fb;
    --select2-option-group-text-color: gray;
    --select2-option-group-background: transparent;

    /* hint */
    --select2-hint-text-color: #888;

    /* for Material ------------------------------------------*/
    --select2-material-underline: #ddd;
    --select2-material-underline-active: #5a419e;
    --select2-material-underline-disabled: linear-gradient(to right, rgba(0, 0, 0, 0.26) 0, rgba(0, 0, 0, 0.26) 33%, transparent 0);
    --select2-material-underline-invalid: red;
    --select2-material-placeholder-color: rgba(0, 0, 0, 0.38);
    --select2-material-selection-background: #ddd;
    --select2-material-option-selected-background: rgba(0, 0, 0, 0.04);
    --select2-material-option-highlighted-text-color: #000;
    --select2-material-option-selected-text-color: #ff5722;
}

For IE11, see css-vars-ponyfill.

Publishing the library

npm run build:lib
cd dist/ng-select2-component
npm publish

Update Demo

npm run build:demo

License

Like Angular, this module is released under the permissive MIT license. Your contributions are always welcome.

7.3.4

3 years ago

1.1.0

3 years ago

7.3.3

3 years ago

7.3.2

3 years ago

7.3.1

3 years ago

7.3.0

3 years ago

1.0.0

3 years ago

7.2.0

3 years ago

0.0.0

3 years ago