React-dates: Calendar Icon click handler

Created on 21 Nov 2019  路  2Comments  路  Source: airbnb/react-dates

Is there any way to:

  • attach a function handler to the click event on the icon?
    or
  • change the behavior of the button to close the calendar when clicked?

Searched everywhere, couldn't find.

Most helpful comment

@rogeriocsilva you can use customInputIcon prop for this purpose:

const TestCustomInputIcon = ({ onClick }) => (
  <span
    style={{
      border: "1px solid #dce0e0",
      backgroundColor: "#fff",
      color: "#484848",
      padding: "3px"
    }}
    onClick={onClick}
  >
    C
  </span>
);

class App extends React.Component {
  state = {
    focusedInput: "startDate"
  };

  onIconClick = () => {
    if (this.state.focusedInput) {
      this.setState({ focusedInput: null });
    }
  };

  render() {
    return (
      <div className="App">
        <DateRangePicker
          // ...
          customInputIcon={<TestCustomInputIcon onClick={this.onIconClick} />}
          focusedInput={this.state.focusedInput}
          onFocusChange={focusedInput => this.setState({ focusedInput })}
        />
      </div>
    );
  }
}

All 2 comments

@rogeriocsilva you can use customInputIcon prop for this purpose:

const TestCustomInputIcon = ({ onClick }) => (
  <span
    style={{
      border: "1px solid #dce0e0",
      backgroundColor: "#fff",
      color: "#484848",
      padding: "3px"
    }}
    onClick={onClick}
  >
    C
  </span>
);

class App extends React.Component {
  state = {
    focusedInput: "startDate"
  };

  onIconClick = () => {
    if (this.state.focusedInput) {
      this.setState({ focusedInput: null });
    }
  };

  render() {
    return (
      <div className="App">
        <DateRangePicker
          // ...
          customInputIcon={<TestCustomInputIcon onClick={this.onIconClick} />}
          focusedInput={this.state.focusedInput}
          onFocusChange={focusedInput => this.setState({ focusedInput })}
        />
      </div>
    );
  }
}

Yeah, my bad. Pretty simple solution. Even though had to stopPropagation to actually close. Thanks man.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

augnustin picture augnustin  路  3Comments

AsasinCree picture AsasinCree  路  3Comments

prztrz picture prztrz  路  3Comments

krissalvador27 picture krissalvador27  路  3Comments

jpollard-cs picture jpollard-cs  路  3Comments