-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from fyndiq/component-alert
New Component: Alert
- Loading branch information
Showing
10 changed files
with
489 additions
and
0 deletions.
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,41 @@ | ||
# fyndiq-component-alert | ||
|
||
An alert component for Fyndiq | ||
|
||
# Installation | ||
|
||
The component can be installed through NPM: | ||
|
||
``` bash | ||
npm i -S fyndiq-component-alert | ||
``` | ||
|
||
# Usage | ||
|
||
``` js | ||
import React from 'react' | ||
import Alert from 'fyndiq-component-alert' | ||
|
||
// Normal usage | ||
<Alert> | ||
Here is my alert | ||
</Alert> | ||
|
||
// Red alert | ||
<Alert type="bad"> | ||
You have some issues | ||
</Alert> | ||
|
||
// Alert than can't be closed | ||
<Alert unclosable> | ||
You cannot close me | ||
</Alert> | ||
``` | ||
|
||
# API | ||
|
||
The component `Alert` has the following customizable props: | ||
|
||
- **type**: (String) One of `info`, `good`, `bad`, `warning`. Changes the color style of the alert. (Default: `info`) | ||
- **unclosable**: (Boolean) if true, will not display a closing cross | ||
- **onClose**: (Function) Handler called when the alert is closed |
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,17 @@ | ||
{ | ||
"name": "fyndiq-component-alert", | ||
"version": "0.0.5", | ||
"author": "Thibaut REMY <thibaut.remy@fyndiq.com>", | ||
"main": "lib/index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/fyndiq/fyndiq-ui.git" | ||
}, | ||
"homepage": "http://developers.fyndiq.com/fyndiq-ui/?selectedKind=Alert&selectedStory=default", | ||
"dependencies": { | ||
"fyndiq-styles-colors": "^0.0.5" | ||
}, | ||
"peerDependencies": { | ||
"react": "^0.14.0 || ^15.0.0" | ||
} | ||
} |
204 changes: 204 additions & 0 deletions
204
packages/fyndiq-component-alert/src/__snapshots__/index.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,204 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`fyndiq-component-alert should be rendered without props 1`] = ` | ||
<div | ||
className=" | ||
alertWrapper | ||
" | ||
> | ||
<div | ||
className=" | ||
alert | ||
type-info | ||
" | ||
> | ||
<span | ||
className="text" | ||
> | ||
Notification | ||
</span> | ||
<div | ||
className="close" | ||
onClick={[Function]} | ||
> | ||
× | ||
</div> | ||
</div> | ||
</div> | ||
`; | ||
|
||
exports[`fyndiq-component-alert should be self-closable 1`] = ` | ||
<Alert | ||
onClose={[Function]} | ||
type="info" | ||
unclosable={false} | ||
> | ||
<div | ||
className=" | ||
alertWrapper | ||
hidden | ||
" | ||
> | ||
<div | ||
className=" | ||
alert | ||
type-info | ||
" | ||
> | ||
<span | ||
className="text" | ||
/> | ||
<div | ||
className="close" | ||
onClick={[Function]} | ||
> | ||
× | ||
</div> | ||
</div> | ||
</div> | ||
</Alert> | ||
`; | ||
|
||
exports[`fyndiq-component-alert should be self-closable 2`] = ` | ||
<Alert | ||
onClose={[Function]} | ||
type="info" | ||
unclosable={false} | ||
> | ||
<div | ||
className=" | ||
alertWrapper | ||
hidden | ||
removed | ||
" | ||
> | ||
<div | ||
className=" | ||
alert | ||
type-info | ||
" | ||
> | ||
<span | ||
className="text" | ||
/> | ||
<div | ||
className="close" | ||
onClick={[Function]} | ||
> | ||
× | ||
</div> | ||
</div> | ||
</div> | ||
</Alert> | ||
`; | ||
|
||
exports[`fyndiq-component-alert should have a type prop 1`] = ` | ||
<div | ||
className=" | ||
alertWrapper | ||
" | ||
> | ||
<div | ||
className=" | ||
alert | ||
type-warning | ||
" | ||
> | ||
<span | ||
className="text" | ||
> | ||
Warning | ||
</span> | ||
<div | ||
className="close" | ||
onClick={[Function]} | ||
> | ||
× | ||
</div> | ||
</div> | ||
</div> | ||
`; | ||
|
||
exports[`fyndiq-component-alert should have a type prop 2`] = ` | ||
<div | ||
className=" | ||
alertWrapper | ||
" | ||
> | ||
<div | ||
className=" | ||
alert | ||
type-good | ||
" | ||
> | ||
<span | ||
className="text" | ||
> | ||
Good | ||
</span> | ||
<div | ||
className="close" | ||
onClick={[Function]} | ||
> | ||
× | ||
</div> | ||
</div> | ||
</div> | ||
`; | ||
|
||
exports[`fyndiq-component-alert should have a type prop 3`] = ` | ||
<div | ||
className=" | ||
alertWrapper | ||
" | ||
> | ||
<div | ||
className=" | ||
alert | ||
type-bad | ||
" | ||
> | ||
<span | ||
className="text" | ||
> | ||
Bad | ||
</span> | ||
<div | ||
className="close" | ||
onClick={[Function]} | ||
> | ||
× | ||
</div> | ||
</div> | ||
</div> | ||
`; | ||
|
||
exports[`fyndiq-component-alert should have unclosable prop 1`] = ` | ||
<div | ||
className=" | ||
alertWrapper | ||
" | ||
> | ||
<div | ||
className=" | ||
alert | ||
type-info | ||
" | ||
> | ||
<span | ||
className="text" | ||
/> | ||
</div> | ||
</div> | ||
`; |
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,90 @@ | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
import styles from '../styles.less' | ||
|
||
export default class Alert extends React.Component { | ||
static propTypes = { | ||
children: PropTypes.node, | ||
type: PropTypes.oneOf([ | ||
'info', | ||
'good', | ||
'warning', | ||
'bad', | ||
]), | ||
unclosable: PropTypes.bool, | ||
onClose: PropTypes.func, | ||
} | ||
|
||
static defaultProps = { | ||
children: '', | ||
type: 'info', | ||
unclosable: false, | ||
onClose: noop => noop, | ||
} | ||
|
||
constructor(props) { | ||
super(props) | ||
this.state = { | ||
displayed: true, | ||
removed: false, | ||
} | ||
} | ||
|
||
close() { | ||
const height = this.nodeWrapper.clientHeight | ||
|
||
// Set the height on the wrapper node, to start the animation | ||
this.nodeWrapper.style.height = height + 'px' | ||
|
||
// Wait a tick before starting the animation | ||
setTimeout(() => this.setState({ | ||
displayed: false, | ||
}), 10) | ||
|
||
// Once the animation is done, remove the div with display none | ||
// Keep this 200 in sync with the styles.less file | ||
setTimeout(() => this.setState({ | ||
removed: true, | ||
}), 200) | ||
} | ||
|
||
handleCloseClick() { | ||
this.close() | ||
|
||
if (this.props.onClose) { | ||
this.props.onClose() | ||
} | ||
} | ||
|
||
render() { | ||
const { children, type, unclosable } = this.props | ||
|
||
return ( | ||
<div | ||
className={` | ||
${styles.alertWrapper} | ||
${!this.state.displayed ? styles.hidden : ''} | ||
${this.state.removed ? styles.removed : ''} | ||
`} | ||
ref={e => { this.nodeWrapper = e }} | ||
> | ||
<div | ||
className={` | ||
${styles.alert} | ||
${styles['type-' + type]} | ||
`} | ||
> | ||
<span className={styles.text}>{children}</span> | ||
{unclosable ? null : ( | ||
<div | ||
className={styles.close} | ||
onClick={() => this.handleCloseClick()} | ||
> | ||
× | ||
</div> | ||
)} | ||
</div> | ||
</div> | ||
) | ||
} | ||
} |
Oops, something went wrong.