Skip to content

Commit

Permalink
Merge pull request #7 from fyndiq/component-alert
Browse files Browse the repository at this point in the history
New Component: Alert
  • Loading branch information
Dildaarkhan committed May 18, 2017
2 parents 1fa9ac9 + b5eed50 commit 02e10fd
Show file tree
Hide file tree
Showing 10 changed files with 489 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ This git repository is a monorepo built using [Lerna](//lernajs.io). It contains

| Package | Version | Description |
|------|----|----|
| [`fyndiq-component-alert`](/packages/fyndiq-component-alert) | [![npm](https://img.shields.io/npm/v/fyndiq-component-alert.svg?maxAge=3600)](https://www.npmjs.com/package/fyndiq-component-alert) | Alert component |
| [`fyndiq-component-button`](/packages/fyndiq-component-button) | [![npm](https://img.shields.io/npm/v/fyndiq-component-button.svg?maxAge=3600)](https://www.npmjs.com/package/fyndiq-component-button) | Button component |
| [`fyndiq-component-checkbox`](/packages/fyndiq-component-checkbox) | [![npm](https://img.shields.io/npm/v/fyndiq-component-checkbox.svg?maxAge=3600)](https://www.npmjs.com/package/fyndiq-component-checkbox) | Checkbox component |
| [`fyndiq-component-dropdown`](/packages/fyndiq-component-dropdown) | [![npm](https://img.shields.io/npm/v/fyndiq-component-dropdown.svg?maxAge=3600)](https://www.npmjs.com/package/fyndiq-component-dropdown) | Dropdown component |
Expand Down
41 changes: 41 additions & 0 deletions packages/fyndiq-component-alert/README.md
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
17 changes: 17 additions & 0 deletions packages/fyndiq-component-alert/package.json
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 packages/fyndiq-component-alert/src/__snapshots__/index.test.js.snap
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>
`;
90 changes: 90 additions & 0 deletions packages/fyndiq-component-alert/src/index.js
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()}
>
&times;
</div>
)}
</div>
</div>
)
}
}
Loading

0 comments on commit 02e10fd

Please sign in to comment.