Skip to content

Commit

Permalink
Fix adding to array with enum
Browse files Browse the repository at this point in the history
  • Loading branch information
Yevhen Zavhorodnii committed Nov 19, 2024
1 parent 151853c commit 9343172
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions server/static/js/property-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ class EditorGenerator {
const arrayContainer = $('<div>').addClass('property-editor-array');
const arrayItems = this.object[key] || [];
const itemSchema = property.items || { type: 'string' }; // Default to strings if items schema is missing
const customEnum = this.customEnumFields[key];

const renderArrayItems = () => {
arrayContainer.empty();
Expand Down Expand Up @@ -305,20 +306,21 @@ class EditorGenerator {
const addButton = $('<button>')
.text('Add')
.on('click', () => {
callback(key, 'added');
if (itemSchema.enum) {
arrayItems.push(itemSchema.enum[0]); // Default to the first enum value
} else if (itemSchema.type === 'object') {
arrayItems.push({});
} else if (itemSchema.type === 'string') {
arrayItems.push(''); // Default to empty string
const defaultValue = customEnum && customEnum.length > 0 ? customEnum[0] : '';
arrayItems.push(defaultValue);
} else if (itemSchema.type === 'number' || itemSchema.type === 'integer') {
arrayItems.push(0); // Default value for numbers
} else {
console.warn('Unsupported item type for addition:', itemSchema.type);
return;
}
renderArrayItems();
callback(key, 'added');
});

renderArrayItems();
Expand Down

0 comments on commit 9343172

Please sign in to comment.