-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add <Avatar> to display an image along with the text #208
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8789a56
Add a basic Avatar component
hsunpei ba7e9d1
Add different types of avatars
hsunpei 2ab1484
Add tests for Avatar
hsunpei c34bbfe
Adjust the styles of Avatar
hsunpei b8df563
Add the avatar to rowComp
hsunpei b425c33
Add Avatar to the SelectOption
hsunpei 225d756
Add Avatar to SelectRow
hsunpei 927eb1e
Refine the examples of the avatars
hsunpei e7d5c89
Add the avatar test for SelectRow
hsunpei 22ba40e
Add CHANGELOG for Avatar
hsunpei a0c7eee
Prevent the avatar from shrinking
hsunpei edf8fee
Refine the CSS styles by using the parent selector
hsunpei 847c092
Refine the example avatar
hsunpei ad3cee0
Add the missing PropTypes
hsunpei 880938c
Merge the ways to get labels and avatars
hsunpei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing type check here?