* feat: 🎸 generate new dropdown component in core ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-12060 * feat: 🎸 add template and class code to new dropdown component ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-12060 * chore: 🤖 add lodash package to core addon ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-12060 * test: 💍 add tests for core/dropdown component ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-12060 * refactor: 💡 use new core/dropdown in targets list screen ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-12060 * refactor: 💡 remove rose/filterabledropdown ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-12060 * feat: 🎸 add maxHeight to core/dropdown component ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-12060 * style: 💄 fix lint errors in dropdown/index-tests ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-12060 * refactor: 💡 update height to match designs ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-12060 * chore: 🤖 remove lodash from ui/admin ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-12060 * chore: 🤖 remove lodash from ui/desktop ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-12060 * chore: 🤖 move lodash to devDep in core ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-12060 * chore: 🤖 add lodash as core dependency ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-12060 * test: 💍 use waitUntil & findAll instead of setTimeout ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-12060 * chore: 🤖 add lodash as peerDependency for core ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-12060 * refactor: 💡 update waitUntil with suggestion from PR ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-12060
83 lines
1.9 KiB
JavaScript
83 lines
1.9 KiB
JavaScript
/**
|
|
* Copyright (c) HashiCorp, Inc.
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
*/
|
|
|
|
import Component from '@glimmer/component';
|
|
import { action } from '@ember/object';
|
|
import { tracked } from '@glimmer/tracking';
|
|
import { debounce } from 'core/decorators/debounce';
|
|
|
|
export default class DropdownComponent extends Component {
|
|
// =attributes
|
|
|
|
@tracked selectedItems = [];
|
|
@tracked searchTerm;
|
|
|
|
/**
|
|
* Takes itemOptions and filters them by searchTerm if there is one
|
|
* @returns {[object]}
|
|
*/
|
|
get itemOptions() {
|
|
let items = this.args.itemOptions;
|
|
if (this.searchTerm) {
|
|
const searchTerm = this.searchTerm.toLowerCase();
|
|
items = this.args.itemOptions.filter((item) => {
|
|
const isNameMatch = item.name?.toLowerCase().includes(searchTerm);
|
|
const isIdMatch = item.id.includes(this.searchTerm);
|
|
return isNameMatch || isIdMatch;
|
|
});
|
|
}
|
|
return items;
|
|
}
|
|
|
|
// =methods
|
|
|
|
/**
|
|
* Sets searchTerm to be used for filtering itemOptions
|
|
* @param {object} event
|
|
*/
|
|
@action
|
|
@debounce(150)
|
|
filterItems(event) {
|
|
const { value } = event.target;
|
|
this.searchTerm = value;
|
|
}
|
|
|
|
/**
|
|
* Handles checkbox event changes for selectedItems
|
|
* @param {object} event
|
|
*/
|
|
@action
|
|
selectItem(event) {
|
|
const { checked, value } = event.target;
|
|
if (checked) {
|
|
this.selectedItems = [...this.selectedItems, value];
|
|
} else {
|
|
this.selectedItems = this.selectedItems.filter(
|
|
(scope) => scope !== value,
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Pass selectedItems to applyFilter callback and call onClose
|
|
* @param {function} close
|
|
*/
|
|
@action
|
|
applyFilter(onClose) {
|
|
this.args.applyFilter(this.selectedItems);
|
|
onClose();
|
|
}
|
|
|
|
/**
|
|
* Custom close method for dropdown that resets selectedItems
|
|
* to checkedItems when the user closes the dropdown
|
|
*/
|
|
@action
|
|
close() {
|
|
this.selectedItems = [...this.args.checkedItems];
|
|
this.searchTerm = '';
|
|
}
|
|
}
|