Giter VIP home page Giter VIP logo

ng-custom-select's Introduction

Angular Custom Dropdown

Create customizable Angular 6 dropdown that can works as either select or datalist with no constraints on styles. Dropdown can be used as form element or independent element. formConrol or ngModel can be used on this for bindings. Support 2-way binding.

Installation

npm install --save ng-custom-select

For Angular[2,4,5], install older version

npm install --save [email protected]

Demo

Check working Demo. You can test and generate code snippet from demo.

Usage

Import NgSelectModule into your module

your-module.module.ts

import { NgSelectModule } from 'ng-custom-select';

@NgModule({
  imports: [
    ...
    NgSelectModule,
  ],

Now Start using select box in your components.

your-component.component.html

<ng-select [options]="options" [displayKey]="displayKey" [disable]="isDisable" [styleGuide]="styleGuide" [isDatalist]="isDataList" [searchKeys]="searchKeys" [formControl]="selectBox"  [(ngModel)]="selectBox" (onChange)="onChange($event)"></ng-select>

Properties

* options : Array<Object | String>; //Required | Pass any list of objects(similar struct) or strings, each entry is an option of select box.
* displayKey : String; //Optional | Any key name present in object of options Array. Value of this key will be displayed to user as options. By default value of first key will be shown. in case of Array<String> , each entry is displayed as option.
* disable : Boolean; //Optional | if True, dropdown will be disabled. Default is false.
* styleGuide : Object = {
    caretClass: <caret-icon-className>,
    selectBoxClass: <select-wrapper-className>,
    selectMenuClass: <options-wrapper-className>,
    optionsClass: <individual-option-className>
  } // Optional | Styling breaks if css not provided. Write your SCSS/CSS using above class names and proper hierarchy (selectBoxClass > selectMenuClass > optionsClass) and refer `Styling` block.
* isDatalist : Boolean ; //Optional | if True, behave like a datalist. User can search upon options of dropdown. Default is false.
* searchKeys : Array<String>; //Optional | Provide list of key names of objects of `options` array, user can search upon values of these keys. By default first key name is used as search key. 
* formControl : AbstractControl; //Optional | FormControlName to bind dropdown with.
* ngModel : NgControl; //Optional | Used for binding value.

Note: By default First option of options list is selected. If you want some other option to be selected, use it with ngModel.

Output

It emits onChange event to the parent component when selected value is changed by user, So there should be an event handler in parent component class. $event emits clicked option of options Array.

Styling

For styling of dropdown, SCSS or CSS can be written as global styles for uniform styling across the project, or you write styles inside your parent component styles using /deep/ selector. Don't forget to add your css classnames in styleGuide property of ng-select.

Note: Please maintain the hierarchy(selectBoxClass > selectMenuClass > optionsClass) of classes while writing style and don't forget to add /deep/ before <selectBoxClass> in case you are writing styles inside your parent component. You can use your own classnames for select box, just pass the classnames in styleGuide property of ng-select.

sample.style.scss

.dropdown-wrapper {
    padding: 12px 16px;
    border: 1px solid rgb(206, 205, 201);
    cursor: pointer;
    outline: none;
    color: rgb(110, 108, 105);
    &:focus {
        border: 1px solid #80bdff;
    }
    .dropdown {
        /* Styles */
        background: #fff;
        border-radius: 0 0 5px 5px;
        border: 1px solid rgb(206, 205, 201);
        border-top: none;
        box-sizing: border-box;
        max-height: 400px;
        border-bottom: 1px solid rgb(206, 205, 201);
        /* Hover state */
        .option:hover  {
            color: #d15947;
        }
        .option {
            padding: 0.8em;
            border-bottom: 1px solid rgb(206, 205, 201);
            text-decoration: none;
            color: rgb(110, 108, 105);
        }
    }
}

.caret {
    &::after {
        content: "";
        text-align: center;
    }
}
styleGuide: {
    caretClass: 'caret',
    selectBoxClass: 'dropdown-wrapper',
    selectMenuClass: 'dropdown',
    optionsClass: 'option' 
}

Sample Inputs

options = [
    {"key":"google","value":"Angular"},
    {"key":"facebook","value":"React"},
    {"key":"evan","value":"Vue"},
    {"key":"tilde","value":"Ember"},
    {"key":"twitter","value":"Bootstrap"}
];
displayKey = "value";
isDisable = false;
styleGuide = {
    caretClass: 'caret',
    selectBoxClass: 'dropdown-wrapper',
    selectMenuClass: 'dropdown',
    optionsClass: 'option' 
};
isDataList = false;
searchKeys = ['key','value'];

License

MIT © Uttam Pratap Choudhary

ng-custom-select's People

Contributors

uttamchoudhary avatar amolborse avatar

Watchers

James Cloos 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.