Skip to content

Commit 848167d

Browse files
committed
Refactor Avatar and its stories
1 parent 8499bcb commit 848167d

File tree

4 files changed

+55
-62
lines changed

4 files changed

+55
-62
lines changed
Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,71 @@
1-
import { Story } from "@storybook/react";
2-
import { Avatar, AvatarIcon, AvatarProps } from "./Avatar";
1+
import type { Meta, StoryObj } from "@storybook/react";
2+
import { Avatar, AvatarIcon } from "./Avatar";
33
import PauseIcon from "@mui/icons-material/PauseOutlined";
44

5-
export default {
5+
const meta: Meta<typeof Avatar> = {
66
title: "components/Avatar",
77
component: Avatar,
88
};
99

10-
const Template: Story<AvatarProps> = (args: AvatarProps) => (
11-
<Avatar {...args} />
12-
);
10+
export default meta;
11+
type Story = StoryObj<typeof Avatar>;
1312

14-
export const Letter = Template.bind({});
15-
Letter.args = {
16-
children: "Coder",
13+
export const Letter: Story = {
14+
args: {
15+
children: "Coder",
16+
},
1717
};
1818

19-
export const LetterXL = Template.bind({});
20-
LetterXL.args = {
21-
children: "Coder",
22-
size: "xl",
19+
export const LetterXL = {
20+
args: {
21+
children: "Coder",
22+
size: "xl",
23+
},
2324
};
2425

25-
export const LetterDarken = Template.bind({});
26-
LetterDarken.args = {
27-
children: "Coder",
28-
colorScheme: "darken",
26+
export const LetterDarken = {
27+
args: {
28+
children: "Coder",
29+
colorScheme: "darken",
30+
},
2931
};
3032

31-
export const Image = Template.bind({});
32-
Image.args = {
33-
src: "https://avatars.githubusercontent.com/u/95932066?s=200&v=4",
33+
export const Image = {
34+
args: {
35+
src: "https://avatars.githubusercontent.com/u/95932066?s=200&v=4",
36+
},
3437
};
3538

36-
export const ImageXL = Template.bind({});
37-
ImageXL.args = {
38-
src: "https://avatars.githubusercontent.com/u/95932066?s=200&v=4",
39-
size: "xl",
39+
export const ImageXL = {
40+
args: {
41+
src: "https://avatars.githubusercontent.com/u/95932066?s=200&v=4",
42+
size: "xl",
43+
},
4044
};
4145

42-
export const MuiIcon = Template.bind({});
43-
MuiIcon.args = {
44-
children: <PauseIcon />,
46+
export const MuiIcon = {
47+
args: {
48+
children: <PauseIcon />,
49+
},
4550
};
4651

47-
export const MuiIconDarken = Template.bind({});
48-
MuiIconDarken.args = {
49-
children: <PauseIcon />,
50-
colorScheme: "darken",
52+
export const MuiIconDarken = {
53+
args: {
54+
children: <PauseIcon />,
55+
colorScheme: "darken",
56+
},
5157
};
5258

53-
export const MuiIconXL = Template.bind({});
54-
MuiIconXL.args = {
55-
children: <PauseIcon />,
56-
size: "xl",
59+
export const MuiIconXL = {
60+
args: {
61+
children: <PauseIcon />,
62+
size: "xl",
63+
},
5764
};
5865

59-
export const AvatarIconDarken = Template.bind({});
60-
AvatarIconDarken.args = {
61-
children: <AvatarIcon src="/icon/database.svg" />,
62-
colorScheme: "darken",
66+
export const AvatarIconDarken = {
67+
args: {
68+
children: <AvatarIcon src="/icon/database.svg" />,
69+
colorScheme: "darken",
70+
},
6371
};

site/src/components/Avatar/Avatar.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import MuiAvatar, { AvatarProps as MuiAvatarProps } from "@mui/material/Avatar";
44
import { makeStyles } from "@mui/styles";
55
import { FC } from "react";
66
import { combineClasses } from "utils/combineClasses";
7-
import { firstLetter } from "./firstLetter";
87

98
export type AvatarProps = MuiAvatarProps & {
109
size?: "sm" | "md" | "xl";
@@ -32,7 +31,6 @@ export const Avatar: FC<AvatarProps> = ({
3231
fitImage && styles.fitImage,
3332
])}
3433
>
35-
{/* If the children is a string, we always want to render the first letter */}
3634
{typeof children === "string" ? firstLetter(children) : children}
3735
</MuiAvatar>
3836
);
@@ -46,6 +44,14 @@ export const AvatarIcon: FC<{ src: string }> = ({ src }) => {
4644
return <img src={src} alt="" className={styles.avatarIcon} />;
4745
};
4846

47+
const firstLetter = (str: string): string => {
48+
if (str.length > 0) {
49+
return str[0].toLocaleUpperCase();
50+
}
51+
52+
return "";
53+
};
54+
4955
const useStyles = makeStyles((theme) => ({
5056
// Size styles
5157
sm: {

site/src/components/Avatar/firstLetter.test.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

site/src/components/Avatar/firstLetter.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)