Skip to content

Commit

Permalink
Merge pull request #11 from mycolorway/scl-add-multiinput-option
Browse files Browse the repository at this point in the history
Scl add multiinput option
  • Loading branch information
farthinker committed May 16, 2016
2 parents a6a5741 + f5998fe commit 5445b01
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 17 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ simple.select({
el: $('select'), // * 必须
cls: "", // 额外的 class
onItemRender: $.noop, // 渲染列表每个元素后调用的函数
placeholder: "" // input 元素的 placeholder 属性
placeholder: "", // input 元素的 placeholder 属性
multiline: false // input 元素是否可换行,默认为true
});
```

Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-select",
"version": "2.0.11",
"version": "2.0.12",
"homepage": "https://github.com/mycolorway/simple-select",
"authors": [
"kshift <[email protected]>"
Expand Down
9 changes: 5 additions & 4 deletions demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
$(function() {
simple.select({
el: $('#select-one'),
placeholder: 'type sth...'
placeholder: 'type sth...',
multiline: false
});

simple.select({
Expand Down Expand Up @@ -75,7 +76,7 @@
<body>
<div class="wrapper">
<div class="demo-one">
<h3>Demo one: without non value option</h3>
<h3>Demo one: multiline as false</h3>
<select id="select-one">
<option value="0" data-hint="#1" data-key="George Washington">George Washington</option>
<option value="1" data-hint="#2" data-key="John Adams">John Adams</option>
Expand All @@ -84,7 +85,7 @@ <h3>Demo one: without non value option</h3>
</div>

<div class="demo-two">
<h3>Demo two</h3>
<h3>Demo two: without non value option</h3>

<select id="select-two">
</select>
Expand All @@ -96,7 +97,7 @@ <h3>Demo three: with non value option</h3>
<option value="">select something</option>
<option value="0" data-key="George Washington">George Washington</option>
<option value="1" data-key="John Adams">John Adams</option>
<option value="2" data-key="Thomas Jefferson">Thomas Jefferson</option>
<option value="2" data-key="Thomas Jefferson">Thomas Jefferson Thomas Jefferson Thomas Jefferson</option>
</select>
</div>
</div>
Expand Down
19 changes: 15 additions & 4 deletions lib/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ Select = (function(superClass) {
cls: "",
onItemRender: $.noop,
placeholder: "",
allowInput: false
allowInput: false,
multiline: true
};

Select.i18n = {
Expand All @@ -48,7 +49,8 @@ Select = (function(superClass) {
};

Select._tpl = {
input: "<textarea rows=1 type=\"text\" class=\"select-result\" autocomplete=\"off\"></textarea>",
textarea: "<textarea rows=1 type=\"text\" class=\"select-result\" autocomplete=\"off\"></textarea>",
input: "<input type=\"text\" class=\"select-result\" autocomplete=\"off\">",
item: "<div class=\"select-item\">\n <a href=\"javascript:;\" class=\"label\"><span></span></a>\n <span class=\"hint\"></span>\n</div>"
};

Expand All @@ -67,12 +69,18 @@ Select = (function(superClass) {
};

Select.prototype._render = function() {
var items;
var inputTpl, items;
Select._tpl.select = "<div class=\"simple-select\">\n <span class=\"link-expand\" title=\"" + (this._t('all_options')) + "\">\n <i class=\"icon-caret-down\"><span>&#9662;</span></i>\n </span>\n <span class=\"link-clear\" title=\"" + (this._t('clear_selection')) + "\">\n <i class=\"icon-delete\"><span>&#10005;</span></i>\n </span>\n <div class=\"select-list\">\n <div class=\"loading\">" + (this._t('loading')) + "</div>\n </div>\n</div>";
this.el = $(this.opts.el).hide();
this.el.data("select", this);
this.select = $(Select._tpl.select).data("select", this).addClass(this.opts.cls).insertBefore(this.el);
this.input = $(Select._tpl.input).attr("placeholder", this.opts.placeholder || this.el.data('placeholder') || "").prependTo(this.select);
if (this.opts.multiline) {
this.select.addClass('multiline');
inputTpl = Select._tpl.textarea;
} else {
inputTpl = Select._tpl.input;
}
this.input = $(inputTpl).attr("placeholder", this.opts.placeholder || this.el.data('placeholder') || "").prependTo(this.select);
this.list = this.select.find(".select-list");
this.requireSelect = true;
items = this.el.find("option").map((function(_this) {
Expand Down Expand Up @@ -417,6 +425,9 @@ Select = (function(superClass) {
};

Select.prototype.autoresizeInput = function() {
if (!this.opts.multiline) {
return;
}
return setTimeout((function(_this) {
return function() {
_this.input.css("height", 0);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-select",
"version": "2.0.11",
"version": "2.0.12",
"description": "a simple select plugin based on Simple Module",
"main": "lib/select.js",
"repository": {
Expand Down
16 changes: 14 additions & 2 deletions src/select.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Select extends SimpleModule
onItemRender: $.noop
placeholder: ""
allowInput: false
multiline: true

@i18n:
"zh-CN":
Expand All @@ -18,10 +19,14 @@ class Select extends SimpleModule
loading: "Loading..."

@_tpl:
input: """
textarea: """
<textarea rows=1 type="text" class="select-result" autocomplete="off"></textarea>
"""

input: """
<input type="text" class="select-result" autocomplete="off">
"""

item: """
<div class="select-item">
<a href="javascript:;" class="label"><span></span></a>
Expand Down Expand Up @@ -61,7 +66,13 @@ class Select extends SimpleModule
.data("select", @)
.addClass(@opts.cls)
.insertBefore @el
@input = $(Select._tpl.input)
if @opts.multiline
@select.addClass('multiline')
inputTpl = Select._tpl.textarea
else
inputTpl = Select._tpl.input

@input = $(inputTpl)
.attr("placeholder", @opts.placeholder || @el.data('placeholder') || "")
.prependTo @select
@list = @select.find ".select-list"
Expand Down Expand Up @@ -333,6 +344,7 @@ class Select extends SimpleModule


autoresizeInput: () ->
return unless @opts.multiline
setTimeout () =>
@input.css("height", 0)
@input.css("height", parseInt(@input.get(0).scrollHeight) + parseInt(@input.css("border-top-width")) + parseInt(@input.css("border-bottom-width")))
Expand Down
12 changes: 10 additions & 2 deletions styles/select.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
.simple-select .select-result {
width: 100%;
height: 24px;
resize: none;
overflow: hidden;
box-sizing: border-box;
padding: 4px;
padding-right: 24px;
Expand Down Expand Up @@ -80,6 +78,9 @@
color: #666666;
border-bottom: 1px solid #dfdfdf;
cursor: pointer;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.simple-select .select-list .select-item:first-child {
-webkit-border-radius: 4px 4px 0 0;
Expand All @@ -103,3 +104,10 @@
float: right;
color: #aaa;
}
.simple-select.multiline .select-result {
resize: none;
overflow: hidden;
}
.simple-select.multiline .select-list .select-item {
white-space: normal;
}
18 changes: 16 additions & 2 deletions styles/select.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
.select-result {
width: 100%;
height: 24px;
resize: none;
overflow: hidden;
box-sizing: border-box;
padding: 4px;
padding-right: 24px;
Expand Down Expand Up @@ -50,6 +48,7 @@
.link-clear {
display: block;
}

}

&.require-select {
Expand Down Expand Up @@ -95,6 +94,9 @@
color: #666666;
border-bottom: 1px solid #dfdfdf;
cursor: pointer;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;

&:first-child {
-webkit-border-radius: 4px 4px 0 0;
Expand Down Expand Up @@ -124,4 +126,16 @@
}
}
}

&.multiline {
.select-result {
resize: none;
overflow: hidden;
}
.select-list {
.select-item {
white-space: normal;
}
}
}
}

0 comments on commit 5445b01

Please sign in to comment.