Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to style the suggestion popup #40

Open
andrewjdavison opened this issue Jun 26, 2018 · 12 comments
Open

How to style the suggestion popup #40

andrewjdavison opened this issue Jun 26, 2018 · 12 comments

Comments

@andrewjdavison
Copy link

G'day,

I'm trying to use this component, but when I render it the popup appears to take the entire window width rather than conforming to the enveloping div. Here's what it looks like rendered...

image

Any hints on whether this is a bug or is there a prop somewhere I can tweak to rest the style?

@Giners
Copy link
Owner

Giners commented Jun 26, 2018

Its probably a bug/missing functionality. Do you have a snippet/repo that I can check out?

@andrewjdavison
Copy link
Author

Just added you to the repo that has the client code... The component in question is /src/Component/PersonalDetails.jsx

Cheers!

@Giners
Copy link
Owner

Giners commented Jun 28, 2018

Hi @andrewjdavison,

Thanks for access to the repo with the clientcode. So the problem isn't a bug but a lack of functionality that provides fine grained control over the appearance of <MUIPlacesAutocomplete>. There is a feature request filed to track this functionality that would allow fine grained control over the appearance: #29.

I've confirmed that this is indeed the issue as 1) if you change this line (https://github.com/Giners/mui-places-autocomplete/blob/master/src/MUIPlacesAutocomplete.jsx#L70) to read:

-        style={{ left: 0, right: 0, zIndex: 1 }}
+        style={{ left: 0, right: 0, zIndex: 1, width: 400 }}

You will see that the <div> element that holds the suggestions is modified and doesn't take the full width of the screen (which may be what you desire). Also, 2) there is no prop you can pass today to modify the appearance of the suggestions container as we have just done.

If you want to see this for yourself you can checkout this repo and make changes to <MUIPlacesAutocomplete> and then yarn build and then yarn demo.

I would love to resolve this for you and have you use <MUIPlacesAutocomplete>. Unfortunately I don't have much time at the moment otherwise I would have worked on #29 already. I'm starting a new job and am packing up the house to move in a couple of weeks.

I'll try to find a couple of hours to work on it this weekend. If I can't then I would be happy to help you find a replacement for <MUIPlacesAutocomplete> if you aren't able to wait.

@andrewjdavison
Copy link
Author

andrewjdavison commented Jun 28, 2018 via email

@Giners
Copy link
Owner

Giners commented Jun 28, 2018

If you could and wanted to open a PR that would be great. Let me know if you have any questions. We can collaborate on it and get it done probably pretty fast together. My thoughts with #29 was to use styling/withStyles() but this seems more complicated than it needs to be at the moment.

One thought would be to take a new prop named suggestionsContainerProps which would take an object with style properties you wanted to apply to the container that contains rendered suggestions. Its properties could then be spread on the line that I mentioned. Things "ought to work" after that.

Let me know if you take a crack at it and what you think.

@andrewjdavison
Copy link
Author

andrewjdavison commented Jun 28, 2018 via email

@robinknox
Copy link

Hi @Giners and @andrewjdavison I've created a pull request #41 to resolve this issue based upon the suggestionsContainerProps method mentioned above.

@BartoGabriel
Copy link

I had the same problem. I solved it in another way. I use the property anchorEl (https://material-ui.com/api/popper/)

I share it in case it helps someone:

Setting the anchorEl property of the Popper element.

      <Popper
      anchorEl={this.inputEl}
      placement="bottom-start"
        //modifiers={({ inner: { enabled: true } })}
        //style={{ left: 0, right: 0, zIndex: 1 }}
      >

Considering defining the input variable in the constructor:

  constructor() {
    super()

    // Control the <input> element/<Autosuggest> component and make this React component the source
    // of truth for their state.
    this.state = {
      suggestions: [],
    }
    this.inputEl = null; 
    this.onInputValueChange = this.onInputValueChange.bind(this)
    this.onSuggestionSelected = this.onSuggestionSelected.bind(this)
    this.renderAutocomplete = this.renderAutocomplete.bind(this)
  }

And changing the renderAutocomplete, so that it defines the variable inputEl with the reference of the TextField

  renderAutocomplete({
    getInputProps,
    getItemProps,
    isOpen,
    inputValue,
    highlightedIndex,
  }) {
    const { suggestions } = this.state
    const { renderTarget, textFieldProps } = this.props

    // We need to store the input reference for our Popper element containg the suggestions
    const storeInputRef = input => {
      this.inputEl = input;
    };
    // We set the value of 'tag' on the <Manager> component to false to allow the rendering of
    // children instead of a specific DOM element.
    //
    // We only want to render our suggestions container if Downshift says we are open AND there are
    // suggestions to actually render. There may not be suggestions yet due to the async nature of
    // requesting them from the Google Maps/Places service.
    //
    // Provide an 'id' to the input props (see <TextField>) to accommodate SSR. If we don't then we
    // will see checksum errors with the 'id' prop of the <input> element not matching what was
    // rendered on the server vs. what was rendered on the client after rehydration due to automatic
    // 'id' prop generation by <Downshift>.
    return (
      <div>
        <Manager tag={false}>
          <TextField
            inputRef={storeInputRef}
            {...getInputProps({ id: 'mui-places-autocomplete-input', ...textFieldProps })} />
          <Target>{renderTarget()}</Target>
          {isOpen ? MUIPlacesAutocomplete.renderSuggestionsContainer(
            suggestions,
            { getItemProps, inputValue, highlightedIndex },
          )
            : null}
        </Manager>
      </div>
    )
  }

@yosefprogramming
Copy link

If I would like to style it in a way where the width wouldn't be 100% how would I do so?

@robinknox
Copy link

Hi @yosefprogramming I raised pull request #41 for this feature however I've been unable to get a reply from @Giners for a few months now. I'll probably publish my fork to npm.

@yosefprogramming
Copy link

Hey @robinknox. Thanks would really be usefull. Also would appreciate if you let me know once you publish the fork.

@danielmana
Copy link

As a workaround it's also possible to use a wrapper. eg:

<div style={{ position: 'relative' }}>
  <MUIPlacesAutocomplete
     onSuggestionSelected={this.onSuggestionSelected}
     renderTarget={() => <div />}
     textFieldProps={{ fullWidth: true }}
  />
</div>

This way the popover matches the input's width

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants