react-select: [v2.0.0-beta.2] Custom ValueContainer doesn't work without rendering "children"
I’m trying to use the ValueContainer option to render the selected option differently. Seems I cannot do that and still let the selected option (ValueContainer) be clickable and open the menu without also rendering the children prop. However, in rendering the children prop, I’m basically rendering the selected option twice. Clicking the arrow still works though.
Basically, this works fine (example from docs):
const ValueContainer = ({children, ...props}) => (
<components.ValueContainer {...props}>{children}</components.ValueContainer>
);
This does not work:
const ValueContainer = ({children, ...props}) => {
if (!props.hasValue) {
return <components.ValueContainer {...props}>{children}</components.ValueContainer>;
}
const value = props.getValue()[0];
return (
<components.ValueContainer {...props}>
<NodeItem node={value} />
</components.ValueContainer>
);
};
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 36
- Comments: 30
You could filter out the input from
childrenso you can still keep standard functionality:We were also facing this problem for a while. And below is the working code for us.
inputIdprop to<Select>component like below:@dpiotti I would not recommend to filter by
type.namebecause of minification (as you experienced yourself). Instead filter bytypeand compare withcomponents.But I would now recommend to do reverse filtering. Instead of filtering for the components you want to keep, you filter for the components you do not want to keep and prohibit them from rendering.
This also circumvents the problem that
DummyInputis not exported withcomponents.The bug still exists in the version 3.0.8 An easy fix is to set the property
blurInputOnSelecttotrue. The downside of it though is that it closes the drop-down every time the user makes a selection, even with the propertycloseMenuOnSelectset tofalse…I was able to get a workaround to work in the meantime. Note that this is for a select that has
isSearchable=falseso it’s using the dummy input component, but it may work with searchable selects with more CSS tweaks to hide the input component.With this approach, we render the children that has the focus/blur events and just hide the children content. For non-searchable selects, the dummy input should already be hidden, so in this case we just need to hide the value.
I am also having this exact issue, breaks
onFocusandonBlurprops on main<ReactSelect>component.Version 2.2.0Thank you for this solution, it solved my problem but now I can’t see placeholder text. Could you please tell how to render placeholder if no values are selected.
Nope, haven’t tried that, sorry
This may or may not help but I found in my case it was the focused prop on the multivalue child that causes this.
I had to add in
multiValueProps.isFocused = falseInside my custom ValueContainer is:
As children[1] is always the input.
So I assume you still need some kind of multi value in there or something which states it at least isn’t focused as it will be grabbing the focus over the input on click.
Would be nice to make this easier/get some clarification.