diff --git a/libraries/core-react/src/Avatar/Avatar.jsx b/libraries/core-react/src/Avatar/Avatar.jsx deleted file mode 100644 index ba9dde9866..0000000000 --- a/libraries/core-react/src/Avatar/Avatar.jsx +++ /dev/null @@ -1,86 +0,0 @@ -// @ts-nocheck -import React, { forwardRef } from 'react' -import PropTypes from 'prop-types' -import styled, { css } from 'styled-components' -import { avatar as tokens } from './Avatar.tokens' - -const { - enabled: { border }, - disabled: { image: disabledImage }, -} = tokens - -const StyledAvatar = styled.div` - 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; - `} -` - -const StyledImage = styled.img` - height: 100%; - text-align: center; - color: transparent; - - ${({ disabled }) => - disabled && - css` - opacity: ${disabledImage.opacity}; - `}; -` - -export const Avatar = forwardRef(function EdsAvatar( - { src, alt, size, disabled, ...rest }, - ref, -) { - const props = { - ...rest, - ref, - size, - disabled, - } - - const imageProps = { - src, - alt, - disabled, - } - - return ( - - - - ) -}) - -Avatar.displayName = 'eds-avatar' - -Avatar.propTypes = { - /** @ignore */ - className: PropTypes.string, - /** @ignore */ - children: PropTypes.node, - /** Image source */ - src: PropTypes.string, - /** Alt image description */ - alt: PropTypes.string.isRequired, - /** Size */ - size: PropTypes.oneOf([16, 24, 32, 40, 48]), - /** Disabled */ - disabled: PropTypes.bool, -} - -Avatar.defaultProps = { - className: '', - children: [], - src: null, - size: 24, - disabled: false, -} diff --git a/libraries/core-react/src/Avatar/Avatar.test.jsx b/libraries/core-react/src/Avatar/Avatar.test.tsx similarity index 83% rename from libraries/core-react/src/Avatar/Avatar.test.jsx rename to libraries/core-react/src/Avatar/Avatar.test.tsx index 31881c0f32..90aeeed913 100644 --- a/libraries/core-react/src/Avatar/Avatar.test.jsx +++ b/libraries/core-react/src/Avatar/Avatar.test.tsx @@ -24,8 +24,8 @@ describe('Avatar', () => { }) it('Image has provided src', () => { const src = 'https://i.imgur.com/UM3mrju.jpg' - const { container } = render() - const avatarImg = container.firstChild.children[0] + const { getByAltText } = render() + const avatarImg = getByAltText('avatar') expect(avatarImg).toHaveAttribute('src', src) }) it('Has correct size', () => { @@ -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() - const avatarImg = container.firstChild.children[0] + const { getByAltText } = render() + const avatarImg = getByAltText(altText) expect(avatarImg).toHaveStyleRule('opacity', disabledImage.opacity) }) }) diff --git a/libraries/core-react/src/Avatar/Avatar.tokens.js b/libraries/core-react/src/Avatar/Avatar.tokens.ts similarity index 90% rename from libraries/core-react/src/Avatar/Avatar.tokens.js rename to libraries/core-react/src/Avatar/Avatar.tokens.ts index 4c9f59ae0e..c08df4c484 100644 --- a/libraries/core-react/src/Avatar/Avatar.tokens.js +++ b/libraries/core-react/src/Avatar/Avatar.tokens.ts @@ -1,4 +1,3 @@ -// @ts-nocheck export const avatar = { enabled: { border: { diff --git a/libraries/core-react/src/Avatar/Avatar.tsx b/libraries/core-react/src/Avatar/Avatar.tsx new file mode 100644 index 0000000000..34980d0a06 --- /dev/null +++ b/libraries/core-react/src/Avatar/Avatar.tsx @@ -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` + 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` + 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((props, ref) => { + const { src = null, alt, size = 24, disabled = false, ...rest } = props + + return ( + + + + ) +}) + +Avatar.displayName = 'Avatar' diff --git a/libraries/core-react/src/Avatar/index.js b/libraries/core-react/src/Avatar/index.ts similarity index 69% rename from libraries/core-react/src/Avatar/index.js rename to libraries/core-react/src/Avatar/index.ts index 04353fa36b..2591a10ea5 100644 --- a/libraries/core-react/src/Avatar/index.js +++ b/libraries/core-react/src/Avatar/index.ts @@ -1,2 +1 @@ -// @ts-nocheck export { Avatar } from './Avatar' diff --git a/libraries/core-react/src/Divider/Divider.tsx b/libraries/core-react/src/Divider/Divider.tsx index 0783817dc7..c6055508b7 100644 --- a/libraries/core-react/src/Divider/Divider.tsx +++ b/libraries/core-react/src/Divider/Divider.tsx @@ -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'