-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathUSDStage.tsx
138 lines (127 loc) · 5 KB
/
USDStage.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: LicenseRef-NvidiaProprietary
*
* NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
* property and proprietary rights in and to this material, related
* documentation and any modifications thereto. Any use, reproduction,
* disclosure or distribution of this material and related documentation
* without an express license agreement from NVIDIA CORPORATION or
* its affiliates is strictly prohibited.
*/
import React from "react";
import './App.css';
import './USDStage.css';
interface USDPrimType {
name?: string;
path: string;
children?: USDPrimType[];
}
interface USDStageProps {
width: number;
usdPrims: USDPrimType[];
selectedUSDPrims: Set<USDPrimType>;
onSelectUSDPrims: (selectedUsdPrims: Set<USDPrimType>) => void;
fillUSDPrim: (usdPrim: USDPrimType) => void;
onReset: () => void;
}
export default class USDStage extends React.Component<USDStageProps, { expandedIds: Set<string> }> {
constructor(props: USDStageProps) {
super(props);
this.state = { expandedIds: new Set<string>() };
}
/**
* @function resetExpandedIds
*
* Public function for resetting the expanded state of the list.
*/
public resetExpandedIds (): void {
this.setState({ expandedIds: new Set<string>() });
}
/**
* @function _toggleExpand
*
* Toggle the expanded states in the list.
*/
private _toggleExpand(obj: USDPrimType, event: React.MouseEvent<HTMLSpanElement, MouseEvent>): void {
event.stopPropagation(); // Prevents the click from bubbling up to parent elements
this.props.fillUSDPrim(obj);
this.setState(prevState => {
const newExpandedIds = new Set(prevState.expandedIds); // Create a copy of the current Set
if (newExpandedIds.has(obj.path)) {
newExpandedIds.delete(obj.path); // Remove id if it's already expanded
} else {
newExpandedIds.add(obj.path); // Add id if it's not expanded
}
return { expandedIds: newExpandedIds };
});
}
/**
* @function _handleListClick
*
* Change state when list selection changes.
*/
private _handleListClick(obj: USDPrimType, event: React.MouseEvent<HTMLDivElement, MouseEvent>): void {
event.stopPropagation();
const newSelectedItems = new Set(this.props.selectedUSDPrims);
if (newSelectedItems.has(obj)) {
newSelectedItems.delete(obj); // Deselect if already selected
} else {
newSelectedItems.add(obj); // Add to selection if not already selected
}
this.props.onSelectUSDPrims(newSelectedItems);
}
/**
* @function _renderList
*
* Render the list.
*/
private _renderList(usdPrims: USDPrimType[]): JSX.Element[] | undefined {
if (usdPrims === null || !Array.isArray(usdPrims)) {
return;
}
return usdPrims.map((obj, index) => {
const isLeaf = !obj.children || obj.children.length === 0;
const isOpen = this.state.expandedIds.has(obj.path);
const isSelected = this.props.selectedUSDPrims.has(obj);
const listItemClass = `list-item ${isLeaf ? 'leaf' : 'parent'} ${isOpen ? 'open' : ''} ${isSelected ? 'selected' : ''}`;
const itemContentClass = `item-content ${isLeaf ? 'leaf' : 'parent'} ${isOpen ? 'open' : ''} ${isSelected ? 'selected' : ''}`;
const expandToggleClass = `expand-toggle ${isSelected ? 'selected' : 'deselected'}`;
return (
<li key={obj.name || index} className={listItemClass}>
<div className={itemContentClass} onClick={(e) => this._handleListClick(obj, e)}
tabIndex={0}
>
{!isLeaf && (
<span onClick={(e) => this._toggleExpand(obj, e)} className={expandToggleClass}>
{isOpen ? '▼' : '▶'}
</span>
)}
{obj.name}
</div>
{isOpen && !isLeaf && obj.children && (
<ul className="nested-list">
{this._renderList(obj.children)}
</ul>
)}
</li>
);
});
}
_onReset = () => {
this.props.onReset();
};
render() {
return (
<div className="usdStageContainer" style={{ width: this.props.width }}>
<div className="usdStageHeader">
{'USD Stage'}
<button className="nvidia-button" onClick={this._onReset}>Reset</button>
</div>
<ul className="list-container">
{this._renderList(this.props.usdPrims)}
</ul>
</div>
);
}
}