-
-
Notifications
You must be signed in to change notification settings - Fork 53
Accessible Accordion Component #358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4970fd9
d08e0d9
2090d15
9cf7d82
756b7a5
37ef389
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import {createContext} from 'react'; | ||
|
|
||
| export const AccordionContext = createContext({}); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import {createContext} from 'react'; | ||
|
|
||
| export const AccordionItemContext = createContext({}); |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,29 +1,44 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import React from 'react'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // @ts-ignore | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import {customClassSwitcher} from '~/core'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import React, {useContext, useState} from 'react'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import {AccordionContext} from '../contexts/AccordionContext'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import {AccordionItemContext} from '../contexts/AccordionItemContext'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type AccordionTriggerProps = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| children: React.ReactNode; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| customRootClass?: string, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| className?: string, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update Accordion Trigger to Use Context and Fix Button Type The updates to use - <button
+ <button type="button"Also applies to: 13-27 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| index: number, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| activeIndex: number, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| handleClick: (index: number) => void | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const AccordionTrigger: React.FC<AccordionTriggerProps> = ({children, handleClick, index, activeIndex, customRootClass}) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const rootClass = customClassSwitcher(customRootClass, 'Accordion'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const AccordionTrigger: React.FC<AccordionTriggerProps> = ({children, index, activeIndex, className=''}) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const {setActiveItem, rootClass, focusNextItem, focusPrevItem, activeItem} = useContext(AccordionContext); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const {itemValue} = useContext(AccordionItemContext); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.log(activeItem, itemValue); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <span className={`${rootClass}-trigger`}> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <button | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| onClick={() => handleClick(index)} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| aria-expanded={activeIndex === index} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| aria-controls={`content-${index}`} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| > | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {children} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </button> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <button | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| className={`${rootClass}-trigger ${className}`} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| onKeyDown={(e) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (e.key === 'ArrowDown') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| focusNextItem(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (e.key === 'ArrowUp') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| focusPrevItem(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| onClick={() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setActiveItem(itemValue); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| aria-expanded={activeItem === itemValue} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| aria-controls={`content-${index}`} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| > | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+22
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Specify Button Type to Prevent Default Form Submission As previously noted, the button inside - <button
+ <button type="button"Committable suggestion
Suggested change
ToolsBiome
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {children} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </button> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </span> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add Missing Key Property in List Rendering
The map function in JSX should have a
keyprop for each child element to maintain proper state and avoid potential rendering issues.Committable suggestion