mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Throw if root node is collapsed
This commit is contained in:
@@ -264,3 +264,8 @@ exports[`Store collapseNodesByDefault:true should support mount and update opera
|
||||
`;
|
||||
|
||||
exports[`Store collapseNodesByDefault:true should support mount and update operations: 3: unmount 1`] = ``;
|
||||
|
||||
exports[`Store should not allow a root node to be collapsed: 1: mount 1`] = `
|
||||
[root]
|
||||
<Component>
|
||||
`;
|
||||
|
||||
@@ -22,7 +22,20 @@ describe('Store', () => {
|
||||
});
|
||||
|
||||
it('should not allow a root node to be collapsed', () => {
|
||||
// TODO
|
||||
const Component = () => <div>Hi</div>;
|
||||
|
||||
act(() =>
|
||||
ReactDOM.render(<Component count={4} />, document.createElement('div'))
|
||||
);
|
||||
expect(store).toMatchSnapshot('1: mount');
|
||||
|
||||
expect(store.roots).toHaveLength(1);
|
||||
|
||||
const rootID = store.roots[0];
|
||||
|
||||
expect(() => store.toggleIsCollapsed(rootID, true)).toThrow(
|
||||
'Root nodes cannot be collapsed'
|
||||
);
|
||||
});
|
||||
|
||||
describe('collapseNodesByDefault:false', () => {
|
||||
|
||||
@@ -452,6 +452,10 @@ export default class Store extends EventEmitter {
|
||||
toggleIsCollapsed(id: number, isCollapsed: boolean): void {
|
||||
const element = this.getElementByID(id);
|
||||
if (element !== null) {
|
||||
if (element.type === ElementTypeRoot) {
|
||||
throw Error('Root nodes cannot be collapsed');
|
||||
}
|
||||
|
||||
const oldWeight = element.isCollapsed ? 1 : element.weight;
|
||||
element.isCollapsed = isCollapsed;
|
||||
const newWeight = element.isCollapsed ? 1 : element.weight;
|
||||
@@ -576,7 +580,7 @@ export default class Store extends EventEmitter {
|
||||
depth: -1,
|
||||
displayName: null,
|
||||
id,
|
||||
isCollapsed: false, // Never collapse roots
|
||||
isCollapsed: false, // Never collapse roots; it would hide the entire tree.
|
||||
key: null,
|
||||
ownerID: 0,
|
||||
parentID: 0,
|
||||
|
||||
Reference in New Issue
Block a user