Skip to content
This repository was archived by the owner on Jan 9, 2023. It is now read-only.

Commit 79a8b44

Browse files
author
CVVPK
committed
feat(icon): add className and style props
1 parent 82159f3 commit 79a8b44

5 files changed

Lines changed: 58 additions & 3 deletions

File tree

src/components/Icon/Icon.tsx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react'
1+
import React, { CSSProperties } from 'react'
22
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
33
import { IconProp } from '@fortawesome/fontawesome-svg-core'
44
import { IconType } from './interfaces'
@@ -34,14 +34,28 @@ function getFontAwesomeIcon(icon: IconType): string {
3434
interface Props {
3535
/** The type of icon to display */
3636
icon: IconType
37+
/**
38+
* Defines the class of the icon.
39+
*/
40+
className?: string
41+
/**
42+
* Defines the style of the icon.
43+
*/
44+
style?: CSSProperties
3745
}
3846

3947
/**
4048
* Icons provide contextual clues to users to make it easier to recognize functionality
4149
*/
4250
const Icon = (props: Props) => {
43-
const { icon } = props
44-
return <FontAwesomeIcon icon={getFontAwesomeIcon(icon) as IconProp} />
51+
const { icon, className, style } = props
52+
return (
53+
<FontAwesomeIcon
54+
icon={getFontAwesomeIcon(icon) as IconProp}
55+
className={className}
56+
style={style}
57+
/>
58+
)
4559
}
4660

4761
export { Icon }

stories/CustomClasses.css

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
.customClass {
2+
border: 5px solid green;
3+
background-color: yellow;
4+
font-style: italic;
5+
color: red;
6+
}
7+
8+
.customClass2 {
9+
background-color: violet;
10+
color: black;
11+
border: 2px solid purple;
12+
font-weight: bold;
13+
text-transform: uppercase;
14+
}
15+
16+
.customClass:hover {
17+
background-color: cornflowerblue;
18+
}

stories/icons.stories.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,12 @@ storiesOf('Icons', module)
7474
<br />
7575
</div>
7676
))
77+
.add('Icon with custom class & style', () => (
78+
<div>
79+
<span>Custom class: </span>
80+
<Icon icon="setting" className="customClass2" />
81+
<br />
82+
<span>Custom style: </span>
83+
<Icon icon="setting" style={{ color: 'red' }} />
84+
</div>
85+
))

stories/welcome.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ import React from 'react'
22

33
import { storiesOf } from '@storybook/react'
44

5+
import './CustomClasses.css'
6+
57
storiesOf('Welcome', module).add('Introduction', () => <h1>Welcome to HospitalRun Storybook</h1>)

test/icon.test.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,16 @@ describe('Icon', () => {
126126
expect(fontAwesomeIcon).toHaveLength(1)
127127
expect(fontAwesomeIcon.props().icon).toBe('cog')
128128
})
129+
130+
it('Icon can use custom class', () => {
131+
const iconWrapper = shallow(<Icon icon="setting" className="customClass" />)
132+
const fontAwesomeIcon = iconWrapper.find(FontAwesomeIcon)
133+
expect(fontAwesomeIcon.props().className).toEqual('customClass')
134+
})
135+
136+
it('Icon can use custom style', () => {
137+
const iconWrapper = shallow(<Icon icon="setting" style={{ background: 'red' }} />)
138+
const fontAwesomeIcon = iconWrapper.find(FontAwesomeIcon)
139+
expect(fontAwesomeIcon.props().style).toMatchObject({ background: 'red' })
140+
})
129141
})

0 commit comments

Comments
 (0)