JSFiddle - React, Tailwind, and code Playground

by sendil J

HTML

<script src="http://code.angularjs.org/1.2.2/angular.js"></script>
<script src="http://fonts.googleapis.com/css?family=Roboto"></script>
<body ng-app="demo" ng-controller="DemoController">
    <h3>rn-stepper demo (3/5)</h3>
    <form name="form" >
        Model value : {{ rating }}<br>
        Form Has been modified : {{ form.$dirty }}
        <hr>
        <div  ng-model="rating" rn-stepper></div>
    </form>
</body>

SCSS

body {
 font-family: 'Roboto', sans-serif;
}


$stepper-height: 40px;
$stepper-value-width: 40px;
$stepper-button-width: 40px;
$stepper-border-width: 1px;
$stepper-button-bg: #4D4DFF;
$stepper-value-bg: #eee;
div[rn-stepper] {
    border:1px solid #bbb;
    display:inline-block;
    height:$stepper-height + ($stepper-border-width * 2);
    box-sizing:border-box;
    button {
        appearance:none;
        -webkit-appearance:none;
        margin:0;
        border:0;
        width: $stepper-button-width;
        height:$stepper-height;
        box-sizing:border-box;
        background: $stepper-button-bg;
        color: white;
        font-weight:bold;
        font-size:20px;
         outline: none;
        &:active {
            box-shadow: inset 0 2px 2px rgba(0, 0, 0, 0.25);
            background: darken($stepper-button-bg, 5%);
       }
    }
    div {
        vertical-align:top;
        width:$stepper-value-width;
        background:$stepper-value-bg;
        text-align:center;
        display:inline-block;    
        height:$stepper-height;
        line-height:$stepper-height;
        box-sizing:border-box;
        
    }
}

JavaScript

angular.module('demo', [])

.controller('DemoController', function($scope) {
    $scope.rating = 42;      
})

.directive('rnStepper', function() {
    return {
        restrict: 'AE',
        require: 'ngModel',
        scope: {},
        template: '<button ng-click="decrement()">-</button>' +
                  '<div>{{ value }}</div>' +
                  '<button ng-click="increment()">+</button>',
        link: function(scope, iElement, iAttrs, ngModelController) {
            // we can now use our ngModelController builtin methods
            // that do the heavy-lifting for us

            // when model change, update our view (just update the div content)
            ngModelController.$render = function() {
                iElement.find('div').text(ngModelController.$viewValue);
            };

            // update the model then the view
            function updateModel(offset) {
                // call $parsers pipeline then update $modelValue
                ngModelController.$setViewValue(ngModelController.$viewValue + offset);
                // update the local view
                ngModelController.$render();
            }

            // update the value when user clicks the buttons
            scope.decrement = function() {
                updateModel(-1);
            };
            scope.increment = function() {
                updateModel(+1);
            };
        }
    };
});