use templates from instance, fixes #461 (#660)

* use template from instance

* adjust test

* fix lint

* try to move `this` out of loop

* fixes late init bound
This commit is contained in:
Konstantin Vyatkin 2019-10-21 05:48:49 -04:00 committed by Josh Johnson
parent 4e8842d013
commit 7de0887e7d
3 changed files with 9 additions and 5 deletions

View file

@ -101,6 +101,7 @@ class Choices {
this.passedElement = new WrappedSelect({
element: passedElement,
classNames: this.config.classNames,
template: data => this.config.templates.option(data),
});
}

View file

@ -1,9 +1,9 @@
import WrappedElement from './wrapped-element';
import templates from './../templates';
export default class WrappedSelect extends WrappedElement {
constructor({ element, classNames }) {
constructor({ element, classNames, template }) {
super({ element, classNames });
this.template = template;
}
get placeholderOption() {
@ -22,9 +22,9 @@ export default class WrappedSelect extends WrappedElement {
const fragment = document.createDocumentFragment();
const addOptionToFragment = data => {
// Create a standard select option
const template = templates.option(data);
const option = this.template(data);
// Append it to fragment
fragment.appendChild(template);
fragment.appendChild(option);
};
// Add each list item to list

View file

@ -1,8 +1,9 @@
import { expect } from 'chai';
import { stub } from 'sinon';
import { stub, spy } from 'sinon';
import WrappedElement from './wrapped-element';
import WrappedSelect from './wrapped-select';
import { DEFAULT_CLASSNAMES } from '../constants';
import Templates from '../templates';
describe('components/wrappedSelect', () => {
let instance;
@ -28,6 +29,7 @@ describe('components/wrappedSelect', () => {
instance = new WrappedSelect({
element: document.getElementById('target'),
classNames: DEFAULT_CLASSNAMES,
template: spy(Templates.option),
});
});
@ -133,6 +135,7 @@ describe('components/wrappedSelect', () => {
selectElement.appendChild(fragment);
expect(fragment).to.be.instanceOf(DocumentFragment);
expect(instance.template.callCount).to.equal(2);
expect(selectElement.options.length).to.equal(2);
expect(selectElement.options[0].value).to.equal(options[0].value);
expect(selectElement.options[1].value).to.equal(options[1].value);