-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathkv-object-editor.js
103 lines (96 loc) · 3.58 KB
/
kv-object-editor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { isNone } from '@ember/utils';
import { assert } from '@ember/debug';
import { action } from '@ember/object';
import { guidFor } from '@ember/object/internals';
import KVObject from 'vault/lib/kv-object';
import {
hasWhitespace,
isNonString,
NON_STRING_WARNING,
WHITESPACE_WARNING,
} from 'vault/utils/model-helpers/validators';
/**
* @module KvObjectEditor
* KvObjectEditor components are called in FormFields when the editType on the model is kv. They are used to show a key-value input field.
*
* @example
* <KvObjectEditor @value={{hash foo="bar"}} @onChange={{log "on change called"}} @label="Label here" />
*
* @param {string} value - the value is captured from the model.
* @param {function} onChange - function that captures the value on change
* @param {boolean} [isMasked = false] - when true the `MaskedInput` renders instead of the default `textarea` to input the value portion of the key/value object
* @param {boolean} [isSingleRow = false] - when true the kv object editor will only show one row and hide the Add button
* @param {function} [onKeyUp] - function passed in that handles the dom keyup event. Used for validation on the kv custom metadata.
* @param {string} [label] - label displayed over key value inputs
* @param {string} [labelClass] - override default label class in FormFieldLabel component
* @param {string} [warning] - warning that is displayed
* @param {string} [helpText] - helper text. In tooltip.
* @param {string} [subText] - placed under label.
* @param {string} [keyPlaceholder] - placeholder for key input
* @param {string} [valuePlaceholder] - placeholder for value input
* @param {boolean} [allowWhiteSpace = false] - when true, allows whitespace in the key input
* @param {boolean} [warnNonStringValues = false] - when true, shows a warning if the value is a non-string
*/
export default class KvObjectEditor extends Component {
// kvData is type ArrayProxy, so addObject etc are fine here
@tracked kvData;
whitespaceWarning = WHITESPACE_WARNING('key');
nonStringWarning = NON_STRING_WARNING;
get placeholders() {
return {
key: this.args.keyPlaceholder || 'key',
value: this.args.valuePlaceholder || 'value',
};
}
get hasDuplicateKeys() {
return this.kvData.uniqBy('name').length !== this.kvData.length;
}
// fired on did-insert from render modifier
@action
createKvData(elem, [value]) {
this.kvData = KVObject.create({ content: [] }).fromJSON(value);
if (!this.args.isSingleRow || !value || Object.keys(value).length < 1) {
this.addRow();
}
}
@action
addRow() {
if (!isNone(this.kvData.find((datum) => datum.name === ''))) {
return;
}
const newObj = { name: '', value: '' };
guidFor(newObj);
this.kvData.addObject(newObj);
}
@action
updateRow() {
this.args.onChange(this.kvData.toJSON());
}
@action
deleteRow(object, index) {
const oldObj = this.kvData.objectAt(index);
assert('object guids match', guidFor(oldObj) === guidFor(object));
this.kvData.removeAt(index);
this.args.onChange(this.kvData.toJSON());
}
@action
handleKeyUp(event) {
if (this.args.onKeyUp) {
this.args.onKeyUp(event.target.value);
}
}
showWhitespaceWarning = (name) => {
if (this.args.allowWhiteSpace) return false;
return hasWhitespace(name);
};
showNonStringWarning = (value) => {
if (!this.args.warnNonStringValues) return false;
return isNonString(value);
};
}