-
Notifications
You must be signed in to change notification settings - Fork 376
Fixes #881 - Add LoadingState component #886
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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 hidden or 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 | ||
| }); | ||
| }) | ||
| ); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be nice to add a knob to set the size |
||
41 changes: 41 additions & 0 deletions
41
packages/patternfly-3/patternfly-react/src/components/LoadingState/LoadingState.test.js
This file contains hidden or 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(); | ||
| }); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add tests for sizes. |
||
|
|
||
| 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the propTypes need comments to add descriptions in storybook.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should take a size for the entire loading state (rather than
spinnerSize) and addloading-state-pf-[lg, sm, xs]to the outer div and set the spinner size appropriately