Skip to content

Commit ed03cd6

Browse files
committed
Preserve button focus during loading state
This change addresses an issue where the tree collapses when toggling to fetch child tree nodes in field lookups in other applications using this component.
1 parent 829e548 commit ed03cd6

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/scripts/TreeNode.tsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const TreeNodeItem: FC<TreeNodeProps & { icon?: string }> = (props) => {
4747
selected,
4848
leaf,
4949
opened,
50+
level = 0,
5051
children,
5152
itemRender: ItemRender,
5253
onClick,
@@ -58,28 +59,38 @@ const TreeNodeItem: FC<TreeNodeProps & { icon?: string }> = (props) => {
5859
'slds-is-open': opened,
5960
'slds-is-selected': selected,
6061
});
62+
const spinnerClassNames = classnames(
63+
'react-slds-spinner',
64+
'slds-m-right_x-small'
65+
);
66+
const loadingPositionStyle = {
67+
left: `${level}rem`,
68+
};
6169
return (
6270
<div
6371
className={itmClassNames}
6472
onClick={onClick}
6573
style={{ position: 'relative' }}
6674
{...rprops}
6775
>
68-
{loading ? (
76+
{loading && (
6977
<Spinner
78+
className={spinnerClassNames}
7079
container={false}
7180
size='small'
72-
className='slds-m-right_x-small'
73-
style={{ position: 'static', marginTop: 14, marginLeft: -2 }}
81+
style={loadingPositionStyle}
7482
/>
75-
) : !leaf ? (
83+
)}
84+
{!leaf ? (
7685
<Button
7786
className='slds-m-right_small'
7887
aria-controls=''
7988
type='icon-bare'
8089
icon={icon}
8190
iconSize='small'
8291
onClick={onToggle}
92+
// Prevent focus loss during loading by keeping the toggle button in the DOM with opacity set to 0.
93+
style={loading ? { opacity: 0, pointerEvents: 'none' } : undefined}
8394
/>
8495
) : null}
8596
<a
@@ -150,7 +161,7 @@ export const TreeNode: FC<TreeNodeProps> = (props) => {
150161
>
151162
<TreeNodeItem
152163
{...rprops}
153-
{...{ leaf, opened, children, onClick, onLabelClick, onToggle }}
164+
{...{ leaf, opened, level, children, onClick, onLabelClick, onToggle }}
154165
/>
155166
{!leaf ? (
156167
<ul className={grpClassNames} role='group'>

0 commit comments

Comments
 (0)