-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add an isDisabled function to identify whether elements are dis…
…abled or not (#919) Co-authored-by: Sebastian Silbermann <silbermann.sebastian@gmail.com>
- Loading branch information
Showing
4 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"dom-accessibility-api": minor | ||
--- | ||
|
||
add an `isDisabled` function to check if elements are disabled or not |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { isDisabled } from "../is-disabled"; | ||
import { cleanup, renderIntoDocument } from "./helpers/test-utils"; | ||
|
||
describe("isDisabled", () => { | ||
afterEach(cleanup); | ||
test.each([ | ||
["<button data-test />", false], | ||
['<button data-test aria-disabled="true" />', true], | ||
['<button data-test disabled="true" />', true], | ||
["<button data-test disabled />", true], | ||
['<button data-test aria-disabled="false" />', false], | ||
["<div data-test disabled />", false], | ||
])("markup #%#", (markup, expectedIsDisabled) => { | ||
const container = renderIntoDocument(markup); | ||
expect(container).not.toBe(null); | ||
|
||
const testNode = container.querySelector("[data-test]"); | ||
expect(isDisabled(testNode)).toBe(expectedIsDisabled); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { getLocalName } from "./getRole"; | ||
|
||
const elementsSupportingDisabledAttribute = new Set([ | ||
"button", | ||
"fieldset", | ||
"input", | ||
"optgroup", | ||
"option", | ||
"select", | ||
"textarea", | ||
]); | ||
|
||
/** | ||
* Check if an element is disabled | ||
* https://www.w3.org/TR/html-aam-1.0/#html-attribute-state-and-property-mappings | ||
* https://www.w3.org/TR/wai-aria-1.1/#aria-disabled | ||
* | ||
* @param element | ||
* @returns {boolean} true if disabled, otherwise false | ||
*/ | ||
export function isDisabled(element: Element): boolean { | ||
const localName = getLocalName(element); | ||
return elementsSupportingDisabledAttribute.has(localName) && | ||
element.hasAttribute("disabled") | ||
? true | ||
: element.getAttribute("aria-disabled") === "true"; | ||
} |