Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Convert a11y utils and prop-validators to TypeScript (#1226)

Olga Bulat72aefea5c872
diff --git a/src/utils/a11y/get-tab-index.js b/src/utils/a11y/get-tab-index.ts
similarity index 61%
rename from src/utils/a11y/get-tab-index.js
rename to src/utils/a11y/get-tab-index.ts
index 6ff387426..2cf9006fb 100644
--- a/src/utils/a11y/get-tab-index.js
+++ b/src/utils/a11y/get-tab-index.ts
@@ -1,17 +1,16 @@
/**
* @see https://github.com/reakit/reakit/blob/f05d36daa6fbcc52c70cf2c71baea69025aa2402/packages/reakit/src/Tabbable/Tabbable.ts#L54
- * @param {boolean} trulyDisabled
- * @param {boolean} nativeTabbable
- * @param {boolean} supportsDisabled
- * @param {number} [htmlTabIndex]
- * @returns {number}
+ * @param trulyDisabled - disabled and inaccessible when disabled
+ * @param nativeTabbable - some elements are not natively tabbable
+ * @param supportsDisabled - whether the element natively supports `disabled` attribute
+ * @param htmlTabIndex - tabindex value in the HTML
*/
export function getTabIndex(
- trulyDisabled,
- nativeTabbable,
- supportsDisabled,
- htmlTabIndex
-) {
+ trulyDisabled: boolean,
+ nativeTabbable: boolean,
+ supportsDisabled: boolean,
+ htmlTabIndex?: number
+): number | undefined {
if (trulyDisabled) {
if (nativeTabbable && !supportsDisabled) {
// Anchor, audio and video tags don't support the `disabled` attribute.
@@ -22,7 +21,7 @@ export function getTabIndex(
return undefined
}
if (nativeTabbable) {
- // If the element is enabled and it's natively tabbable, we don't need to
+ // If the element is enabled, and it's natively tabbable, we don't need to
// specify a tabIndex attribute unless it's explicitly set by the user.
return htmlTabIndex
}
diff --git a/src/utils/a11y/is-native-tabbale.js b/src/utils/a11y/is-native-tabbable.ts
similarity index 71%
rename from src/utils/a11y/is-native-tabbale.js
rename to src/utils/a11y/is-native-tabbable.ts
index 77956ecd8..07f438827 100644
--- a/src/utils/a11y/is-native-tabbale.js
+++ b/src/utils/a11y/is-native-tabbable.ts
@@ -1,9 +1,7 @@
/**
* @see https://github.com/reakit/reakit/blob/f05d36daa6fbcc52c70cf2c71baea69025aa2402/packages/reakit/src/Tabbable/Tabbable.ts#L44
- * @param {Element} element
- * @returns {boolean}
*/
-export function isNativeTabbable(element) {
+export function isNativeTabbable(element: Element) {
return ['BUTTON', 'INPUT', 'SELECT', 'TEXTAREA', 'A'].includes(
element.tagName
)
diff --git a/src/utils/a11y/supports-disabled-attribute.js b/src/utils/a11y/supports-disabled-attribute.ts
similarity index 68%
rename from src/utils/a11y/supports-disabled-attribute.js
rename to src/utils/a11y/supports-disabled-attribute.ts
index 6f6b92461..6b21668b3 100644
--- a/src/utils/a11y/supports-disabled-attribute.js
+++ b/src/utils/a11y/supports-disabled-attribute.ts
@@ -1,8 +1,6 @@
/**
* @see https://github.com/reakit/reakit/blob/f05d36daa6fbcc52c70cf2c71baea69025aa2402/packages/reakit/src/Tabbable/Tabbable.ts#L50
- * @param {Element} element
- * @returns {boolean}
*/
-export function supportsDisabledAttribute(element) {
+export function supportsDisabledAttribute(element: Element) {
return ['BUTTON', 'INPUT', 'SELECT', 'TEXTAREA'].includes(element.tagName)
}
diff --git a/src/utils/prop-validators.js b/src/utils/prop-validators.ts
similarity index 54%
rename from src/utils/prop-validators.js
rename to src/utils/prop-validators.ts
index 6f1f8efda..eaae7b4c4 100644
--- a/src/utils/prop-validators.js
+++ b/src/utils/prop-validators.ts
@@ -1,4 +1,4 @@
-import { supportedSearchTypes } from '~/constants/media'
+import { SupportedSearchType, supportedSearchTypes } from '~/constants/media'
/**
* Validates the search types that have supported API endpoints.
@@ -7,4 +7,8 @@ import { supportedSearchTypes } from '~/constants/media'
* Any future search type that is not fully supported (video, 3d)
* will also be invalid.
*/
-export const isValidSearchType = (v) => supportedSearchTypes.includes(v)
+export const isValidSearchType = (
+ value: string
+): value is SupportedSearchType => {
+ return supportedSearchTypes.includes(value as SupportedSearchType)
+}