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

When using ValueContainer as a custom component, if children is not rendered, the menu does not close when clicking outside #5982

Open
uu29 opened this issue Dec 9, 2024 · 5 comments
Labels
issue/bug-unconfirmed Issues that describe a bug that hasn't been confirmed by a maintainer yet

Comments

@uu29
Copy link

uu29 commented Dec 9, 2024

I'm using the react-select 5.8.3 version, and I'm using components properties to extend the ValueContainer to custom components, and I'm writing this after finding an issue.

When using a multi-select, I implemented a conditional statement as shown below to customize the appearance of the ValueContainer as a custom component.

// my custom ValueContainer.tsx
function ValueContainer(props: ValueContainerProps<OptionBase[] | OptionBase>) {
  const {
    children,
    selectProps: { value, isMulti },
  } = props;

  if (!isMulti) {
    return <components.ValueContainer {...props}>{children}</components.ValueContainer>;
  }

  const valueLength = Array.isArray(value) ? value.length : 0;
  const text = valueLength > 0 ? {{MY_CUSTOM_TEXT}} : children; // If isMulti is true and there is at least one selected value, it renders custom text.

  return <components.ValueContainer {...props}>{text}</components.ValueContainer>; // clicking outside the Select’s DOM does not close the menu
}

It seems that when children is not rendered in the ValueContainer as per the condition mentioned above, the menu close does not work when clicking outside.

@uu29 uu29 added the issue/bug-unconfirmed Issues that describe a bug that hasn't been confirmed by a maintainer yet label Dec 9, 2024
@ssabii
Copy link

ssabii commented Dec 9, 2024

I have this same issue.

@uu29
Copy link
Author

uu29 commented Dec 9, 2024

I resolved this issue using this method

// when rendering custom component
const noValueChildren = React.Children.toArray(children).filter((child) => [components.Input, components.Placeholder].includes(child.type));

    return (
      <components.ValueContainer {...props}>
        {MY_CUSTOM_TEXT}
        {noValueChildren}
      </components.ValueContainer>
    );

@kukurudz-volodymyr
Copy link

@uu29 didn't work for me, but this answer helped to solve this issue without filtering - #4845 (comment)

@uu29
Copy link
Author

uu29 commented Dec 12, 2024

@uu29 didn't work for me, but this answer helped to solve this issue without filtering - #4845 (comment)

thanks, but still doesn't work. I Already seperate the Custom Component from Select.

@DeOne4eg
Copy link

I have this same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue/bug-unconfirmed Issues that describe a bug that hasn't been confirmed by a maintainer yet
Projects
None yet
Development

No branches or pull requests

4 participants