react-mdl-extra icon indicating copy to clipboard operation
react-mdl-extra copied to clipboard

SelectField doesn't work inside Dialog

Open vsviridov opened this issue 8 years ago • 3 comments

The drop downs are rendered underneath the dialog and are invisible.

import { Dialog, DialogContent } from 'react-mdl';
import { SelectField, Option } from 'react-mdl-extra';

export default (props) => (
  <Dialog open={props.open}>
    <DialogContent>
      <SelectField>
        <Option value={1}>One</Option>
      </SelectField>
    </DialogContent>
  </Dialog>
);

vsviridov avatar Nov 20 '16 04:11 vsviridov

This seems to be caused by two things:

  • the <Dialog> component from react-mdl uses the <dialog> html tag, which places its content in the top layer of the page, above all other content.
  • the <SelectField> component from this package (or, more accurately, the <Dropdown> component rendered by <SelectField>) renders the <OptionList> within a portal, making it a child of <body> and not the dialog.

There's not really an easy way around this, though I think it is something worth addressing since it's not exactly a rare case. I see two options:

  • sending the <OptionList> to the top layer itself by calling showModal() on it, where it will be stacked atop the dialog. This would require some extra handlers and pseudoclasses to get it to act naturally within the dialog. I'm not a fan of this method because <dialog> (the HTML tag, not the react-mdl element) is only supported natively in chrome and opera, so polyfill would have to be included just to use <SelectField>. Plus this just seems like a hack.
  • ditching the portal and making the <OptionList> a child of the dialog. I don't know the implications of this (I imagine there might be some overflow issues), but it seems like it might be the cleaner solution.

What do you think @HriBB ?

shanehughes3 avatar Jan 24 '17 02:01 shanehughes3

@shanehughes3 thanks for the info! Yeah there are some problems with react-portal. This is one of them. Another one is controlling portal isOpened prop manually which is needed for keyboard handling. One of the early versions used its own implementation of portal, but that brings a lot of complexity into the project. And bugs.

Maybe we can add inline prop, which renders dropdown inside component?

HriBB avatar Jan 24 '17 11:01 HriBB

In the future I will probably switch to https://material-components-web.appspot.com/ or something, because MDL v1 is not very react friendly.

HriBB avatar Jan 24 '17 11:01 HriBB