Skip to content

Commit

Permalink
Tooltip.js → Tooltip.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
colebemis committed Feb 9, 2021
1 parent b6eb246 commit c641c1d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
47 changes: 27 additions & 20 deletions src/Tooltip.js → src/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,12 @@ import React from 'react'
import PropTypes from 'prop-types'
import classnames from 'classnames'
import styled from 'styled-components'
import {COMMON, get} from './constants'
import {COMMON, get, SystemCommonProps} from './constants'
import theme from './theme'
import sx from './sx'
import sx, {SxProp} from './sx'
import {ComponentProps} from './utils/types'

function TooltipBase({direction, children, className, text, noDelay, align, wrap, theme, ...rest}) {
const classes = classnames(
className,
`tooltipped-${direction}`,
align && `tooltipped-align-${align}-2`,
noDelay && 'tooltipped-no-delay',
wrap && 'tooltipped-multiline'
)
return (
<span aria-label={text} {...rest} className={classes}>
{children}
</span>
)
}

const Tooltip = styled(TooltipBase)`
const TooltipBase = styled.span<SystemCommonProps & SxProp>`
position: relative;
&::before {
Expand Down Expand Up @@ -249,13 +235,34 @@ const Tooltip = styled(TooltipBase)`
${sx};
`

export type TooltipProps = {
direction?: 'n' | 'ne' | 'e' | 'se' | 's' | 'sw' | 'w' | 'nw'
text?: string
noDelay?: boolean
align?: 'left' | 'right'
wrap?: boolean
} & ComponentProps<typeof TooltipBase>

function Tooltip({direction = 'n', children, className, text, noDelay, align, wrap, ...rest}: TooltipProps) {
const classes = classnames(
className,
`tooltipped-${direction}`,
align && `tooltipped-align-${align}-2`,
noDelay && 'tooltipped-no-delay',
wrap && 'tooltipped-multiline'
)
return (
<TooltipBase aria-label={text} {...rest} className={classes}>
{children}
</TooltipBase>
)
}
Tooltip.alignments = ['left', 'right']

Tooltip.directions = ['n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw']

Tooltip.defaultProps = {
theme,
direction: 'n'
theme
}

Tooltip.propTypes = {
Expand Down
6 changes: 4 additions & 2 deletions src/__tests__/Tooltip.js → src/__tests__/Tooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import {Tooltip} from '..'
import Tooltip, {TooltipProps} from '../Tooltip'
import {render, renderClasses, rendersClass, behavesAsComponent, checkExports} from '../utils/testing'
import {COMMON} from '../constants'
import {render as HTMLRender, cleanup} from '@testing-library/react'
Expand Down Expand Up @@ -33,7 +33,9 @@ describe('Tooltip', () => {

it('respects the "direction" prop', () => {
for (const direction of Tooltip.directions) {
expect(rendersClass(<Tooltip direction={direction} />, `tooltipped-${direction}`)).toBe(true)
expect(
rendersClass(<Tooltip direction={direction as TooltipProps['direction']} />, `tooltipped-${direction}`)
).toBe(true)
}
})

Expand Down
File renamed without changes.

0 comments on commit c641c1d

Please sign in to comment.