Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow custom feedback icons #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ icons to your form fields.

![Validation in action](./docs/iconstates.png)

## Bonus bonus round: custom feedback icons!

`feedback-template` will look for a template in cache to use instead of the default one.
*note:* this template needs to have a css class of `form-control-feedback` or
you will get strange behavior.

`valid-icon` will use this instead of the default `glyphicon-ok` when valid

`invalid-icon` will use this instead of the default `glyphicon-remove` when invalid


## Contributing

Expand Down
3 changes: 0 additions & 3 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,5 @@
},
"dependencies": {
"angular": ">1.3.0"
},
"resolutions": {
"angular": "1.3.6"
}
}
11 changes: 8 additions & 3 deletions src/has-feedback.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ angular.module("ng-form-group")
toArray(el[0].querySelectorAll(".form-control")).forEach (input) ->
input.setAttribute("has-feedback-watcher", "")

.directive "hasFeedbackWatcher", ->
.directive "hasFeedbackWatcher", ($templateCache) ->
require: "ngModel",
link: (scope, input, attrs, ctrl) ->

validIcon = attrs.validIcon || "glyphicon-ok"
invalidIcon = attrs.invalidIcon || "glyphicon-remove"
feedbackTemplate = $templateCache.get(attrs.feedbackTemplate) || "<span class=\"glyphicon {{feedbackIcon}} form-control-feedback\"></span>";

feedbackIcon = (isGood = false) ->
icon = if isGood then "glyphicon-ok" else "glyphicon-remove"
"<span class=\"glyphicon #{icon} form-control-feedback\"></span>"
icon = if isGood then validIcon else invalidIcon
feedbackTemplate.replace /{{feedbackIcon}}/, icon

unref = scope.$watch ->
return unless ctrl.$dirty
Expand Down
22 changes: 22 additions & 0 deletions tests/has-feedback.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,25 @@ describe 'The informative has-feedback directive', ->

expect(find(el, '.glyphicon-ok').length).toBe(0)
expect(find(el, '.glyphicon-remove').length).toBe(1)

it "should allow the use of custom feedback icons", ->
[el, ctrl] = factory('<input name="input" ng-model="foo" required class="form-control" valid-icon="ohai" invalid-icon="kbai">')
ctrl.input.$setViewValue('1000')
ctrl.input.$setViewValue('')

expect(find(el, '.glyphicon-ok').length).toBe(0)
expect(find(el, '.glyphicon-remove').length).toBe(0)
expect(find(el, '.kbai').length).toBe(1)

it "should allow the use of custom feedback templates", ->
inject ($templateCache) ->
$templateCache.put 'custom-feedback.html', '<div class="form-control-feedback custom-feedback"></div>'
[el, ctrl] = factory('<input name="input" ng-model="foo" required class="form-control" valid-icon="ohai" invalid-icon="kbai" feedback-template="custom-feedback.html">')

# [el, ctrl] = factory('<input name="input" ng-model="foo" required class="form-control" feedback-template="custom-feedback.html"/>')
ctrl.input.$setViewValue('1000')
ctrl.input.$setViewValue('')

expect(find(el, '.custom-feedback').length).toBe(1)
expect(find(el, '.glyphicon-ok').length).toBe(0)
expect(find(el, '.glyphicon-remove').length).toBe(0)