-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
[v2.0.0-beta.2] Custom ValueContainer doesn't work without rendering "children" #2597
Comments
Any update on this? Or am I doing something wrong? I don't think I will be able to upgrade to v2 until this is resolved. |
I'm running into this issue as well. I even tried wrapping the children in a div with visibility hidden or display none and neither kept the click event. Is there a good workaround or solution for this issue? Thanks! |
I was able to get a workaround to work in the meantime. Note that this is for a select that has 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.
|
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 Inside 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. |
hey @einarq did you know how to change the |
Nope, haven't tried that, sorry |
I'm running into a very similar issue. |
I managed to make it work thanks to @maureenwaggl hack https://codesandbox.io/s/6w1nxnyqkz. |
I am also having this exact issue, breaks |
I believe this is because Lines 1380 to 1408 in 770ba08
There should be a |
You could filter out the input from const ValueContainer = ({children, ...props}) =>{
var selectInput = React.Children.toArray(children).find((input) => input.type.name === "Input" || input.type.name === "DummyInput");
return <components.ValueContainer { ...props }>
{ /* Whatever you want to render */ }
{selectInput}
</components.ValueContainer>
} |
@Rall3n that's a nice solution, hadn't thought about that. For now I used a workaround with the |
@Rall3n FYI, I tried the suggested approach, still doesn't work. The input is being rendered correctly but the functionality still isn't the same ( Can't investigate this any further right now, so I'll keep my |
Be careful with the solution proposed by @Rall3n as it might not work in production because of minification. To fix the problem I have added the displayName property manually to the Input component to have the component name in production: export const Input: React.FC<InputProps> = props => (
<components.Input {...props} />
);
Input.displayName = 'Input'; Then you can filter your Input component using |
The bug still exists in the version 3.0.8 |
I found out that checking for |
We were also facing this problem for a while.
|
Thanks @Rall3n . This worked for local but not after production build. Is there a way we can set displayName for the components input? |
@carlostxm May I know how to insert this code? Do we need to fork the library? |
@wesleywong @carlostxm There is a better solution that does not require you to add a You can check if the type of the component is the same as const CustomValueContainer = ({ children, ...props }) => <components.ValueContainer {...props}>
{React.Children.map(children, (child) => child.type === components.Input ? child : null)}
</components.ValueContainer> |
@merykozlowska Then either replace <components.ValueContainer>
{React.Children.map(children, (child) => [components.Input, components.DummyInput].indexOf(child.type) >= 0 ? child : null)}
</components.ValueContainer> |
I would've done exactly that but... |
@Rall3n I found that I also needed to add 'w' to the array. After building my project I noticed the name for the dummyInputs got converted to w. This worked for me: const childsToRender = React.Children.toArray(children).filter((child) => ['Input', 'DummyInput', 'Placeholder', 'w'].indexOf(child.type.name) >= 0); |
@dpiotti I would not recommend to filter by 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. const ValueContainer = ({children, ...props}) => <components.ValueContainer {...props}>
{React.Children.map(children, (child) => (child && [components.SingleValue].indexOf(child.type) === -1) ? child : null)}
</components.ValueContainer>; This also circumvents the problem that |
@Rall3n Good note. Reverse filtering was the way to go for me |
Couldn't get this to work. Thanks for being of help on SO: https://stackoverflow.com/questions/61467417/how-to-change-valuecontainer-of-react-select-so-that-minifying-does-not-introduc |
I will be closing out this ticket to continue focusing on existing bugs and issues. I can reopen this if necessary but it seems that @Rall3n has identified a working solution. Thank you! @gregholst if you would still like assistance, please feel free to reply to this with a codesandbox, so we can take a look into this further with you. |
Thank 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. |
To the people who are trying to fix this issue, please upgrade to the latest version (5.7.4). The issue was fixed for me without any additional fixes required. I have done this directly: const ValueContainer = ({ children, ...props }: ValueContainerProps<OptionType>): ReactElement => {
const [values, input] = children as ReactElement[];
const valueLength = props?.getValue()?.length;
return (
<components.ValueContainer {...props}>
{valueLength > 1 ? (
<CustomTag label={`${valueLength} Items Selected`} pr={3} />
) : (
values
)}
{input}
</components.ValueContainer>
);
}; |
Thanks! Works perfect 👍 |
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):
This does not work:
The text was updated successfully, but these errors were encountered: