Skip to content

Commit

Permalink
fix(pager): correctly associate labels to input fields
Browse files Browse the repository at this point in the history
Neither the numeric input for page number nor the page size select had a label that was
programatically associated to the input and contained descriptive text. This is now fixed.

fix #5284 and #5632
  • Loading branch information
robinzigmond committed Dec 2, 2022
1 parent 6dd3ede commit 6412ddc
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 18 deletions.
24 changes: 12 additions & 12 deletions src/components/pager/__internal__/pager-navigation.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,19 @@ const PagerNavigation = ({
{!hasOnePage && renderButtonsBeforeCount()}
{showPageCount && (
<StyledPagerNavInner>
<StyledPagerNoSelect>{l.pager.pageX()}</StyledPagerNoSelect>
<Label htmlFor={currentPageId}>
<NumberInput
value={currentPage.toString()}
data-element="current-page"
onChange={handleCurrentPageChange}
onBlur={handlePageInputChange}
id={currentPageId}
onKeyUp={(ev) =>
Events.isEnterKey(ev) ? handlePageInputChange(ev) : false
}
/>
<Label inline htmlFor={currentPageId}>
<StyledPagerNoSelect>{l.pager.pageX()}</StyledPagerNoSelect>
</Label>
<NumberInput
value={currentPage.toString()}
data-element="current-page"
onChange={handleCurrentPageChange}
onBlur={handlePageInputChange}
id={currentPageId}
onKeyUp={(ev) =>
Events.isEnterKey(ev) ? handlePageInputChange(ev) : false
}
/>
<StyledPagerNoSelect>{l.pager.ofY(pageCount)}</StyledPagerNoSelect>
</StyledPagerNavInner>
)}
Expand Down
2 changes: 1 addition & 1 deletion src/components/pager/__internal__/pager-navigation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe("Pager Navigation", () => {
assertStyleMatch(
{
padding: "0",
margin: "8px 4px 0 4px",
margin: "4px",
lineHeight: "26px",
minHeight: "24px",
},
Expand Down
11 changes: 9 additions & 2 deletions src/components/pager/pager.component.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React, { useState, useCallback, useEffect } from "react";
import React, { useState, useCallback, useEffect, useRef } from "react";
import PropTypes from "prop-types";

import { Select } from "../select";
import PagerNavigation from "./__internal__/pager-navigation.component";
import Option from "../select/option/option.component";
import useLocale from "../../hooks/__internal__/useLocale";
import createGuid from "../../__internal__/utils/helpers/guid";
import {
StyledPagerContainer,
StyledPagerSizeOptions,
Expand Down Expand Up @@ -44,6 +45,9 @@ const Pager = ({
const [currentPageSize, setCurrentPageSize] = useState(pageSize);
const [value, setValue] = useState(pageSize);

const guid = useRef(createGuid());
const pageSizeSelectId = `Pager_size_selector_${guid.current}`;

const getPageCount = useCallback(() => {
if (Number(totalRecords) < 0 || Number.isNaN(Number(totalRecords))) {
return 1;
Expand Down Expand Up @@ -138,6 +142,7 @@ const Pager = ({
onBlur={() => setValue(currentPageSize)}
onKeyDown={handleKeyDown}
data-element="page-select"
id={pageSizeSelectId}
>
{pageSizeSelectionOptions.map((sizeOption) => (
<Option
Expand All @@ -156,7 +161,9 @@ const Pager = ({
return (
showPageSizeSelection && (
<StyledPagerSizeOptionsInner>
{showPageSizeLabelBefore && <span>{l.pager.show()}</span>}
{showPageSizeLabelBefore && (
<label htmlFor={pageSizeSelectId}>{l.pager.show()}</label>
)}
{sizeSelector()}
{showPageSizeLabelAfter && (
<div>{l.pager.records(currentPageSize, false)}</div>
Expand Down
4 changes: 2 additions & 2 deletions src/components/pager/pager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ describe("Pager", () => {
wrapper.find(StyledPagerSizeOptionsInner).getDOMNode().firstChild
.textContent
).toEqual(
wrapper.find(StyledPagerSizeOptionsInner).find("span").first().text()
wrapper.find(StyledPagerSizeOptionsInner).find("label").first().text()
);
expect(wrapper.find(StyledPagerSizeOptionsInner).exists()).toBeTruthy();
expect(
Expand Down Expand Up @@ -308,7 +308,7 @@ describe("Pager", () => {
wrapper.find(StyledPagerSizeOptionsInner).getDOMNode().firstChild
.textContent
).toEqual(
wrapper.find(StyledPagerSizeOptionsInner).find("span").first().text()
wrapper.find(StyledPagerSizeOptionsInner).find("label").first().text()
);
expect(
wrapper.find(StyledPagerSizeOptionsInner).getDOMNode().lastChild
Expand Down
4 changes: 3 additions & 1 deletion src/components/pager/pager.style.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const StyledPagerNavigation = styled.div`
&& ${StyledInputPresentation} {
padding: 0;
margin: 8px 4px 0 4px;
margin: 4px;
height: 26px;
line-height: 26px;
min-height: 24px;
Expand All @@ -95,6 +95,7 @@ const StyledPagerNavInner = styled.div`
display: flex;
align-items: center;
padding: 0 12px;
margin: 4px 0;
`;

const StyledPagerLinkStyles = styled(Link)`
Expand All @@ -106,6 +107,7 @@ const StyledPagerLinkStyles = styled(Link)`
const StyledPagerNoSelect = styled.div`
user-select: none;
white-space: nowrap;
font-weight: normal;
`;

const StyledPagerSummary = styled.div`
Expand Down

0 comments on commit 6412ddc

Please sign in to comment.