From c4aedef0bc9f5b203d4dd5bae5a90c0e4f0e3f91 Mon Sep 17 00:00:00 2001 From: Sarah Rimron-Soutter Date: Fri, 29 Sep 2023 11:27:04 +0100 Subject: [PATCH] Implement feedback - fix typos - remove `parseInt` - DRY --- src/js/clickable-component.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/js/clickable-component.js b/src/js/clickable-component.js index 790a1786c7..6c09621729 100644 --- a/src/js/clickable-component.js +++ b/src/js/clickable-component.js @@ -34,7 +34,7 @@ class ClickableComponent extends Component { * A class or space separated list of classes to add the component * * @param {number | boolean} [options.throttle] - * A throttle will be applied to the clickHander if the number is >= 1 or the value is `true` + * A throttle will be applied to the clickHandler if the number is >= 1 or the value is `true` * A number specifies the desired wait time in ms or a default wait of 50ms will be applied * */ @@ -46,20 +46,22 @@ class ClickableComponent extends Component { this.controlText(this.options_.controlText); } + const throttleIsNumber = typeof this.options_.throttle === 'number'; + const boundClick = this.handleClick.bind(this); const selectClickHandler = () => { - if (typeof this.options_.throttle === 'number' || this.options_.throttle === true) { - const wait = typeof this.options_.throttle === 'number' ? parseInt(this.options_.throttle, 10) : 50; + if (throttleIsNumber || this.options_.throttle === true) { + const wait = throttleIsNumber ? this.options_.throttle : 50; - return throttle(this.handleClick.bind(this), wait); + return throttle(boundClick, wait); } - return this.handleClick.bind(this); + return boundClick; }; - const selectedClickHander = selectClickHandler(); + const selectedClickHandler = selectClickHandler(); this.handleMouseOver_ = (e) => this.handleMouseOver(e); this.handleMouseOut_ = (e) => this.handleMouseOut(e); - this.handleClick_ = (e) => selectedClickHander(e); + this.handleClick_ = (e) => selectedClickHandler(e); this.handleKeyDown_ = (e) => this.handleKeyDown(e); this.emitTapEvents();