open-vault/ui/lib/core/addon/components/autocomplete-input.js
Jordan Reimer e811821ac7
Transform Advanced Templating (#13908)
* updates regex-validator component to optionally show pattern input and adds capture groups support

* adds form-field-label component

* adds autocomplete-input component

* updates kv-object-editor component to yield block for value and glimmerizes

* updates transform template model

* adds transform-advanced-templating component

* updates form-field with child component changes

* updates transform template serializer to handle differences in regex named capture groups

* fixes regex-validator test

* adds changelog entry

* updates for pr review feedback

* reverts kv-object-editor guidFor removal
2022-02-07 13:07:53 -07:00

49 lines
1.8 KiB
JavaScript

import Component from '@glimmer/component';
import { action } from '@ember/object';
/**
* @module AutocompleteInput
* AutocompleteInput components are used as standard string inputs or optionally select options to append to input value
*
* @example
* ```js
* <AutocompleteInput @requiredParam={requiredParam} @optionalParam={optionalParam} @param1={{param1}}/>
* ```
* @callback inputChangeCallback
* @param {string} value - input value
* @param {inputChangeCallback} onChange - fires when input value changes to mutate value param by caller
* @param {string} [optionsTrigger] - display options dropdown when trigger character is input
* @param {Object[]} [options] - array of { label, value } objects where label is displayed in options dropdown and value is appended to input value
* @param {string} [label] - label to display above input
* @param {string} [subText] - text to display below label
* @param {string} [placeholder] - input placeholder
*/
export default class AutocompleteInput extends Component {
dropdownAPI;
inputElement;
@action
setElement(element) {
this.inputElement = element.querySelector('.input');
}
@action
onInput(event) {
const { options = [], optionsTrigger } = this.args;
if (optionsTrigger && options.length) {
const method = event.data === optionsTrigger ? 'open' : 'close';
this.dropdownAPI.actions[method]();
}
this.args.onChange(event.target.value);
}
@action
selectOption(value) {
// if trigger character is at start of value it needs to be trimmed
const appendValue = value.startsWith(this.args.optionsTrigger) ? value.slice(1) : value;
const newValue = this.args.value + appendValue;
this.args.onChange(newValue);
this.dropdownAPI.actions.close();
this.inputElement.focus();
}
}