react-autocomplete icon indicating copy to clipboard operation
react-autocomplete copied to clipboard

Dropdown list in modal window

Open Dashevskyi opened this issue 7 years ago • 4 comments

Hello

I'm set Autocomplete component in main window and all work good, but when i set in modal window (mdb), dropdownlist place not under input. Can help me ? 2018-07-31 09 11 15

thanks

Dashevskyi avatar Jul 31 '18 05:07 Dashevskyi

having the same problem... Have you found a way to solve this ? help much appreciated

leoparis89 avatar Sep 17 '18 14:09 leoparis89

Found a workaround: Set position to absolute for menuStyle and 'relative' for wrapperStyle. Then you have to ajust the top property according to the height of your input box


   wrapperStyle={{
          position: 'relative'
        }}

        menuStyle={{
          top: // dependent of the height of your input
          left: 0,
          position: 'absolute'
        }}

leoparis89 avatar Sep 17 '18 14:09 leoparis89

The original algorithm is using getBoundingClientRect(), ignoring the fact that the menu and the input could belong to different offset parents. I came up with the following workaround:

function fixMenuPosition(autocomplete: ReactAutocomplete | null, style: CSSProperties) {
   if (!autocomplete) { return style }

   let node = autocomplete.refs.input as HTMLElement;
   const menu = autocomplete.refs.menu as HTMLElement;
   if (!node || !menu) { return style }

   let { left, top } = style;
   if (typeof left !== 'number' || typeof top !== 'number') { return style }

   // undo the original positioning, but keep the margins
   const rect = node.getBoundingClientRect();
   left -= rect.left;
   top -= rect.top;

   // go up the child tree until both children belong to the same offset parent
   for (;;) {
      left += node.offsetLeft;
      top += node.offsetTop;

      const parent = node.offsetParent;
      if (parent === menu.offsetParent) {
         break;
      }

      if (!(parent instanceof HTMLElement)) { return style }
      node = parent;
   }

   return { ...style, left, top };
}

It assumes that menu and input have a common offset parent somewhere up the tree, which should be the case unless you did something crazy like rendering your input into a portal. Example of usage:


<Autocomplete ref={this.autoCompleteRef} renderMenu={renderMenu} .... />
...
function renderMenu(items: any, value: string, style: React.CSSProperties) {
   return <div style={fixMenuPosition(this.autoCompleteRef.current, style)} ... />
}

@CMTegner @ryanflorence can we please fix that so we don't have to use hacky workarounds?

Torvin avatar Apr 01 '19 02:04 Torvin

Solution from @leoparis89 worked as a charm

FacundoGFlores avatar Apr 23 '19 09:04 FacundoGFlores