[V2] autosize replacement
What is the replacement for autosize property in V2? I have tried to style the various style keys, but I can only get the control class to float or inline-block, which makes the component very small or the component extends the whole length of the component. I would like it to autosize to the size of the longest menu option like this. I tried posting this on stackoverflow too, but it appears no one answers that tag now. also related, how does the react-input-autosize component work with react-select. I read up that it is dependent on react-input-autosize, but I couldn't find any documentation on how it works.
Also confused about how this would work. An example would be great.
Any update on this issue? Nobody needs the autosize functionality?
Yeap. Any updates on this one plz?
Any update please? Stackoverflow thread
Temporary solution until they provide more options to customise it. Wrap the select component with a div and set whatever styles you want on that parent div.
Example:
<div style={{ float: "left", width: "90%" }}>
<Select
options={this.state.configOptions}
value={this.state.selectedOption}
getOptionValue={this.getConfigId}
getOptionLabel={this.getConfigLabel}
onChange={this.fetchConfig}
/>
</div>
I hope I didn't misunderstand your question.
Within Material UI v4.5.0 and React Select v2.4.3, I'm autosizing the select input by unsetting the following CSS properties in the inner divs (just the 3rd one should do though):
const useStyles = makeStyles(theme => ({
autosize: {
'& div': {
position: 'unset',
transform: 'unset',
},
},
}));
...
<ReactSelect className={classes.autosize} ... />
Greetings @toddcoulson
Sizing the select text input to the longest option is certainly possible and I've laid out some ways to achieve this in this thread: https://github.com/JedWatson/react-select/issues/4201
If you read the last post, you will see there are a litany of concerns for achieving this... but I can talk through again how one could implement such a thing.
But let's make some assumptions:
- This is a single Select (select container otherwise wouldn't be able to contain longest option + one more)
- Not async (Async could give varying widths at different points of typing).
- Not createable (As createable entry could be longest option in window as user types)
- Showing all options (Trimming this list to top n results would prevent the menu from knowing the longest option).
- Applying calculation once options are ready (not readjusting every key stroke changing the width of the Select with every key press as options filter)
- Not worried about resizing window (resizing would change available width for Options either too much or too little)
So now we have a non-async, non-createable single select that calculates width of longest option onMount.
I would approach this by unsetting the Menu width, so it would naturally fit to the content of its children
To get this information, it needs to render the menu in a way that is indistinguishable to the user (height: 0), calculating the width, hiding the menu, and then using the calculated width with the styles api to set the width of the Select.
... an hour later...
Here is a working example: https://codesandbox.io/s/react-select-longest-option-width-geunu?file=/src/App.js
Turns out that setting menuIsOpen does not actually trigger onMenuOpen, so I had to leverage the internal methods to do so after using timeouts to let the refs update.
You can add extra padding to adjust for the extra width of the Indicators (dropdown and clear indicator if enabled), if you like.
That said, this issue has been open for quite awhile and given the age and that there is a viable solution, I will be closing this ticket. In regards to feature requests, I believe this would be FAR easier to do if the options menu was show/hide instead of render/don't render, but in regards to this as a feature request, I think the complexities for all the edge cases outweigh the code bloat
Willing to explore more if there is enough interest, but this type of request would be better suited for the new discussions section so we can have users weigh in in conversation without taking up space from known issues and bugs. Thanks everyone and hope this answers some questions. Please feel free to reply if there's anything else.
This CSS style works for me. No need to use refs and it's worked for the latest version(of 5.2.1v) as well.
const stylesWidth = { control: css => ({ ...css, width: 150, }), menu: ({ width, ...css }) => ({ ...css, width: '400px', minWidth: '20%', }), option: css => ({ ...css, width: 370 }), };
sample project example: :100: https://codesandbox.io/s/react-select-options-width-resize-forked-h0sfg?file=/example.js