Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
bower_components/
node_modules/
yarn.lock
18 changes: 0 additions & 18 deletions bower.json

This file was deleted.

66 changes: 38 additions & 28 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,46 @@

<title>multi-select-combo-box demo</title>

<script src="../../webcomponentsjs/webcomponents-loader.js"></script>

<link rel="import" href="../../iron-demo-helpers/demo-pages-shared-styles.html">
<link rel="import" href="../../iron-demo-helpers/demo-snippet.html">
<link rel="import" href="../multi-select-combo-box.html">
<link rel="import" href="../../vaadin-combo-box/theme/lumo/vaadin-combo-box-light.html">
<link rel="import" href="../../vaadin-text-field/theme/lumo/vaadin-text-field.html">

<custom-style>
<style is="custom-style" include="demo-pages-shared-styles">
</style>
</custom-style>
<script src="../../@webcomponents/webcomponentsjs/webcomponents-loader.js"></script>

<script type="module" src="../../@polymer/iron-demo-helpers/demo-pages-shared-styles.js"></script>
<script type="module" src="../../@polymer/iron-demo-helpers/demo-snippet.js"></script>
<script type="module" src="../multi-select-combo-box.js"></script>

<script type="module">
const $_documentContainer = document.createElement('template');

$_documentContainer.innerHTML = `<custom-style>
<style is="custom-style" include="demo-pages-shared-styles">
</style>
</custom-style>`;

document.body.appendChild($_documentContainer.content);
</script>
</head>

<body>
<div class="vertical-section-container centered">
<h3>Basic multi-select-combo-box demo</h3>
<demo-snippet>
<template>
<multi-select-combo-box items='["List item 1", "List item 2", "List item 3", "List item 4", "List item 5"]'></multi-select-combo-box>
</template>
</demo-snippet>

<h3>Objects multi-select-combo-box demo</h3>
<demo-snippet>
<template>
<multi-select-combo-box items='[{ "id": 1, "name": "Name1" }, { "id": 2, "name": "Name2" }, { "id": 3, "name": "Name3" }, { "id": 4, "name": "Name4" }, { "id": 5, "name": "Name5" }]' value-field="id" display-field="name"></multi-select-combo-box>
</template>
</demo-snippet>
</div>
<script type="module">
const $_documentContainer = document.createElement('template');

$_documentContainer.innerHTML = `<div class="vertical-section-container centered">
<h3>Basic multi-select-combo-box demo</h3>
<demo-snippet>
<template>
<multi-select-combo-box items="[&quot;List item 1&quot;, &quot;List item 2&quot;, &quot;List item 3&quot;, &quot;List item 4&quot;, &quot;List item 5&quot;]"></multi-select-combo-box>
</template>
</demo-snippet>

<h3>Objects multi-select-combo-box demo</h3>
<demo-snippet>
<template>
<multi-select-combo-box items="[{ &quot;id&quot;: 1, &quot;name&quot;: &quot;Name1&quot; }, { &quot;id&quot;: 2, &quot;name&quot;: &quot;Name2&quot; }, { &quot;id&quot;: 3, &quot;name&quot;: &quot;Name3&quot; }, { &quot;id&quot;: 4, &quot;name&quot;: &quot;Name4&quot; }, { &quot;id&quot;: 5, &quot;name&quot;: &quot;Name5&quot; }]" value-field="id" display-field="name"></multi-select-combo-box>
</template>
</demo-snippet>
</div>`;

document.body.appendChild($_documentContainer.content);
</script>
</body>

</html>
</html>
147 changes: 0 additions & 147 deletions multi-select-combo-box.html

This file was deleted.

145 changes: 145 additions & 0 deletions multi-select-combo-box.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import {html, PolymerElement} from '@polymer/polymer/polymer-element.js';
import '@vaadin/vaadin-text-field/vaadin-text-field.js';
import '@vaadin/vaadin-combo-box/vaadin-combo-box-light.js';
import '@polymer/iron-icon/iron-icon.js';
import '@polymer/iron-icons/iron-icons.js';

/**
* `multi-select-combo-box`
* Multi select combo box based on vaadin-combo-box component
*
* @customElement
* @polymer
* @demo demo/index.html
*/
class MultiSelectComboBox extends PolymerElement {
static get template() {
return html`
<style>
:host {
display: block;
width: 400px;
}

vaadin-combo-box-light {
display: block;
width: 100%;
}

vaadin-text-field {
width: 100%;
}

#tokens {
display: flex;
max-width: 100%;
overflow: hidden;
}

.token {
font-size: .875rem;
display: flex;
padding: 0.25rem;
border-radius: 0.125em;
background-color: hsla(214, 53%, 23%, 0.16);
margin-right: 0.25rem;
cursor: pointer;
white-space: nowrap;
}

iron-icon {
--iron-icon-width: .875rem;
--iron-icon-height: .875rem;
}
</style>

<vaadin-combo-box-light id="cb" items="[[items]]" value="{{_cbValue}}" item-value-path="[[valueField]]" item-label-path="[[displayField]]">
<template>[[_getItemDisplayText(item)]]</template>
<vaadin-text-field on-keydown="_onKeyDown" label="[[label]]" id="tf">
<div slot="prefix" id="tokens">
<template is="dom-repeat" items="[[selectedItems]]">
<div class="token" on-click="_onTokenClick">[[_getItemDisplayText(item)]]
<iron-icon icon="icons:close"></iron-icon>
</div>
</template>
</div>
</vaadin-text-field>
</vaadin-combo-box-light>
`;
}

static get is() { return 'multi-select-combo-box'; }
static get properties() {
return {
items: {
type: Object,
value: () => []
},
selectedItems: {
value: () => [],
notify: true
},
label: String,
_cbValue: Object,
displayField: {
type: String
},
valueField: {
type: String
}
}
}

static get observers() {
return [
'_cbValueChanged(_cbValue)'
]
}

_cbValueChanged(cbValue) {
if (cbValue) {
var selectedItem = this.items.find(item => {
if (this.valueField != null && this.valueField !== '') {
if (item[this.valueField] != null) {
return item[this.valueField] == cbValue;
}
}
return item == cbValue;
});
this.push('selectedItems', selectedItem);
this.splice('items', this.items.indexOf(selectedItem), 1);
}

this._cbValue = '';

// TODO: This shouldn't be needed!
setTimeout(() => this.$.cb.$.overlay._selectedItem = '');
}

_onTokenClick(e) {
this._removeSelected(e.model.item);
e.stopPropagation();
}

_removeSelected(item) {
this.splice('selectedItems', this.selectedItems.indexOf(item), 1);
this.push('items', item);
}

_onKeyDown(e) {
if (e.keyCode === 8 && this.selectedItems.length && this.$.tf.value === '') {
this._removeSelected(this.selectedItems[this.selectedItems.length - 1]);
}
}

_getItemDisplayText(item) {
if (this.displayField != null && this.displayField !== '') {
if (item[this.displayField] != null) {
return item[this.displayField];
}
}
return item;
}
}

window.customElements.define(MultiSelectComboBox.is, MultiSelectComboBox);
Loading