Skip to content

Commit

Permalink
feat: scroll to elements on the list
Browse files Browse the repository at this point in the history
  • Loading branch information
danielo515 committed Oct 22, 2023
1 parent c46dc40 commit f9804a7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
5 changes: 4 additions & 1 deletion src/core/formDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ export function validateFields(fields: unknown) {
}
console.error('Fields issues', result.issues)
return result.issues.map(issue =>
({ message: issue.message, path: issue.path?.map(item => item.key).join('.') })
({
message: issue.message, path: issue.path?.map(item => item.key).join('.'),
index: issue.path?.[0]?.key ?? 0
})
);
}

Expand Down
26 changes: 19 additions & 7 deletions src/views/FormBuilder.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
name: "",
fields: [],
};
export let app: App;
export let onChange: () => void;
export let onSubmit: (formDefinition: FormDefinition) => void;
export let onCancel: () => void;
Expand All @@ -28,13 +27,18 @@
$: errors = validateFields(definition.fields);
$: activeFieldIndex = 0;
function scrollWhenActive(element: HTMLElement, index: number) {
function update() {
if (index === activeFieldIndex) {
element.scrollIntoView({ behavior: "smooth", block: "center" });
function scrollWhenActive(element: HTMLElement, isActive: boolean) {
function update(isActive: boolean) {
if (isActive) {
setTimeout(() => {
element.scrollIntoView({
behavior: "smooth",
block: "center",
});
}, 100);
}
}
update();
update(isActive);
return {
update,
};
Expand Down Expand Up @@ -83,6 +87,7 @@
...definition.fields.slice(fieldIndex + 1),
];
onChange();
activeFieldIndex = fieldIndex + 1;
}
function moveField(from: number, direction: "up" | "down") {
Expand All @@ -95,6 +100,7 @@
definition.fields[to] = tmp;
definition.fields = definition.fields;
onChange();
activeFieldIndex = to;
}
const handleSubmit = () => {
Expand Down Expand Up @@ -173,6 +179,12 @@
{#if error.path}
at {error.path}
{/if}
<button
type="button"
on:click={() => {
activeFieldIndex = error.index;
}}>Go to problem</button
>
</li>
{/each}
</ul>
Expand All @@ -187,7 +199,7 @@
{@const delete_id = `delete_${index}`}
<div
class="flex column md-row gap2"
use:scrollWhenActive={index}
use:scrollWhenActive={index === activeFieldIndex}
>
<div class="flex column gap1">
<label for={`name_${index}`}>Name</label>
Expand Down

0 comments on commit f9804a7

Please sign in to comment.