Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 13e844c

Browse files
authored
Merge pull request #39 from github/required-attr-change
Respond to required attribute changes
2 parents 4379194 + 5000ef9 commit 13e844c

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ export default class AutoCheckElement extends HTMLElement {
4040
input.setCustomValidity('')
4141
}
4242

43+
attributeChangedCallback(name: string) {
44+
if (name === 'required') {
45+
const input = this.input
46+
if (!input) return
47+
input.required = this.required
48+
}
49+
}
50+
51+
static get observedAttributes(): Array<string> {
52+
return ['required']
53+
}
54+
4355
get input(): ?HTMLInputElement {
4456
const input = this.querySelector('input')
4557
return input instanceof HTMLInputElement ? input : null
@@ -71,10 +83,6 @@ export default class AutoCheckElement extends HTMLElement {
7183
}
7284

7385
set required(required: boolean) {
74-
const input = this.input
75-
if (!input) return
76-
77-
input.required = required
7886
if (required) {
7987
this.setAttribute('required', '')
8088
} else {

test/test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ describe('auto-check element', function() {
4040
})
4141

4242
it('invalidates empty input', function() {
43-
// FIXME Must implement attributeChanged callback.
44-
checker.required = true
43+
assert.isTrue(input.hasAttribute('required'))
4544
assert.isFalse(input.checkValidity())
4645
})
4746

0 commit comments

Comments
 (0)