forked from patternfly/patternfly-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(LoadingState): Add LoadingState component (patternfly#881) (patt…
- Loading branch information
Showing
7 changed files
with
173 additions
and
32 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
packages/patternfly-3/patternfly-react/src/components/LoadingState/LoadingState.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import React, { Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import classNames from 'classnames'; | ||
import { Spinner } from '../Spinner'; | ||
|
||
class LoadingState extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
render: false | ||
}; | ||
} | ||
|
||
componentDidMount() { | ||
this.onTimeout = setTimeout(() => { | ||
this.setState({ render: true }); | ||
}, this.props.timeout); | ||
} | ||
|
||
componentWillUnmout() { | ||
clearTimeout(this.onTimeout); | ||
} | ||
|
||
render() { | ||
const { loading, loadingText, children, size, additionalClasses } = this.props; | ||
|
||
const spinner = ( | ||
<div className={classNames('loading-state-pf', `loading-state-pf-${size}`, additionalClasses)}> | ||
<Spinner loading={loading} size={size} /> | ||
{loadingText} | ||
</div> | ||
); | ||
|
||
if (loading) { | ||
return this.state.render ? spinner : null; | ||
} | ||
return children; | ||
} | ||
} | ||
|
||
LoadingState.propTypes = { | ||
/** determines whether to show spinner or children */ | ||
loading: PropTypes.bool, | ||
/** text to show with spinner */ | ||
loadingText: PropTypes.string, | ||
/** children nodes */ | ||
children: PropTypes.node, | ||
/** delay in showing the children */ | ||
timeout: PropTypes.number, | ||
/** size of the spinner */ | ||
size: PropTypes.oneOf(['lg', 'md', 'sm', 'xs']), | ||
/** additional css classes for LoadingState */ | ||
additionalClasses: PropTypes.string | ||
}; | ||
|
||
LoadingState.defaultProps = { | ||
loading: false, | ||
loadingText: 'Loading', | ||
children: null, | ||
timeout: 300, | ||
size: 'lg', | ||
additionalClasses: '' | ||
}; | ||
|
||
export default LoadingState; |
35 changes: 35 additions & 0 deletions
35
packages/patternfly-3/patternfly-react/src/components/LoadingState/LoadingState.stories.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React from 'react'; | ||
import { storiesOf } from '@storybook/react'; | ||
import { withKnobs, boolean, select } from '@storybook/addon-knobs'; | ||
import { withInfo } from '@storybook/addon-info'; | ||
import { inlineTemplate } from 'storybook/decorators/storyTemplates'; | ||
import { storybookPackageName, DOCUMENTATION_URL, STORYBOOK_CATEGORY } from 'storybook/constants/siteConstants'; | ||
|
||
import { LoadingState } from './index'; | ||
|
||
import { name } from '../../../package.json'; | ||
|
||
const stories = storiesOf(`${storybookPackageName(name)}/${STORYBOOK_CATEGORY.COMMUNICATION}/Loading State`, module); | ||
|
||
stories.addDecorator(withKnobs); | ||
|
||
stories.add( | ||
'Loading State', | ||
withInfo('Loading State shows centered spinner when loading')(() => { | ||
const loading = boolean('Loading', true); | ||
|
||
const size = select('Size', ['lg', 'md', 'sm', 'xs'], 'lg'); | ||
|
||
const story = ( | ||
<LoadingState loading={loading} size={size}> | ||
<div>Look at me! I am loaded content!</div> | ||
</LoadingState> | ||
); | ||
|
||
return inlineTemplate({ | ||
title: 'Loading State', | ||
documentationLink: `${DOCUMENTATION_URL.PATTERNFLY_ORG_COMMUNICATION}loading-state/`, | ||
story | ||
}); | ||
}) | ||
); |
41 changes: 41 additions & 0 deletions
41
packages/patternfly-3/patternfly-react/src/components/LoadingState/LoadingState.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
import toJson from 'enzyme-to-json'; | ||
|
||
import { LoadingState } from './index'; | ||
|
||
jest.useFakeTimers(); | ||
|
||
test('Loading State renders properly while loading', () => { | ||
const component = shallow( | ||
<LoadingState loading loadingText="Loading"> | ||
<p>Loading Complete</p> | ||
</LoadingState> | ||
); | ||
jest.runAllTimers(); | ||
component.update(); | ||
expect(component.state('render')).toEqual(true); | ||
expect(toJson(component.render())).toMatchSnapshot(); | ||
}); | ||
|
||
test('Loading State renders properly while not loading', () => { | ||
const component = shallow( | ||
<LoadingState loading={false} loadingText="Loading"> | ||
<p>Loading Complete</p> | ||
</LoadingState> | ||
); | ||
jest.runAllTimers(); | ||
component.update(); | ||
expect(toJson(component.render())).toMatchSnapshot(); | ||
}); | ||
|
||
test('Loadin State renders in xs size', () => { | ||
const component = shallow( | ||
<LoadingState loading loadingText="Loading" size="xs"> | ||
<p>Loading Complete</p> | ||
</LoadingState> | ||
); | ||
jest.runAllTimers(); | ||
component.update(); | ||
expect(toJson(component.render())).toMatchSnapshot(); | ||
}); |
29 changes: 29 additions & 0 deletions
29
...ly-3/patternfly-react/src/components/LoadingState/__snapshots__/LoadingState.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Loadin State renders in xs size 1`] = ` | ||
<div | ||
class="loading-state-pf loading-state-pf-xs" | ||
> | ||
<div | ||
class="spinner spinner-xs" | ||
/> | ||
Loading | ||
</div> | ||
`; | ||
|
||
exports[`Loading State renders properly while loading 1`] = ` | ||
<div | ||
class="loading-state-pf loading-state-pf-lg" | ||
> | ||
<div | ||
class="spinner spinner-lg" | ||
/> | ||
Loading | ||
</div> | ||
`; | ||
|
||
exports[`Loading State renders properly while not loading 1`] = ` | ||
<p> | ||
Loading Complete | ||
</p> | ||
`; |
1 change: 1 addition & 0 deletions
1
packages/patternfly-3/patternfly-react/src/components/LoadingState/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as LoadingState } from './LoadingState'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters