Skip to content

Commit 545f3b0

Browse files
committed
autofocus first form element
currently not working for date selection (calendar) since this one did not support keyboard navigation part of #94
1 parent 06b33a7 commit 545f3b0

12 files changed

+65
-1
lines changed

app/components/bs-input.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import BsInput from 'ember-bootstrap/components/bs-input';
2+
import AutofocusSupport from 'croodle/mixins/autofocus-support';
3+
4+
export default BsInput.extend(AutofocusSupport);

app/components/simple-select.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import SimpleSelect from 'ember-simple-select/components/simple-select';
2+
import AutofocusSupport from 'croodle/mixins/autofocus-support';
23

34
export default SimpleSelect.reopen({
45
classNames: ['form-control']
5-
});
6+
}).extend(AutofocusSupport);

app/mixins/autofocus-support.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Ember from 'ember';
2+
3+
const { on } = Ember;
4+
5+
/*
6+
* A work-a-round to support autofocus in Ember.js components.
7+
* Background: https://github.com/emberjs/ember.js/issues/12589
8+
*/
9+
10+
export default Ember.Mixin.create({
11+
autofocusSupport: on('didInsertElement', function() {
12+
if (this.get('autofocus')) {
13+
this.$().focus();
14+
}
15+
})
16+
});

app/templates/components/bs-input.hbs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{yield}}

app/templates/components/create-options-datetime.hbs

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
}}
3535
<div class="input-group">
3636
{{bs-input
37+
autofocus=(unless index true false)
3738
id=id
3839
placeholder='00:00'
3940
type='time'

app/templates/components/create-options-text.hbs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
}}
1010
<div class="input-group">
1111
{{bs-input
12+
autofocus=(unless index true false)
1213
id=id
1314
value=value
1415
}}

app/templates/create/index.hbs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
as |value id|
1515
}}
1616
{{simple-select
17+
autofocus=true
1718
id=id
1819
content=pollTypes
1920
optionLabelPath="label"

app/templates/create/meta.hbs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
action="submit"
77
}}
88
{{bs-form-element
9+
autofocus=true
910
classNames='title'
1011
controlType="text"
1112
label=(t 'create.meta.input.title.label')

app/templates/create/settings.hbs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
as |value id|
1515
}}
1616
{{simple-select
17+
autofocus=true
1718
id=id
1819
content=answerTypes
1920
optionLabelPath="label"

app/templates/poll/participation.hbs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
novalidate=true
77
}}
88
{{bs-form-element
9+
autofocus=true
910
controlType='text'
1011
label=(t 'poll.input.newUserName.label')
1112
placeholder=(t 'poll.input.newUserName.placeholder')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { moduleForComponent, test } from 'ember-qunit';
2+
import hbs from 'htmlbars-inline-precompile';
3+
4+
moduleForComponent('bs-input', 'Integration | Component | bs input', {
5+
integration: true
6+
});
7+
8+
test('it renders', function(assert) {
9+
// Set any properties with this.set('myProperty', 'value');
10+
// Handle any actions with this.on('myAction', function(val) { ... });
11+
12+
this.render(hbs`{{bs-input}}`);
13+
14+
assert.equal(this.$().text().trim(), '');
15+
16+
// Template block usage:
17+
this.render(hbs`
18+
{{#bs-input}}
19+
template block text
20+
{{/bs-input}}
21+
`);
22+
23+
assert.equal(this.$().text().trim(), 'template block text');
24+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import Ember from 'ember';
2+
import AutofocusSupportMixin from 'croodle/mixins/autofocus-support';
3+
import { module, test } from 'qunit';
4+
5+
module('Unit | Mixin | autofocus support');
6+
7+
// Replace this with your real tests.
8+
test('it works', function(assert) {
9+
let AutofocusSupportObject = Ember.Object.extend(AutofocusSupportMixin);
10+
let subject = AutofocusSupportObject.create();
11+
assert.ok(subject);
12+
});

0 commit comments

Comments
 (0)