Skip to content

Commit

Permalink
Add support for hidden inputs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Perez committed May 24, 2016
1 parent 2b25986 commit 27a847f
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 10 deletions.
44 changes: 42 additions & 2 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.

7 changes: 5 additions & 2 deletions lib/components/rf-form.tag
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<rf-form>
<form name={ opts.model.name } class={ opts.className } onsubmit={ opts.onsubmit }>
<rf-text-input
each={ name, input in opts.model.hiddenInputs }
model={ input } />
<rf-input
each={ name, input in opts.model.inputs }
model={ input }>
each={ name, input in opts.model.visibleInputs }
model={ input } />
<yield />
</form>
</rf-form>
18 changes: 18 additions & 0 deletions lib/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ export default class Form {
return this._inputs
}

filterInputs(inputs, p) {
const filtered = {}
for (const name in inputs) {
if (p(inputs[name])) {
filtered[name] = inputs[name]
}
}
return filtered
}

get visibleInputs() {
return this.filterInputs(this.inputs, (input) => input.type !== 'hidden')
}

get hiddenInputs() {
return this.filterInputs(this.inputs, (input) => input.type === 'hidden')
}

get forms() {
return this._forms
}
Expand Down
8 changes: 7 additions & 1 deletion lib/inputs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class TextareaInput extends BaseInput {
TextareaInput.defaultTag = 'rf-textarea-input'
TextareaInput.type = 'textarea'

class HiddenInput extends BaseInput {
}
HiddenInput.defaultTag = 'rf-text-input'
HiddenInput.type = 'hidden'


export default {
TextInput : TextInput,
Expand All @@ -50,5 +55,6 @@ export default {
URLInput : URLInput,
TelInput : TelInput,
DateInput : DateInput,
TextareaInput : TextareaInput
TextareaInput : TextareaInput,
HiddenInput : HiddenInput
}
11 changes: 10 additions & 1 deletion tests/unit/components/rf-form_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ describe('rf-form', () => {
const form = new Form.Builder('hello')
.addInput({name: 'username', type: 'text'})
.addInput({name: 'other', type: 'text'})
.setModel({username: 'world'})
.addInput({name: 'hideme', type: 'hidden'})
.setModel({username: 'world', hideme: 'i-am-hidden'})
.build()

before(() => {
Expand Down Expand Up @@ -36,6 +37,14 @@ describe('rf-form', () => {
expect(input.value).to.eq('world')
})

it('should render hidden inputs without label', () => {
const input = document.querySelector('input[name="hello_hideme"]')
expect(input).not.to.be.null
expect(input.value).to.eq('i-am-hidden')
const label = document.querySelector('label[for="hello_hideme"]')
expect(label).to.be.null
})

it('should not set undefined initial values', () => {
const input = document.querySelector('input[name="hello_other"]')
expect(input).not.to.be.null
Expand Down

0 comments on commit 27a847f

Please sign in to comment.