Skip to content
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

fix(editor): Update design system Avatar component to show initials also when only firstName or lastName is given #10308

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions packages/design-system/src/components/N8nAvatar/Avatar.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<template>
<span :class="['n8n-avatar', $style.container]" v-bind="$attrs">
<Avatar
v-if="firstName"
v-if="name"
:size="getSize(size)"
:name="firstName + ' ' + lastName"
:name="name"
variant="marble"
:colors="getColors(colors)"
/>
<div v-else :class="[$style.empty, $style[size]]"></div>
<span v-if="firstName" :class="$style.initials">{{ initials }}</span>
<span v-else :class="[$style.empty, $style[size]]" />
<span v-if="name" :class="$style.initials">{{ initials }}</span>
</span>
</template>

Expand Down Expand Up @@ -38,7 +38,8 @@ const props = withDefaults(defineProps<AvatarProps>(), {
],
});

const initials = computed(() => getInitials(`${props.firstName} ${props.lastName}`));
const name = computed(() => `${props.firstName} ${props.lastName}`.trim());
const initials = computed(() => getInitials(name.value));

const getColors = (colors: string[]): string[] => {
const style = getComputedStyle(document.body);
Expand All @@ -62,6 +63,7 @@ const getSize = (size: string): number => sizes[size];
}

.empty {
display: block;
border-radius: 50%;
background-color: var(--color-foreground-dark);
opacity: 0.3;
Expand All @@ -72,7 +74,8 @@ const getSize = (size: string): number => sizes[size];
font-size: var(--font-size-2xs);
font-weight: var(--font-weight-bold);
color: var(--color-avatar-font);
text-shadow: 0px 1px 6px rgba(25, 11, 9, 0.3);
text-shadow: 0 1px 6px rgba(25, 11, 9, 0.3);
text-transform: uppercase;
}

.small {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { render } from '@testing-library/vue';
import N8nAvatar from '../Avatar.vue';

describe('components', () => {
describe('N8nAlert', () => {
test.each([
['Firstname', 'Lastname', 'FL'],
['Firstname', undefined, 'Fi'],
[undefined, 'Lastname', 'La'],
[undefined, undefined, ''],
['', '', ''],
Comment on lines +6 to +11
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice setup :)

])('should render initials for name "%s %s" as %s', (firstName, lastName, initials) => {
const { container, getByText } = render(N8nAvatar, {
props: { firstName, lastName },
});

if (firstName || lastName) {
expect(container.querySelector('svg')).toBeVisible();
expect(getByText(initials)).toBeVisible();
} else {
expect(container.querySelector('svg')).not.toBeInTheDocument();
}
});
});
});
Loading