Skip to content

Commit b36dc74

Browse files
committed
ui: set default key value when is empty
1 parent 810ec39 commit b36dc74

File tree

4 files changed

+52
-12
lines changed

4 files changed

+52
-12
lines changed

ui/app/lib/kv-object.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,21 @@ export default ArrayProxy.extend({
3838
return this.fromJSON(JSON.parse(jsonString));
3939
},
4040

41-
toJSON(includeBlanks = false) {
41+
toJSON(includeBlanks = false, defaultKey = 'key') {
4242
return this.reduce((obj, item) => {
4343
if (!includeBlanks && item.value === '' && item.name === '') {
4444
return obj;
4545
}
4646
const val = typeof item.value === 'undefined' ? '' : item.value;
47-
obj[item.name || ''] = val;
47+
// Use defaultKey if name is empty and value is not empty
48+
const keyName = item.name || (val !== '' ? defaultKey : '');
49+
obj[keyName] = val;
4850
return obj;
4951
}, {});
5052
},
5153

52-
toJSONString(includeBlanks) {
53-
return JSON.stringify(this.toJSON(includeBlanks), null, 2);
54+
toJSONString(includeBlanks, defaultKey) {
55+
return JSON.stringify(this.toJSON(includeBlanks, defaultKey), null, 2);
5456
},
5557

5658
isAdvanced() {

ui/lib/core/addon/components/kv-object-editor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ export default class KvObjectEditor extends Component {
7676
}
7777
@action
7878
updateRow() {
79-
this.args.onChange(this.kvData.toJSON());
79+
this.args.onChange(this.kvData.toJSON(false, this.placeholders.key));
8080
}
8181
@action
8282
deleteRow(object, index) {
8383
const oldObj = this.kvData.objectAt(index);
8484
assert('object guids match', guidFor(oldObj) === guidFor(object));
8585
this.kvData.removeAt(index);
86-
this.args.onChange(this.kvData.toJSON());
86+
this.args.onChange(this.kvData.toJSON(false, this.placeholders.key));
8787
}
8888
@action
8989
handleKeyUp(event) {

ui/tests/integration/components/kv-object-editor-test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,27 @@ module('Integration | Component | kv-object-editor', function (hooks) {
123123
await fillIn('[data-test-kv-value="0"]', 7);
124124
assert.dom('[data-test-kv-object-warning="0"]').exists();
125125
});
126+
127+
test('it defaults empty key to "key" when value is not empty', async function (assert) {
128+
await render(hbs`<KvObjectEditor @onChange={{this.spy}} />`);
129+
// Fill in only the value, leave the key empty
130+
await component.rows.objectAt(0).kvVal('myvalue');
131+
assert.strictEqual(this.spy.callCount, 1, 'calls onChange when value changes');
132+
assert.deepEqual(
133+
this.spy.lastCall.args[0],
134+
{ key: 'myvalue' },
135+
'uses "key" as default when key is empty but value is not'
136+
);
137+
});
138+
139+
test('it uses custom placeholder as default key when keyPlaceholder is provided', async function (assert) {
140+
await render(hbs`<KvObjectEditor @onChange={{this.spy}} @keyPlaceholder="customKey" />`);
141+
// Fill in only the value, leave the key empty
142+
await component.rows.objectAt(0).kvVal('myvalue');
143+
assert.deepEqual(
144+
this.spy.lastCall.args[0],
145+
{ customKey: 'myvalue' },
146+
'uses custom placeholder as default key'
147+
);
148+
});
126149
});

ui/tests/unit/lib/kv-object-test.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,26 +64,41 @@ module('Unit | Lib | kv object', function () {
6464
[
6565
'types',
6666
false,
67+
'key',
6768
{ string: 'string', false: false, zero: 0, number: 1, null: null, true: true, object: { one: 'two' } },
6869
{ false: false, null: null, number: 1, object: { one: 'two' }, string: 'string', true: true, zero: 0 },
6970
],
70-
['include blanks = true', true, { string: 'string', '': '' }, { string: 'string', '': '' }],
71-
['include blanks = false', false, { string: 'string', '': '' }, { string: 'string' }],
71+
['include blanks = true', true, 'key', { string: 'string', '': '' }, { string: 'string', '': '' }],
72+
['include blanks = false', false, 'key', { string: 'string', '': '' }, { string: 'string' }],
73+
[
74+
'empty key with value defaults to "key"',
75+
false,
76+
'key',
77+
{ string: 'string', '': 'value' },
78+
{ string: 'string', key: 'value' },
79+
],
80+
[
81+
'empty key with value and custom default',
82+
false,
83+
'custom',
84+
{ string: 'string', '': 'value' },
85+
{ string: 'string', custom: 'value' },
86+
],
7287
];
7388

74-
toJSONTests.forEach(function ([name, includeBlanks, input, output]) {
89+
toJSONTests.forEach(function ([name, includeBlanks, defaultKey, input, output]) {
7590
test(`toJSON: ${name}`, function (assert) {
7691
const data = KVObject.create({ content: [] }).fromJSON(input);
77-
const result = data.toJSON(includeBlanks);
92+
const result = data.toJSON(includeBlanks, defaultKey);
7893
assert.deepEqual(result, output, 'has expected output');
7994
});
8095
});
8196

82-
toJSONTests.forEach(function ([name, includeBlanks, input, output]) {
97+
toJSONTests.forEach(function ([name, includeBlanks, defaultKey, input, output]) {
8398
test(`toJSONString: ${name}`, function (assert) {
8499
const expected = JSON.stringify(output, null, 2);
85100
const data = KVObject.create({ content: [] }).fromJSON(input);
86-
const result = data.toJSONString(includeBlanks);
101+
const result = data.toJSONString(includeBlanks, defaultKey);
87102
assert.strictEqual(result, expected, 'has expected output string');
88103
});
89104
});

0 commit comments

Comments
 (0)