-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add ErrorLabel component * Version bump: 4.1.0 Co-authored-by: Versionator <[email protected]>
- Loading branch information
Showing
6 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
import classnames from 'classnames' | ||
|
||
/** | ||
* | ||
* A label for displaying error message. | ||
* | ||
* @name ErrorLabel | ||
* @type Function | ||
* @param {String} children - A message to display | ||
* | ||
* @example | ||
* | ||
* function MyView () { | ||
* const [errorMessage, setErrorMessage] = useState() | ||
* return ( | ||
* <div> | ||
* <button onClick={ () => doSomething().catch(setErrorMessage) }> | ||
* Do something | ||
* </button> | ||
* { | ||
* errorMessage && <ErrorLabel>{ errorMessage }</ErrorLabel> | ||
* } | ||
* </div> | ||
* ) | ||
* } | ||
* | ||
*/ | ||
|
||
const propTypes = { | ||
children: PropTypes.node, | ||
className: PropTypes.string, | ||
} | ||
|
||
const defaultProps = { | ||
className: '', | ||
} | ||
|
||
function ErrorLabel({ children, className, ...rest }) { | ||
return ( | ||
<span className={classnames('error-message', className)} {...rest}> | ||
{children} | ||
</span> | ||
) | ||
} | ||
|
||
ErrorLabel.propTypes = propTypes | ||
ErrorLabel.defaultProps = defaultProps | ||
|
||
export default ErrorLabel |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export ErrorLabel from './error-label' | ||
export InputError from './input-error' | ||
export InputLabel from './input-label' | ||
export LabeledField from './labeled-field' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import React from 'react' | ||
import { storiesOf } from '@storybook/react' | ||
import { ErrorLabel } from 'src' | ||
|
||
storiesOf('ErrorLabel', module).add('with a single error', () => ( | ||
<ErrorLabel>An error occurred</ErrorLabel> | ||
)) |
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,15 @@ | ||
import React from 'react' | ||
import { shallow } from 'enzyme' | ||
import { ErrorLabel } from '../../../src' | ||
|
||
test('passes class to span element correctly', () => { | ||
const wrapper = shallow(<ErrorLabel className="small">Error!</ErrorLabel>) | ||
expect(wrapper.hasClass('error-message')).toBe(true) | ||
expect(wrapper.hasClass('small')).toBe(true) | ||
}) | ||
|
||
test('passes extra props to span element', () => { | ||
const onClick = () => 'More info' | ||
const wrapper = shallow(<ErrorLabel onClick={onClick}>Error!</ErrorLabel>) | ||
expect(wrapper.props().onClick).toBe(onClick) | ||
}) |