Skip to content

Commit

Permalink
Feat #30 - Adress PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-dassana committed Aug 21, 2020
1 parent 6c5e5f9 commit 9a48102
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 23 deletions.
File renamed without changes
File renamed without changes
File renamed without changes
6 changes: 3 additions & 3 deletions src/components/Icon/Icon.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ const Template: Story<IconProps> = args => <Icon {...args} />

export const DassanaBlue = Template.bind({})
DassanaBlue.args = {
icon: 'dassana-blue',
iconKey: 'dassana-blue',
size: 64
}

export const DassanaOrange = Template.bind({})
DassanaOrange.args = {
icon: 'dassana-orange'
iconKey: 'dassana-orange'
}

export const AWS = Template.bind({})
AWS.args = {
icon: 'aws-logo',
iconKey: 'aws-logo',
size: 64
}
8 changes: 6 additions & 2 deletions src/components/Icon/Icon.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from 'react'
import Icon, { IconProps } from '.'
import { mount, ReactWrapper } from 'enzyme'
import { mount, ReactWrapper, shallow } from 'enzyme'

let wrapper: ReactWrapper
const mockProps: IconProps = {
icon: 'dassana-blue',
iconKey: 'dassana-blue',
size: 64
}

Expand All @@ -31,4 +31,8 @@ describe('Icon', () => {
expect(wrapper.getDOMNode().getAttribute('height')).toBe('64')
expect(wrapper.getDOMNode().getAttribute('width')).toBe('64')
})

it('throws an error if both icon and iconType props are undefined', () => {
expect(() => shallow(<Icon />)).toThrow()
})
})
13 changes: 13 additions & 0 deletions src/components/Icon/IconsMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import AWS from '../../assets/icons/aws.svg'
import DASSANA_BLUE from '../../assets/icons/dassana-blue.png'
import DASSANA_ORANGE from '../../assets/icons/dassana-orange.png'

const Icons = {
'aws-logo': AWS,
'dassana-blue': DASSANA_BLUE,
'dassana-orange': DASSANA_ORANGE
}

export type IconName = keyof typeof Icons

export default Icons
13 changes: 0 additions & 13 deletions src/components/Icon/icons/index.ts

This file was deleted.

22 changes: 17 additions & 5 deletions src/components/Icon/index.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
import Icons, { IconName } from './icons'
import Icons, { IconName } from './IconsMap'
import React, { FC } from 'react'

export type { IconName }

export interface IconProps {
/**
* The name of the icon.
* The url of the icon if you want Icon to render a custom icon.
*/
icon?: string
/**
* The name of the icon if using icons provided by Dassana.
*/
icon: IconName
iconKey?: IconName
/**
* The width and height of the icon, in pixels.
*/
size?: number
}

const Icon: FC<IconProps> = ({ icon, size = 32 }: IconProps) => {
return <img alt={icon} height={size} src={Icons[icon]} width={size} />
const Icon: FC<IconProps> = ({ icon, iconKey, size = 32 }: IconProps) => {
if (!icon && !iconKey)
throw new Error('Icon requires either an iconKey or icon prop.')

const imgSrc = iconKey ? Icons[iconKey] : icon
const imgAlt = iconKey ? iconKey : icon

return <img alt={imgAlt} height={size} src={imgSrc} width={size} />
}

export default Icon

0 comments on commit 9a48102

Please sign in to comment.