Skip to content

Commit ba83853

Browse files
author
Marty Wallace
committed
Allow single layout vs array, bug fixes.
1 parent 20c71fe commit ba83853

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

dist/vue-keyboard.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/vue-keyboard.js

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
props: {
1818
layouts: {
19-
type: Array,
19+
type: [String, Array],
2020
required: true
2121
},
2222
maxlength: {
@@ -34,16 +34,28 @@
3434
},
3535

3636
computed: {
37+
/**
38+
* Whether or not the keyboard input has hit its maximum length.
39+
* @returns {Boolean}
40+
*/
3741
full() {
3842
return this.value.length >= this.maxlength;
3943
},
4044

45+
/**
46+
* Whether or not the keyboard input is empty.
47+
* @return {Boolean}
48+
*/
4149
empty() {
4250
return this.value.length === 0;
4351
},
4452

53+
/**
54+
* Returns an array of buttons to render in the component.
55+
* @returns {Array[]}
56+
*/
4557
buttons() {
46-
let lines = this.layouts[this.layout].split('|');
58+
let lines = (Array.isArray(this.layouts) ? this.layouts : [this.layouts])[this.layout].split('|');
4759

4860
return lines.map(chars => {
4961
let stream = chars.split('');
@@ -91,6 +103,10 @@
91103
},
92104

93105
methods: {
106+
/**
107+
* Mutates the keyboard value to a new value.
108+
* @param {String} value The new value.
109+
*/
94110
mutate(value) {
95111
this.value = value;
96112

@@ -101,26 +117,47 @@
101117
this.$emit('input', this.value);
102118
},
103119

120+
/**
121+
* Appends a new value to the end of the current keyboard value.
122+
* @param {String} char The character(s) to append.
123+
*/
104124
append(char) {
105125
this.mutate(this.value + char);
106126
},
107127

128+
/**
129+
* Remove the last character from the current keyboard value.
130+
*/
108131
backspace() {
109132
this.mutate(this.value.slice(0, this.value.length - 1));
110133
},
111134

135+
/**
136+
* Add one whitespace character to the current keyboard value.
137+
*/
112138
space() {
113139
this.append(' ');
114140
},
115141

142+
/**
143+
* Go to a new layout.
144+
* @param {Number} The layout index.
145+
*/
116146
goto(layout) {
117-
if (layout >= 0 && layout < this.layouts.length) {
118-
this.layout = layout;
147+
if (Array.isArray(this.layouts)) {
148+
if (layout >= 0 && layout < this.layouts.length) {
149+
this.layout = layout;
150+
} else {
151+
throw new Error('The requested layout does not exist.');
152+
}
119153
} else {
120-
throw new Error('The requested layout does not exist.');
154+
throw new Error('A single non-array layout was provided.');
121155
}
122156
},
123157

158+
/**
159+
* Clear the entire keyboard value.
160+
*/
124161
clear() {
125162
this.mutate('');
126163
}

0 commit comments

Comments
 (0)