Files
threshold-ui/ui/admin/app/components/form/host-set/index.js
T
Dharini Jeeva 722acdc1fc chore: 🤖 refactor key value list component, text input support (#1957)
refactor: 💡 refactor list wrapper component and fix test
2023-10-30 12:30:05 -07:00

40 lines
1.1 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import Component from '@glimmer/component';
import { action } from '@ember/object';
import { set } from '@ember/object';
export default class FormHostSetComponent extends Component {
// =attributes
// =actions
/**
* Adds a string to the array. We recreate a new array after adding
* so that ember is aware that the array has been modified.
* @param field {string}
* @param value {string}
*/
@action
addOption(field, { value }) {
const existingArray = this.args.model[field] ?? [];
const newArray = [...existingArray, { value }];
set(this.args.model, field, newArray);
}
/**
* Removes an option by index. We recreate a new array after
* splicing out the item so that ember is aware that the array has been modified.
* @param field {string}
* @param index {number}
*/
@action
removeOptionByIndex(field, index) {
const newArray = this.args.model[field].filter((_, i) => i !== index);
set(this.args.model, field, newArray);
}
}