Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Component "closing" when lost focus

I'm trying to create a select2 style component in React.

I have got 90% functionality down, the one bit I just can't fathom is hiding the result box when the user clicks away

The render method is:

render() {
    let resultBlock;

    if (this.state.showSearch) {
        resultBlock = (
            <div className="search-input-container" onBlur={this.onBlur}>
                <div className="search-input-results">
                    <input 
                        type="text" 
                        name={this.props.name}
                        placeholder={this.props.placeholder}
                        className="form-control"
                        onChange={this.inputKeyUp}
                        autoComplete="false" />
                    <ul>
                        {this.state.items.map((item, i) => <li key={i} data-value={item.id} onClick={this.itemSelected} className={item.isSelected ? 'selected' : ''}>{item.text}</li>)}
                    </ul>
                </div>
            </div>
        );
    }

    let displayBlock;
    if (this.props.value.text) {
        displayBlock = this.props.value.text;
    } else {
        displayBlock = <span className="placeholder">{this.props.placeholder}</span>;
    }

    return (
        <div className="form-group">
            <label htmlFor={this.props.name}>{this.props.label}:</label>

            <div className="form-input">
                <div className="searchable-dropdown" onClick={this.revealSearch}>
                    {displayBlock}
                    <div className="arrow"><i className="fa fa-chevron-down" aria-hidden="true" /></div>
                </div>
                {resultBlock}
            </div>
        </div>
    );
}

I've tried moving onBlur={this.onBlur} around, but it only fires if the <input... had focus before one clicked away.

It can't be that complicated, the only approach I thought of, is attaching a global click handler to the page, and diff'ing clicks to understand if a user hasn't clicked on my component. But this seems over engineered.

How can this be achieved?

like image 726
Callum Linington Avatar asked Dec 20 '16 09:12

Callum Linington


1 Answers

I achieved this functionality by:

Putting this in the constructor:

this.windowClick = this.windowClick.bind(this);

(From what dfsq said) Put this in componentDidMount:

if (window) {
    window.addEventListener('click', this.windowClick, false);
}

This event handler:

windowClick(event) {
    event.preventDefault();

    if (event.target.classList.contains('searchable-marker')) {
        return;
    } else {
        this.setState({
            showSearch: false
        });
    }
}

Where searchable-marker is just a class I put on all the div's, ul's, li's and inputs to make sure that if I clicked one of these, it wouldn't close the the box.

Adding the unmount:

componentWillUnmount() {
    window.removeEventListener('click', this.windowClick, false);
}
like image 108
Callum Linington Avatar answered Sep 22 '22 06:09

Callum Linington