Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit 600a09f

Browse files
committed
more consistent structure for Loading component
1 parent aa4fce7 commit 600a09f

File tree

3 files changed

+42
-42
lines changed

3 files changed

+42
-42
lines changed

src/components/Loading.react.js

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import DefaultSpinner from '../fragments/Loading/spinners/DefaultSpinner.jsx';
55
import CubeSpinner from '../fragments/Loading/spinners/CubeSpinner.jsx';
66
import CircleSpinner from '../fragments/Loading/spinners/CircleSpinner.jsx';
77
import DotSpinner from '../fragments/Loading/spinners/DotSpinner.jsx';
8-
import {type} from 'ramda';
98

109
function getSpinner(spinnerType) {
1110
switch (spinnerType) {
@@ -22,6 +21,19 @@ function getSpinner(spinnerType) {
2221
}
2322
}
2423

24+
const hiddenContainer = {visibility: 'hidden', position: 'relative'};
25+
26+
const coveringSpinner = {
27+
visibility: 'visible',
28+
position: 'absolute',
29+
top: '0',
30+
height: '100%',
31+
width: '100%',
32+
display: 'flex',
33+
justifyContent: 'center',
34+
alignItems: 'center',
35+
};
36+
2537
/**
2638
* A Loading component that wraps any other component and displays a spinner until the wrapped component has rendered.
2739
*/
@@ -37,40 +49,26 @@ export default class Loading extends Component {
3749
type: spinnerType,
3850
} = this.props;
3951

40-
if (loading_state && loading_state.is_loading) {
41-
const Spinner = getSpinner(spinnerType);
42-
return (
43-
<div style={{visibility: 'hidden', position: 'relative'}}>
44-
{this.props.children}
45-
<Spinner
46-
className={className}
47-
style={{
48-
visibility: 'visible',
49-
position: 'absolute',
50-
top: '0',
51-
height: '100%',
52-
width: '100%',
53-
display: 'flex',
54-
justifyContent: 'center',
55-
alignItems: 'center',
56-
...style,
57-
}}
58-
status={loading_state}
59-
color={color}
60-
debug={debug}
61-
fullscreen={fullscreen}
62-
/>
52+
const isLoading = loading_state && loading_state.is_loading;
53+
const Spinner = isLoading && getSpinner(spinnerType);
54+
55+
return (
56+
<div style={isLoading ? hiddenContainer : {}}>
57+
{this.props.children}
58+
<div style={isLoading ? coveringSpinner : {}}>
59+
{isLoading && (
60+
<Spinner
61+
className={className}
62+
style={style}
63+
status={loading_state}
64+
color={color}
65+
debug={debug}
66+
fullscreen={fullscreen}
67+
/>
68+
)}
6369
</div>
64-
);
65-
}
66-
67-
if (
68-
type(this.props.children) !== 'Object' ||
69-
type(this.props.children) !== 'Function'
70-
) {
71-
return <div className={className}>{this.props.children}</div>;
72-
}
73-
return this.props.children;
70+
</div>
71+
);
7472
}
7573
}
7674

@@ -98,27 +96,29 @@ Loading.propTypes = {
9896
]),
9997

10098
/**
101-
* Property that determines which spinner to show - one of 'graph', 'cube', 'circle', 'dot', or 'default'.
99+
* Property that determines which spinner to show
100+
* one of 'graph', 'cube', 'circle', 'dot', or 'default'.
102101
*/
103102
type: PropTypes.oneOf(['graph', 'cube', 'circle', 'dot', 'default']),
104103

105104
/**
106-
* Boolean that determines if the loading spinner will be displayed full-screen or not
105+
* Boolean that makes the spinner display full-screen
107106
*/
108107
fullscreen: PropTypes.bool,
109108

110109
/**
111-
* Boolean that determines if the loading spinner will display the status.prop_name and component_name
110+
* If true, the spinner will display the component_name and prop_name
111+
* while loading
112112
*/
113113
debug: PropTypes.bool,
114114

115115
/**
116-
* Additional CSS class for the root DOM node
116+
* Additional CSS class for the spinner root DOM node
117117
*/
118118
className: PropTypes.string,
119119

120120
/**
121-
* Additional CSS styling for the root DOM node
121+
* Additional CSS styling for the spinner root DOM node
122122
*/
123123
style: PropTypes.object,
124124

tests/unit/Loading.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ test('Loading renders without loading_state', () => {
2828
</Loading>
2929
);
3030

31-
expect(loading.html()).toEqual('<div>Loading is done!</div>');
31+
expect(loading.html()).toEqual('<div>Loading is done!</div><div></div>');
3232
});
3333
test('Loading renders without loading_state.is_loading', () => {
3434
const statusMock = {
@@ -41,7 +41,7 @@ test('Loading renders without loading_state.is_loading', () => {
4141
</Loading>
4242
);
4343

44-
expect(loading.html()).toEqual('<div>Loading is done!</div>');
44+
expect(loading.html()).toEqual('<div>Loading is done!</div><div></div>');
4545
});
4646
test('Loading renders without prop_name', () => {
4747
const statusMock = {

tests/unit/__snapshots__/Loading.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Loading checks all it's children for a loading_state: Loading spinner for children 1`] = `"<div>Child 1</div><div>Child 2</div><div loading_state=\\"[object Object]\\">Child 3</div>"`;
3+
exports[`Loading checks all it's children for a loading_state: Loading spinner for children 1`] = `"<div>Child 1</div><div>Child 2</div><div loading_state=\\"[object Object]\\">Child 3</div><div></div>"`;
44
55
exports[`Loading renders with multiple children: Loading with is_loading=true 1`] = `
66
"<div class=\\"dash-spinner dash-default-spinner\\"><div class=\\"dash-default-spinner-rect1\\"></div><div class=\\"dash-default-spinner-rect2\\"></div><div class=\\"dash-default-spinner-rect3\\"></div><div class=\\"dash-default-spinner-rect4\\"></div><div class=\\"dash-default-spinner-rect5\\"></div></div><style>

0 commit comments

Comments
 (0)