Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow click handler to be specified in clickable component's options #6140

Merged
merged 2 commits into from
Aug 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions docs/guides/components.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ Creation:
```js
// adding a button to the player
var player = videojs('some-video-id');
var Component = videojs.getComponent('Component');
var button = new Component(player);
var Button = videojs.getComponent('Button');
var button = new Button(player, {
clickHandler: function(event) {
videojs.log('Clicked');
}
});

console.log(button.el());
```
Expand Down
9 changes: 8 additions & 1 deletion src/js/clickable-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class ClickableComponent extends Component {
*
* @param {Object} [options]
* The key/value store of player options.
*
* @param {function} [options.clickHandler]
* The function to call when the button is clicked / activated
*/
constructor(player, options) {
super(player, options);
Expand Down Expand Up @@ -183,7 +186,11 @@ class ClickableComponent extends Component {
* @listens click
* @abstract
*/
handleClick(event) {}
handleClick(event) {
if (this.options_.clickHandler) {
this.options_.clickHandler.call(this, arguments);
}
}

/**
* Event handler that is called when a `ClickableComponent` receives a
Expand Down
18 changes: 18 additions & 0 deletions test/unit/clickable-component.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,21 @@ QUnit.test('handleClick should not be triggered more than once when enabled', fu
testClickableComponent.dispose();
player.dispose();
});

QUnit.test('handleClick should use handler from options', function(assert) {
let clicks = 0;

const player = TestHelpers.makePlayer({});
const testClickableComponent = new ClickableComponent(player, {
clickHandler() {
clicks++;
}
});
const el = testClickableComponent.el();

Events.trigger(el, 'click');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a test that if we trigger a space or enter key on it, the click handler gets called?

assert.equal(clicks, 1, 'options handler was called');

testClickableComponent.dispose();
player.dispose();
});