Skip to content

Commit 5fa26b6

Browse files
committed
added custom attributes
1 parent 0545b26 commit 5fa26b6

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

src/directives/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default function(Vue){
2+
/**
3+
* Allows the user to pass extra attributes that should be added to the element. Such as placeholder etc etc
4+
* @param {Object} value
5+
*/
6+
Vue.directive('formly-atts', function(value){
7+
if ( typeof value == 'undefined' ) return;
8+
Object.keys(value).forEach((key) => {
9+
this.el.setAttribute(key, value[key]);
10+
});
11+
});
12+
}

src/fields/fieldInput.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<div class="form-group">
33
<label v-if="form[key].label" :for="form[key].id ? form[key].id : null">{{form[key].label}}</label>
4-
<input class="form-control" :id="form[key].id ? form[key].id : null" :type="form[key].inputType" v-model="form[key].value" :placeholder="form[key].placeholder" @blur="onBlur" @focus="onFocus" @click="onClick" @change="onChange" @keyup="onKeyup" @keydown="onKeydown">
4+
<input class="form-control" :id="form[key].id ? form[key].id : null" :type="form[key].inputType" v-model="form[key].value" :placeholder="form[key].placeholder" @blur="onBlur" @focus="onFocus" @click="onClick" @change="onChange" @keyup="onKeyup" @keydown="onKeydown" v-formly-atts="form[key].atts">
55
</div>
66
</template>
77

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import directives from './directives/index.js';
12
let Fields = require.context("./fields/", false, /^\.\/field([\w-_]+)\.vue$/);
23
let FormlyBootstrap = {
34
install(Vue, options){
5+
directives(Vue);
6+
47
Fields.keys().forEach((key) => {
58
//remove all the .vue crap
69
let component = key

test/unit/specs/index.spec.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function trigger (target, event, process) {
2929
target.dispatchEvent(e);
3030
}
3131

32-
describe('components', () => {
32+
describe('Bootstrap Field Inputs', () => {
3333

3434
beforeEach(() => {
3535
data = {
@@ -108,6 +108,21 @@ describe('components', () => {
108108

109109
});
110110

111+
describe('attributes', () => {
112+
it('should pass attributes', () => {
113+
data.form.test.type = 'input';
114+
data.form.test.inputType = 'text';
115+
data.form.test.atts = {
116+
'data-foo': 'bar',
117+
'data-bar': 'foo'
118+
};
119+
createForm();
120+
let input = vm.$el.querySelectorAll('input')[0];
121+
expect(input.dataset.foo).to.equal('bar');
122+
expect(input.dataset.bar).to.equal('foo');
123+
});
124+
});
125+
111126
describe('input', () => {
112127

113128
it('basic functions', (done) => {

0 commit comments

Comments
 (0)