Skip to content

Commit

Permalink
Merge pull request #30 from dmmat/master
Browse files Browse the repository at this point in the history
add unfocus and textarea input type
  • Loading branch information
ajimix authored Dec 13, 2017
2 parents 97f9e26 + 1365fd3 commit 5943a73
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ input = new InputModule.Input
autofocus: false # Change to true to enable autofocus
goButton: false # Set true here in order to use "Go" instead of "Return" as button (only works on real devices)
submit: false # Change to true if you want to enable form submission

textarea: true # Use textarea instead of input

y: 240 # y position
x: 90 # x position
width: 500
Expand Down Expand Up @@ -89,25 +90,24 @@ input.on "keyup", ->
print @value
```

#### Focusing the input via code
#### Focusing and Unfocusing the input via code

Imagine that you want to focus the input once you click "myButton", here is an example:
Imagine that you want to `focus` the input once you click "myButton", here is an example:

```coffeescript
myButton.on Events.Click, ->
input.focus()
```

#### Blur the input via code
```

To blur the input use the offFocus method:
Imagine that you want to `unfocus` the input once you press enter, here is an example:

```coffeescript
myButton.on Events.Click, ->
input.offFocus()
input.on 'keyup', (e) ->
if e.keyCode == 13
input.unfocus()
```

#### Focus and Blur events
#### Focus and Blur(Unfocus) events

You can add your own custom actions using the `onFocus` and `onBlur` helpers.

Expand All @@ -117,6 +117,9 @@ input.onFocus ->

input.onBlur ->
print "Input lost focus"

input.onUnfocus ->
print "Input lost focus"
```

### [Advanced] Accessing original elements
Expand Down
7 changes: 5 additions & 2 deletions input.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class exports.Input extends Layer
options.fontWeight ?= "500"
options.submit ?= false
options.tabIndex ?= 0
options.textarea ?= false

super options

Expand All @@ -110,7 +111,7 @@ class exports.Input extends Layer
@_properties.padding = options.padding

@placeholderColor = options.placeholderColor if options.placeholderColor?
@input = document.createElement "input"
@input = document.createElement if options.textarea then 'textarea' else 'input'
@input.id = "input-#{_.now()}"

# Add styling to the input element
Expand Down Expand Up @@ -171,7 +172,7 @@ class exports.Input extends Layer
focus: () ->
@input.focus()

offFocus: () ->
unfocus: () ->
@input.blur()

onFocus: (cb) ->
Expand All @@ -181,3 +182,5 @@ class exports.Input extends Layer
onBlur: (cb) ->
@input.addEventListener "blur", ->
cb.apply(@)

onUnfocus: this.onBlur

0 comments on commit 5943a73

Please sign in to comment.