-
Notifications
You must be signed in to change notification settings - Fork 331
/
Copy pathauto-complete-dropdown.tsx
72 lines (65 loc) · 2.08 KB
/
auto-complete-dropdown.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import React, { CSSProperties, useMemo } from 'react'
import useTheme from '../use-theme'
import { useAutoCompleteContext } from './auto-complete-context'
import Dropdown from '../shared/dropdown'
import useClasses from '../use-classes'
interface Props {
visible: boolean
className?: string
disableMatchWidth?: boolean
dropdownStyle?: CSSProperties
getPopupContainer?: () => HTMLElement | null
}
const defaultProps = {
className: '',
dropdownStyle: {},
}
type NativeAttrs = Omit<React.HTMLAttributes<any>, keyof Props>
export type AutoCompleteDropdownProps = Props & NativeAttrs
const AutoCompleteDropdown: React.FC<
React.PropsWithChildren<AutoCompleteDropdownProps>
> = ({
children,
visible,
className,
dropdownStyle,
disableMatchWidth,
getPopupContainer,
}: React.PropsWithChildren<AutoCompleteDropdownProps> & typeof defaultProps) => {
const theme = useTheme()
const { ref } = useAutoCompleteContext()
const isEmpty = useMemo(() => {
return !visible || React.Children.count(children) === 0
}, [children, visible])
const classes = useClasses('auto-complete-dropdown', className)
const clickHandler = (event: React.MouseEvent<HTMLDivElement>) => {
event.preventDefault()
event.stopPropagation()
event.nativeEvent.stopImmediatePropagation()
}
return (
<Dropdown
parent={ref}
visible={visible}
disableMatchWidth={disableMatchWidth}
getPopupContainer={getPopupContainer}
>
<div className={classes} style={dropdownStyle} onClick={clickHandler}>
{children}
<style jsx>{`
.auto-complete-dropdown {
border-radius: ${theme.layout.radius};
box-shadow: ${isEmpty ? 'none' : theme.expressiveness.shadowLarge};
background-color: ${theme.palette.background};
overflow-y: auto;
max-height: 15rem;
overflow-anchor: none;
}
`}</style>
</div>
</Dropdown>
)
}
AutoCompleteDropdown.defaultProps = defaultProps
AutoCompleteDropdown.displayName = 'GeistAutoCompleteDropdown'
export default AutoCompleteDropdown