-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
[TreeView] Improve node registration and fix other issues #21574
Changes from all commits
8f855d0
911e2e6
3edb4e4
8f3bccd
753fece
fceb38d
3645d20
e120da9
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 |
---|---|---|
|
@@ -333,13 +333,10 @@ describe('<TreeItem />', () => { | |
|
||
it('should work with programmatic focus', () => { | ||
const { getByTestId } = render( | ||
<React.Fragment> | ||
<div data-testid="start" tabIndex={0} /> | ||
eps1lon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<TreeView> | ||
<TreeItem nodeId="1" label="one" data-testid="one" /> | ||
<TreeItem nodeId="2" label="two" data-testid="two" /> | ||
</TreeView> | ||
</React.Fragment>, | ||
<TreeView> | ||
<TreeItem nodeId="1" label="one" data-testid="one" /> | ||
<TreeItem nodeId="2" label="two" data-testid="two" /> | ||
</TreeView>, | ||
); | ||
|
||
expect(getByTestId('one')).to.have.attribute('tabindex', '0'); | ||
|
@@ -840,8 +837,9 @@ describe('<TreeItem />', () => { | |
|
||
describe('asterisk key interaction', () => { | ||
it('expands all siblings that are at the same level as the current node', () => { | ||
const toggleSpy = spy(); | ||
const { getByTestId } = render( | ||
<TreeView> | ||
<TreeView onNodeToggle={toggleSpy}> | ||
<TreeItem nodeId="one" label="one" data-testid="one"> | ||
<TreeItem nodeId="two" label="two" data-testid="two" /> | ||
</TreeItem> | ||
|
@@ -853,6 +851,7 @@ describe('<TreeItem />', () => { | |
<TreeItem nodeId="seven" label="seven" data-testid="seven" /> | ||
</TreeItem> | ||
</TreeItem> | ||
<TreeItem nodeId="eight" label="eight" data-testid="eight" /> | ||
</TreeView>, | ||
); | ||
|
||
|
@@ -864,10 +863,13 @@ describe('<TreeItem />', () => { | |
|
||
fireEvent.keyDown(getByTestId('one'), { key: '*' }); | ||
|
||
expect(toggleSpy.args[0][1]).to.have.length(3); | ||
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. We need to check the change handler here because eight will never have aria-expanded set due to a check when applying that attribute. |
||
|
||
expect(getByTestId('one')).to.have.attribute('aria-expanded', 'true'); | ||
expect(getByTestId('three')).to.have.attribute('aria-expanded', 'true'); | ||
expect(getByTestId('five')).to.have.attribute('aria-expanded', 'true'); | ||
expect(getByTestId('six')).to.have.attribute('aria-expanded', 'false'); | ||
expect(getByTestId('eight')).not.to.have.attribute('aria-expanded'); | ||
}); | ||
}); | ||
}); | ||
|
@@ -990,27 +992,27 @@ describe('<TreeItem />', () => { | |
fireEvent.keyDown(getByTestId('three'), { key: 'ArrowDown', shiftKey: true }); | ||
|
||
expect(getByTestId('four')).toHaveFocus(); | ||
expect(container.querySelectorAll('[aria-selected=true]').length).to.equal(2); | ||
expect(container.querySelectorAll('[aria-selected=true]')).to.have.length(2); | ||
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. Consistency changes. |
||
|
||
fireEvent.keyDown(getByTestId('four'), { key: 'ArrowDown', shiftKey: true }); | ||
|
||
expect(getByTestId('three')).to.have.attribute('aria-selected', 'true'); | ||
expect(getByTestId('four')).to.have.attribute('aria-selected', 'true'); | ||
expect(getByTestId('five')).to.have.attribute('aria-selected', 'true'); | ||
expect(container.querySelectorAll('[aria-selected=true]').length).to.equal(3); | ||
expect(container.querySelectorAll('[aria-selected=true]')).to.have.length(3); | ||
|
||
fireEvent.keyDown(getByTestId('five'), { key: 'ArrowUp', shiftKey: true }); | ||
|
||
expect(getByTestId('four')).toHaveFocus(); | ||
expect(container.querySelectorAll('[aria-selected=true]').length).to.equal(2); | ||
expect(container.querySelectorAll('[aria-selected=true]')).to.have.length(2); | ||
|
||
fireEvent.keyDown(getByTestId('four'), { key: 'ArrowUp', shiftKey: true }); | ||
|
||
expect(container.querySelectorAll('[aria-selected=true]').length).to.equal(1); | ||
expect(container.querySelectorAll('[aria-selected=true]')).to.have.length(1); | ||
|
||
fireEvent.keyDown(getByTestId('three'), { key: 'ArrowUp', shiftKey: true }); | ||
|
||
expect(container.querySelectorAll('[aria-selected=true]').length).to.equal(2); | ||
expect(container.querySelectorAll('[aria-selected=true]')).to.have.length(2); | ||
|
||
fireEvent.keyDown(getByTestId('two'), { key: 'ArrowUp', shiftKey: true }); | ||
|
||
|
@@ -1019,7 +1021,7 @@ describe('<TreeItem />', () => { | |
expect(getByTestId('three')).to.have.attribute('aria-selected', 'true'); | ||
expect(getByTestId('four')).to.have.attribute('aria-selected', 'false'); | ||
expect(getByTestId('five')).to.have.attribute('aria-selected', 'false'); | ||
expect(container.querySelectorAll('[aria-selected=true]').length).to.equal(3); | ||
expect(container.querySelectorAll('[aria-selected=true]')).to.have.length(3); | ||
}); | ||
|
||
specify('keyboard arrow does not select when selectionDisabled', () => { | ||
|
@@ -1037,11 +1039,11 @@ describe('<TreeItem />', () => { | |
fireEvent.keyDown(getByTestId('three'), { key: 'ArrowDown', shiftKey: true }); | ||
|
||
expect(getByTestId('four')).toHaveFocus(); | ||
expect(container.querySelectorAll('[aria-selected=true]').length).to.equal(0); | ||
expect(container.querySelectorAll('[aria-selected=true]')).to.have.length(0); | ||
|
||
fireEvent.keyDown(getByTestId('four'), { key: 'ArrowUp', shiftKey: true }); | ||
|
||
expect(container.querySelectorAll('[aria-selected=true]').length).to.equal(0); | ||
expect(container.querySelectorAll('[aria-selected=true]')).to.have.length(0); | ||
}); | ||
|
||
specify('keyboard arrow merge', () => { | ||
|
@@ -1067,12 +1069,12 @@ describe('<TreeItem />', () => { | |
fireEvent.keyDown(getByTestId('four'), { key: 'ArrowUp', shiftKey: true }); | ||
fireEvent.keyDown(getByTestId('three'), { key: 'ArrowUp', shiftKey: true }); | ||
|
||
expect(container.querySelectorAll('[aria-selected=true]').length).to.equal(5); | ||
expect(container.querySelectorAll('[aria-selected=true]')).to.have.length(5); | ||
|
||
fireEvent.keyDown(getByTestId('two'), { key: 'ArrowDown', shiftKey: true }); | ||
fireEvent.keyDown(getByTestId('three'), { key: 'ArrowDown', shiftKey: true }); | ||
|
||
expect(container.querySelectorAll('[aria-selected=true]').length).to.equal(3); | ||
expect(container.querySelectorAll('[aria-selected=true]')).to.have.length(3); | ||
}); | ||
|
||
specify('keyboard space', () => { | ||
|
@@ -1179,11 +1181,11 @@ describe('<TreeItem />', () => { | |
getByTestId('five').focus(); | ||
fireEvent.keyDown(getByTestId('five'), { key: 'End', shiftKey: true, ctrlKey: true }); | ||
|
||
expect(container.querySelectorAll('[aria-selected=true]').length).to.equal(0); | ||
expect(container.querySelectorAll('[aria-selected=true]')).to.have.length(0); | ||
|
||
fireEvent.keyDown(getByTestId('nine'), { key: 'Home', shiftKey: true, ctrlKey: true }); | ||
|
||
expect(container.querySelectorAll('[aria-selected=true]').length).to.equal(0); | ||
expect(container.querySelectorAll('[aria-selected=true]')).to.have.length(0); | ||
}); | ||
|
||
specify('mouse', () => { | ||
|
@@ -1237,7 +1239,7 @@ describe('<TreeItem />', () => { | |
|
||
fireEvent.click(getByText('five')); | ||
fireEvent.click(getByText('nine'), { shiftKey: true }); | ||
expect(container.querySelectorAll('[aria-selected=true]').length).to.equal(0); | ||
expect(container.querySelectorAll('[aria-selected=true]')).to.have.length(0); | ||
}); | ||
}); | ||
|
||
|
@@ -1318,7 +1320,7 @@ describe('<TreeItem />', () => { | |
getByTestId('one').focus(); | ||
fireEvent.keyDown(getByTestId('one'), { key: 'a', ctrlKey: true }); | ||
|
||
expect(container.querySelectorAll('[aria-selected=true]').length).to.equal(5); | ||
expect(container.querySelectorAll('[aria-selected=true]')).to.have.length(5); | ||
}); | ||
|
||
specify('ctrl + a does not select all when disableSelection', () => { | ||
|
@@ -1335,7 +1337,7 @@ describe('<TreeItem />', () => { | |
getByTestId('one').focus(); | ||
fireEvent.keyDown(getByTestId('one'), { key: 'a', ctrlKey: true }); | ||
|
||
expect(container.querySelectorAll('[aria-selected=true]').length).to.equal(0); | ||
expect(container.querySelectorAll('[aria-selected=true]')).to.have.length(0); | ||
}); | ||
}); | ||
}); | ||
|
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.
Using descendants allows us to get tree items in the correct order without reading children' props meaning we aren't so fragile and allow wrapped TreeItems.