Skip to content

Commit

Permalink
fixing keyboard navigation, added a prop to Accordion to accommodate,…
Browse files Browse the repository at this point in the history
… amendments per Laura's review, still working on tests
  • Loading branch information
chris-cedrone-cengage committed Sep 21, 2023
1 parent 4a2ddd4 commit 41a7ba7
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ export interface AccordionButtonProps
/**
* @internal
*/
hasCustomArray?: boolean;
theme?: ThemeInterface;
}

const StyledButton = styled.button<{
hasCustomArray?: boolean;
isInverse?: boolean;
isExpanded?: boolean;
}>`
Expand Down Expand Up @@ -90,7 +92,13 @@ export const AccordionButton = React.forwardRef<
HTMLButtonElement,
AccordionButtonProps
>((props, forwardedRef) => {
const { children, testId, isInverse: isInverseProp, ...rest } = props;
const {
children,
testId,
hasCustomArray,
isInverse: isInverseProp,
...rest
} = props;
const theme = React.useContext(ThemeContext);
const isInverse = useIsInverse(isInverseProp);

Expand Down Expand Up @@ -122,11 +130,12 @@ export const AccordionButton = React.forwardRef<
aria-expanded={Boolean(isExpanded)}
data-testid={testId}
disabled={isDisabled}
hasCustomArray={hasCustomArray}
id={buttonId}
isExpanded={isExpanded}
isInverse={isInverse}
onClick={handleClick}
onKeyDown={handleKeyDown}
onKeyDown={hasCustomArray ? null : handleKeyDown}
ref={ref}
theme={theme}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ export const ExpandableItems = args => {

export const ExpandableItemsWithIcons = args => {
return (
<Dropdown {...args}>
<Dropdown {...args} width={120}>
<DropdownButton>Expandable Items Dropdown</DropdownButton>
<DropdownContent>
<DropdownExpandableMenuGroup isMulti={false} defaultIndex={0}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export const Dropdown = React.forwardRef<HTMLDivElement, DropdownProps>(

function handleKeyDown(event: React.KeyboardEvent) {
if (event.key === 'Escape') {
event.nativeEvent.stopImmediatePropagation();
closeDropdown(event);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export const DropdownContent = React.forwardRef<

let hasItemChildren = false;

// Default styling for Expandable Menus to override the max-height used for regular dropdowns
// For Expandable Dropdowns that don't require a max-height
let hasExpandableItems = false;

React.Children.forEach(children, (child: any) => {
Expand All @@ -144,7 +144,9 @@ export const DropdownContent = React.forwardRef<
maxHeight={context.maxHeight}
ref={ref}
style={
hasExpandableItems ? { maxHeight: 'inherit', overflow: 'hidden' } : null
hasExpandableItems
? { maxHeight: 'inherit', overflow: 'hidden' }
: props.style
}
tabIndex={-1}
testId={testId || 'dropdownContent'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { IconWrapper, menuBackground } from './DropdownMenuItem';
import { IconProps } from 'react-magma-icons';
import { ThemeContext } from '../../theme/ThemeContext';
import { DropdownContext } from './Dropdown';
import { DropdownExpandableContext } from './DropdownExpandableMenuGroup';
import { DropdownExpandableMenuGroupContext } from './DropdownExpandableMenuGroup';
import { useForkedRef } from '../../utils';

export interface DropdownExpandableMenuButtonProps
Expand All @@ -15,12 +15,13 @@ export interface DropdownExpandableMenuButtonProps
}

const StyledAccordionButton = styled(AccordionButton)<{
hasIcon?: boolean;
hasCustomArray?: boolean;
expandableMenuButtonHasIcon?: boolean;
icon?: React.ReactElement<IconProps>;
}>`
font-weight: 400;
padding: ${props =>
!props.icon && props.hasIcon
!props.icon && props.expandableMenuButtonHasIcon
? `${props.theme.spaceScale.spacing03} ${props.theme.spaceScale.spacing05} ${props.theme.spaceScale.spacing03} ${props.theme.spaceScale.spacing11}`
: `${props.theme.spaceScale.spacing03} ${props.theme.spaceScale.spacing05}`};
margin: 0;
Expand All @@ -36,18 +37,19 @@ const StyledAccordionButton = styled(AccordionButton)<{

const StyledIconWrapper = styled(IconWrapper)`
justify-content: center;
/* align-items: center; */
`;

export const DropdownExpandableMenuButton = React.forwardRef<
HTMLDivElement,
DropdownExpandableMenuButtonProps
>((props, forwardedRef) => {
const { children, icon, testId, ...other } = props;
const { children, hasCustomArray, icon, testId, ...other } = props;

const theme = React.useContext(ThemeContext);
const context = React.useContext(DropdownContext);
const expandableContext = React.useContext(DropdownExpandableContext);
const expandableContext = React.useContext(
DropdownExpandableMenuGroupContext
);

const ownRef = React.useRef<HTMLDivElement>();
const ref = useForkedRef(forwardedRef, ownRef);
Expand All @@ -60,9 +62,12 @@ export const DropdownExpandableMenuButton = React.forwardRef<
<StyledAccordionButton
{...other}
ref={ref}
icon={props.icon}
hasCustomArray
icon={icon}
theme={theme}
hasIcon={expandableContext.hasIcon}
expandableMenuButtonHasIcon={
expandableContext.expandableMenuButtonHasIcon
}
isInverse={context.isInverse}
testId={testId}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@ import styled from '../../theme/styled';
import { Accordion, AccordionProps } from '../Accordion';
import { DropdownContext } from './Dropdown';

const StyledAccordion = styled(Accordion)<{
testId?: string;
}>`
const StyledAccordion = styled(Accordion)<{}>`
border: none;
`;

export interface DropdownExpandableContext {
hasIcon?: boolean;
export interface DropdownExpandableMenuGroupContextInterface {
expandableMenuButtonHasIcon?: boolean;
isExpandablePanel?: boolean;
}

export const DropdownExpandableContext =
React.createContext<DropdownExpandableContext>({});
export const DropdownExpandableMenuGroupContext =
React.createContext<DropdownExpandableMenuGroupContextInterface>({});

export const DropdownExpandableMenuGroup = React.forwardRef<
HTMLDivElement,
Expand All @@ -25,15 +23,15 @@ export const DropdownExpandableMenuGroup = React.forwardRef<

const context = React.useContext(DropdownContext);

let hasIcon = false;
let expandableMenuButtonHasIcon = false;
let isExpandablePanel = false;

React.Children.forEach(children, (child: any) => {
if (child.type?.displayName === 'DropdownExpandableMenuItem') {
React.Children.forEach(child.props.children, (c: any) => {
if (c.type?.displayName === 'DropdownExpandableMenuButton') {
if (c.props.icon) {
hasIcon = true;
expandableMenuButtonHasIcon = true;
return;
}
}
Expand All @@ -45,16 +43,18 @@ export const DropdownExpandableMenuGroup = React.forwardRef<
});

return (
<DropdownExpandableContext.Provider value={{ hasIcon, isExpandablePanel }}>
<DropdownExpandableMenuGroupContext.Provider
value={{ expandableMenuButtonHasIcon, isExpandablePanel }}
>
<StyledAccordion
{...other}
isInverse={context.isInverse}
role="expandable group"
role="group"
testId={testId}
>
{children}
</StyledAccordion>
</DropdownExpandableContext.Provider>
</DropdownExpandableMenuGroupContext.Provider>
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export interface DropdownExpandableMenuItemProps extends AccordionItemProps {
export const DropdownExpandableMenuItem = React.forwardRef<
HTMLDivElement,
DropdownExpandableMenuItemProps
>(props => {
>((props, ref) => {
const { children, disabled, testId, ...other } = props;

return (
<AccordionItem isDisabled={disabled} {...other} testId={testId}>
<AccordionItem {...other} isDisabled={disabled} ref={ref} testId={testId}>
{children}
</AccordionItem>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const StyledAccordionPanel = styled(AccordionPanel)`
export const DropdownExpandableMenuPanel = React.forwardRef<
HTMLDivElement,
DropdownExpandableMenuPanelProps
>(props => {
>((props, ref) => {
const { children, testId, ...other } = props;

React.Children.map(children, child => {
Expand All @@ -22,15 +22,15 @@ export const DropdownExpandableMenuPanel = React.forwardRef<
console.warn(
`
--------------------------------------------------------------------------------------------------------
Only one group level is supported, anything nested two levels or more isn't accounted for in the styling
React Magma Warning: Only one group level is supported for Expandable Dropdowns, anything nested two levels or more isn't accounted for in the styling
--------------------------------------------------------------------------------------------------------
`
);
}
});

return (
<StyledAccordionPanel testId={testId} {...other}>
<StyledAccordionPanel testId={testId} {...other} ref={ref}>
{children}
</StyledAccordionPanel>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { DropdownContext } from './Dropdown';
import { IconProps, CheckIcon } from 'react-magma-icons';
import { transparentize } from 'polished';
import { Omit, useForkedRef } from '../../utils';
import { DropdownExpandableContext } from './DropdownExpandableMenuGroup';
import { DropdownExpandableMenuGroupContext } from './DropdownExpandableMenuGroup';

export interface DropdownMenuItemProps
extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onClick'> {
Expand Down Expand Up @@ -63,7 +63,7 @@ export function menuBackground(props) {

function menuItemPadding(props) {
//For DropdownExpandableMenu styling with an icon
if (props.hasIcon && props.isExpandablePanel) {
if (props.expandableMenuButtonHasIcon && props.isExpandablePanel) {
return `${props.theme.spaceScale.spacing03} ${props.theme.spaceScale.spacing05} ${props.theme.spaceScale.spacing03} 72px`;
}
//For DropdownExpandableMenu styling without an icon
Expand Down Expand Up @@ -111,7 +111,7 @@ export const MenuItemStyles = props => {
const StyledItem = styled.div<{
as?: string;
disabled?: boolean;
hasIcon?: boolean;
expandableMenuButtonHasIcon?: boolean;
isExpandablePanel?: boolean;

isFixedWidth?: boolean;
Expand Down Expand Up @@ -146,7 +146,7 @@ export const DropdownMenuItem = React.forwardRef<
const theme = React.useContext(ThemeContext);
const context = React.useContext(DropdownContext);

const expandableContext = React.useContext(DropdownExpandableContext);
const menuGroupContext = React.useContext(DropdownExpandableMenuGroupContext);

const ref = useForkedRef(forwardedRef, ownRef);

Expand Down Expand Up @@ -195,8 +195,8 @@ export const DropdownMenuItem = React.forwardRef<
{...other}
aria-disabled={disabled}
disabled={disabled}
hasIcon={expandableContext.hasIcon}
isExpandablePanel={expandableContext.isExpandablePanel}
expandableMenuButtonHasIcon={menuGroupContext.expandableMenuButtonHasIcon}
isExpandablePanel={menuGroupContext.isExpandablePanel}
isFixedWidth={context.isFixedWidth}
isInactive={isInactive}
isInverse={context.isInverse}
Expand Down
1 change: 1 addition & 0 deletions packages/react-magma-dom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export { Container, ContainerProps } from './components/Container';
export { Datagrid, DatagridProps } from './components/Datagrid';
export { DatePicker, DatePickerProps } from './components/DatePicker';
export {
Dropdown,
DropdownAlignment,
DropdownDropDirection,
DropdownProps,
Expand Down

0 comments on commit 41a7ba7

Please sign in to comment.