Skip to content

Commit

Permalink
feat(treeview): add support for isDisabled (#1480)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaliirumiantsev-cengage authored Oct 11, 2024
1 parent b21f4a7 commit 1caced4
Show file tree
Hide file tree
Showing 10 changed files with 641 additions and 116 deletions.
5 changes: 5 additions & 0 deletions .changeset/feat-treeview-add-support-for-isdisabled.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-magma-dom': minor
---

feat(TreeView): Add support for isDisabled
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@ describe('TreeItem', () => {
describe('isDisabled', () => {
it('the label is disabled', () => {
const { getByTestId } = render(
<TreeItem
label={labelText}
testId={testId}
itemId={itemId}
isDisabled
/>
<TreeView>
<TreeItem
label={labelText}
testId={testId}
itemId={itemId}
isDisabled
/>
</TreeView>
);

expect(getByTestId(`${testId}-label`)).toHaveStyleRule(
Expand Down
51 changes: 29 additions & 22 deletions packages/react-magma-dom/src/components/TreeView/TreeItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ export const TreeItem = React.forwardRef<HTMLLIElement, TreeItemProps>(
children,
icon,
index,
isDisabled,
label,
labelStyle,
style,
Expand All @@ -210,6 +209,8 @@ export const TreeItem = React.forwardRef<HTMLLIElement, TreeItemProps>(
props,
forwardedRef
);

const { isDisabled} = contextValue;

const {
checkboxChangeHandler,
Expand Down Expand Up @@ -253,7 +254,12 @@ export const TreeItem = React.forwardRef<HTMLLIElement, TreeItemProps>(
id={`${itemId}-label`}
data-testid={`${testId || itemId}-label`}
onClick={(e: any) => {
if (selectable === TreeViewSelectable.single && !isDisabled) {
if (isDisabled) {
e.stopPropagation();
return;
}

if (selectable === TreeViewSelectable.single) {
handleClick(e, itemId);
}
}}
Expand Down Expand Up @@ -297,6 +303,14 @@ export const TreeItem = React.forwardRef<HTMLLIElement, TreeItemProps>(
onExpandedChange(event);
};

const tabIndex = React.useMemo(() => {
if (isDisabled) {
return undefined;
}

return itemToFocus === itemId ? 0 : -1;
}, [isDisabled, itemToFocus, itemId]);

return (
<TreeItemContext.Provider value={contextValue}>
<StyledTreeItem
Expand All @@ -315,7 +329,7 @@ export const TreeItem = React.forwardRef<HTMLLIElement, TreeItemProps>(
selectableType={selectable}
selected={selectedItem}
theme={theme}
tabIndex={itemToFocus === itemId ? 0 : -1}
tabIndex={tabIndex}
onKeyDown={handleKeyDown}
onClick={event => {
if (selectable===TreeViewSelectable.off && hasOwnTreeItems) {
Expand Down Expand Up @@ -378,25 +392,18 @@ export const TreeItem = React.forwardRef<HTMLLIElement, TreeItemProps>(
{React.Children.map(
children,
(child: React.ReactElement<any>, index) => {
const component =
child.type === TreeItem ? (
<Transition isOpen={expanded} collapse unmountOnExit>
<ul role="group">
{React.cloneElement(child, {
index,
key: index,
itemDepth,
parentDepth,
})}
</ul>
</Transition>
) : (
child
);
// hide the disabled item + the children
if (isDisabled) return <></>;

return component;
return child.type === TreeItem ? (
<Transition isOpen={expanded} collapse unmountOnExit>
<ul role="group">
{React.cloneElement(child, {
index,
key: index,
itemDepth,
parentDepth,
})}
</ul>
</Transition>
) : child;
}
)}
</StyledTreeItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ export const Complex = args => {
</>
}
itemId="pt2ch5.1"
isDisabled
>
<TreeItem
icon={<ArticleIcon aria-hidden={true} />}
Expand All @@ -287,6 +288,7 @@ export const Complex = args => {
</>
}
itemId="pt2ch5.1.1"
isDisabled
/>
<TreeItem
icon={<ArticleIcon aria-hidden={true} />}
Expand Down Expand Up @@ -413,15 +415,18 @@ Complex.args = {
{ itemId: 'pt1ch1', checkedStatus: IndeterminateCheckboxStatus.checked },
{ itemId: 'pt1', checkedStatus: IndeterminateCheckboxStatus.indeterminate },
{ itemId: 'pt2ch4', checkedStatus: IndeterminateCheckboxStatus.checked },
{ itemId: 'pt2ch5.1.1', checkedStatus: IndeterminateCheckboxStatus.checked },
{
itemId: 'pt2ch5.1.1',
checkedStatus: IndeterminateCheckboxStatus.checked,
itemId: 'pt2ch5.1.2',
checkedStatus: IndeterminateCheckboxStatus.unchecked,
isDisabled: true,
},
{ itemId: 'pt2ch5.2', checkedStatus: IndeterminateCheckboxStatus.checked },
{ itemId: 'pt2ch5.3', checkedStatus: IndeterminateCheckboxStatus.checked },
],
checkParents: true,
checkChildren: true,
isDisabled: false,
testId: 'complex-example',
};

Expand Down
Loading

2 comments on commit 1caced4

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.