Skip to content

Commit 789ca05

Browse files
authored
refactor(dropdown): fix inefficient event handler .bind(this) bindings (#8035)
1 parent cd6be49 commit 789ca05

1 file changed

Lines changed: 27 additions & 35 deletions

File tree

src/components/Dropdown/Dropdown.jsx

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,30 @@ export default class Dropdown extends Component {
1414
};
1515

1616
componentDidMount() {
17-
this._boundCloseOnEsc = this._closeDropdownOnEsc.bind(this);
18-
this._boundCloseLostFocus = this._closeDropdownIfFocusLost.bind(this);
19-
20-
document.addEventListener("keyup", this._boundCloseOnEsc, true);
21-
document.addEventListener("focus", this._boundCloseLostFocus, true);
22-
document.addEventListener("click", this._boundCloseLostFocus, true);
17+
document.addEventListener("keyup", this._closeDropdownOnEsc, true);
18+
document.addEventListener("focus", this._closeDropdownIfFocusLost, true);
19+
document.addEventListener("click", this._closeDropdownIfFocusLost, true);
2320
}
2421

2522
componentWillUnmount() {
26-
document.removeEventListener("keyup", this._boundCloseOnEsc, true);
27-
document.removeEventListener("focus", this._boundCloseLostFocus, true);
28-
document.removeEventListener("click", this._boundCloseLostFocus, true);
23+
document.removeEventListener("keyup", this._closeDropdownOnEsc, true);
24+
document.removeEventListener("focus", this._closeDropdownIfFocusLost, true);
25+
document.removeEventListener("click", this._closeDropdownIfFocusLost, true);
2926
}
3027

31-
_closeDropdownOnEsc(event) {
28+
_closeDropdownOnEsc = (event) => {
3229
if (event.key === "Escape" && this.state.active) {
3330
this.setState({ active: false }, () => {
3431
this.dropdownButton.focus();
3532
});
3633
}
37-
}
34+
};
3835

39-
_closeDropdownIfFocusLost(event) {
36+
_closeDropdownIfFocusLost = (event) => {
4037
if (this.state.active && !this.dropdown.contains(event.target)) {
4138
this.setState({ active: false });
4239
}
43-
}
40+
};
4441

4542
render() {
4643
const { className = "", items = [] } = this.props;
@@ -50,15 +47,15 @@ export default class Dropdown extends Component {
5047
<nav
5148
className={`dropdown ${className}`}
5249
ref={(el) => (this.dropdown = el)}
53-
onMouseOver={this._toggle.bind(this, true)}
54-
onMouseLeave={this._toggle.bind(this, false)}
50+
onMouseOver={this._handleMouseOver}
51+
onMouseLeave={this._handleMouseLeave}
5552
>
5653
<button
5754
ref={(el) => (this.dropdownButton = el)}
5855
aria-haspopup="true"
5956
aria-expanded={String(this.state.active)}
6057
aria-label="Select language"
61-
onClick={this._handleClick.bind(this)}
58+
onClick={this._handleClick}
6259
>
6360
<svg
6461
viewBox="0 0 610 560"
@@ -98,11 +95,9 @@ export default class Dropdown extends Component {
9895
{items.map((item, i) => (
9996
<li key={item.title}>
10097
<a
101-
onKeyDown={this._handleArrowKeys.bind(
102-
this,
103-
i,
104-
items.length - 1,
105-
)}
98+
onKeyDown={(event) =>
99+
this._handleArrowKeys(i, items.length - 1, event)
100+
}
106101
ref={(node) =>
107102
this.links ? this.links.push(node) : (this.links = [node])
108103
}
@@ -119,7 +114,7 @@ export default class Dropdown extends Component {
119114
);
120115
}
121116

122-
_handleArrowKeys(currentIndex, lastIndex, event) {
117+
_handleArrowKeys = (currentIndex, lastIndex, event) => {
123118
if (["ArrowDown", "ArrowUp"].includes(event.key)) {
124119
event.preventDefault();
125120
}
@@ -140,24 +135,21 @@ export default class Dropdown extends Component {
140135
}
141136

142137
this.links[newIndex].focus();
143-
}
138+
};
144139

145-
_handleClick() {
140+
_handleClick = () => {
146141
this.setState({ active: !this.state.active }, () => {
147142
if (this.state.active) {
148143
this.links[0].focus();
149144
}
150145
});
151-
}
146+
};
152147

153-
/**
154-
* Toggle visibility of dropdown items
155-
*
156-
* @param {boolean} state - Whether to display or hide the items
157-
*/
158-
_toggle(state = false) {
159-
this.setState({
160-
active: state,
161-
});
162-
}
148+
_handleMouseOver = () => {
149+
this.setState({ active: true });
150+
};
151+
152+
_handleMouseLeave = () => {
153+
this.setState({ active: false });
154+
};
163155
}

0 commit comments

Comments
 (0)