Skip to content

Commit

Permalink
Extract input logic to mixin (#2)
Browse files Browse the repository at this point in the history
* Modify reset value

* Add date input

* Move rf-input logic to mixin
  • Loading branch information
sonechiman authored and Daniel Perez committed May 17, 2016
1 parent a2c88d2 commit 498a44c
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 58 deletions.
96 changes: 67 additions & 29 deletions dist/riot-form.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion dist/riot-form.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/riot-form.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/riot-form.min.js.map

Large diffs are not rendered by default.

23 changes: 1 addition & 22 deletions lib/components/rf-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,5 @@ import riot from 'riot'
import html from './rf-input.html'

riot.tag('rf-input', html, function (opts) {
this.mixin('rf-input-helpers')
let tag = null
let currentValue = null

const makeData = () => {
return { model: opts.model, formName: opts.formName }
}

this.on('mount', () => {
const input = this.root.querySelector('[rf-input-elem]')
if (!input) {
throw new Error('element with attribute rf-input-elem not found in rf-input html')
}
tag = riot.mount(input, opts.model.tag, makeData())[0]
})

this.on('update', () => {
if (tag && opts.model.value !== currentValue) {
currentValue = opts.model.value
tag.update(makeData())
}
})
this.mixin('rf-input-helpers', 'rf-base-input')
})
7 changes: 7 additions & 0 deletions lib/inputs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ class TelInput extends BaseInput {
TelInput.defaultTag = 'rf-text-input'
TelInput.type = 'tel'


class DateInput extends BaseInput {
}
DateInput.defaultTag = 'rf-text-input'
DateInput.type = 'date'

class TextareaInput extends BaseInput {
}
TextareaInput.defaultTag = 'rf-textarea-input'
Expand All @@ -43,5 +49,6 @@ export default {
NumberInput : NumberInput,
URLInput : URLInput,
TelInput : TelInput,
DateInput : DateInput,
TextareaInput : TextareaInput
}
1 change: 1 addition & 0 deletions lib/mixins/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import './rf-input-helpers'
import './rf-base-input'
28 changes: 28 additions & 0 deletions lib/mixins/rf-base-input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import riot from 'riot'
import config from '../config'

riot.mixin('rf-base-input', {
init: function () {
let tag = null
let currentValue = null

const makeData = () => {
return { model: this.opts.model, formName: this.opts.formName }
}

this.on('mount', () => {
const input = this.root.querySelector('[rf-input-elem]')
if (!input) {
throw new Error('element with attribute rf-input-elem not found in rf-input html')
}
tag = riot.mount(input, this.opts.model.tag, makeData())[0]
})

this.on('update', () => {
if (tag && this.opts.model.value !== currentValue) {
currentValue = this.opts.model.value
tag.update(makeData())
}
})
}
})
6 changes: 3 additions & 3 deletions lib/mixins/rf-input-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ riot.mixin('rf-input-helpers', {
this.assignValue(e.target.value)
},
initializeValue: function () {
this.on('mount', () => {
this.on('mount update', () => {
const input = this[this.getName()]
if (input && this.opts.model.value !== undefined) {
input.value = this.opts.model.value
if (input) {
input.value = this.opts.model.value || ''
}
})
}
Expand Down
1 change: 1 addition & 0 deletions tests/unit/inputs_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('inputs', () => {
'tel',
'number',
'url',
'date',
'textarea'
]

Expand Down

0 comments on commit 498a44c

Please sign in to comment.