Skip to content

Commit

Permalink
feat: scroll to new fields in the editor
Browse files Browse the repository at this point in the history
  • Loading branch information
danielo515 committed Oct 22, 2023
1 parent defc86c commit ce3ece5
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/views/FormBuilder.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
$: isValid = isValidFormDefinition(definition);
$: errors = validateFields(definition.fields);
$: activeFieldIndex = 0;
function folderField(element: HTMLElement, index: number) {
const field = definition.fields[index];
Expand All @@ -45,6 +46,18 @@
});
}
function scrollWhenActive(element: HTMLElement, index: number) {
function update() {
if (index === activeFieldIndex) {
element.scrollIntoView({ behavior: "smooth", block: "center" });
}
}
update();
return {
update,
};
}
function findFreeName(fieldIndex: number): string {
const field = definition.fields[fieldIndex];
let name = field.name;
Expand Down Expand Up @@ -130,7 +143,8 @@
input: { type: "text" },
},
];
onChange();
// onChange();
activeFieldIndex = definition.fields.length - 1;
}}>Add more fields</button
>
<button
Expand Down Expand Up @@ -169,7 +183,10 @@
{#each definition.fields as field, index}
{@const desc_id = `desc_${index}`}
{@const delete_id = `delete_${index}`}
<div class="flex column md-row gap2">
<div
class="flex column md-row gap2"
use:scrollWhenActive={index}
>
<div class="flex column gap1">
<label for={`name_${index}`}>Name</label>
<input
Expand Down

0 comments on commit ce3ece5

Please sign in to comment.