This repository has been archived by the owner on May 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
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 #120 from steverydz/feature/tooltips
Create tooltip component
- Loading branch information
Showing
6 changed files
with
314 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
## Done | ||
|
||
[List of work items including drive-bys] | ||
|
||
## QA | ||
|
||
- Check out this feature branch | ||
- Run the site using the command `./run serve` | ||
- View the site locally in your web browser at: [http://0.0.0.0:8102/](http://0.0.0.0:8102/) | ||
- [List additional steps to QA the new features or prove the bug has been resolved] | ||
|
||
|
||
## Issue / Card | ||
|
||
[List of links to Github issues/bugs and cards if needed - e.g. `Fixes #1`] | ||
|
||
## Screenshots | ||
|
||
[if relevant, include a screenshot] |
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,47 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
const ToolTip = ({ | ||
id, message, position, children, | ||
}) => { | ||
const modifierClassPrefix = 'p-tooltip--'; | ||
|
||
let className = 'p-tooltip'; | ||
|
||
if (position) { | ||
className += ` ${modifierClassPrefix}${position}`; | ||
} | ||
|
||
return ( | ||
<span className={className} aria-describedby={id}> | ||
{children} | ||
<span className="p-tooltip__message" role="tooltip" id={id}> | ||
{message} | ||
</span> | ||
</span> | ||
); | ||
}; | ||
|
||
ToolTip.defaultProps = { | ||
position: 'btm-right', | ||
}; | ||
|
||
ToolTip.propTypes = { | ||
children: PropTypes.node.isRequired, | ||
position: PropTypes.oneOf([ | ||
'left', | ||
'right', | ||
'top-left', | ||
'btm-left', | ||
'top-right', | ||
'btm-right', | ||
'top-center', | ||
'btm-center', | ||
]), | ||
id: PropTypes.string.isRequired, | ||
message: PropTypes.string.isRequired, | ||
}; | ||
|
||
ToolTip.displayName = 'ToolTip'; | ||
|
||
export default ToolTip; |
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,32 @@ | ||
import React from 'react'; | ||
import { storiesOf } from '@storybook/react'; | ||
import { select } from '@storybook/addon-knobs'; | ||
import { withInfo } from '@storybook/addon-info'; | ||
|
||
import ToolTip from './ToolTip'; | ||
|
||
const label = 'Positions'; | ||
const options = [ | ||
'left', | ||
'right', | ||
'top-left', | ||
'top-right', | ||
'btm-left', | ||
'btm-right', | ||
'top-center', | ||
'btm-center', | ||
]; | ||
const defaultValue = 'btm-left'; | ||
|
||
storiesOf('ToolTip', module) | ||
.add('Default ToolTip', | ||
withInfo('You can set the position of the tooltip by passing a position prop')(() => ( | ||
<ToolTip | ||
id="default-tooltip" | ||
message="Lorem ipsum dolor sit amet" | ||
position={select(label, options, defaultValue)} | ||
> | ||
Example ToolTip | ||
</ToolTip>), | ||
), | ||
); |
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,85 @@ | ||
import React from 'react'; | ||
import ReactTestRenderer from 'react-test-renderer'; | ||
import ToolTip from './ToolTip'; | ||
|
||
describe('ToolTip component', () => { | ||
it('should render a tooltip component', () => { | ||
const toolTip = ReactTestRenderer.create( | ||
<ToolTip id="default-tooltip" message="Lorem ipsum dolor sit amet"> | ||
Bottom left tooltip | ||
</ToolTip>, | ||
); | ||
const json = toolTip.toJSON(); | ||
expect(json).toMatchSnapshot(); | ||
}); | ||
|
||
it('should render a tooltip component with a bottom center tooltip', () => { | ||
const toolTip = ReactTestRenderer.create( | ||
<ToolTip id="btm-cntr" message="Lorem ipsum dolor sit amet" position="btm-center"> | ||
Bottom center tooltip | ||
</ToolTip>, | ||
); | ||
const json = toolTip.toJSON(); | ||
expect(json).toMatchSnapshot(); | ||
}); | ||
|
||
it('should render a tooltip component with a bottomright tooltip', () => { | ||
const toolTip = ReactTestRenderer.create( | ||
<ToolTip id="btm-right" message="Lorem ipsum dolor sit amet" position="btm-right"> | ||
Bottom right tooltip | ||
</ToolTip>, | ||
); | ||
const json = toolTip.toJSON(); | ||
expect(json).toMatchSnapshot(); | ||
}); | ||
|
||
it('should render a tooltip component with a left tooltip', () => { | ||
const toolTip = ReactTestRenderer.create( | ||
<ToolTip id="left" message="Lorem ipsum dolor sit amet" position="left"> | ||
Left tooltip | ||
</ToolTip>, | ||
); | ||
const json = toolTip.toJSON(); | ||
expect(json).toMatchSnapshot(); | ||
}); | ||
|
||
it('should render a tooltip component with a right tooltip', () => { | ||
const toolTip = ReactTestRenderer.create( | ||
<ToolTip id="right" message="Lorem ipsum dolor sit amet" position="right"> | ||
Right tooltip | ||
</ToolTip>, | ||
); | ||
const json = toolTip.toJSON(); | ||
expect(json).toMatchSnapshot(); | ||
}); | ||
|
||
it('should render a tooltip component with a top left tooltip', () => { | ||
const toolTip = ReactTestRenderer.create( | ||
<ToolTip id="to-lft" message="Lorem ipsum dolor sit amet" position="top-left"> | ||
Top left tooltip | ||
</ToolTip>, | ||
); | ||
const json = toolTip.toJSON(); | ||
expect(json).toMatchSnapshot(); | ||
}); | ||
|
||
it('should render a tooltip component with a top center tooltip', () => { | ||
const toolTip = ReactTestRenderer.create( | ||
<ToolTip id="tp-cntr" message="Lorem ipsum dolor sit amet" position="top-center"> | ||
Top center tooltip | ||
</ToolTip>, | ||
); | ||
const json = toolTip.toJSON(); | ||
expect(json).toMatchSnapshot(); | ||
}); | ||
|
||
it('should render a tooltip component with a top right tooltip', () => { | ||
const toolTip = ReactTestRenderer.create( | ||
<ToolTip id="tp-right" message="Lorem ipsum dolor sit amet" position="top-right"> | ||
Top right tooltip | ||
</ToolTip>, | ||
); | ||
const json = toolTip.toJSON(); | ||
expect(json).toMatchSnapshot(); | ||
}) | ||
}); |
129 changes: 129 additions & 0 deletions
129
src/lib/components/ToolTip/__snapshots__/ToolTip.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,129 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`ToolTip component should render a tooltip component 1`] = ` | ||
<span | ||
aria-describedby="default-tooltip" | ||
className="p-tooltip p-tooltip--btm-right" | ||
> | ||
Bottom left tooltip | ||
<span | ||
className="p-tooltip__message" | ||
id="default-tooltip" | ||
role="tooltip" | ||
> | ||
Lorem ipsum dolor sit amet | ||
</span> | ||
</span> | ||
`; | ||
|
||
exports[`ToolTip component should render a tooltip component with a bottom center tooltip 1`] = ` | ||
<span | ||
aria-describedby="btm-cntr" | ||
className="p-tooltip p-tooltip--btm-center" | ||
> | ||
Bottom center tooltip | ||
<span | ||
className="p-tooltip__message" | ||
id="btm-cntr" | ||
role="tooltip" | ||
> | ||
Lorem ipsum dolor sit amet | ||
</span> | ||
</span> | ||
`; | ||
|
||
exports[`ToolTip component should render a tooltip component with a bottomright tooltip 1`] = ` | ||
<span | ||
aria-describedby="btm-right" | ||
className="p-tooltip p-tooltip--btm-right" | ||
> | ||
Bottom right tooltip | ||
<span | ||
className="p-tooltip__message" | ||
id="btm-right" | ||
role="tooltip" | ||
> | ||
Lorem ipsum dolor sit amet | ||
</span> | ||
</span> | ||
`; | ||
|
||
exports[`ToolTip component should render a tooltip component with a left tooltip 1`] = ` | ||
<span | ||
aria-describedby="left" | ||
className="p-tooltip p-tooltip--left" | ||
> | ||
Left tooltip | ||
<span | ||
className="p-tooltip__message" | ||
id="left" | ||
role="tooltip" | ||
> | ||
Lorem ipsum dolor sit amet | ||
</span> | ||
</span> | ||
`; | ||
|
||
exports[`ToolTip component should render a tooltip component with a right tooltip 1`] = ` | ||
<span | ||
aria-describedby="right" | ||
className="p-tooltip p-tooltip--right" | ||
> | ||
Right tooltip | ||
<span | ||
className="p-tooltip__message" | ||
id="right" | ||
role="tooltip" | ||
> | ||
Lorem ipsum dolor sit amet | ||
</span> | ||
</span> | ||
`; | ||
|
||
exports[`ToolTip component should render a tooltip component with a top center tooltip 1`] = ` | ||
<span | ||
aria-describedby="tp-cntr" | ||
className="p-tooltip p-tooltip--top-center" | ||
> | ||
Top center tooltip | ||
<span | ||
className="p-tooltip__message" | ||
id="tp-cntr" | ||
role="tooltip" | ||
> | ||
Lorem ipsum dolor sit amet | ||
</span> | ||
</span> | ||
`; | ||
|
||
exports[`ToolTip component should render a tooltip component with a top left tooltip 1`] = ` | ||
<span | ||
aria-describedby="to-lft" | ||
className="p-tooltip p-tooltip--top-left" | ||
> | ||
Top left tooltip | ||
<span | ||
className="p-tooltip__message" | ||
id="to-lft" | ||
role="tooltip" | ||
> | ||
Lorem ipsum dolor sit amet | ||
</span> | ||
</span> | ||
`; | ||
|
||
exports[`ToolTip component should render a tooltip component with a top right tooltip 1`] = ` | ||
<span | ||
aria-describedby="tp-right" | ||
className="p-tooltip p-tooltip--top-right" | ||
> | ||
Top right tooltip | ||
<span | ||
className="p-tooltip__message" | ||
id="tp-right" | ||
role="tooltip" | ||
> | ||
Lorem ipsum dolor sit amet | ||
</span> | ||
</span> | ||
`; |
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