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

feat #93 - Tooltip component #95

Merged
merged 6 commits into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
567 changes: 130 additions & 437 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"@types/node": "^12.12.53",
"@types/react": "^16.9.44",
"@types/react-dom": "^16.9.8",
"antd": "^4.6.4",
"antd": "^4.6.5",
"bytes": "^3.1.0",
"classnames": "^2.2.6",
"fuse.js": "^6.4.1",
Expand Down
28 changes: 25 additions & 3 deletions src/__snapshots__/storybook.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -831,15 +831,15 @@ exports[`Storyshots Select Search 1`] = `
onKeyDown={[Function]}
onMouseDown={[Function]}
onPaste={[Function]}
readOnly={true}
readOnly={false}
role="combobox"
style={
Object {
"opacity": 0,
"opacity": null,
}
}
type="search"
unselectable="on"
unselectable={null}
value=""
/>
</span>
Expand Down Expand Up @@ -1050,6 +1050,28 @@ exports[`Storyshots Toggle Small 1`] = `
</button>
`;

exports[`Storyshots Tooltip Default 1`] = `
<div
style={
Object {
"padding": 75,
}
}
>
<span
data-test="tooltip-trigger"
onMouseEnter={[Function]}
onMouseLeave={[Function]}
>
<img
alt="dassana"
height={32}
src="dassana-blue.png"
/>
</span>
</div>
`;

exports[`Storyshots Tree Default 1`] = `
<ForwardRef
blockNode={true}
Expand Down
38 changes: 38 additions & 0 deletions src/components/Tooltip/Tooltip.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Icon from '../Icon'
import { placementOptions } from '../utils'
import React from 'react'
import { Meta, Story } from '@storybook/react/types-6-0'
import Tooltip, { TooltipProps } from './index'

export default {
argTypes: {
children: { control: { disable: true } },
placement: {
control: {
options: placementOptions,
type: 'select'
}
},
title: { control: { type: 'text' } }
},
component: Tooltip,
decorators: [
(PopoverStory: Story) => (
<div style={{ padding: 75 }}>
<PopoverStory />
</div>
)
],
title: 'Tooltip'
} as Meta

const Template: Story<TooltipProps> = args => (
<Tooltip {...args}>
<Icon iconKey='dassana' />
</Tooltip>
)

export const Default = Template.bind({})
Default.args = {
title: 'Dassana'
}
40 changes: 40 additions & 0 deletions src/components/Tooltip/Tooltip.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Tooltip as AntDTooltip } from 'antd'
import React from 'react'
import { shallow, ShallowWrapper } from 'enzyme'
import Tooltip, { TooltipProps } from './index'

let wrapper: ShallowWrapper

const mockTitle = 'foo'

const getWrapper = (
additionalProps?: Omit<TooltipProps, 'children' | 'title'>
): ShallowWrapper =>
shallow(
<Tooltip title={mockTitle} {...additionalProps}>
<div>Hello World</div>
</Tooltip>
)

beforeEach(() => {
wrapper = getWrapper()
})

describe('Tooltip', () => {
it('renders', () => {
expect(wrapper).toHaveLength(1)
})

it('correctly passes the title prop', () => {
expect(wrapper.find(AntDTooltip).props().title).toEqual(mockTitle)
})

it('correctly passes the placement prop if one is provided', () => {
wrapper = getWrapper({ placement: 'top' })
expect(wrapper.find(AntDTooltip).props().placement).toEqual('top')
})

it('has a default placement of right', () => {
expect(wrapper.find(AntDTooltip).props().placement).toBe('right')
})
})
51 changes: 51 additions & 0 deletions src/components/Tooltip/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import '../assets/styles/antdAnimations.css'
import 'antd/lib/tooltip/style/index.css'
import { Tooltip as AntDTooltip } from 'antd'
import cn from 'classnames'
import { CommonComponentProps } from '../types'
import { getDataTestAttributeProp } from '../utils'
import { TooltipPlacement } from 'antd/es/tooltip'
import React, { FC, ReactNode } from 'react'

export type TooltipTitle = string | ReactNode

export interface TooltipProps extends CommonComponentProps {
/**
* Element tooltip should be anchored to
*/
children: ReactNode
/**
* Array of classes to pass to element
* @default []
*/
classes?: string[]
/**
* Position of tooltip relative to the target
*/
placement?: TooltipPlacement
/**
* Text shown in the tooltip
*/
title: TooltipTitle
}

const Tooltip: FC<TooltipProps> = ({
children,
classes = [],
dataTag,
placement = 'right',
title
}: TooltipProps) => (
<AntDTooltip
overlayClassName={cn(classes)}
overlayStyle={{ borderRadius: 4 }}
placement={placement}
title={title}
>
<span {...getDataTestAttributeProp('tooltip-trigger', dataTag)}>
{children}
</span>
</AntDTooltip>
)

export default Tooltip
Loading