Skip to content

Commit dd828ab

Browse files
Remove data-invalid, use aria-invalid as the sole state attribute
* There shouldn't be any cases where they would have different states; and using `aria-invalid` ensures the markup is accessible by default * Also reduces the amount of markup that's needed Co-authored-by: Konnor Rogers <konnor5456@gmail.com>
1 parent da8d444 commit dd828ab

File tree

5 files changed

+6
-27
lines changed

5 files changed

+6
-27
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ The following data attributes are used by this package:
148148
- `data-change-validation`: This input should use change validation (eg: `change`)
149149
- `data-focusout-validation`: This input should use `focusout` validation
150150
- `data-skip-validation`: This input should skip any validations
151-
- `data-invalid`: If present, the input is invalid`
152151
- `data-initial-load-errors`: If present, this input has initial load errors, which should not be cleared out when reflecting the constraint validation for the initial load.
153152
- `data-error-container`: Marks an element as an error container
154153
- Error list items
@@ -157,6 +156,10 @@ The following data attributes are used by this package:
157156
- `data-preserve`: This error list item should not be removed when clearing the error list; useful for rendering custom error messages for default error types. *Just because an error is preserved does not mean it is visible*
158157
- `data-error-message` Used in the error list item `template` element to indicate where an error message should be rendered in the markup as the `textContent`
159158

159+
### Marking an input as invalid
160+
161+
We use `aria-invalid="true"` to indicate that an input is invalid. This makes your markup accessible by default while not requiring you to tack on a bunch of extra attributes.
162+
160163
### Linking an `input` or `form` to an error container
161164

162165
To link an `input` or `form` to an error container, you:
@@ -214,7 +217,6 @@ if (!window.customElements.get('app-error-handling')) {
214217

215218
- Render any error list items that should be visible on the initial page load with the `data-visible` attribute
216219
- Mark the invalid inputs with:
217-
- `data-invalid`
218220
- `aria-invalid=true`
219221
- `data-initial-load-errors`
220222

css/util.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
[data-error-container] > ul > li:not([data-visible]) {
66
display: none;
7-
}
7+
}

src/element-utilities.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,11 @@ export function reflectConstraintValidationForElement(element) {
6363
}
6464

6565
/**
66-
* Adds/removes the `data-invalid` attribute based on the given `isValid`, and sets
67-
* `aria-invalid` based on the given `isValid`
66+
* Sets `aria-invalid` based on the given `isValid`
6867
* @params {Element} element the element to apply the validity state attributes to
6968
* @params {boolean} isValid whether or not the given element is valid
7069
*/
7170
export function setValidityStateAttributes(element, isValid) {
72-
if (isValid) {
73-
// Clear `data-invalid` attribute flag`
74-
element.removeAttribute(`data-invalid`)
75-
} else {
76-
element.toggleAttribute(`data-invalid`, !isValid)
77-
}
78-
7971
// Update the `aria-invalid` state based on the given validity boolean.
8072
element.setAttribute(`aria-invalid`, (!isValid).toString());
8173
}

test/element-utilities.test.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,26 +78,20 @@ suite('Element Utilities', async () => {
7878
<a-custom-element>Such as a rich text editor</a-custom-element>
7979
`);
8080

81-
assert.equal(false, input.hasAttribute(`data-invalid`))
8281
assert.equal(false, input.hasAttribute(`aria-invalid`))
8382

8483
ElementUtils.setValidityStateAttributes(input, false)
85-
assert.equal(true, input.hasAttribute(`data-invalid`))
8684
assert.equal("true", input.getAttribute(`aria-invalid`))
8785

8886
ElementUtils.setValidityStateAttributes(input, true)
89-
assert.equal(false, input.hasAttribute(`data-invalid`))
9087
assert.equal("false", input.getAttribute(`aria-invalid`))
9188

92-
assert.equal(false, customElement.hasAttribute(`data-invalid`))
9389
assert.equal(false, customElement.hasAttribute(`aria-invalid`))
9490

9591
ElementUtils.setValidityStateAttributes(customElement, false)
96-
assert.equal(true, customElement.hasAttribute(`data-invalid`))
9792
assert.equal("true", customElement.getAttribute(`aria-invalid`))
9893

9994
ElementUtils.setValidityStateAttributes(customElement, true)
100-
assert.equal(false, customElement.hasAttribute(`data-invalid`))
10195
assert.equal("false", customElement.getAttribute(`aria-invalid`))
10296
})
10397

@@ -124,7 +118,6 @@ suite('Element Utilities', async () => {
124118

125119
ElementUtils.reflectConstraintValidationForElement(input)
126120

127-
assert.equal(true, input.hasAttribute(`data-invalid`))
128121
assert.equal("true", input.getAttribute(`aria-invalid`))
129122

130123
assert.equal(1, document.querySelectorAll(`#name-field-errors li`).length)
@@ -140,7 +133,6 @@ suite('Element Utilities', async () => {
140133

141134
ElementUtils.reflectConstraintValidationForElement(input)
142135

143-
assert.equal(false, input.hasAttribute(`data-invalid`))
144136
assert.equal("false", input.getAttribute(`aria-invalid`))
145137

146138
assert.equal(0, document.querySelectorAll(`#name-field-errors li`).length)
@@ -167,7 +159,6 @@ suite('Element Utilities', async () => {
167159

168160
ElementUtils.reflectConstraintValidationForElement(input)
169161

170-
assert.equal(false, input.hasAttribute(`data-invalid`))
171162
assert.equal("false", input.getAttribute(`aria-invalid`))
172163

173164
assert.equal(0, document.querySelectorAll(`#name-field-errors li`).length)

test/event-handlers.test.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ suite('Event Handlers', async () => {
1212

1313
input.dispatchEvent(new CustomEvent(`test-event`))
1414

15-
assert.equal(false, input.hasAttribute(`data-invalid`))
1615
assert.equal(false, input.hasAttribute(`aria-invalid`))
1716
})
1817

@@ -25,7 +24,6 @@ suite('Event Handlers', async () => {
2524

2625
input.dispatchEvent(new CustomEvent(`test-event`))
2726

28-
assert.equal(true, input.hasAttribute(`data-invalid`))
2927
assert.equal("true", input.getAttribute(`aria-invalid`))
3028
})
3129

@@ -38,7 +36,6 @@ suite('Event Handlers', async () => {
3836

3937
input.dispatchEvent(new CustomEvent(`test-event`))
4038

41-
assert.equal(false, input.hasAttribute(`data-invalid`))
4239
assert.equal(false, input.hasAttribute(`aria-invalid`))
4340
})
4441

@@ -51,7 +48,6 @@ suite('Event Handlers', async () => {
5148

5249
input.dispatchEvent(new CustomEvent(`test-event`))
5350

54-
assert.equal(false, input.hasAttribute(`data-invalid`))
5551
assert.equal(false, input.hasAttribute(`aria-invalid`))
5652
})
5753

@@ -64,7 +60,6 @@ suite('Event Handlers', async () => {
6460

6561
input.dispatchEvent(new CustomEvent(`test-event`))
6662

67-
assert.equal(true, input.hasAttribute(`data-invalid`))
6863
assert.equal("true", input.getAttribute(`aria-invalid`))
6964
})
7065

@@ -77,7 +72,6 @@ suite('Event Handlers', async () => {
7772

7873
input.dispatchEvent(new CustomEvent(`test-event`))
7974

80-
assert.equal(false, input.hasAttribute(`data-invalid`))
8175
assert.equal(false, input.hasAttribute(`aria-invalid`))
8276
})
8377

0 commit comments

Comments
 (0)