1
1
import CircularProgress from "@material-ui/core/CircularProgress"
2
- import { makeStyles , Theme } from "@material-ui/core/styles"
2
+ import { makeStyles } from "@material-ui/core/styles"
3
3
import TextField from "@material-ui/core/TextField"
4
4
import Autocomplete from "@material-ui/lab/Autocomplete"
5
5
import { useMachine } from "@xstate/react"
@@ -8,29 +8,22 @@ import { Avatar } from "components/Avatar/Avatar"
8
8
import { AvatarData } from "components/AvatarData/AvatarData"
9
9
import debounce from "just-debounce-it"
10
10
import { ChangeEvent , FC , useEffect , useState } from "react"
11
- import { combineClasses } from "util/combineClasses"
12
11
import { searchUserMachine } from "xServices/users/searchUserXService"
13
12
14
13
export type UserAutocompleteProps = {
15
14
value : User | null
16
15
onChange : ( user : User | null ) => void
17
16
label ?: string
18
- inputMargin ?: "none" | "dense" | "normal"
19
- inputStyles ?: string
20
17
className ?: string
21
- showAvatar ?: boolean
22
18
}
23
19
24
20
export const UserAutocomplete : FC < UserAutocompleteProps > = ( {
25
21
value,
26
22
onChange,
27
- className,
28
23
label,
29
- inputMargin,
30
- inputStyles,
31
- showAvatar = false ,
24
+ className,
32
25
} ) => {
33
- const styles = useStyles ( { showAvatar } )
26
+ const styles = useStyles ( )
34
27
const [ isAutocompleteOpen , setIsAutocompleteOpen ] = useState ( false )
35
28
const [ searchState , sendSearch ] = useMachine ( searchUserMachine )
36
29
const { searchResults } = searchState . context
@@ -53,6 +46,9 @@ export const UserAutocomplete: FC<UserAutocompleteProps> = ({
53
46
54
47
return (
55
48
< Autocomplete
49
+ className = { className }
50
+ options = { searchResults }
51
+ loading = { searchState . matches ( "searching" ) }
56
52
value = { value }
57
53
id = "user-autocomplete"
58
54
open = { isAutocompleteOpen }
@@ -80,22 +76,21 @@ export const UserAutocomplete: FC<UserAutocompleteProps> = ({
80
76
src = { option . avatar_url }
81
77
/>
82
78
) }
83
- options = { searchResults }
84
- loading = { searchState . matches ( "searching" ) }
85
- className = { combineClasses ( [ styles . autocomplete , className ] ) }
86
79
renderInput = { ( params ) => (
87
80
< TextField
88
81
{ ...params }
82
+ fullWidth
89
83
variant = "outlined"
90
- margin = { inputMargin ?? "normal" }
91
84
label = { label ?? undefined }
92
85
placeholder = "User email or username"
93
- className = { inputStyles }
86
+ className = { styles . textField }
94
87
InputProps = { {
95
88
...params . InputProps ,
96
89
onChange : handleFilterChange ,
97
- startAdornment : showAvatar && value && (
98
- < Avatar src = { value . avatar_url } > { value . username } </ Avatar >
90
+ startAdornment : value && (
91
+ < Avatar size = "sm" src = { value . avatar_url } >
92
+ { value . username }
93
+ </ Avatar >
99
94
) ,
100
95
endAdornment : (
101
96
< >
@@ -105,61 +100,24 @@ export const UserAutocomplete: FC<UserAutocompleteProps> = ({
105
100
{ params . InputProps . endAdornment }
106
101
</ >
107
102
) ,
103
+ classes : {
104
+ root : styles . inputRoot ,
105
+ } ,
108
106
} }
109
107
/>
110
108
) }
111
109
/>
112
110
)
113
111
}
114
112
115
- interface styleProps {
116
- showAvatar : boolean
117
- }
118
-
119
- export const useStyles = makeStyles < Theme , styleProps > ( ( theme ) => {
120
- return {
121
- autocomplete : ( props ) => ( {
122
- width : "100%" ,
123
-
124
- "& .MuiFormControl-root" : {
125
- width : "100%" ,
126
- } ,
127
-
128
- "& .MuiInputBase-root" : {
129
- width : "100%" ,
130
- // Match button small height
131
- height : props . showAvatar ? 60 : 40 ,
132
- } ,
133
-
134
- "& input" : {
135
- fontSize : 16 ,
136
- padding : `${ theme . spacing ( 0 , 0.5 , 0 , 0.5 ) } !important` ,
137
- } ,
138
- } ) ,
139
- }
140
- } )
141
-
142
- export const UserAutocompleteInline : React . FC < UserAutocompleteProps > = (
143
- props ,
144
- ) => {
145
- const style = useInlineStyle ( )
146
-
147
- return < UserAutocomplete { ...props } className = { style . inline } />
148
- }
149
-
150
- export const useInlineStyle = makeStyles ( ( ) => {
151
- return {
152
- inline : {
153
- width : "300px" ,
154
-
155
- "& .MuiFormControl-root" : {
156
- margin : 0 ,
157
- } ,
158
-
159
- "& .MuiInputBase-root" : {
160
- // Match button small height
161
- height : 36 ,
162
- } ,
113
+ export const useStyles = makeStyles ( ( theme ) => ( {
114
+ textField : {
115
+ "&:not(:has(label))" : {
116
+ margin : 0 ,
163
117
} ,
164
- }
165
- } )
118
+ } ,
119
+ inputRoot : {
120
+ paddingLeft : `${ theme . spacing ( 1.75 ) } px !important` , // Same padding left as input
121
+ gap : theme . spacing ( 0.5 ) ,
122
+ } ,
123
+ } ) )
0 commit comments