Files
react/components/select/select-dropdown.tsx
witt 4125e6f0a3 feat(dropdown): allow dropdown to set the specified container (#344)
* feat(dropdown): allow dropdown to set the specified container

* test(modal): update snapshots

* docs(select): add example for custom popup container

* fix(dropdown): fix type of getPopupContainer

* test(dropdown): add testcase for specified container rendering
2020-07-21 21:38:17 +08:00

59 lines
1.5 KiB
TypeScript

import React from 'react'
import useTheme from '../styles/use-theme'
import withDefaults from '../utils/with-defaults'
import { useSelectContext } from './select-context'
import Dropdown from '../shared/dropdown'
interface Props {
visible: boolean
className?: string
dropdownStyle?: object
disableMatchWidth?: boolean
getPopupContainer?: () => HTMLElement | null
}
const defaultProps = {
className: '',
dropdownStyle: {},
}
type NativeAttrs = Omit<React.HTMLAttributes<any>, keyof Props>
export type SelectDropdownProps = Props & typeof defaultProps & NativeAttrs
const SelectDropdown: React.FC<React.PropsWithChildren<SelectDropdownProps>> = ({
visible,
children,
className,
dropdownStyle,
disableMatchWidth,
getPopupContainer,
}) => {
const theme = useTheme()
const { ref } = useSelectContext()
return (
<Dropdown
parent={ref}
visible={visible}
disableMatchWidth={disableMatchWidth}
getPopupContainer={getPopupContainer}>
<div className={`select-dropdown ${className}`} style={dropdownStyle}>
{children}
<style jsx>{`
.select-dropdown {
border-radius: ${theme.layout.radius};
box-shadow: ${theme.expressiveness.shadowLarge};
background-color: ${theme.palette.background};
max-height: 15rem;
overflow-y: auto;
overflow-anchor: none;
padding: ${theme.layout.gapQuarter} 0;
}
`}</style>
</div>
</Dropdown>
)
}
export default withDefaults(SelectDropdown, defaultProps)