Skip to content

Commit e88af33

Browse files
Jason MyersJason-AM
authored andcommitted
Keep children in layout when spinner is loading. Tests updated.
1 parent e2f9fc8 commit e88af33

File tree

2 files changed

+88
-29
lines changed

2 files changed

+88
-29
lines changed

src/components/Spinner.js

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,35 +59,56 @@ const Spinner = props => {
5959
justifyContent: 'center',
6060
alignItems: 'center',
6161
zIndex: 99,
62+
visibility: 'visible',
6263
...fullscreen_style
6364
};
6465

65-
if (!children || (loading_state && loading_state.is_loading)) {
66-
if (fullscreen) {
67-
return (
68-
<div className={fullscreenClassName} style={fullscreenStyle}>
66+
const coveringStyle = {
67+
visibility: 'visible'
68+
};
69+
70+
const hiddenStyle = {
71+
visibility: 'hidden',
72+
position: 'relative',
73+
display: 'inline-block'
74+
};
75+
76+
// Defaulted styles above to the situation where spinner has no children
77+
// now include properties if spinner has children
78+
if (children) {
79+
// include covering style additions
80+
coveringStyle.position = 'absolute';
81+
coveringStyle.top = 0;
82+
coveringStyle.height = '100%';
83+
coveringStyle.width = '100%';
84+
coveringStyle.display = 'flex';
85+
coveringStyle.justifyContent = 'center';
86+
coveringStyle.alignItems = 'center';
87+
88+
// remove hidden style additions
89+
delete hiddenStyle.display;
90+
}
91+
92+
const showSpinner = !children || (loading_state && loading_state.is_loading);
93+
94+
return (
95+
<div style={showSpinner ? hiddenStyle : {}}>
96+
{children}
97+
{showSpinner && (
98+
<div
99+
style={fullscreen ? fullscreenStyle : coveringStyle}
100+
className={fullscreen && fullscreenClassName}
101+
>
69102
<RSSpinner
70103
color={isSpinnerColor ? color : null}
71104
style={{color: !isSpinnerColor && color, ...spinnerStyle}}
72105
className={spinnerClassName}
73106
{...omit(['setProps'], otherProps)}
74107
/>
75108
</div>
76-
);
77-
}
78-
return (
79-
<RSSpinner
80-
color={isSpinnerColor ? color : null}
81-
style={{color: !isSpinnerColor && color, ...spinnerStyle}}
82-
className={spinnerClassName}
83-
{...omit(['setProps'], otherProps)}
84-
/>
85-
);
86-
}
87-
if (type(children) !== 'Object' || type(children) !== 'Function') {
88-
return <Fragment>{children}</Fragment>;
89-
}
90-
return children;
109+
)}
110+
</div>
111+
);
91112
};
92113

93114
Spinner._dashprivate_isLoadingComponent = true;

src/components/__tests__/Spinner.test.js

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,74 @@ describe('Spinner', () => {
1212
});
1313

1414
test("renders its content if object isn't loading", () => {
15-
const {container: spinner, rerender} = render(<Spinner>Some spinner content</Spinner>);
15+
const {container: All, rerender} = render(
16+
<Spinner>Some spinner content</Spinner>
17+
);
1618

17-
expect(spinner).toHaveTextContent('Some spinner content');
19+
expect(All).toHaveTextContent('Some spinner content');
1820

1921
rerender(
2022
<Spinner loading_state={{is_loading: true}}>Some spinner content</Spinner>
2123
);
2224

23-
expect(spinner).not.toHaveTextContent('Some spinner content');
25+
const overAll = All.firstChild;
26+
const spinner = overAll.lastChild;
27+
28+
expect(overAll).toHaveTextContent('Some spinner content');
2429
expect(spinner.firstChild).toHaveClass('spinner-border');
2530
});
2631

2732
test('applies additional CSS classes when props are set', () => {
2833
// grow spinner
29-
const {container: {firstChild: spinnerGrow}} = render(<Spinner type="grow" />);
34+
const {
35+
container: {firstChild: overAll}
36+
} = render(<Spinner type="grow" />);
37+
const spinner = overAll.lastChild;
3038

31-
expect(spinnerGrow).toHaveClass('spinner-grow');
39+
expect(spinner.firstChild).toHaveClass('spinner-grow');
3240

3341
// spinner sizes
34-
const {container: {firstChild: spinnerSm}} = render(<Spinner size="sm" />);
35-
const {container: {firstChild: spinnerLg}} = render(<Spinner size="lg" />);
42+
const {
43+
container: {
44+
firstChild: {
45+
firstChild: {firstChild: spinnerSm}
46+
}
47+
}
48+
} = render(<Spinner size="sm" />);
49+
const {
50+
container: {
51+
firstChild: {
52+
firstChild: {firstChild: spinnerLg}
53+
}
54+
}
55+
} = render(<Spinner size="lg" />);
3656

3757
expect(spinnerSm).toHaveClass('spinner-border-sm');
3858
expect(spinnerLg).toHaveClass('spinner-border-lg');
3959
});
4060

4161
test('applies contextual colors with "color" prop', () => {
42-
const {container: {firstChild: spinnerPrimary}} = render(<Spinner color="primary" />);
43-
const {container: {firstChild: spinnerSuccess}} = render(<Spinner color="success" />);
44-
const {container: {firstChild: spinnerDark}} = render(<Spinner color="dark" />);
62+
const {
63+
container: {
64+
firstChild: {
65+
firstChild: {firstChild: spinnerPrimary}
66+
}
67+
}
68+
} = render(<Spinner color="primary" />);
69+
const {
70+
container: {
71+
firstChild: {
72+
firstChild: {firstChild: spinnerSuccess}
73+
}
74+
}
75+
} = render(<Spinner color="success" />);
76+
const {
77+
container: {
78+
firstChild: {
79+
firstChild: {firstChild: spinnerDark}
80+
}
81+
}
82+
} = render(<Spinner color="dark" />);
4583

4684
expect(spinnerPrimary).toHaveClass('text-primary');
4785
expect(spinnerSuccess).toHaveClass('text-success');

0 commit comments

Comments
 (0)