Skip to content

Commit

Permalink
Add input default values.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Perez committed Mar 4, 2016
1 parent 5c6ee9c commit 628d40c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/inputs/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ export default class BaseInput {
riot.observable(this)
assert(config.name, 'An input must have a name')
this.config = config
if (config.value) {
this._setValue(config.value)
}
this._setValue(config.value || this.defaultValue, {silent: true})
if (config.formName) {
this.formName = config.formName
}
Expand Down Expand Up @@ -66,6 +64,10 @@ export default class BaseInput {
return this.config.type || this.constructor.type
}

get defaultValue() {
return undefined
}

// TODO: pre pack some validators to avoid having to pass a callback
validate() {
if (this.config.validate) {
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/base-input_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import {expect} from 'chai'
import {BaseInput} from 'riot-form'

class DummyInput extends BaseInput {
get defaultValue() {
return 'dummy'
}
}
DummyInput.defaultTag = 'dummy-tag'

Expand All @@ -15,6 +18,11 @@ describe('BaseInput', () => {
const input = new DummyInput({name: 'hello', value: 'foobar'})
expect(input.value).to.eq('foobar')
})

it('should set value to default value', () => {
const input = new DummyInput({name: 'hello'})
expect(input.value).to.eq('dummy')
})
})

describe('name', () => {
Expand Down

0 comments on commit 628d40c

Please sign in to comment.