Skip to content

Commit 643eb00

Browse files
committed
Disabled 'Add argument to list' button when maxItems is reached
1 parent 8f3c4ba commit 643eb00

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

src/lib/components/common/jschema/ArrayProperty.svelte

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@
5555
}
5656
5757
function addNestedProperty() {
58-
const maxItems = getMaxItems(schemaProperty.referenceSchema);
59-
if (maxItems !== null && nestedProperties.length === maxItems) {
60-
// It is not possible to add more properties than maxItems
58+
if (!canAddMoreItems(nestedProperties)) {
6159
return;
6260
}
6361
schemaProperty.addNestedSchemaProperty(undefined, nestedProperties.length);
@@ -68,13 +66,13 @@
6866
* @param {number} index
6967
*/
7068
function removeNestedProperty(index) {
71-
schemaProperty.removeNestedSchemaProperty(index);
72-
nestedProperties = schemaProperty.nestedProperties;
7369
const minItems = getMinItems(schemaProperty.referenceSchema);
74-
if (schemaProperty.isRequired() && minItems !== null && nestedProperties.length < minItems) {
75-
schemaProperty.addNestedSchemaProperty(undefined, index);
76-
nestedProperties = schemaProperty.nestedProperties;
70+
if (schemaProperty.isRequired() && minItems !== null && nestedProperties.length === minItems) {
71+
schemaProperty.updateNestedPropertyValue(undefined, index);
72+
} else {
73+
schemaProperty.removeNestedSchemaProperty(index);
7774
}
75+
nestedProperties = schemaProperty.nestedProperties;
7876
}
7977
8078
/**
@@ -90,6 +88,16 @@
9088
function moveDown(index) {
9189
nestedProperties = schemaProperty.moveNestedPropertyDown(index);
9290
}
91+
92+
/**
93+
* @param {any[]} nestedProperties
94+
*/
95+
function canAddMoreItems(nestedProperties) {
96+
const maxItems = getMaxItems(schemaProperty.referenceSchema);
97+
return maxItems === null || nestedProperties.length < maxItems;
98+
}
99+
100+
$: addNestedPropertyBtnDisabled = !canAddMoreItems(nestedProperties);
93101
</script>
94102

95103
{#if schemaProperty}
@@ -120,7 +128,12 @@
120128
>
121129
<div class="accordion-body p-1">
122130
<div class="d-flex justify-content-center p-2">
123-
<button class="btn btn-primary" type="button" on:click={addNestedProperty}>
131+
<button
132+
class="btn btn-primary"
133+
type="button"
134+
on:click={addNestedProperty}
135+
disabled={addNestedPropertyBtnDisabled}
136+
>
124137
Add argument to list
125138
</button>
126139
</div>

src/lib/components/common/jschema/schema_management.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,19 @@ export class SchemaProperty {
249249
return nestedProperty;
250250
}
251251

252+
/**
253+
* @param {any} value
254+
* @param {number} index
255+
*/
256+
updateNestedPropertyValue(value, index) {
257+
const nestedProperty = this.nestedProperties[index];
258+
nestedProperty.value = value;
259+
this.nestedProperties = this.nestedProperties.filter((p, i) =>
260+
i === index ? nestedProperty : p
261+
);
262+
this.manager.updateValue(this.key, this.getUpdatedNestedProperties());
263+
}
264+
252265
/**
253266
* @param {number} index
254267
* @returns {any[]}

tests/jschema.spec.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ test('JSON Schema validation', async ({ page, browserName, workflow }) => {
146146
const addBtn = form.getByRole('button', { name: 'Add argument to list' }).first();
147147
await addBtn.click();
148148
await addBtn.click();
149-
await addBtn.click();
149+
expect(await addBtn.isDisabled()).toEqual(true);
150150
// Fill items
151151
await form.locator('id=property-requiredArrayWithMinMaxItems###0').fill('a');
152152
await form.locator('id=property-requiredArrayWithMinMaxItems###1').fill('b');
@@ -163,6 +163,7 @@ test('JSON Schema validation', async ({ page, browserName, workflow }) => {
163163
await checkFirstArray(['a', 'b', 'd', 'c']);
164164
// Remove items
165165
await form.getByRole('button', { name: 'Remove' }).nth(3).click();
166+
expect(await addBtn.isDisabled()).toEqual(false);
166167
await form.getByRole('button', { name: 'Remove' }).nth(2).click();
167168
await checkFirstArray(['a', 'b']);
168169
await form.getByRole('button', { name: 'Remove' }).nth(1).click();
@@ -186,11 +187,12 @@ test('JSON Schema validation', async ({ page, browserName, workflow }) => {
186187
await addBtn.click();
187188
await addBtn.click();
188189
await addBtn.click();
189-
await addBtn.click();
190+
expect(await addBtn.isDisabled()).toEqual(true);
190191
await form.locator('id=property-optionalArrayWithMinMaxItems###0').fill('a');
191192
await form.locator('id=property-optionalArrayWithMinMaxItems###1').fill('b');
192193
await form.locator('id=property-optionalArrayWithMinMaxItems###2').fill('c');
193194
await form.getByRole('button', { name: 'Remove' }).nth(4).click();
195+
expect(await addBtn.isDisabled()).toEqual(false);
194196
await form.getByRole('button', { name: 'Remove' }).nth(3).click();
195197
await form.getByRole('button', { name: 'Remove' }).nth(2).click();
196198
expect(form.locator('id=property-optionalArrayWithMinMaxItems###0')).toHaveCount(0);

0 commit comments

Comments
 (0)