-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace did-insert modifier with focus modifier.
drop-in replacement.
- Loading branch information
1 parent
dc3348e
commit 1da4c49
Showing
8 changed files
with
68 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { modifier } from 'ember-modifier'; | ||
|
||
/** | ||
The `{{focus}}` element modifier sets the focus its DOM element. | ||
It can take one optional boolean argument, which can be used to conditionally allow or prevent focusing. | ||
For example - focusing the first item in an ordered list: | ||
```handlebars | ||
<ol> | ||
{{#each @items as |item index|}} | ||
<li {{focus (eq index 0)}}> | ||
{{/each} | ||
</ol> | ||
``` | ||
*/ | ||
export default modifier(function focus(element, positional /*, named*/) { | ||
// If no argument is provided, then the given element is focused on. | ||
// If an argument has been provided, then only set focus if that arg is truthy. | ||
if (!positional.length || !!positional[0]) { | ||
element.focus(); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from 'ilios-common/modifiers/focus'; |
29 changes: 29 additions & 0 deletions
29
packages/test-app/tests/integration/modifiers/focus-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { module, test } from 'qunit'; | ||
import { setupRenderingTest } from 'test-app/tests/helpers'; | ||
import { render } from '@ember/test-helpers'; | ||
import { hbs } from 'ember-cli-htmlbars'; | ||
|
||
module('Integration | Modifier | focus', function (hooks) { | ||
setupRenderingTest(hooks); | ||
|
||
test('it focuses by default without condition', async function (assert) { | ||
this.set('label', 'foo bar'); | ||
await render(hbs`<label><input {{focus}} />{{this.label}}</label> | ||
`); | ||
assert.dom('input').isFocused(); | ||
}); | ||
|
||
test('it focuses when condition is met', async function (assert) { | ||
this.set('label', 'foo bar'); | ||
await render(hbs`<label><input {{focus true}} />{{this.label}}</label> | ||
`); | ||
assert.dom('input').isFocused(); | ||
}); | ||
|
||
test('it does not focus when condition is not met', async function (assert) { | ||
this.set('label', 'foo bar'); | ||
await render(hbs`<label><input {{focus false}} />{{this.label}}</label> | ||
`); | ||
assert.dom('input').isNotFocused(); | ||
}); | ||
}); |