Skip to content

FormProvider

Hosts the forms opened from code with FormService (typed) and DynamicFormService (runtime), in a modal, a drawer or full screen. Mount one provider in a layout and open a form from any component of the same scope.

On this page

Examples

Typed form in a drawer

CreateFormAsync returns a task that completes after a submit with IsCompleted true and the submitted Model, or after a close with IsCompleted false. The overlay is a native modal dialog (a slideover for the drawer): focus stays inside, Escape and a click on the backdrop close it, and it cannot be closed while the form saves. The layout of the hub site mounts the KlyFormProvider; this page does not add a second one, which would draw every overlay twice. Statically rendered the button does nothing.

No project yet.

Razor
@inject FormService Forms

@* The layout of the hub site mounts one KlyFormProvider, which draws the overlay this service fills. *@
<div class="flex w-full max-w-md flex-col gap-3">
    <KlyButton Label="New project" OnClick="CreateAsync" />
    @if (_projects.Count == 0)
    {
        <p class="m-0 text-sm text-(--klyroo-text-muted)">No project yet.</p>
    }
    else
    {
        <ul class="m-0 list-disc ps-5 text-sm">
            @foreach (var name in _projects)
            {
                <li>@name</li>
            }
        </ul>
    }
</div>

@code {
    private readonly List<string> _projects = [];

    private static readonly FormSchema<NewProject> Schema = FormSchema.For<NewProject>(s =>
    {
        s.FormKey = "new-project";
        s.Title = "New project";
        s.Field(m => m.Name, f => f.Required());
    });

    private async Task CreateAsync()
    {
        var outcome = await Forms.CreateFormAsync(Schema, mode: FormShellMode.Drawer);
        // IsCompleted is false when the user closed the form without submitting.
        if (outcome.IsCompleted) _projects.Add(outcome.Model!.Name);
    }

    public sealed class NewProject
    {
        public string Name { get; set; } = "";
    }
}

Runtime form in a modal

DynamicFormService.CreateFormAsync takes a DynamicFormSchema and returns a DynamicFormOutcome whose FormData holds the output. One KlyFormProvider hosts the forms of both services, and opening a form while another is open replaces it.

No project yet.

Razor
@using Klyroo.Form.Dynamic
@inject DynamicFormService Forms

@* The same KlyFormProvider hosts the forms of both services. *@
<div class="flex w-full max-w-md flex-col gap-3">
    <KlyButton Label="New project" OnClick="CreateAsync" />
    @if (_projects.Count == 0)
    {
        <p class="m-0 text-sm text-(--klyroo-text-muted)">No project yet.</p>
    }
    else
    {
        <ul class="m-0 list-disc ps-5 text-sm">
            @foreach (var name in _projects)
            {
                <li>@name</li>
            }
        </ul>
    }
</div>

@code {
    private readonly List<string> _projects = [];

    private static readonly DynamicFormSchema NewProject = new()
    {
        FormKey = "new-project",
        Title = "New project",
        Fields = [new DynamicFieldSchema { Key = "name", Label = "Name", Required = true }],
    };

    private async Task CreateAsync()
    {
        var outcome = await Forms.CreateFormAsync(NewProject, mode: FormShellMode.Modal);
        if (outcome.IsCompleted) _projects.Add((string)outcome.FormData["name"]!);
    }
}

API

KlyFormProvider

Host of the overlay forms opened with FormService (typed) and DynamicFormService (runtime). Mount one instance in a layout, wrapping the page content, and call FormShellMode) or FormShellMode) from anywhere in the same scope.

Parameters

Name Type Description
Class string Classes added to the root element, merged last so they win over the theme.

Slots

Name Type Description
ChildContent RenderFragment The content of the provider, usually the page body. Overlay forms render after it.