Files
react/components/auto-complete/auto-complete-dropdown.tsx
witt d4a1e02430 feat(scaleable): add scaleable props to each component (#531)
* feat(scaleable): add scaleable props to each component

* chore(scaleable): update the exported type

* feat: apply scaleable to components

chore: remove with-default

test: improve testcase for scaleable

chore: resolve test warning

ci: upgrade nodejs to latest lts

docs: fix type error in document site

* docs: update documents to be compatible with scaleable

chore: fix build errors

* chore: remove all size-related attributes

docs: improve guide document

* docs: add scaleable documentation

test: update snapshots

chore: remove unused

* feat: add scaleable to grid components

* docs: improve docs

* test: update snapshots

* fix(grid): fix basic component props
2021-06-23 10:53:30 +08:00

72 lines
2.0 KiB
TypeScript

import React, { CSSProperties, useMemo } from 'react'
import useTheme from '../use-theme'
import { useAutoCompleteContext } from './auto-complete-context'
import Dropdown from '../shared/dropdown'
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 clickHandler = (event: React.MouseEvent<HTMLDivElement>) => {
event.preventDefault()
event.stopPropagation()
event.nativeEvent.stopImmediatePropagation()
}
return (
<Dropdown
parent={ref}
visible={visible}
disableMatchWidth={disableMatchWidth}
getPopupContainer={getPopupContainer}>
<div
className={`auto-complete-dropdown ${className}`}
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