question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Trigger focus from an action

See original GitHub issue

I am not sure if I am doing this wrong, or if this just isn’t supported. I have an action in the component and I would like this action to open and set focus to the ember-power-select element.

When I focus the element, by tabbing or clicking it the onfocus does fire. However, when I use the jquery trigger from the focusSearch() action: power select does not get focus, nor does the onfocus handler fire. I have tried grabbing the classes assocuated with other divs and no luck.

Is there a way to trigger it to open from an action and I am just doing it wrong?

Component.hbs {{#power-select class='ls-search-box' options=searchList selected=selected onfocus=(action "onfocus") as |item| }} ... Component.js actions: { focusSearch(){ //console.log('focus Search'); var input = Ember.$(".ls-search-box ember-power-select-trigger"); if(input) {input.focus();} }, onfocus(){ console.log('onfocus'); },

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
cibernoxcommented, Jul 26, 2016

@brancusi Yes, there is cleaner ways.

If the only thing you want to do is to render the component open initially, you can pass initiallyOpened=true. In your example that is enough.

If what you want to do is open or close the component after it has been rendered, there is another way that is not public API yet, but it will be soon, although maybe with a different name. The select accepts an action named registerAPI (I might change that name, but the idea will remain) that will be invoked with an instance of the public API that you can grab and store somewhere to use it as a “remote controller” of the componen later on.

{{#power-select ...blahblah registerAPI=(mut selectRemoteController) as |opt|}}
  {{opt}}
{{/power-select}}

Then in your code

this.get('selectRemoteController').actions.open();
// or even this.get('selectRemoteController').actions.search('foobar'); You can control almost everything of the select.
0reactions
atdcommented, Jan 13, 2017

For other people looking for this, the correct way to access the publicAPI is:

{{#power-select ...blahblah registerAPI=(action (mut selectRemoteController)) as |opt|}}
  {{opt}}
{{/power-select}}

as described by @cibernox in https://github.com/cibernox/ember-power-select/issues/549#issuecomment-227229404

Read more comments on GitHub >

github_iconTop Results From Across the Web

.focus() | jQuery API Documentation
.focus( handler )Returns: jQuery. Description: Bind an event handler to the "focus" JavaScript event, or trigger that event on an element.
Read more >
HTMLElement.focus() - Web APIs | MDN
The HTMLElement.focus() method sets focus on the specified element, if it can be focused. The focused element is the element that will ...
Read more >
jquery trigger action on focus or click but not both
Put all functionality on focus and tell jQuery to set the foucus on the element when it is clicked if you run into...
Read more >
How to trigger an action when an input gets focus? - Ember.JS
I want to know how to trigger an action when an input gets focused… right now I'm using this on my template: {{view...
Read more >
jQuery focus() Method - W3Schools
The focus() method triggers the focus event, or attaches a function to run when a focus event occurs. Tip: This method is often...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found