From cc7ec9663d2faf879d1361167b65fbbeffcd7658 Mon Sep 17 00:00:00 2001 From: Melloware Date: Wed, 28 Jun 2023 08:43:56 -0400 Subject: [PATCH] Fix #4563: Paginator fix ARIA for page links (#4564) --- components/lib/api/Locale.js | 32 ++++++++++++++++++++++----- components/lib/paginator/PageLinks.js | 5 ++--- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/components/lib/api/Locale.js b/components/lib/api/Locale.js index 1ac31a437f..a2a7ff22dc 100644 --- a/components/lib/api/Locale.js +++ b/components/lib/api/Locale.js @@ -67,7 +67,7 @@ let locales = { moveToSource: 'Move to Source', moveAllToTarget: 'Move All to Target', moveAllToSource: 'Move All to Source', - pageLabel: 'Page', + pageLabel: 'Page {page}', firstPageLabel: 'First Page', lastPageLabel: 'Last Page', nextPageLabel: 'Next Page', @@ -136,13 +136,35 @@ function localeOption(key, locale) { } } -function ariaLabel(key) { +/** + * Find an ARIA label in the locale by key. If options are passed it will replace all options: + * ```ts + * const ariaValue = "Page {page}, User {user}, Role {role}"; + * const options = { page: 2, user: "John", role: "Admin" }; + * const result = ariaLabel('yourLabel', { page: 2, user: "John", role: "Admin" }) + * console.log(result); // Output: Page 2, User John, Role Admin + * ``` + * @param {string} ariaKey key of the ARIA label to look up in locale. + * @param {any} options JSON options like { page: 2, user: "John", role: "Admin" } + * @returns the ARIA label with replaced values + */ +function ariaLabel(ariaKey, options) { const _locale = PrimeReact.locale; try { - return localeOptions(_locale)['aria'][key]; + let ariaLabel = localeOptions(_locale)['aria'][ariaKey]; + + if (ariaLabel) { + for (const key in options) { + if (options.hasOwnProperty(key)) { + ariaLabel = ariaLabel.replace(`{${key}}`, options[key]); + } + } + } + + return ariaLabel; } catch (error) { - throw new Error(`The ${key} option is not found in the current locale('${_locale}').`); + throw new Error(`The ${ariaKey} option is not found in the current locale('${_locale}').`); } } @@ -152,4 +174,4 @@ function localeOptions(locale) { return locales[_locale]; } -export { locale, addLocale, updateLocaleOption, updateLocaleOptions, localeOption, localeOptions, ariaLabel }; +export { addLocale, ariaLabel, locale, localeOption, localeOptions, updateLocaleOption, updateLocaleOptions }; diff --git a/components/lib/paginator/PageLinks.js b/components/lib/paginator/PageLinks.js index 800e1816f2..50d16d37b7 100644 --- a/components/lib/paginator/PageLinks.js +++ b/components/lib/paginator/PageLinks.js @@ -1,9 +1,8 @@ import * as React from 'react'; -import { ariaLabel } from '../api/Api'; +import { ariaLabel, PrimeReactContext } from '../api/Api'; import { Ripple } from '../ripple/Ripple'; import { classNames, mergeProps, ObjectUtils } from '../utils/Utils'; import { PageLinksBase } from './PaginatorBase'; -import { PrimeReactContext } from '../api/Api'; export const PageLinks = React.memo((inProps) => { const context = React.useContext(PrimeReactContext); @@ -47,7 +46,7 @@ export const PageLinks = React.memo((inProps) => { onClick: (e) => onPageLinkClick(e, pageLink), className, disabled: props.disabled, - 'aria-label': ariaLabel('pageLabel', { page: pageLink + 1 }) + 'aria-label': ariaLabel('pageLabel', { page: pageLink }) }, getPTOptions(pageLink, 'pageButton') );