Skip to content

Commit

Permalink
fixing disabled prop on DropdownExpandableMenuListItem and DropdownEx…
Browse files Browse the repository at this point in the history
…pandableMenuButton
  • Loading branch information
chris-cedrone-cengage committed Oct 18, 2023
1 parent 1312e97 commit fa75dc8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import { ThemeContext } from '../../theme/ThemeContext';
import { DropdownContext } from './Dropdown';
import { DropdownExpandableMenuGroupContext } from './DropdownExpandableMenuGroup';
import { useForkedRef } from '../../utils';
import { DropdownExpandableMenuItemContext } from './DropdownExpandableMenuItem';

export interface DropdownExpandableMenuButtonProps
extends AccordionButtonProps {
/**
* @internal
*/
disabled?: boolean;
icon?: React.ReactElement<IconProps>;
testId?: string;
}
Expand Down Expand Up @@ -50,19 +50,23 @@ export const DropdownExpandableMenuButton = React.forwardRef<
HTMLDivElement,
DropdownExpandableMenuButtonProps
>((props, forwardedRef) => {
const { children, disabled, customOnKeyDown, icon, testId, ...other } = props;
const { children, customOnKeyDown, icon, testId, ...other } = props;

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

const expandableMenuItemContext = React.useContext(
DropdownExpandableMenuItemContext
);

const ownRef = React.useRef<HTMLDivElement>();
const ref = useForkedRef(forwardedRef, ownRef);

React.useEffect(() => {
if (!disabled) {
if (!expandableMenuItemContext.disabled) {
context.registerDropdownMenuItem(context.itemRefArray, ownRef);
}
}, []);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import * as React from 'react';
import { AccordionItem, AccordionItemProps } from '../Accordion';
import {
DropdownExpandableMenuButton,
DropdownExpandableMenuButtonProps,
} from './DropdownExpandableMenuButton';
export interface DropdownExpandableMenuItemProps extends AccordionItemProps {
/**
* If true, item will be disabled; it will appear dimmed and onClick event (or any other events) will not fire
Expand All @@ -16,32 +12,25 @@ export interface DropdownExpandableMenuItemProps extends AccordionItemProps {
testId?: string;
}

export interface DropdownExpandableMenuItemContextInterface {
disabled?: boolean;
}

export const DropdownExpandableMenuItemContext =
React.createContext<DropdownExpandableMenuItemContextInterface>({});

export const DropdownExpandableMenuItem = React.forwardRef<
HTMLDivElement,
DropdownExpandableMenuItemProps
>((props, ref) => {
const { children, disabled, testId, ...other } = props;

const dropdownExpandableMenuItemChildren = React.Children.map(
children,
child => {
const item = child as React.ReactElement<
React.PropsWithChildren<DropdownExpandableMenuButtonProps>
>;

if (item.type === DropdownExpandableMenuButton) {
if (disabled) {
return React.cloneElement(item, { disabled: true });
}
}
return child;
}
);

return (
<AccordionItem {...other} isDisabled={disabled} ref={ref} testId={testId}>
{dropdownExpandableMenuItemChildren}
</AccordionItem>
<DropdownExpandableMenuItemContext.Provider value={{ disabled }}>
<AccordionItem {...other} isDisabled={disabled} ref={ref} testId={testId}>
{children}
</AccordionItem>
</DropdownExpandableMenuItemContext.Provider>
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import { DropdownContext } from './Dropdown';
import { Omit, useForkedRef } from '../../utils';
import { DropdownExpandableMenuGroupContext } from './DropdownExpandableMenuGroup';
import { DropdownMenuItem, DropdownMenuItemProps } from './DropdownMenuItem';
import { DropdownExpandableMenuItemContext } from './DropdownExpandableMenuItem';

export interface DropdownExpandableMenuListItemProps
extends Omit<DropdownMenuItemProps, 'icon'> {
extends Omit<DropdownMenuItemProps, 'icon' | 'disabled'> {
testId?: string;
}

Expand All @@ -31,18 +32,22 @@ export const DropdownExpandableMenuListItem = React.forwardRef<
HTMLDivElement,
DropdownExpandableMenuListItemProps
>((props, forwardedRef) => {
const { children, disabled, ...other } = props;
const { children, ...other } = props;

const ownRef = React.useRef<HTMLDivElement>();
const theme = React.useContext(ThemeContext);
const context = React.useContext(DropdownContext);

const menuGroupContext = React.useContext(DropdownExpandableMenuGroupContext);

const expandableMenuItemContext = React.useContext(
DropdownExpandableMenuItemContext
);

const ref = useForkedRef(forwardedRef, ownRef);

React.useEffect(() => {
if (!disabled)
if (!expandableMenuItemContext.disabled)
context.registerDropdownMenuItem(context.itemRefArray, ownRef);
}, []);

Expand All @@ -51,7 +56,7 @@ export const DropdownExpandableMenuListItem = React.forwardRef<
{...other}
expandableMenuButtonHasIcon={menuGroupContext.expandableMenuButtonHasIcon}
isExpandablePanel={menuGroupContext.isExpandablePanel}
ref={disabled ? null : ref}
ref={expandableMenuItemContext.disabled ? null : ref}
theme={theme}
>
{children}
Expand Down

0 comments on commit fa75dc8

Please sign in to comment.