This repository has been archived by the owner on Mar 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
fix(Avatar): fixing issues for the height of the element and the initials generation #38
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
02b7247
-improved logic for generating initials of the avatar
829a007
-added tests for the generateInitials methods im the Avatar component
c9b416b
-refactored generateInitials method and added as a default prop
1b60a84
-fixed Avatar-test.tsx
09de9f0
Merge branch 'master' into fix/avatar-issues
2c9ad61
-added records in the changelog
749deef
-removed empty line
20041ed
Merge branch 'master' into fix/avatar-issues
529222c
-replaced Avatar presence content and wrapper with circular icon
52d86e1
-removed borderSize from the icon component
a102b66
-removed inline style in the Avatar size example
b6fe3f8
-implementing the background of the padding as inherited
501ef56
-replaced generateInitials with getInitials
36ca9dc
-added presence indicator background variable with default to white
63483f6
Merge branch 'master' into fix/avatar-issues
c794820
-updated changelog
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
16 changes: 16 additions & 0 deletions
16
docs/src/examples/components/Avatar/Variations/AvatarExampleExcludedInitials.shorthand.tsx
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,16 @@ | ||
import React from 'react' | ||
import { Avatar } from '@stardust-ui/react' | ||
|
||
const AvatarExampleExcludedInitialsShorthand = () => ( | ||
<div> | ||
<Avatar name="John Doe (Software Developer)" status="Available" /> | ||
  | ||
<Avatar name="John Doe {Software Developer}" status="Available" /> | ||
  | ||
<Avatar name="John Doe [Software Developer]" status="Available" /> | ||
  | ||
<Avatar name="John A B Doe" status="Available" /> | ||
</div> | ||
) | ||
|
||
export default AvatarExampleExcludedInitialsShorthand |
10 changes: 10 additions & 0 deletions
10
docs/src/examples/components/Avatar/Variations/AvatarExampleGetInitials.shorthand.tsx
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,10 @@ | ||
import React from 'react' | ||
import { Avatar } from '@stardust-ui/react' | ||
|
||
const getInitials = name => name.split(' ').map(word => `${word[0]}.`) | ||
|
||
const AvatarExampleGetInitialsShorthand = () => ( | ||
<Avatar status="Available" name="John Doe" getInitials={getInitials} /> | ||
) | ||
|
||
export default AvatarExampleGetInitialsShorthand |
13 changes: 13 additions & 0 deletions
13
...es/components/Avatar/Variations/AvatarExamplePresenceIndicatorCustomization.shorthand.tsx
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,13 @@ | ||
import React from 'react' | ||
import { Avatar } from '@stardust-ui/react' | ||
|
||
const AvatarExamplePresenceIndicatorCustomizationShorthand = () => ( | ||
<Avatar | ||
src="/public/images/avatar/small/matt.jpg" | ||
alt="Profile picture of Matt" | ||
status="Available" | ||
variables={{ presenceIndicatorBackground: '#d3d3d3' }} | ||
/> | ||
) | ||
|
||
export default AvatarExamplePresenceIndicatorCustomizationShorthand |
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
6 changes: 0 additions & 6 deletions
6
docs/src/examples/components/Avatar/Variations/AvatarExampleSrc.shorthand.tsx
This file was deleted.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
docs/src/examples/components/Avatar/Variations/AvatarExampleSrc.tsx
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,6 @@ | ||
import React from 'react' | ||
import { Avatar } from '@stardust-ui/react' | ||
|
||
const AvatarExampleSrc = () => <Avatar src="public/images/avatar/small/matt.jpg" /> | ||
|
||
export default AvatarExampleSrc |
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 |
---|---|---|
|
@@ -16,7 +16,7 @@ class Avatar extends UIComponent<any, any> { | |
|
||
static displayName = 'Avatar' | ||
|
||
static handledProps = ['alt', 'as', 'className', 'name', 'size', 'src', 'status'] | ||
static handledProps = ['alt', 'as', 'className', 'getInitials', 'name', 'size', 'src', 'status'] | ||
|
||
static rules = avatarRules | ||
|
||
|
@@ -51,10 +51,34 @@ class Avatar extends UIComponent<any, any> { | |
'Offline', | ||
'PresenceUnknown', | ||
]), | ||
|
||
/** Custom method for generating the initials from the name property, shown in the avatar if there is no image provided. */ | ||
getInitials: PropTypes.func, | ||
} | ||
|
||
static defaultProps = { | ||
size: 5, | ||
getInitials(name: string) { | ||
if (!name) { | ||
return '' | ||
} | ||
|
||
const reducedName = name | ||
.replace(/\s*\(.*?\)\s*/g, ' ') | ||
.replace(/\s*{.*?}\s*/g, ' ') | ||
.replace(/\s*\[.*?]\s*/g, ' ') | ||
|
||
const initials = reducedName | ||
.split(' ') | ||
.filter(item => item !== '') | ||
.map(name => name.charAt(0)) | ||
.reduce((accumulator, currentValue) => accumulator + currentValue) | ||
|
||
if (initials.length > 2) { | ||
return initials.charAt(0) + initials.charAt(initials.length - 1) | ||
} | ||
return initials | ||
}, | ||
} | ||
|
||
static statusToIcon = { | ||
|
@@ -89,55 +113,43 @@ class Avatar extends UIComponent<any, any> { | |
} | ||
|
||
renderComponent({ ElementType, classes, rest }) { | ||
const { src, alt, name, status } = this.props | ||
const { src, alt, name, status, getInitials, size } = this.props | ||
const { icon = '', color = '' } = Avatar.statusToIcon[status] || {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just to not forget - there is a problem here that colors are defined based on status, and thus
Both these issues should be addressed by means of dedicated PRs - there is a corresponding issue created to track this work: #52 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the review! Will try to create issues for the other things that are not part of this PR |
||
|
||
const iconVariables = { | ||
color: 'white', | ||
backgroundColor: color, | ||
} | ||
|
||
return ( | ||
<ElementType {...rest} className={classes.root}> | ||
{src ? ( | ||
<Image className={classes.imageAvatar} avatar src={src} alt={alt} title={name} /> | ||
) : ( | ||
<Label | ||
className={classes.avatarLabel} | ||
className={classes.avatarNameContainer} | ||
as="div" | ||
content={this.getInitials(name)} | ||
content={getInitials(name)} | ||
variables={{ padding: '0px' }} | ||
circular | ||
title={name} | ||
/> | ||
)} | ||
{status && ( | ||
<span className={classes.presenceSpan}> | ||
<Label | ||
className={classes.presenceIconLabel} | ||
as="div" | ||
variables={{ padding: '0px' }} | ||
style={{ background: color }} | ||
<div className={classes.presenceIndicatorWrapper}> | ||
<Icon | ||
className={classes.presenceIndicator} | ||
size={size < 4 ? 'micro' : size < 6 ? 'mini' : 'tiny'} | ||
circular | ||
title={name} | ||
> | ||
<Icon | ||
size="mini" | ||
name={icon} | ||
color="white" | ||
style={avatarRules.presenceIcon()} | ||
title={status} | ||
/> | ||
</Label> | ||
</span> | ||
name={icon} | ||
variables={iconVariables} | ||
title={status} | ||
/> | ||
</div> | ||
)} | ||
</ElementType> | ||
) | ||
} | ||
|
||
getInitials(name: string): string { | ||
if (!name) { | ||
return '' | ||
} | ||
const names = name.split(' ') | ||
return names | ||
.map(name => (name.length ? name.charAt(0) : '')) | ||
.reduce((accumulator, currentValue) => accumulator + currentValue) | ||
} | ||
} | ||
|
||
export default Avatar |
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.
a bit worrying about performance implications of this - in general, we could have a linear complexity algorithm to achieve the same goal, while currently, because of regexp, we would use an algorithm of polynomial complexity (cubic at best). Taking the following things into account:
I would rather suggest to address this issue by introducing linear algorithm for handling this aspect and not introduce possibility for bottlenecks that could surprise client of the library.
So, maybe we should create an issue and address it by means of dedicated PR
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.
👍 to separate PR. would be good to see data before making changes.