refactor: centralize form and record helpers
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
<script lang="ts">
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import PlusIcon from '@lucide/svelte/icons/plus';
|
||||
import { Field as FormField, Control, FieldErrors } from 'formsnap';
|
||||
import * as Form from '$lib/components/ui/form/index.js';
|
||||
import MoneyField from '$lib/components/money-field.svelte';
|
||||
import * as Field from '$lib/components/ui/field/index.js';
|
||||
import { Button } from '$lib/components/ui/button/index.js';
|
||||
import * as Dialog from '$lib/components/ui/dialog/index.js';
|
||||
import { Input } from '$lib/components/ui/input/index.js';
|
||||
@@ -34,56 +33,48 @@
|
||||
<Dialog.Description>Add a new contract record.</Dialog.Description>
|
||||
</Dialog.Header>
|
||||
<form method="POST" action="?/create" class="grid gap-2" use:enhanceCreate>
|
||||
<FormField form={createForm} name="title">
|
||||
<Field.Field>
|
||||
<Control id="create-title">
|
||||
{#snippet children({ props })}
|
||||
<Field.Label>Title</Field.Label>
|
||||
<Input {...props} type="text" bind:value={$createData.title} />
|
||||
{/snippet}
|
||||
</Control>
|
||||
<Field.Error><FieldErrors /></Field.Error>
|
||||
</Field.Field>
|
||||
</FormField>
|
||||
<FormField form={createForm} name="startDate">
|
||||
<Field.Field>
|
||||
<Control id="create-startDate">
|
||||
{#snippet children({ props })}
|
||||
<Field.Label>Start date</Field.Label>
|
||||
<Input {...props} type="date" bind:value={$createData.startDate} />
|
||||
{/snippet}
|
||||
</Control>
|
||||
<Field.Error><FieldErrors /></Field.Error>
|
||||
</Field.Field>
|
||||
</FormField>
|
||||
<FormField form={createForm} name="endDate">
|
||||
<Field.Field>
|
||||
<Control id="create-endDate">
|
||||
{#snippet children({ props })}
|
||||
<Field.Label>End date</Field.Label>
|
||||
<Input {...props} type="date" bind:value={$createData.endDate} />
|
||||
{/snippet}
|
||||
</Control>
|
||||
<Field.Error><FieldErrors /></Field.Error>
|
||||
</Field.Field>
|
||||
</FormField>
|
||||
<FormField form={createForm} name="status">
|
||||
<Field.Field>
|
||||
<Control id="create-status">
|
||||
{#snippet children({ props })}
|
||||
<Field.Label>Status</Field.Label>
|
||||
<NativeSelect.Root {...props} class="w-full" bind:value={$createData.status}>
|
||||
<NativeSelect.Option value="">Not set</NativeSelect.Option>
|
||||
<NativeSelect.Option value="draft">Draft</NativeSelect.Option>
|
||||
<NativeSelect.Option value="active">Active</NativeSelect.Option>
|
||||
<NativeSelect.Option value="expired">Expired</NativeSelect.Option>
|
||||
<NativeSelect.Option value="cancelled">Cancelled</NativeSelect.Option>
|
||||
</NativeSelect.Root>
|
||||
{/snippet}
|
||||
</Control>
|
||||
<Field.Error><FieldErrors /></Field.Error>
|
||||
</Field.Field>
|
||||
</FormField>
|
||||
<Form.Field form={createForm} name="title">
|
||||
<Form.Control id="create-title">
|
||||
{#snippet children({ props })}
|
||||
<Form.Label>Title</Form.Label>
|
||||
<Input {...props} type="text" bind:value={$createData.title} />
|
||||
{/snippet}
|
||||
</Form.Control>
|
||||
<Form.FieldErrors />
|
||||
</Form.Field>
|
||||
<Form.Field form={createForm} name="startDate">
|
||||
<Form.Control id="create-startDate">
|
||||
{#snippet children({ props })}
|
||||
<Form.Label>Start date</Form.Label>
|
||||
<Input {...props} type="date" bind:value={$createData.startDate} />
|
||||
{/snippet}
|
||||
</Form.Control>
|
||||
<Form.FieldErrors />
|
||||
</Form.Field>
|
||||
<Form.Field form={createForm} name="endDate">
|
||||
<Form.Control id="create-endDate">
|
||||
{#snippet children({ props })}
|
||||
<Form.Label>End date</Form.Label>
|
||||
<Input {...props} type="date" bind:value={$createData.endDate} />
|
||||
{/snippet}
|
||||
</Form.Control>
|
||||
<Form.FieldErrors />
|
||||
</Form.Field>
|
||||
<Form.Field form={createForm} name="status">
|
||||
<Form.Control id="create-status">
|
||||
{#snippet children({ props })}
|
||||
<Form.Label>Status</Form.Label>
|
||||
<NativeSelect.Root {...props} class="w-full" bind:value={$createData.status}>
|
||||
<NativeSelect.Option value="">Not set</NativeSelect.Option>
|
||||
<NativeSelect.Option value="draft">Draft</NativeSelect.Option>
|
||||
<NativeSelect.Option value="active">Active</NativeSelect.Option>
|
||||
<NativeSelect.Option value="expired">Expired</NativeSelect.Option>
|
||||
<NativeSelect.Option value="cancelled">Cancelled</NativeSelect.Option>
|
||||
</NativeSelect.Root>
|
||||
{/snippet}
|
||||
</Form.Control>
|
||||
<Form.FieldErrors />
|
||||
</Form.Field>
|
||||
<MoneyField
|
||||
form={createForm}
|
||||
data={createData}
|
||||
@@ -91,17 +82,15 @@
|
||||
label="Value"
|
||||
id="create-valueGbp"
|
||||
/>
|
||||
<FormField form={createForm} name="notes">
|
||||
<Field.Field>
|
||||
<Control id="create-notes">
|
||||
{#snippet children({ props })}
|
||||
<Field.Label>Notes</Field.Label>
|
||||
<Textarea {...props} bind:value={$createData.notes} />
|
||||
{/snippet}
|
||||
</Control>
|
||||
<Field.Error><FieldErrors /></Field.Error>
|
||||
</Field.Field>
|
||||
</FormField>
|
||||
<Form.Field form={createForm} name="notes">
|
||||
<Form.Control id="create-notes">
|
||||
{#snippet children({ props })}
|
||||
<Form.Label>Notes</Form.Label>
|
||||
<Textarea {...props} bind:value={$createData.notes} />
|
||||
{/snippet}
|
||||
</Form.Control>
|
||||
<Form.FieldErrors />
|
||||
</Form.Field>
|
||||
<Dialog.Footer>
|
||||
<Dialog.Close
|
||||
>{#snippet child({ props })}<Button variant="outline" {...props}>Cancel</Button
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
<script lang="ts">
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import { Field as FormField, Control, FieldErrors } from 'formsnap';
|
||||
import * as Form from '$lib/components/ui/form/index.js';
|
||||
import MoneyField from '$lib/components/money-field.svelte';
|
||||
import * as Field from '$lib/components/ui/field/index.js';
|
||||
import { Button } from '$lib/components/ui/button/index.js';
|
||||
import * as Dialog from '$lib/components/ui/dialog/index.js';
|
||||
import { Input } from '$lib/components/ui/input/index.js';
|
||||
@@ -32,56 +31,48 @@
|
||||
{#if editingRecord}
|
||||
<form method="POST" action="?/edit" class="grid gap-2" use:enhanceEdit>
|
||||
<input type="hidden" name="id" value={$editData.id} />
|
||||
<FormField form={editForm} name="title">
|
||||
<Field.Field>
|
||||
<Control id="edit-title">
|
||||
{#snippet children({ props })}
|
||||
<Field.Label>Title</Field.Label>
|
||||
<Input {...props} type="text" bind:value={$editData.title} />
|
||||
{/snippet}
|
||||
</Control>
|
||||
<Field.Error><FieldErrors /></Field.Error>
|
||||
</Field.Field>
|
||||
</FormField>
|
||||
<FormField form={editForm} name="startDate">
|
||||
<Field.Field>
|
||||
<Control id="edit-startDate">
|
||||
{#snippet children({ props })}
|
||||
<Field.Label>Start date</Field.Label>
|
||||
<Input {...props} type="date" bind:value={$editData.startDate} />
|
||||
{/snippet}
|
||||
</Control>
|
||||
<Field.Error><FieldErrors /></Field.Error>
|
||||
</Field.Field>
|
||||
</FormField>
|
||||
<FormField form={editForm} name="endDate">
|
||||
<Field.Field>
|
||||
<Control id="edit-endDate">
|
||||
{#snippet children({ props })}
|
||||
<Field.Label>End date</Field.Label>
|
||||
<Input {...props} type="date" bind:value={$editData.endDate} />
|
||||
{/snippet}
|
||||
</Control>
|
||||
<Field.Error><FieldErrors /></Field.Error>
|
||||
</Field.Field>
|
||||
</FormField>
|
||||
<FormField form={editForm} name="status">
|
||||
<Field.Field>
|
||||
<Control id="edit-status">
|
||||
{#snippet children({ props })}
|
||||
<Field.Label>Status</Field.Label>
|
||||
<NativeSelect.Root {...props} class="w-full" bind:value={$editData.status}>
|
||||
<NativeSelect.Option value="">Not set</NativeSelect.Option>
|
||||
<NativeSelect.Option value="draft">Draft</NativeSelect.Option>
|
||||
<NativeSelect.Option value="active">Active</NativeSelect.Option>
|
||||
<NativeSelect.Option value="expired">Expired</NativeSelect.Option>
|
||||
<NativeSelect.Option value="cancelled">Cancelled</NativeSelect.Option>
|
||||
</NativeSelect.Root>
|
||||
{/snippet}
|
||||
</Control>
|
||||
<Field.Error><FieldErrors /></Field.Error>
|
||||
</Field.Field>
|
||||
</FormField>
|
||||
<Form.Field form={editForm} name="title">
|
||||
<Form.Control id="edit-title">
|
||||
{#snippet children({ props })}
|
||||
<Form.Label>Title</Form.Label>
|
||||
<Input {...props} type="text" bind:value={$editData.title} />
|
||||
{/snippet}
|
||||
</Form.Control>
|
||||
<Form.FieldErrors />
|
||||
</Form.Field>
|
||||
<Form.Field form={editForm} name="startDate">
|
||||
<Form.Control id="edit-startDate">
|
||||
{#snippet children({ props })}
|
||||
<Form.Label>Start date</Form.Label>
|
||||
<Input {...props} type="date" bind:value={$editData.startDate} />
|
||||
{/snippet}
|
||||
</Form.Control>
|
||||
<Form.FieldErrors />
|
||||
</Form.Field>
|
||||
<Form.Field form={editForm} name="endDate">
|
||||
<Form.Control id="edit-endDate">
|
||||
{#snippet children({ props })}
|
||||
<Form.Label>End date</Form.Label>
|
||||
<Input {...props} type="date" bind:value={$editData.endDate} />
|
||||
{/snippet}
|
||||
</Form.Control>
|
||||
<Form.FieldErrors />
|
||||
</Form.Field>
|
||||
<Form.Field form={editForm} name="status">
|
||||
<Form.Control id="edit-status">
|
||||
{#snippet children({ props })}
|
||||
<Form.Label>Status</Form.Label>
|
||||
<NativeSelect.Root {...props} class="w-full" bind:value={$editData.status}>
|
||||
<NativeSelect.Option value="">Not set</NativeSelect.Option>
|
||||
<NativeSelect.Option value="draft">Draft</NativeSelect.Option>
|
||||
<NativeSelect.Option value="active">Active</NativeSelect.Option>
|
||||
<NativeSelect.Option value="expired">Expired</NativeSelect.Option>
|
||||
<NativeSelect.Option value="cancelled">Cancelled</NativeSelect.Option>
|
||||
</NativeSelect.Root>
|
||||
{/snippet}
|
||||
</Form.Control>
|
||||
<Form.FieldErrors />
|
||||
</Form.Field>
|
||||
<MoneyField
|
||||
form={editForm}
|
||||
data={editData}
|
||||
@@ -89,17 +80,15 @@
|
||||
label="Value"
|
||||
id="edit-valueGbp"
|
||||
/>
|
||||
<FormField form={editForm} name="notes">
|
||||
<Field.Field>
|
||||
<Control id="edit-notes">
|
||||
{#snippet children({ props })}
|
||||
<Field.Label>Notes</Field.Label>
|
||||
<Textarea {...props} bind:value={$editData.notes} />
|
||||
{/snippet}
|
||||
</Control>
|
||||
<Field.Error><FieldErrors /></Field.Error>
|
||||
</Field.Field>
|
||||
</FormField>
|
||||
<Form.Field form={editForm} name="notes">
|
||||
<Form.Control id="edit-notes">
|
||||
{#snippet children({ props })}
|
||||
<Form.Label>Notes</Form.Label>
|
||||
<Textarea {...props} bind:value={$editData.notes} />
|
||||
{/snippet}
|
||||
</Form.Control>
|
||||
<Form.FieldErrors />
|
||||
</Form.Field>
|
||||
<Dialog.Footer>
|
||||
<Dialog.Close
|
||||
>{#snippet child({ props })}<Button variant="outline" {...props}>Cancel</Button
|
||||
|
||||
Reference in New Issue
Block a user