-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- make logic and styles from button component reusable - center align button component, and therefore link button - add link button to docs
- Loading branch information
Showing
9 changed files
with
179 additions
and
12 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
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 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import { Link as ReactRouterLink } from 'react-router-dom'; | ||
|
||
import safeRest from '../../../safeRest'; | ||
import { getClassName, preventDisabling } from '../../Button/Button'; | ||
|
||
import styles from '../../Button/Button.modules.scss'; | ||
|
||
/** | ||
* <span class="docs--badge green">new!</span> <span class="docs--badge purple">v0.21.0</span> | ||
*/ | ||
const ButtonLink = ({ variant, invert, children, ...rest }) => { | ||
const restNoDisabled = preventDisabling(rest); | ||
|
||
return React.createElement( | ||
rest.to ? ReactRouterLink : 'a', | ||
{ | ||
...safeRest(restNoDisabled), | ||
className: getClassName(variant, invert) | ||
}, | ||
children | ||
); | ||
}; | ||
ButtonLink.propTypes = { | ||
variant: PropTypes.oneOf([ | ||
'primary', | ||
'secondary', | ||
'outlined' | ||
]), | ||
invert: PropTypes.bool, | ||
children: PropTypes.string.isRequired | ||
}; | ||
ButtonLink.defaultProps = { | ||
variant: 'primary', | ||
invert: false | ||
}; | ||
ButtonLink.displayName = 'Link.Button'; | ||
|
||
export default ButtonLink; |
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,27 @@ | ||
### Minimal Usage | ||
|
||
``` | ||
<Link.Button>Find out how</Link.Button> | ||
``` | ||
|
||
### Variants | ||
|
||
``` | ||
const PurpleBlock = require('../../__docs__/PurpleBlock').default; | ||
<div> | ||
<Link.Button variant="primary">Explore</Link.Button> | ||
<Link.Button variant="secondary">Take me there</Link.Button> | ||
<br/> | ||
<br/> | ||
<PurpleBlock> | ||
<Link.Button variant="outlined">Find me</Link.Button> | ||
</PurpleBlock> | ||
</div> | ||
``` | ||
|
||
### Inverted Button Links | ||
|
||
``` | ||
<Link.Button invert>Learn more</Link.Button> | ||
``` |
87 changes: 87 additions & 0 deletions
87
src/components/Link/ButtonLink/__tests__/ButtonLink.spec.jsx
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,87 @@ | ||
import React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
import { MemoryRouter } from 'react-router-dom'; | ||
|
||
import { warn } from '../../../../warn'; | ||
|
||
import Link from '../../Link'; | ||
|
||
jest.mock('../../../../warn', () => ( | ||
{ warn: jest.fn() } | ||
)); | ||
|
||
describe('Link.Button', () => { | ||
const doShallow = (overrides = {}) => shallow( | ||
<Link.Button {...overrides}>Go home</Link.Button> | ||
); | ||
const doShallowWithRouter = (overrides = {}) => shallow( | ||
<MemoryRouter> | ||
<Link.Button {...overrides}>Go home</Link.Button> | ||
</MemoryRouter> | ||
); | ||
|
||
it('is an anchor HTML element when using the href attribute', () => { | ||
const link = doShallow({ href: 'http://telus.com' }); | ||
|
||
expect(link).toHaveTagName('a'); | ||
expect(link).toHaveProp('href', 'http://telus.com'); | ||
}); | ||
|
||
it('is a React Router Link when using the to attribute', () => { | ||
const link = doShallowWithRouter({ to: '/about' }); | ||
|
||
const reactRouterLink = link.find('Router').dive().dive(); | ||
expect(reactRouterLink).toMatchSelector('Link'); | ||
expect(reactRouterLink).toHaveProp('to', '/about'); | ||
}); | ||
|
||
it('can be presented as one of the allowed variants', () => { | ||
let button = doShallow(); | ||
expect(button).toHaveClassName('primary'); | ||
|
||
button = doShallow({ variant: 'primary' }); | ||
expect(button).toHaveClassName('primary'); | ||
|
||
button = doShallow({ variant: 'secondary' }); | ||
expect(button).toHaveClassName('secondary'); | ||
|
||
button = doShallow({ variant: 'outlined' }); | ||
expect(button).toHaveClassName('outlined'); | ||
}); | ||
|
||
it('can be inverted for secondary and outlined variants', () => { | ||
const secondaryButton = doShallow({ variant: 'secondary', invert: true }); | ||
expect(secondaryButton).toHaveClassName('secondaryInverted'); | ||
|
||
const outlinedButton = doShallow({ variant: 'outlined', invert: true }); | ||
expect(outlinedButton).toHaveClassName('outlinedInverted'); | ||
}); | ||
|
||
it('can not be inverted for primary variant', () => { | ||
const button = doShallow({ variant: 'primary', invert: true }); | ||
|
||
expect(button).toHaveClassName('primary'); | ||
expect(warn).toHaveBeenCalled(); | ||
}); | ||
|
||
it('can not be disabled', () => { | ||
const button = doShallow({ disabled: true }); | ||
|
||
expect(button).not.toHaveProp('disabled'); | ||
expect(warn).toHaveBeenCalled(); | ||
}); | ||
|
||
it('passes additional attributes to button element', () => { | ||
const button = doShallow({ id: 'the-button', tabindex: 1 }); | ||
|
||
expect(button).toHaveProp('id', 'the-button'); | ||
expect(button).toHaveProp('tabindex', 1); | ||
}); | ||
|
||
it('does not allow custom CSS', () => { | ||
const button = doShallow({ className: 'my-custom-class', style: { color: 'hotpink' } }); | ||
|
||
expect(button).not.toHaveProp('className', 'my-custom-class'); | ||
expect(button).not.toHaveProp('style'); | ||
}); | ||
}) |
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
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