Skip to content

Commit 9f1d5cc

Browse files
authored
Merge pull request #1872 from fabienwnklr/feature-clear_button
Feature clear button
2 parents 896f871 + b3f9221 commit 9f1d5cc

File tree

6 files changed

+150
-1
lines changed

6 files changed

+150
-1
lines changed

docs/plugins.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ complete control.
1313
- All plugins live in their own folders in ["src/plugins"](../src/plugins).
1414
- Plugin names should be in follow the format: `/[a-z_]+$`
1515
- JS source should live in a "plugin.js" file (required).
16-
- CSS should live in a "plugin.less" file (optional). It will be bundled at build time.
16+
- CSS should live in a "plugin.less" and "plugin.scss" file (optional) if use SCSS file, include it into `src/scss/selectize.scss`. It will be bundled at build time.
1717
- Plugins are initialized right before the control is setup.
1818
This means that if you want to listen for events on any of the control's
1919
elements, you should override the `setup()` method (see ["DOM Events"](#dom-events)).

examples/plugins.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,29 @@
2121
<div id="wrapper">
2222
<h1>Selectize.js</h1>
2323

24+
<div class="demo">
25+
<h2>Plugin: "clear_button"</h2>
26+
<div class="control-group">
27+
<label for="clear-btn">Tags: Simple select</label>
28+
<select id="clear-btn" class="clear-btn demo-default" name="clear-btn">
29+
<option value="awesome">awesome</option>
30+
<option value="neat">neat</option>
31+
</select>
32+
</div>
33+
<div class="control-group">
34+
<label for="clear-btn">Tags: Multiple select</label>
35+
<select multiple id="clear-btn" class="clear-btn demo-default" name="clear-btn">
36+
<option value="awesome">awesome</option>
37+
<option value="neat">neat</option>
38+
</select>
39+
</div>
40+
<script>
41+
$('.clear-btn').selectize({
42+
plugins: ['clear_button']
43+
});
44+
</script>
45+
</div>
46+
2447
<div class="demo">
2548
<h2>Plugin: "remove_button"</h2>
2649
<div class="control-group">

src/plugins/clear_button/plugin.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Plugin: "clear_button" (selectize.js)
3+
* Copyright (c) 2013 Brian Reavis & contributors
4+
* Copyright (c) 2020-2022 Selectize Team & contributors
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
7+
* file except in compliance with the License. You may obtain a copy of the License at:
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software distributed under
11+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
12+
* ANY KIND, either express or implied. See the License for the specific language
13+
* governing permissions and limitations under the License.
14+
*
15+
* @author Fabien Winkler <[email protected]>
16+
*/
17+
18+
Selectize.define("clear_button", function (options) {
19+
var self = this;
20+
21+
options = $.extend(
22+
{
23+
title: "Clear",
24+
className: "clear",
25+
label: "×",
26+
html: function (data) {
27+
return (
28+
'<a class="' + data.className + '" title="' + data.title + '"> ' + data.label + '</a>'
29+
);
30+
},
31+
},
32+
options
33+
);
34+
35+
self.setup = (function () {
36+
var original = self.setup;
37+
return function () {
38+
original.apply(self, arguments);
39+
self.$button_clear = $(options.html(options));
40+
41+
if (self.settings.mode === "single") self.$wrapper.addClass("single");
42+
43+
self.$wrapper.append(self.$button_clear);
44+
45+
if (self.getValue() === "" || self.getValue().length === 0) {
46+
self.$wrapper.find("." + options.className).css("display", "none");
47+
}
48+
49+
self.on("change", function () {
50+
if (self.getValue() !== "" || self.getValue().length === 0) {
51+
self.$wrapper.find("." + options.className).css("display", "");
52+
} else {
53+
self.$wrapper.find("." + options.className).css("display", "none");
54+
}
55+
});
56+
57+
self.$wrapper.on("click", "." + options.className, function (e) {
58+
e.preventDefault();
59+
e.stopImmediatePropagation();
60+
e.stopPropagation();
61+
62+
if (self.isLocked) return;
63+
64+
self.clear();
65+
self.$wrapper.find("." + options.className).css("display", "none");
66+
});
67+
};
68+
})();
69+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
.selectize-control.plugin-clear_button {
2+
.clear {
3+
display: flex;
4+
position: absolute;
5+
height: 100%;
6+
width: 25px;
7+
top: 0;
8+
right: calc(@selectize-padding-x - @selectize-padding-item-x);
9+
color: rgba(0, 0, 0);
10+
opacity: 0.4;
11+
font-weight: bold;
12+
border: none;
13+
cursor: pointer;
14+
z-index: 1;
15+
font-size: 21px;
16+
justify-content: center;
17+
align-items: center;
18+
}
19+
20+
.clear:hover {
21+
opacity: 1;
22+
}
23+
24+
&.single .clear {
25+
right: calc(@selectize-padding-x - @selectize-padding-item-x + 1.5rem);
26+
}
27+
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
.#{$selectize}-control.plugin-clear_button {
2+
.clear {
3+
display: flex;
4+
position: absolute;
5+
height: 100%;
6+
width: 25px;
7+
top: 0;
8+
right: calc(#{$select-padding-x} - #{$select-padding-item-x});
9+
color: rgba(0, 0, 0);
10+
opacity: 0.4;
11+
font-weight: bold;
12+
border: none;
13+
cursor: pointer;
14+
z-index: 1;
15+
font-size: 21px;
16+
justify-content: center;
17+
align-items: center;
18+
}
19+
20+
.clear:hover {
21+
opacity: 1;
22+
}
23+
24+
&.single .clear {
25+
right: calc(#{$select-padding-x} - #{$select-padding-item-x} + 1.5rem);
26+
}
27+
28+
}

src/scss/selectize.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ $select-spinner-border-color: $select-color-border;
9494
@import "../plugins/dropdown_header/plugin.scss";
9595
@import "../plugins/optgroup_columns/plugin.scss";
9696
@import "../plugins/remove_button/plugin.scss";
97+
@import "../plugins/clear_button/plugin.scss";
9798

9899
.#{$selectize}-control {
99100
position: relative;

0 commit comments

Comments
 (0)