Skip to content

Commit 9096c5b

Browse files
author
Graham Butler
committed
add helper methods and event handlers
1 parent f8ed95c commit 9096c5b

File tree

4 files changed

+119
-19
lines changed

4 files changed

+119
-19
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@author.io/element-control",
3-
"version": "1.0.15",
3+
"version": "1.1.0",
44
"description": "author-control custom element (web component).",
55
"main": "dist/author-control.min.js",
66
"scripts": {

showroom/src/index.html

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ <h2 class="hidden">Navigation</h2>
3636
</header>
3737

3838
<main>
39-
<div class="content">
39+
<form class="content">
4040
<author-control class="theme">
4141
<label>Theme</label>
4242
<select placeholder="Select a Theme">
@@ -46,35 +46,56 @@ <h2 class="hidden">Navigation</h2>
4646
</select>
4747
</author-control>
4848

49-
<author-control>
50-
<label>Datalist</label>
51-
<input type="text">
52-
<datalist>
49+
<author-control id="input">
50+
<label>Text Input</label>
51+
<input type="text" required>
52+
</author-control>
53+
54+
<author-control id="textarea">
55+
<label>Textarea</label>
56+
<textarea rows="8"></textarea>
57+
</author-control>
58+
59+
<author-control id="select">
60+
<label>Select Menu</label>
61+
62+
<select placeholder="Select an Option">
5363
<option value="1">Option 1</option>
5464
<option value="2">Option 2</option>
5565
<option value="3">Option 3</option>
56-
</datalist>
66+
67+
<optgroup label="Grouped Options">
68+
<option value="4">Option 4</option>
69+
<option value="5">Option 5</option>
70+
<option value="6">Option 6</option>
71+
</optgroup>
72+
</select>
5773
</author-control>
5874

59-
<author-control>
60-
<label>Text Input</label>
75+
<author-control id="datalist">
76+
<div class="label-wrapper">
77+
<label>Datalist</label>
78+
</div>
79+
6180
<input type="text">
81+
<datalist>
82+
<option value="1">Option 1</option>
83+
<option value="2">Option 2</option>
84+
<option value="3">Option 3</option>
85+
</datalist>
6286
</author-control>
6387

64-
<author-control>
65-
<label>Textarea</label>
66-
<textarea rows="8"></textarea>
67-
</author-control>
88+
<button type="submit" class="submit">Submit</button>
6889

69-
<author-control>
90+
<!-- <author-control>
7091
<div class="label-wrapper">
7192
<label>Text Input</label>
7293
</div>
7394
<div class="input-wrapper">
7495
<input type="text">
7596
</div>
76-
</author-control>
77-
</div>
97+
</author-control> -->
98+
</form>
7899
</main>
79100
</body>
80101

showroom/src/js/index.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,27 @@ const Demo = new NGNX.VIEW.Registry({
33
namespace: 'demo.',
44

55
references: {
6-
content: 'main .content'
6+
content: 'main .content',
7+
input: '#input',
8+
textarea: '#textarea',
9+
datalist: '#datalist',
10+
select: '#select',
11+
submitButton: 'button.submit'
712
},
813

914
templates: {
1015
select: './js/templates/select.html'
1116
},
1217

1318
init () {
14-
this.render('select', {}, this.ref.content.element)
19+
window.input = this.ref.input
20+
window.textarea = this.ref.textarea
21+
window.datalist = this.ref.datalist
22+
window.select = this.ref.select
23+
24+
input.on('invalid', evt => console.log(evt))
25+
26+
// setTimeout(() => select.element.input.focus(), 2000)
27+
// this.render('select', {}, this.ref.content.element)
1528
}
1629
})

src/element.js

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class AuthorFormControlElement extends AuthorBaseElement(HTMLElement) {
126126
return
127127

128128
default: if (node.children.length > 0) {
129-
return Array.from(node.children).forEach(child => this.PRIVATE.catalogChild(child))
129+
Array.from(node.children).forEach(child => this.PRIVATE.catalogChild(child))
130130
}
131131
}
132132
},
@@ -173,6 +173,48 @@ class AuthorFormControlElement extends AuthorBaseElement(HTMLElement) {
173173
break
174174
}
175175

176+
this.UTIL.registerListener(this.input, 'invalid', evt => this.emit('invalid', {}))
177+
178+
switch (this.type) {
179+
case 'input':
180+
case 'textarea':
181+
// this.UTIL.defineProperties({
182+
// leng
183+
// })
184+
185+
break
186+
187+
case 'select':
188+
case 'datalist':
189+
this.UTIL.defineProperties({
190+
length: {
191+
readonly: true,
192+
get: () => this.input.length
193+
},
194+
195+
selectedIndex: {
196+
set: value => this.input.selectedIndex = value,
197+
get: () => this.input.selectedIndex
198+
}
199+
})
200+
201+
;[
202+
'add',
203+
'deselectAll',
204+
'addFilter',
205+
'hasFilter',
206+
'removeAllFilters',
207+
'removeFilter',
208+
'item',
209+
'namedItem',
210+
'remove',
211+
'reportValidity',
212+
'setCustomValidity'
213+
].forEach(method => this[method] = (...args) => this.input[method](...args))
214+
215+
break
216+
}
217+
176218
this.PRIVATE.initialized = true
177219
this.emit('initialized')
178220
},
@@ -263,6 +305,30 @@ class AuthorFormControlElement extends AuthorBaseElement(HTMLElement) {
263305
static get observedAttributes () {
264306
return ['disabled']
265307
}
308+
309+
get value () {
310+
return this.input.value
311+
}
312+
313+
blur () {
314+
this.input.blur()
315+
}
316+
317+
checkValidity () {
318+
this.input.checkValidity()
319+
}
320+
321+
clear () {
322+
if ('clear' in this.input) {
323+
return this.input.clear()
324+
}
325+
326+
this.input.value = ''
327+
}
328+
329+
focus () {
330+
this.input.focus()
331+
}
266332
}
267333

268334
customElements.define('author-control', AuthorFormControlElement)

0 commit comments

Comments
 (0)