|
16 | 16 |
|
17 | 17 | props: {
|
18 | 18 | layouts: {
|
19 |
| - type: Array, |
| 19 | + type: [String, Array], |
20 | 20 | required: true
|
21 | 21 | },
|
22 | 22 | maxlength: {
|
|
34 | 34 | },
|
35 | 35 |
|
36 | 36 | computed: {
|
| 37 | + /** |
| 38 | + * Whether or not the keyboard input has hit its maximum length. |
| 39 | + * @returns {Boolean} |
| 40 | + */ |
37 | 41 | full() {
|
38 | 42 | return this.value.length >= this.maxlength;
|
39 | 43 | },
|
40 | 44 |
|
| 45 | + /** |
| 46 | + * Whether or not the keyboard input is empty. |
| 47 | + * @return {Boolean} |
| 48 | + */ |
41 | 49 | empty() {
|
42 | 50 | return this.value.length === 0;
|
43 | 51 | },
|
44 | 52 |
|
| 53 | + /** |
| 54 | + * Returns an array of buttons to render in the component. |
| 55 | + * @returns {Array[]} |
| 56 | + */ |
45 | 57 | buttons() {
|
46 |
| - let lines = this.layouts[this.layout].split('|'); |
| 58 | + let lines = (Array.isArray(this.layouts) ? this.layouts : [this.layouts])[this.layout].split('|'); |
47 | 59 |
|
48 | 60 | return lines.map(chars => {
|
49 | 61 | let stream = chars.split('');
|
|
91 | 103 | },
|
92 | 104 |
|
93 | 105 | methods: {
|
| 106 | + /** |
| 107 | + * Mutates the keyboard value to a new value. |
| 108 | + * @param {String} value The new value. |
| 109 | + */ |
94 | 110 | mutate(value) {
|
95 | 111 | this.value = value;
|
96 | 112 |
|
|
101 | 117 | this.$emit('input', this.value);
|
102 | 118 | },
|
103 | 119 |
|
| 120 | + /** |
| 121 | + * Appends a new value to the end of the current keyboard value. |
| 122 | + * @param {String} char The character(s) to append. |
| 123 | + */ |
104 | 124 | append(char) {
|
105 | 125 | this.mutate(this.value + char);
|
106 | 126 | },
|
107 | 127 |
|
| 128 | + /** |
| 129 | + * Remove the last character from the current keyboard value. |
| 130 | + */ |
108 | 131 | backspace() {
|
109 | 132 | this.mutate(this.value.slice(0, this.value.length - 1));
|
110 | 133 | },
|
111 | 134 |
|
| 135 | + /** |
| 136 | + * Add one whitespace character to the current keyboard value. |
| 137 | + */ |
112 | 138 | space() {
|
113 | 139 | this.append(' ');
|
114 | 140 | },
|
115 | 141 |
|
| 142 | + /** |
| 143 | + * Go to a new layout. |
| 144 | + * @param {Number} The layout index. |
| 145 | + */ |
116 | 146 | 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 | + } |
119 | 153 | } else {
|
120 |
| - throw new Error('The requested layout does not exist.'); |
| 154 | + throw new Error('A single non-array layout was provided.'); |
121 | 155 | }
|
122 | 156 | },
|
123 | 157 |
|
| 158 | + /** |
| 159 | + * Clear the entire keyboard value. |
| 160 | + */ |
124 | 161 | clear() {
|
125 | 162 | this.mutate('');
|
126 | 163 | }
|
|
0 commit comments