Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c9bedc5

Browse files
authored
Feat: add showAvatar option to User Autocomplete (#4269)
* added avatar * remove commented code
1 parent 8c4de49 commit c9bedc5

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import Avatar from "@material-ui/core/Avatar"
2+
import { makeStyles } from "@material-ui/core/styles"
3+
import { User } from "api/typesGenerated"
4+
import { FC } from "react"
5+
import { firstLetter } from "../../util/firstLetter"
6+
7+
export const AutocompleteAvatar: FC<{ user: User }> = ({ user }) => {
8+
const styles = useStyles()
9+
10+
return (
11+
<div className={styles.avatarContainer}>
12+
{user.avatar_url ? (
13+
<img className={styles.avatar} alt={`${user.username}'s Avatar`} src={user.avatar_url} />
14+
) : (
15+
<Avatar>{firstLetter(user.username)}</Avatar>
16+
)}
17+
</div>
18+
)
19+
}
20+
21+
export const useStyles = makeStyles((theme) => {
22+
return {
23+
avatarContainer: {
24+
margin: "0px 10px",
25+
},
26+
avatar: {
27+
width: theme.spacing(4.5),
28+
height: theme.spacing(4.5),
29+
borderRadius: "100%",
30+
},
31+
}
32+
})

site/src/components/UserAutocomplete/UserAutocomplete.tsx

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
11
import CircularProgress from "@material-ui/core/CircularProgress"
2-
import { makeStyles } from "@material-ui/core/styles"
2+
import { makeStyles, Theme } from "@material-ui/core/styles"
33
import TextField from "@material-ui/core/TextField"
44
import Autocomplete from "@material-ui/lab/Autocomplete"
55
import { useMachine } from "@xstate/react"
66
import { User } from "api/typesGenerated"
77
import { AvatarData } from "components/AvatarData/AvatarData"
88
import debounce from "just-debounce-it"
9-
import { ChangeEvent, useEffect, useState } from "react"
9+
import { ChangeEvent, FC, useEffect, useState } from "react"
1010
import { searchUserMachine } from "xServices/users/searchUserXService"
11+
import { AutocompleteAvatar } from "./AutocompleteAvatar"
1112

1213
export type UserAutocompleteProps = {
1314
value: User | null
1415
onChange: (user: User | null) => void
1516
label?: string
1617
inputMargin?: "none" | "dense" | "normal"
1718
inputStyles?: string
19+
showAvatar?: boolean
1820
}
1921

20-
export const UserAutocomplete: React.FC<UserAutocompleteProps> = ({
22+
export const UserAutocomplete: FC<UserAutocompleteProps> = ({
2123
value,
2224
onChange,
2325
label,
2426
inputMargin,
2527
inputStyles,
28+
showAvatar = false,
2629
}) => {
27-
const styles = useStyles()
30+
const styles = useStyles({ showAvatar })
2831
const [isAutocompleteOpen, setIsAutocompleteOpen] = useState(false)
2932
const [searchState, sendSearch] = useMachine(searchUserMachine)
3033
const { searchResults } = searchState.context
@@ -94,6 +97,9 @@ export const UserAutocomplete: React.FC<UserAutocompleteProps> = ({
9497
InputProps={{
9598
...params.InputProps,
9699
onChange: handleFilterChange,
100+
startAdornment: (
101+
<>{showAvatar && selectedValue && <AutocompleteAvatar user={selectedValue} />}</>
102+
),
97103
endAdornment: (
98104
<>
99105
{searchState.matches("searching") ? <CircularProgress size={16} /> : null}
@@ -106,9 +112,14 @@ export const UserAutocomplete: React.FC<UserAutocompleteProps> = ({
106112
/>
107113
)
108114
}
109-
export const useStyles = makeStyles((theme) => {
115+
116+
interface styleProps {
117+
showAvatar: boolean
118+
}
119+
120+
export const useStyles = makeStyles<Theme, styleProps>((theme) => {
110121
return {
111-
autocomplete: {
122+
autocomplete: (props) => ({
112123
width: "100%",
113124

114125
"& .MuiFormControl-root": {
@@ -118,14 +129,14 @@ export const useStyles = makeStyles((theme) => {
118129
"& .MuiInputBase-root": {
119130
width: "100%",
120131
// Match button small height
121-
height: 40,
132+
height: props.showAvatar ? 60 : 40,
122133
},
123134

124135
"& input": {
125136
fontSize: 16,
126137
padding: `${theme.spacing(0, 0.5, 0, 0.5)} !important`,
127138
},
128-
},
139+
}),
129140

130141
avatar: {
131142
width: theme.spacing(4.5),

site/src/pages/CreateWorkspacePage/CreateWorkspacePageView.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ export const CreateWorkspacePageView: FC<React.PropsWithChildren<CreateWorkspace
149149
onChange={(user) => props.setOwner(user)}
150150
label={t("ownerLabel")}
151151
inputMargin="dense"
152+
showAvatar
152153
/>
153154
)}
154155

0 commit comments

Comments
 (0)