Skip to content
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

Alert component #55

Merged
merged 4 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"main": "dist/index.js",
"scripts": {
"start": "docz dev",
"start": "rm -rf .docz && docz dev",
"build": "rm -rf dist && tsc --project tsconfig-build.json && yarn copy:assets",
"test": "jest --detectOpenHandles --watchAll",
"document": "docz build",
Expand Down
24 changes: 24 additions & 0 deletions src/components/Alert/Alert.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
name: Alert
menu: Components
route: /alert
---

import { Playground, Props } from 'docz';
import { Alert } from '../..';

# Alert

Alerts allow you to display error messages relevant to an entire form or application.

## Examples

### Basic usage

<Playground>
<Alert intent='error' message='Something went wrong.' />
</Playground>

## API

<Props of={Alert} />
18 changes: 18 additions & 0 deletions src/components/Alert/Alert.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.alert {
padding: 10px 18px;
background-color: var(--color-error-light);
display: flex;
align-items: center;
border-radius: var(--border-radius-small);
}

.message {
display: inline-block;
}

svg.icon {
margin-right: 10px;
color: var(--color-error);
height: 16px;
width: 16px;
}
32 changes: 32 additions & 0 deletions src/components/Alert/Alert.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from 'react';
import { render } from '@testing-library/react';

import Alert from './Alert';

describe('Input', () => {
const defaultProps = {
message: 'Something went wrong.',
};

test('default snapshot', () => {
const { asFragment } = render(<Alert {...defaultProps} />);
expect(asFragment()).toMatchSnapshot();
});

test('the message should be rendered', () => {
const { queryByText } = render(<Alert {...defaultProps} />);
const alertElement = queryByText(/Something went wrong./);
expect(alertElement).not.toBe(null);
});

test('className prop', () => {
const className = 'center';
const { getByText } = render(<Alert {...defaultProps} className={className} />);
const alertElement = getByText(/Something went wrong./).parentElement!;

const renderedClassNames = alertElement.className.split(' ');
expect(renderedClassNames).toContain(className);
// className in prop should be the last in the row
expect(renderedClassNames.indexOf(className)).toBe(renderedClassNames.length - 1);
});
});
25 changes: 25 additions & 0 deletions src/components/Alert/Alert.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react';
import { AlertCircle } from 'react-feather';
import classNames from 'classnames';

import cssReset from '../../css-reset.module.css';
import styles from './Alert.module.css';

interface Props {
intent?: 'error';
message: string;
className?: string;
}

const Alert: React.FC<Props> = ({ intent = 'error', message, className }: Props) => {
const mergedClassNames = classNames(cssReset.ventura, styles.alert, className);

return (
<div className={mergedClassNames}>
{intent === 'error' && <AlertCircle className={styles.icon} />}
<span className={styles.message}>{message}</span>
</div>
);
};

export default Alert;
45 changes: 45 additions & 0 deletions src/components/Alert/__snapshots__/Alert.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Input default snapshot 1`] = `
<DocumentFragment>
<div
class="ventura alert"
>
<svg
class="icon"
fill="none"
height="24"
stroke="currentColor"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
viewBox="0 0 24 24"
width="24"
xmlns="http://www.w3.org/2000/svg"
>
<circle
cx="12"
cy="12"
r="10"
/>
<line
x1="12"
x2="12"
y1="8"
y2="12"
/>
<line
x1="12"
x2="12.01"
y1="16"
y2="16"
/>
</svg>
<span
class="message"
>
Something went wrong.
</span>
</div>
</DocumentFragment>
`;
3 changes: 1 addition & 2 deletions src/components/Input/Input.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ menu: Components
route: /input
---

import { useState } from 'react';
import { Playground, Props } from 'docz';
import { Input } from '../../index.ts'
import styles from './docAssets/docs.module.css';
Expand Down Expand Up @@ -66,7 +65,7 @@ A Input can be used to let the user insert text using key strokes. A crucial for

<Playground>
{() => {
const [serialCode, setSerialCode] = useState('AAAA-BBBB-CCCC-DDDD');
const [serialCode, setSerialCode] = React.useState('AAAA-BBBB-CCCC-DDDD');
const formattedSerialCode = (
serialCode
.split('-')
Expand Down
5 changes: 4 additions & 1 deletion src/index.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
@import './theme.css';

svg[viewBox='0 0 24 24'][stroke-linejoin='round'][stroke-linecap='round'] {
/* Multiple issues open to set the default svg size */
/* https://github.com/feathericons/react-feather/pull/62 */
/* Remove this when a better solution is available */
svg[viewBox='0 0 24 24'] {
width: var(--font-size);
height: var(--font-size);
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import './index.css';

export { default as Alert } from './components/Alert/Alert';
export { default as Button } from './components/Button/Button';
export { default as Input } from './components/Input/Input';
export { default as Radio } from './components/Radio/Radio';
Expand Down