Skip to content

Commit

Permalink
Select-one : clear current selection
Browse files Browse the repository at this point in the history
  • Loading branch information
yabab-dev committed Jul 30, 2016
1 parent f67e9a3 commit 8d7ad12
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 5 deletions.
1 change: 1 addition & 0 deletions assets/icons/cross-black.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 19 additions & 2 deletions assets/scripts/src/choices.js
Expand Up @@ -762,6 +762,13 @@ export class Choices {
e.preventDefault();

const hasShiftKey = e.shiftKey ? true : false;

if(e.target.hasAttribute('data-clear-one')) {
const itemToRemove = activeItems[0];
this._removeItem(itemToRemove);
e.target.style.display = 'none';
return;
}

if(this.passedElement.type !== 'text' && !this.dropdown.classList.contains(this.config.classNames.activeState)) {
// For select inputs we always want to show the dropdown if it isn't already showing
Expand All @@ -772,7 +779,7 @@ export class Choices {
if(this.input !== document.activeElement) {
this.input.focus();
}

if(e.target.hasAttribute('data-button')) {
if(this.config.removeItems && this.config.removeItemButton) {
const itemId = e.target.parentNode.getAttribute('data-id');
Expand Down Expand Up @@ -1033,6 +1040,12 @@ export class Choices {
if(this.passedElement.type === 'select-one') {
this.removeActiveItems(id);
}

// Display clear button on select-one element
if (this.config.removeItemButton && this.passedElement.type === 'select-one') {
const clearButton = this.containerInner.querySelector('[data-clear-one]');
clearButton.style.display = 'inline-block';
}

// Run callback if it is a function
if(this.config.callbackOnAddItem){
Expand Down Expand Up @@ -1142,7 +1155,11 @@ export class Choices {
return strToEl(`<div class="${ classNames.containerOuter }" data-type="${ this.passedElement.type }"></div>`);
},
containerInner: () => {
return strToEl(`<div class="${ classNames.containerInner }"></div>`);
if(this.config.removeItemButton && this.passedElement.type === 'select-one') {
return strToEl(`<div class="${ classNames.containerInner }"><button class="${ classNames.button } choices__clear_one" data-clear-one>Remove current selection</button></div>`);
}else{
return strToEl(`<div class="${ classNames.containerInner }"></div>`);
}
},
itemList: () => {
return strToEl(`<div class="${ classNames.list } ${ this.passedElement.type === 'select-one' ? classNames.listSingle : classNames.listItems }"></div>`);
Expand Down
7 changes: 7 additions & 0 deletions assets/styles/css/choices.css
Expand Up @@ -173,6 +173,13 @@
line-height: 1;
cursor: pointer; }

.choices__clear_one {
background-image: url("../../icons/cross-black.svg");
margin: 0;
border: 0;
float: right;
margin-top: 6px; }

.choices__input {
background-color: #f9f9f9;
font-size: 1.4rem;
Expand Down
2 changes: 1 addition & 1 deletion assets/styles/css/choices.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions assets/styles/scss/choices.scss
Expand Up @@ -11,6 +11,7 @@ $choices-keyline-color: #DDDDDD;
$choices-primary-color: #00BCD4;
$choices-disabled-color: #eaeaea;
$choices-button-icon-path: '../../icons/cross.svg';
$choices-button-icon-black-path: '../../icons/cross-black.svg';

.choices {
position: relative;
Expand Down Expand Up @@ -192,6 +193,14 @@ $choices-button-icon-path: '../../icons/cross.svg';
cursor: pointer;
}

.choices__clear_one {
background-image: url($choices-button-icon-black-path);
margin: 0;
border: 0;
float: right;
margin-top: 6px;
}

.choices__input {
background-color: mix(#000000, #FFFFFF, 2.5%);
font-size: 1.4rem;
Expand Down
5 changes: 3 additions & 2 deletions index.html
Expand Up @@ -4,8 +4,8 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Choices</title>
<link rel="stylesheet" href="assets/styles/css/base.min.css">
<link rel="stylesheet" href="assets/styles/css/choices.min.css">
<link rel="stylesheet" href="assets/styles/css/base.css">
<link rel="stylesheet" href="assets/styles/css/choices.css">
<script src="assets/scripts/dist/choices.min.js"></script>
</head>
<body>
Expand Down Expand Up @@ -230,6 +230,7 @@ <h2>Single select input</h2>
});

const choices15 = new Choices('#choices-15', {
removeItemButton: true,
choices: [
{value: 'One', label: 'Label One', selected: true, disabled: false},
{value: 'Two', label: 'Label Two', disabled: true},
Expand Down

0 comments on commit 8d7ad12

Please sign in to comment.