
test/example.html in the browser, click on input . $ component create dropdown-example repo (username/project): mbykov/component-dropdown-example description: simple dropdown example . . .  "dependencies": { "component/domify": "*", "component/each": "*", "component/classes": "*" }, $ make . In the Makefile file you will see, in particular, (about the makefile format in the makefile vs. grunt note): build: components index.js dropdown.css @component build --dev components: component.json @component install --dev component install all the dependencies and create the build/build.js build/build.css . (I renamed the original dd-example.css to dropdown.css, and corrected the Makefile accordingly).test directory and in it a file example.html , the essence of which  <p><input type="text" id="anchor"><p> <script> var anchor = document.querySelector('#anchor'); var Dropdown = require('dd-example'); var dd = new Dropdown(anchor); </script> index.js file we add the main content: var domify = require('domify'); var classes = require('classes'); var each = require('each'); module.exports = Dropdown; function Dropdown(anchor) { console.log(anchor); return this; } return this - this is important. Now we can create a chain of methods for our component. Each following method gets all the same this.dropdown component itself. function Dropdown(anchor) { var _div = domify('<div class="dropdown-div"></div>'); var _list = domify('<ul class="dropdown-list"></ul>'); _div.appendChild(_list); this.list = _list; this.div = _div; this.anchor = anchor; anchor.parentNode.insertBefore(_div, anchor.nextSibling); anchor.focus(); return this; } _list and _list . Underlining it is convenient for me to select DOM objects (we no longer need underscore, and the $ sign somehow doesn’t look, but it's a matter of taste). In real life, of course, you need to place the html in template.html, and call it require('template') . I do not here for clarity of the code.this.list = _list save these objects as attributes of this object, this is useful in the method chain. And add a new object to the page. $ make   var self = this; anchor.onclick = function(e){ self.toggle(); classes(this.div).remove('hidden'); return false; }  Dropdown.prototype.render = function(values) { var self = this; values.each(function(value){ var added = domify('<li class="dropdown-line"></li>'); added.textContent = value; self.list.appendChild(added); }); return this; } render method with predefined values: var values = [' '...  dd.render(values)  Dropdown.prototype.select = function(cb){ var self = this; this.list.onclick = function(e) { var value = e.target.textContent; anchor.value = value; self.hide(); cb(value); }; } return this at the end of the function, because this is the end of the chain. Call the .select method in example.html , in which we output the value to the console. Voila.  _div.style.left = getOffset(anchor).left +'px'; component/dropdown , stagas/dropdown , bmcmahen/dropdown , matthewmueller/autocomplete . True, they all use jQuery, which is why I wrote my mbykov/dropdown - it is designed for json-data obtained from couchdb , so it has its own characteristics.Source: https://habr.com/ru/post/209354/
All Articles