Skip to content

Commit 5ad845f

Browse files
authored
Merge pull request #185 from dfrencham/accessibility-template
Apply accessibility attributes to suggestion template
2 parents 6dccc29 + 9347375 commit 5ad845f

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/typeahead/dataset.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,14 @@ var Dataset = (function() {
319319
pending: templates.pending && _.templatify(templates.pending),
320320
header: templates.header && _.templatify(templates.header),
321321
footer: templates.footer && _.templatify(templates.footer),
322-
suggestion: templates.suggestion || suggestionTemplate
322+
suggestion: templates.suggestion ? userSuggestionTemplate : suggestionTemplate
323323
};
324324

325+
function userSuggestionTemplate(context) {
326+
var template = templates.suggestion;
327+
return $(template(context)).attr("id", _.guid());
328+
}
329+
325330
function suggestionTemplate(context) {
326331
return $('<div role="option">').attr('id', _.guid()).text(displayFn(context));
327332
}

test/typeahead/dataset_spec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,33 @@ describe('Dataset', function() {
421421
});
422422
});
423423

424+
it('should apply id attribute when using default suggestion template', function() {
425+
this.dataset = new Dataset({
426+
source: this.source,
427+
node: $('<div>'),
428+
}, www);
429+
430+
this.source.andCallFake(syncMockSuggestions);
431+
this.dataset.update('woah');
432+
433+
expect(this.dataset.$el.find('div[role="option"]')).toHaveAttr('id');
434+
});
435+
436+
it('should apply id attribute when using custom suggestion template', function() {
437+
this.dataset = new Dataset({
438+
source: this.source,
439+
node: $('<div>'),
440+
templates: {
441+
suggestion: function(c) { return '<p class="search-result"> result' + c.query + '</p>'; }
442+
}
443+
}, www);
444+
445+
this.source.andCallFake(syncMockSuggestions);
446+
this.dataset.update('woah');
447+
448+
expect(this.dataset.$el.find('p.search-result')).toHaveAttr('id');
449+
});
450+
424451
describe('#clear', function() {
425452
it('should clear suggestions', function() {
426453
this.source.andCallFake(syncMockSuggestions);

0 commit comments

Comments
 (0)