-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #208 from iCHEF/feature/avatar
Add <Avatar> to display an image along with the text
- Loading branch information
Showing
15 changed files
with
262 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import classNames from 'classnames'; | ||
|
||
import './styles/Avatar.scss'; | ||
|
||
import icBEM from './utils/icBEM'; | ||
import prefixClass from './utils/prefixClass'; | ||
|
||
const COMPONENT_NAME = prefixClass('avatar'); | ||
const ROOT_BEM = icBEM(COMPONENT_NAME); | ||
|
||
const SQUARE = 'square'; | ||
const ROUNDED = 'rounded'; | ||
const CIRCLE = 'circle'; | ||
export const AVATAR_TYPE = { SQUARE, ROUNDED, CIRCLE }; | ||
|
||
function Avatar({ | ||
className, | ||
src, | ||
alt, | ||
type, | ||
...otherProps | ||
}) { | ||
const bemClass = ROOT_BEM.modifier(type); | ||
|
||
const rootClassName = classNames(className, `${bemClass}`); | ||
|
||
return ( | ||
<div className={rootClassName} {...otherProps}> | ||
<img alt={alt} src={src} /> | ||
</div> | ||
); | ||
} | ||
|
||
Avatar.propTypes = { | ||
src: PropTypes.string.isRequired, | ||
alt: PropTypes.string.isRequired, | ||
type: PropTypes.oneOf(Object.values(AVATAR_TYPE)), | ||
}; | ||
|
||
Avatar.defaultProps = { | ||
type: SQUARE, | ||
}; | ||
|
||
export default Avatar; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { shallow } from 'enzyme'; | ||
|
||
import Avatar from '../Avatar'; | ||
|
||
describe('<Avatar>', () => { | ||
it('renders without crashing', () => { | ||
const div = document.createElement('div'); | ||
const element = <Avatar src="LINK" alt="ALT" />; | ||
|
||
ReactDOM.render(element, div); | ||
}); | ||
|
||
it('handles type modifiers', () => { | ||
let wrapper = shallow(<Avatar src="LINK" alt="ALT" />); | ||
expect(wrapper.hasClass('gyp-avatar--square')).toBeTruthy(); | ||
|
||
wrapper = shallow(<Avatar type="square" src="LINK" alt="ALT" />); | ||
expect(wrapper.hasClass('gyp-avatar--square')).toBeTruthy(); | ||
|
||
wrapper = shallow(<Avatar type="rounded" src="LINK" alt="ALT" />); | ||
expect(wrapper.hasClass('gyp-avatar--rounded')).toBeTruthy(); | ||
|
||
wrapper = shallow(<Avatar type="circle" src="LINK" alt="ALT" />); | ||
expect(wrapper.hasClass('gyp-avatar--circle')).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
@import "./mixins"; | ||
|
||
// ------------------------------------- | ||
// Component Block | ||
// ------------------------------------- | ||
.#{$prefix}-avatar { | ||
width: 2.2rem; | ||
height: 2.2rem; | ||
overflow: hidden; | ||
margin: .2rem .45rem .2rem .25rem; | ||
background-color: $c-gray; | ||
flex-shrink: 0; | ||
|
||
// ---------------------- | ||
// <Avatar> variants | ||
// ---------------------- | ||
&--square { | ||
border-radius: 0; | ||
} | ||
|
||
&--rounded { | ||
border-radius: 6px; | ||
} | ||
|
||
&--circle { | ||
border-radius: 50%; | ||
} | ||
|
||
// ---------------------- | ||
// Inner image | ||
// ---------------------- | ||
& > img { | ||
width: 100%; | ||
height: 100%; | ||
text-align: center; | ||
object-fit: cover; | ||
} | ||
} | ||
|
||
// Override the default left margin on <ListRow> | ||
.#{$prefix}-list-row > .#{$prefix}-avatar { | ||
&:first-child { | ||
margin-left: 0; | ||
} | ||
} | ||
|
||
.#{$prefix}-list-row__body > .#{$prefix}-avatar { | ||
&:first-child { | ||
margin-left: 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React from 'react'; | ||
|
||
import Avatar from '@ichef/gypcrete/src/Avatar'; | ||
import FlexRow from 'utils/FlexRow'; | ||
|
||
function BasicAvatarExample() { | ||
return ( | ||
<FlexRow> | ||
<Avatar alt="Avatar of Design" src="https://api.adorable.io/avatars/285/design@ichef.tw" /> | ||
<Avatar type="square" alt="Avatar of RD" src="https://api.adorable.io/avatars/285/rd@ichef.tw" /> | ||
<Avatar type="rounded" alt="Avatar of Marketing" src="https://api.adorable.io/avatars/285/marketing@ichef.tw" /> | ||
<Avatar type="circle" alt="Avatar of Customer Service" src="https://api.adorable.io/avatars/285/customer_service@ichef.tw" /> | ||
</FlexRow> | ||
); | ||
} | ||
|
||
export default BasicAvatarExample; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { storiesOf } from '@storybook/react'; | ||
import { withInfo } from '@storybook/addon-info'; | ||
|
||
import Avatar from '@ichef/gypcrete/src/Avatar'; | ||
import getPropTables from 'utils/getPropTables'; | ||
|
||
import BasicAvatar from './BasicAvatar'; | ||
|
||
storiesOf('@ichef/gypcrete|Avatar', module) | ||
.add('basic usage', withInfo()(BasicAvatar)) | ||
// Props table | ||
.add('props', getPropTables([Avatar])); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.