question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

onBlur doesn't work in custom component

See original GitHub issue

hi, a thousand apologies for post this here, but this crash my mind

i don’t understand why onBlur doesn’t work

what i expect: when i choose some checkboxes with options and click whatever at the page - select will collapse

what i get: select collabes only when i click on arrow

pls help me and sorry for my poor english

you can check code here - https://codesandbox.io/s/elegant-mestorf-7rvl8

and i past it here

import ReactDOM from "react-dom";

import Select, { components } from "react-select";

const styles = {
  option: styles => ({
    ...styles,
    backgroundColor: "#ffff",
    color: "black"
  })
};

const Option = props => (
  <components.Option {...props}>
    <input type="checkbox" checked={props.isSelected} readOnly />
    <label>{props.data.label}</label>
  </components.Option>
);

const ValueContainer = ({ children, selectProps, ...props }) => {
  console.log(selectProps);
  const { getValue, hasValue } = props;
  const countSelectedVal = getValue().length;
  if (!hasValue) {
    return (
      <components.ValueContainer {...props}>
        {children}
      </components.ValueContainer>
    );
  }
  return (
    <components.ValueContainer {...props}>
      {`${selectProps.messages.orderPageAnySelectPreText}: ${countSelectedVal}`}
    </components.ValueContainer>
  );
};

const OrderSelectMulti = ({ options, ...props }) => {
  const components = { ValueContainer, Option };
  const messages = {
    orderPageAnySelectPreText: "Selected"
  };
  return (
    <Select
      styles={styles}
      isMulti
      messages={messages}
      components={components}
      hideSelectedOptions={false}
      options={[
        { value: "Status1", label: "Status1" },
        { value: "Status2", label: "Status2" },
        { value: "Status3", label: "Status3" }
      ]}
      placeholder="BlaBla"
      {...props}
    />
  );
};

const rootElement = document.getElementById("root");
ReactDOM.render(<OrderSelectMulti />, rootElement);

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
colinscruggscommented, Feb 5, 2021

I was facing the same issue you were, where you could only close the menu by clicking the arrow. I got around this with a deceptively simple workaround: render the children of the ValueContainer, but just style them to have opacity: 0. This will maintain the original ValueContainer’s functionality, but allow you to render whatever you want in the ValueContainer. Here’s my code:

const ValueContainer = props => {
  const {
    children,
    getValue,
    innerProps: { ...restInnerProps }
  } = props;
  
  const length = getValue().length;
  return (
    <components.ValueContainer
      {...restInnerProps}
      {...props}
    >

    <div style={{ opacity: 0 }}>{...children}</div>

    {length === 0
    ?  `Select...`
    : `${length} item${length != 1 ? 's' : ''} selected` }
    </components.ValueContainer>
  );
};
3reactions
rabbit0321commented, Jun 15, 2019

I have the same issue. blur is not working and remain the blue border style on the element ‘sometimes’ after selecting an option if I have customized the component. e.g. When the error prop changed from ‘true’ to ‘false’ in the following function.

const customControl = useCallback(
    ({children, ...props}) => (
      <>
        <components.Control
          {...props}
          className={classnames({
            'custom-react-select': true,
            'is-invalid': error,
          })}
        >
          {children}
        </components.Control>
        <span className="invalid-feedback">
          <ErrorMessage name={name} />
        </span>
      </>
    ),
    [name, error]
  );
Read more comments on GitHub >

github_iconTop Results From Across the Web

why I can't get my onChange/onBlur handler work on custom ...
You need to pass along the event handlers as props all the way to the input component. As it is, your input has...
Read more >
react hook form onblur not working - You.com | The AI Search ...
In your code, the form mode is onBlur . it means the validation is triggered on blur event (unfocus the input). When you...
Read more >
SyntheticEvent - React
The onBlur event handler is called when focus has left the element (or left some element inside of it). For example, it's called...
Read more >
Window: blur event - Web APIs | MDN
The blur event fires when an element has lost focus. The opposite of blur is focus . This event is not cancelable and...
Read more >
Advanced - React Select
React-select is committed to providing a custom experience to all users and relies heavily ... These two methods sit as callable methods on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found