Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/NodeList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ interface NodeListProps<TreeDataType extends BasicDataNode> {
tabIndex: number;
checkable?: boolean;
selectable?: boolean;
multiselectable?: boolean;
disabled?: boolean;

expandedKeys: Key[];
Expand Down Expand Up @@ -141,6 +142,7 @@ const NodeList = React.forwardRef<NodeListRef, NodeListProps<any>>((props, ref)
prefixCls,
data,
selectable,
multiselectable,
checkable,
expandedKeys,
selectedKeys,
Expand Down Expand Up @@ -330,6 +332,7 @@ const NodeList = React.forwardRef<NodeListRef, NodeListProps<any>>((props, ref)
prefixCls={`${prefixCls}-list`}
ref={listRef}
role="tree"
aria-multiselectable={!multiselectable ? undefined : true}
onVisibleChange={originList => {
// The best match is using `fullList` - `originList` = `restList`
// and check the `restList` to see if has the MOTION_KEY node
Expand Down
2 changes: 2 additions & 0 deletions src/Tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,7 @@ class Tree<TreeDataType extends DataNode | BasicDataNode = DataNode> extends Rea
focusable,
tabIndex = 0,
selectable,
multiple,
showIcon,
icon,
switcherIcon,
Expand Down Expand Up @@ -1473,6 +1474,7 @@ class Tree<TreeDataType extends DataNode | BasicDataNode = DataNode> extends Rea
data={flattenNodes}
disabled={disabled}
selectable={selectable}
multiselectable={multiple}
checkable={!!checkable}
motion={motion}
dragging={draggingNodeKey !== null}
Expand Down
9 changes: 8 additions & 1 deletion src/TreeNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ const TreeNode: React.FC<Readonly<TreeNodeProps>> = props => {

const [dragNodeHighlight, setDragNodeHighlight] = React.useState<boolean>(false);

const ariaId = React.useId();

// ======= State: Disabled State =======
const isDisabled = !!(context.disabled || props.disabled || unstableContext.nodeDisabled?.(data));

Expand Down Expand Up @@ -382,6 +384,7 @@ const TreeNode: React.FC<Readonly<TreeNodeProps>> = props => {
>
{$icon}
<span
id={`${context.prefixCls}-title-${ariaId}`}
className={classNames(`${context.prefixCls}-title`, treeClassNames?.itemTitle)}
style={styles?.itemTitle}
>
Expand Down Expand Up @@ -415,12 +418,15 @@ const TreeNode: React.FC<Readonly<TreeNodeProps>> = props => {
const draggableWithoutDisabled = !isDisabled && isDraggable;

const dragging = context.draggingNodeKey === eventKey;
const ariaSelected = selectable !== undefined ? { 'aria-selected': !!selectable } : undefined;
const ariaSelected = isSelectable ? { 'aria-selected': !!selected } : undefined;
const ariaChecked = isCheckable ? { 'aria-checked': !!checked } : undefined;

return (
<div
ref={domRef}
role="treeitem"
aria-expanded={isLeaf ? undefined : expanded}
aria-labelledby={`${context.prefixCls}-title-${ariaId}`}
className={classNames(className, `${context.prefixCls}-treenode`, treeClassNames?.item, {
[`${context.prefixCls}-treenode-disabled`]: isDisabled,
[`${context.prefixCls}-treenode-switcher-${expanded ? 'open' : 'close'}`]: !isLeaf,
Expand Down Expand Up @@ -452,6 +458,7 @@ const TreeNode: React.FC<Readonly<TreeNodeProps>> = props => {
onDragEnd={isDraggable ? onDragEnd : undefined}
onMouseMove={onMouseMove}
{...ariaSelected}
{...ariaChecked}
{...dataOrAriaAttributeProps}
>
<Indent prefixCls={context.prefixCls} level={level} isStart={isStart} isEnd={isEnd} />
Expand Down
7 changes: 4 additions & 3 deletions tests/TreeNodeProps.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ describe('TreeNode Props', () => {
}

const { container } = render(<Demo />);
// tree selectable is false ,then children should be selectable = false if not set selectable alone.
expect(container.querySelectorAll('[aria-selected=false]')).toHaveLength(1);
// tree selectable is false, then no treeitem should have aria-selected (neither true or false)
expect(container.querySelectorAll('[aria-selected]')).toHaveLength(0);

fireEvent.click(container.querySelector('.rc-tree-node-content-wrapper'));
expect(onClick).toHaveBeenCalled();
Expand All @@ -209,7 +209,8 @@ describe('TreeNode Props', () => {
fireEvent.click(container.querySelector('.test-button'));
onClick.mockRestore();
onSelect.mockRestore();
expect(container.querySelectorAll('[aria-selected=false]')).toHaveLength(1);
// tree selectable is true, but only one treeitem is selectable
expect(container.querySelectorAll('[aria-selected]')).toHaveLength(1);
fireEvent.click(container.querySelectorAll('.rc-tree-node-content-wrapper')[1]);
expect(onClick).toHaveBeenCalled();
expect(onSelect).not.toHaveBeenCalled();
Expand Down
Loading