Skip to content

Commit

Permalink
Fix date input to accept any parsable date as value
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Perez committed Aug 6, 2016
1 parent ffc2a1b commit eeefcbb
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 10 deletions.
40 changes: 36 additions & 4 deletions dist/riot-form.js

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

2 changes: 1 addition & 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.

6 changes: 5 additions & 1 deletion lib/inputs/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default class BaseInput {
}

setValue(rawValue, options = {}) {
const value = this.process(rawValue)
const value = this.process(this.preProcessValue(rawValue))
if (value === this._value) {
return
}
Expand Down Expand Up @@ -86,6 +86,10 @@ export default class BaseInput {
return this.defaultFormatErrors(this.errors)
}

preProcessValue(value) {
return value
}

// TODO: pre pack some processors to avoid having to pass a callback
get process() {
return this.config.process || this.defaultProcess
Expand Down
13 changes: 13 additions & 0 deletions lib/inputs/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import BaseInput from './base'
import {padLeft} from '../util'

class TextInput extends BaseInput {
}
Expand Down Expand Up @@ -32,6 +33,18 @@ TelInput.type = 'tel'


class DateInput extends BaseInput {
preProcessValue(value) {
const timestamp = Date.parse(value)
if (!timestamp) {
return value
}
const date = new Date(timestamp)
return [
date.getFullYear(),
padLeft((date.getMonth() + 1).toString(), 2, '0'),
padLeft(date.getDate().toString(), 2, '0')
].join('-')
}
}
DateInput.defaultTag = 'rf-text-input'
DateInput.type = 'date'
Expand Down
7 changes: 7 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ export function capitalize(str) {
}
return str[0].toUpperCase() + str.substring(1)
}

export function padLeft(str, length, char = ' ') {
while (str.length < length) {
str = char + str
}
return str
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "riot-form",
"version": "0.1.5",
"version": "0.1.6",
"description": "Form inputs for RiotJS",
"main": "dist/riot-form.js",
"scripts": {
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/inputs/date-input_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {expect} from 'chai'
import {inputs} from 'riot-form'

describe('DateInput', () => {
describe('preProcessValue', () => {
it('should return a formatted date', () => {
const input = new inputs.DateInput({name: 'whatever'})
expect(input.preProcessValue('2016-08-06T06:15:38.864Z')).to.eq('2016-08-06')
expect(input.preProcessValue('2016-08-31T06:15:38.864Z')).to.eq('2016-08-31')
})
})
})
19 changes: 19 additions & 0 deletions tests/unit/util_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {expect} from 'chai'
import * as util from 'riot-form/util'

describe('util', () => {
describe('capitalize', () => {
it('should capitalize the string', () => {
expect(util.capitalize('foobar')).to.eq('Foobar')
expect(util.capitalize('Foobar')).to.eq('Foobar')
expect(util.capitalize('hellOO')).to.eq('HellOO')
})
})

describe('padLeft', () => {
it('should pad string', () => {
expect(util.padLeft('1', 2, '0')).to.eq('01')
expect(util.padLeft('ab', 4, 'x')).to.eq('xxab')
})
})
})

0 comments on commit eeefcbb

Please sign in to comment.