Skip to content

Commit

Permalink
Typescript support for avatar (#524)
Browse files Browse the repository at this point in the history
  • Loading branch information
wenche authored and vnys committed Nov 13, 2020
1 parent eed0b57 commit 5941ca5
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 93 deletions.
86 changes: 0 additions & 86 deletions libraries/core-react/src/Avatar/Avatar.jsx

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ describe('Avatar', () => {
})
it('Image has provided src', () => {
const src = 'https://i.imgur.com/UM3mrju.jpg'
const { container } = render(<Avatar alt="avatar" src={src} />)
const avatarImg = container.firstChild.children[0]
const { getByAltText } = render(<Avatar alt="avatar" src={src} />)
const avatarImg = getByAltText('avatar')
expect(avatarImg).toHaveAttribute('src', src)
})
it('Has correct size', () => {
Expand All @@ -37,9 +37,10 @@ describe('Avatar', () => {
expect(avatar).toHaveStyleRule('height', `${size}px`)
})
it('Has faded image when disabled', () => {
const altText = 'avatar'
const src = 'https://i.imgur.com/UM3mrju.jpg'
const { container } = render(<Avatar disabled src={src} alt="avatar" />)
const avatarImg = container.firstChild.children[0]
const { getByAltText } = render(<Avatar disabled src={src} alt={altText} />)
const avatarImg = getByAltText(altText)
expect(avatarImg).toHaveStyleRule('opacity', disabledImage.opacity)
})
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-nocheck
export const avatar = {
enabled: {
border: {
Expand Down
70 changes: 70 additions & 0 deletions libraries/core-react/src/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import React, { forwardRef } from 'react'
import styled, { css } from 'styled-components'
import { avatar as tokens } from './Avatar.tokens'

const {
enabled: { border },
disabled: { image: disabledImage },
} = tokens

type StyledAvatarProps = {
size: number
disabled: boolean
}

const StyledAvatar = styled.div<StyledAvatarProps>`
position: relative;
display: flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
overflow: hidden;
border-radius: ${border.radius};
${({ size }) =>
css`
height: ${size}px;
width: ${size}px;
`}
`

type StyledImageProps = {
alt: string
src: string
disabled: boolean
}

const StyledImage = styled.img<StyledImageProps>`
height: 100%;
text-align: center;
color: transparent;
${({ disabled }) =>
disabled &&
css`
opacity: ${disabledImage.opacity};
`};
`

type Props = {
alt: string
/** Image source
@default null */
src?: string
/** Avatar size
@default 24 */
size?: 16 | 24 | 32 | 40 | 48
/** @default false */
disabled?: boolean
}

export const Avatar = forwardRef<HTMLHRElement, Props>((props, ref) => {
const { src = null, alt, size = 24, disabled = false, ...rest } = props

return (
<StyledAvatar size={size} disabled={disabled} ref={ref} {...rest}>
<StyledImage src={src} alt={alt} disabled={disabled} />
</StyledAvatar>
)
})

Avatar.displayName = 'Avatar'
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
// @ts-nocheck
export { Avatar } from './Avatar'
1 change: 0 additions & 1 deletion libraries/core-react/src/Divider/Divider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { forwardRef } from 'react'
import PropTypes from 'prop-types'
import styled from 'styled-components'
import { divider as tokens } from './Divider.tokens'

Expand Down

0 comments on commit 5941ca5

Please sign in to comment.