index.tsx 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import styled from '@emotion/styled';
  2. import Menu from './menu';
  3. type MenuProps = React.ComponentProps<typeof Menu>;
  4. type Props = {
  5. // Should clicking the actor toggle visibility
  6. allowActorToggle?: boolean;
  7. } & MenuProps;
  8. const DropdownAutoComplete = ({allowActorToggle = false, children, ...props}: Props) => (
  9. <Menu {...props}>
  10. {renderProps => {
  11. const {isOpen, actions, getActorProps} = renderProps;
  12. // Don't pass `onClick` from `getActorProps`
  13. const {onClick: _onClick, ...actorProps} = getActorProps<HTMLDivElement>();
  14. return (
  15. <Actor
  16. isOpen={isOpen}
  17. role="button"
  18. tabIndex={0}
  19. onClick={isOpen && allowActorToggle ? actions.close : actions.open}
  20. {...actorProps}
  21. >
  22. {children(renderProps)}
  23. </Actor>
  24. );
  25. }}
  26. </Menu>
  27. );
  28. const Actor = styled('div')<{isOpen: boolean}>`
  29. position: relative;
  30. width: 100%;
  31. /* This is needed to be able to cover dropdown menu so that it looks like one unit */
  32. ${p => p.isOpen && `z-index: ${p.theme.zIndex.dropdownAutocomplete.actor}`};
  33. `;
  34. export default DropdownAutoComplete;