From 758e5806ce430d56e0ab7812b6686c0965d2d2cf Mon Sep 17 00:00:00 2001 From: Sachin Kumar Prajapati <156404623+sprajapati-eightfold@users.noreply.github.com> Date: Fri, 25 Oct 2024 10:43:24 +0530 Subject: [PATCH] feat: [Select] make dropdown role=group and pass correct id to options list for a11y (#897) * feat: make dropdown role=group and fix id for a11y in Select * feat: make dropdown role=group and fix id for a11y in Select-update snapshots * feat: select a11y fixes - improve code coverage --- src/components/Dropdown/Dropdown.tsx | 5 +- src/components/List/List.test.tsx | 10 ++ src/components/List/List.tsx | 15 ++- .../List/__snapshots__/List.test.tsx.snap | 109 ++++++++++++++++++ src/components/Select/Select.tsx | 1 + .../Select/__snapshots__/Select.test.tsx.snap | 26 ++--- 6 files changed, 150 insertions(+), 16 deletions(-) diff --git a/src/components/Dropdown/Dropdown.tsx b/src/components/Dropdown/Dropdown.tsx index 7dacda85e..312df531c 100644 --- a/src/components/Dropdown/Dropdown.tsx +++ b/src/components/Dropdown/Dropdown.tsx @@ -321,10 +321,13 @@ export const Dropdown: FC = React.memo( } // If there's an ariaRef, apply the a11y attributes to it, rather than the immediate child. if (ariaRef?.current) { - ariaRef.current.setAttribute('aria-controls', dropdownId); ariaRef.current.setAttribute('aria-expanded', `${mergedVisible}`); ariaRef.current.setAttribute('aria-haspopup', 'true'); + if (!ariaRef.current.hasAttribute('aria-controls')) { + ariaRef.current.setAttribute('aria-controls', dropdownId); + } + if (!ariaRef.current.hasAttribute('role')) { ariaRef.current.setAttribute('role', 'button'); } diff --git a/src/components/List/List.test.tsx b/src/components/List/List.test.tsx index cf236d6a8..2bc85497d 100644 --- a/src/components/List/List.test.tsx +++ b/src/components/List/List.test.tsx @@ -66,6 +66,16 @@ describe('List', () => { expect(container).toMatchSnapshot(); }); + test('Renders ol without crashing', () => { + const { container, getByTestId } = render( + + ); + const list = getByTestId('test-list'); + expect(() => container).not.toThrowError(); + expect(list).toBeTruthy(); + expect(container).toMatchSnapshot(); + }); + test('List is horizontal', () => { const { container } = render(); expect(container.querySelector('.vertical')).toBeFalsy(); diff --git a/src/components/List/List.tsx b/src/components/List/List.tsx index 6678b6b24..a4e7d0baa 100644 --- a/src/components/List/List.tsx +++ b/src/components/List/List.tsx @@ -34,6 +34,7 @@ export const List = ({ role, itemProps, getItem, + id, ...rest }: ListProps) => { const htmlDir: string = useCanvasDirection(); @@ -252,13 +253,23 @@ export const List = ({
{getHeader()} {listType === 'ul' && ( -
    +
      {getItems()} {!!renderAdditionalItem && getAdditionalItem()}
    )} {listType === 'ol' && ( -
      +
        {getItems()} {!!renderAdditionalItem && getAdditionalItem()}
      diff --git a/src/components/List/__snapshots__/List.test.tsx.snap b/src/components/List/__snapshots__/List.test.tsx.snap index c31706a38..32f01f9a6 100644 --- a/src/components/List/__snapshots__/List.test.tsx.snap +++ b/src/components/List/__snapshots__/List.test.tsx.snap @@ -109,6 +109,115 @@ exports[`List List is horizontal 1`] = `
`; +exports[`List Renders ol without crashing 1`] = ` +
+
+
+

+ Header +

+
+
    +
  1. + + User 1 + +
  2. +
  3. + + User 2 + +
  4. +
  5. + + User 3 + +
  6. +
  7. + + User 4 + +
  8. +
  9. + + User 5 + +
  10. +
+
+

+ Footer +

+
+
+
+`; + exports[`List Renders without crashing 1`] = `
= React.forwardRef( options?.length > 0) } ref={dropdownRef} + role={'group'} >
{/* When Dropdown is visible, place Pills in the reference element */} diff --git a/src/components/Select/__snapshots__/Select.test.tsx.snap b/src/components/Select/__snapshots__/Select.test.tsx.snap index 58e948c3d..d5ffc4b94 100644 --- a/src/components/Select/__snapshots__/Select.test.tsx.snap +++ b/src/components/Select/__snapshots__/Select.test.tsx.snap @@ -22,7 +22,7 @@ exports[`Select Renders empty options in multiple mode without crashing 1`] = ` class="select-input-group input-group right-icon" >