Skip to content

Custom onClick on Token #467

@lmgarret

Description

@lmgarret

Describe the solution you'd like
I'm looking for a way to display some information under the input when a Token has been clicked. A nice way to do it would be as follows:

<Typeahead
   id="values-typeahead"
   clearButton
    defaultSelected={initValues}
    multiple
    onChange={this.handleValuesSelected}
    options={values}
    placeholder="Choose values..."
    renderToken={(
        selectedItem: string,
        props: any,
        index: number
    ) => {
        return (
            <Token
                key={index}
                onRemove={props.onRemove!}
                onClick={() => {
                   console.log(`${selectedItem} selected`);
                }}>
                   {selectedItem}
             </Token>
        );
   }}
/>

How is this solution useful to others?
Triggering an action when clicking on a token could interest others I guess.

Describe alternatives you've considered
Writing a custom renderToken method using the tokenContainer, but combined with TypeScript this rapidly seems too much for just a simple click 'listener'.

Additional context
If the feature is to be implemented with the above code, a simple way to do it would be to add to modify in tokenContainer, the _handleActive from

_handleActive = (e) => {
      e.stopPropagation();
      this.setState({active: true});
}

to

_handleActive = (e) => {
      e.stopPropagation();
      this.setState({active: true});
      if (this.props.onClick) {
          this.props.onClick();
      }
}

In fact, it's what I'm using now :)

I'd be glad to open a PR if you want!

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions