GithubHelp home page GithubHelp logo

benjamincharity / angular-keypad Goto Github PK

View Code? Open in Web Editor NEW
18.0 3.0 10.0 183 KB

:iphone::1234: A numerical keypad built for mobile devices.

License: MIT License

JavaScript 76.52% CSS 16.75% HTML 6.73%
angularjs keypad mobile touch

angular-keypad's Introduction

angular-keypad

angular-keypad

MIT License Coverage Status NPM version

๐Ÿ“ฑ๐Ÿ”ข An Angular directive that creates a numeric keypad.

๐Ÿ“บ All Demos

Comments and Pull Requests welcome!

Contents


Installation

NPM

npm install angular-keypad --save

Bower

bower install angular-keypad --save

Manually

Add the script and styles to your HTML:

<link rel="stylesheet" href="../path/to/your/bower_components/angular-keypad/dist/angular-keypad.css">
<script src="../path/to/your/bower_components/angular-keypad/dist/angular-keypad.js"></script>

Dependencies

  • Angular.js (>=1.4.0)

Usage

Include bc.AngularKeypad as a dependency in your project:

angular.module('YourModule', ['bc.AngularKeypad']);

Use the directive anywhere in your HTML. The keypad will expand to fill the width of it's container while maintaining the aspect ratio of the keypad buttons.

<!-- Show the number model in your UI: -->
<span>{{ vm.numbers }}</span>

<!-- Define the keypad: -->
<bc-keypad bc-number-model="vm.numbers"></bc-keypad>

๐Ÿ“บ Simple demo

bc-number-model

Required: String

The directive will store the current string of numbers here. This is the only required attribute.

<bc-keypad bc-number-model="vm.numbers"></bc-keypad>

bc-max-length

Optional: Integer

The directive will use this number to set a hard limit on how many characters are allowed in the number model (vm.numbers in the example below).

๐Ÿ“บ Demo for max-length

<!-- Restrict 'vm.numbers' to the length of 4 -->
<bc-keypad
  bc-number-model="vm.numbers"
  bc-max-length="4"
></bc-keypad>

bc-left-button

Optional: String

You can define a custom Plug'n'Play button type by passing in the name of any valid (PnP) button.

๐Ÿ“บ Demo for Plug'n'Play button types with custom methods

<!-- This would generate a backspace button in the bottom left of the keypad -->
<bc-keypad
  bc-number-model="vm.numbers"
  bc-left-button="backspace"
></bc-keypad>

bc-right-button

Optional: String

You can define a custom Plug'n'Play button type by passing in the name of any valid (PnP) button.

๐Ÿ“บ Demo for Plug'n'Play button types with custom methods

<!-- This would generate a submit button in the bottom right of the keypad -->
<bc-keypad
  bc-number-model="vm.numbers"
  bc-right-button="submit"
></bc-keypad>

Custom Methods

bc-left-button-method

Optional: method

You can pass in a custom method that will be called each time the button is interacted with. This allows you to track interaction or handle custom validation in your controller.

Available Parameters:
Param Type Details
$event Object The original event object from the ng-click
numbers String The current value of bc-number-model

๐Ÿ“บ Demo for Plug'n'Play button types with custom methods

<!-- This would create a backspace button in the bottom left corner of the keypad -->
<bc-keypad
  bc-number-model="vm.numbers"
  bc-left-button="backspace"
  bc-left-button-method="vm.doSomething($event, numbers)"
></bc-keypad>
export class YourController {

    constructor() {
        this.numbers = '';
    }

    // I will be called each time the left (backspace) button is interacted with
    doSomething($event, numbers) {
        console.log('The backspace button on the left was clicked!');
        console.log('Original click event object: ', $event);
        console.log('Current number model value: ', numbers);
    }

}

bc-right-button-method

Optional: method

You can pass in a custom method that will be called each time the button is interacted with. This allows you to track interaction or handle custom validation in your controller.

Available Parameters:
Param Type Details
$event Object The original event object from the ng-click
numbers String The current value of bc-number-model

๐Ÿ“บ Demo for Plug'n'Play button types with custom methods

<!-- This would create a submit button in the bottom right corner of the keypad -->
<bc-keypad
  bc-number-model="vm.numbers"
  bc-right-button="submit"
  bc-right-button-method="vm.doSomething($event, numbers)"
></bc-keypad>
export class YourController {

    constructor() {
        this.numbers = '';
    }

    // I will be called each time the right (submit) button is interacted with
    doSomething($event, numbers) {
        console.log('The submit button on the right was clicked!');
        console.log('Original click event object: ', $event);
        console.log('Current number model value: ', numbers);
    }

}

bc-empty-backspace-method

Optional: method

You can pass in a custom method that will be called when the backspace button is interacted with and bc-number-model is already empty. This can be useful for allowing users to step backwards through a multi-part form.

๐Ÿ“บ Demo for bc-empty-backspace-method

<bc-keypad
  bc-number-model="vm.numbers"
  bc-right-button="backspace"
  bc-empty-backspace-method="vm.backspaceWhenEmpty()"
></bc-keypad>
export class YourController {

    constructor() {
        this.numbers = '';
    }

    // I will be called when the backspace button is clicked and the numbers
    // model is empty
    backspaceWhenEmpty() {
        console.log('Backspace clicked, but "vm.numbers" is empty!');
    }

}

Plug'n'Play Buttons

Plug'n'Play buttons

This directive now supports Plug'n'Play (PnP) button types to the left and right of the final digit. These button types can be used on either side (or both if you really wanted to).

๐Ÿ“บ Demo for Plug'n'Play button types with custom methods

Available (PnP) Button Types

Even when using a (PnP) button, any defined custom methods will still be called.

Any (PnP) button template can be overwritten using methods exposed via the provider.


<!-- Example directive setup for the above image -->
<bc-keypad
  bc-number-model="vm.numbers"
  bc-left-button="backspace"
  bc-right-button="submit"
  bc-right-button-method="vm.submitMyData($event, numbers)"
></bc-keypad>

Backspace

<!-- This would create a backspace button in the bottom left corner of the keypad -->
<bc-keypad
  bc-number-model="vm.numbers"
  bc-right-button="backspace"
></bc-keypad>

This will create a backspace button with styles and functionality already wired together.

Functionality

Each time a backspace button instance is interacted with, the last number will be removed from bc-number-model.

If a custom method was passed to bc-empty-backspace-method it will be called when the backspace button is interacted with and bc-number-model is already empty. This can be useful for allowing users to step backwards through a multi-part form.

Any defined custom methods will still be called.

Style

By default the button is using a raw SVG version of ion-backspace-outline from ionicons since this allows you to customize the SVG styles with your project's CSS.

๐Ÿ“บ Demo for custom button CSS

Ionicons backspace icon

A special class is added to the backspace button which can be used to target specific styles:

.bc-keypad__key-button--backspace {
  fill: #DE1E7E;
}

Submit

<bc-keypad
  bc-number-model="vm.numbers"
  bc-right-button="submit"
></bc-keypad>

This is a purely aesthetic (PnP) button type. No functionality is attached, but any defined custom methods will still be called.

Style

By default the button is using a raw SVG version of ion-log-in from ionicons since this allows you to customize the SVG styles with your project's CSS.

๐Ÿ“บ Demo for custom button CSS

Ionicons submit icon

A special class is added to the submit button which can be used to target specific styles:

.bc-keypad__key-button--submit {
  fill: #101CA7;
}

bcKeypadConfigProvider

This module exposes bcKeypadConfigProvider which can be used to set custom defaults for the directive. Setting options here will overwrite the directive's default options for all instances within your module.

๐Ÿ“บ Demo for bcKeypadConfigProvider

// ES6 Config Example
export function config(bcKeypadConfigProvider) {
    'ngInject';

    // Set a default of '7' for the max length of all keypads within your module
    bcKeypadConfigProvider.setMaxLength(7);

}

// ES5 Config Example
angular.module('myModule')
    .config((bcKeypadConfigProvider) => {
        'ngInject';

        bcKeypadConfigProvider.setMaxLength(7);

    })
;

setBackspaceTemplate

This allows you to specify a custom template for the backspace key.

By default it is using a raw SVG version of ion-backspace-outline from ionicons since this allows you to overwrite the SVG styles with CSS.

๐Ÿ“บ Demo for bcKeypadConfigProvider

Ionicons backspace icon

bcKeypadConfigProvider.setBackspaceTemplate('<span>โ†ฉ</span>');

setSubmitTemplate

This allows you to specify a custom template for the submit key.

By default it is using a raw SVG version of ion-log-in from ionicons since this allows you to overwrite the SVG styles with CSS.

๐Ÿ“บ Demo for bcKeypadConfigProvider

Ionicons log in icon

bcKeypadConfigProvider.setSubmitTemplate('Go');

setMaxLength

The directive will use this number to impose a hard limit on how many characters the model can hold. This is useful for specific data items such as a phone number:

๐Ÿ“บ Demo for bcKeypadConfigProvider

max-length demo

bcKeypadConfigProvider.setMaxLength(10);

Styles

The included styles are 99% layout with just enough style to work out of the box. The true styles should be written at your project level using the associated classes.

Your project CSS should always be included after any library CSS files. This makes it easy for you to override or add to any styles defined by this module. Below are the classes available for styling.

Class Element Details
.bc-keypad <div> Primary container (<div>) around the keypad.
.bc-keypad__key <div> Individual wrapper for each key.
.bc-keypad__key--left <div> Target the left customizable key.
.bc-keypad__key--center <div> Target the last number key (between the two customizable keys).
.bc-keypad__key--right <div> Target the right customizable key.
.bc-keypad__key-button <button> The button inside each key.
.bc-keypad__key-button--backspace <button> Target the backspace key (if used)
.bc-keypad__key-button--submit <button> Target the submit key (if used)

๐Ÿ“บ Demo for custom theming

angular-ripple

The bc-keypad directive was written primarily for mobile so it supports the Google Material 'ripple' style feedback via a module called angular-ripple. As not everyone may want that style of interaction, this project does not automatically install the angular-ripple library, but is however built to support it out of the box.

๐Ÿ“บ Demo for angular-ripple integration

Just install the angular-ripple library:

$ npm install nelsoncash/angular-ripple --save
# or
$ bower install angular-ripple --save

Include it as a dependency in your primary project:

angular.module('YourModule', ['bc.AngularKeypad', 'angularRipple']);

And you should see it working!

Development

  • npm run build - Build JS/CSS/HTML/SVG
  • npm run build:js - Build JS
  • npm run build:css - Build CSS
  • npm run watch:css - Watch CSS and rebuild on change
  • npm run watch:js - Watch JS/HTML and rebuild on change
  • npm run watch - Watch JS/CSS/HTML and rebuild on change

angular-keypad's People

Contributors

agentofgaming avatar alcalyn avatar benjamincharity avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

angular-keypad's Issues

C-button in bc-keypad

In the bc-keypad, I want to add a 'C'-button (to empty the input field number).
Is there a way to do that?
I hope something like: bc-left-button (if necessary combined with bc-left-button-method) and to change the icon with 'C'.

How to include in Angular 4

Hi,

thank you for making this module :)

Do you have an idea of how to include in angular 4? (particularly ionic - but that's not the main concern)

I've tried import { AngularKeypad } from 'angular-keypad'; but that does not seem to work. I think the biggest issue is the dot in the module name.

bc.AngularKeypad (the dot)

Any help would be awesome!!! thanks

Extra buttons outside keypad

Is it possible to have some buttons outside the keypad which have effect on the number?
I want to have two buttons (+ / -) to increase / decrease the number by 1.

Dependencies seem to be misconfigured

webpack-dev-server is only for development use, so it should be a dev-dependency rather than a full blown dependency. This is causing npm install errors in CI because we don't want to package webpack-dev-server with our product.

On that note, the readme mentions Angular 1.4.0+ as a dependency, but Angular only included as a dev-dependency at version 1.5.8+.

Cleaning up the dependencies would be much appreciated!

Doesn't work with masking modules

Hi, may I ask how you achieved the masking with placeholders in this demo in the README?

I've tried using ui-mask but the input doesn't accept any characters entered via angular-keypad, depsite trying both letter and number mask definitions.

I've also tried ngMask but that doesn't support placeholders.

Great module and docs despite this, many thanks.

Make keys to each side of the 0 customizable

Currently the delete key is hard-coded in. It might be nice to allow each button to be customized.

  • Support custom image
  • Support callback function
  • Add note to docs about why the provider was changed
  • Change provider to use method to set template

on screen keyboard keeps popping up

Hi,

I'm probably doing something completely wrong but when I use your app the on screen keyboard keeps popping up. I'm using angular 1.6.2.

Please guide me in the right direction.

Init value of keypad input value numbers

Is it possible to set the value of the input field numbers before launching the keypad? I have an orderlist of several products. I want to change the number of a product by using the keypad.

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.