Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SvelteKit integration #28

Merged
merged 76 commits into from
Nov 15, 2024
Merged

SvelteKit integration #28

merged 76 commits into from
Nov 15, 2024

Conversation

x0k
Copy link
Owner

@x0k x0k commented Nov 11, 2024

Example

+page.svelte.ts

<script lang="ts">
  import { FormContent } from '@sjsf/form';
  import { createValidator } from '@sjsf/ajv8-validator';
  import { theme } from '@sjsf/form/basic-theme';
  import { translation } from '@sjsf/form/translations/en';

  import { useSvelteKitForm } from '@sjsf/sveltekit/client';

  import type { PageData, ActionData } from './$types';

  const { form } = useSvelteKitForm<ActionData, PageData>({
    ...theme,
    name: 'form',
    validator: createValidator(),
    translation
  });
</script>

<form method="POST" novalidate style="display: flex; flex-direction: column; gap: 1rem">
  <FormContent bind:value={form.formValue} />
  <button type="submit" style="padding: 0.5rem;">Submit</button>
</form>

+page.server.ts

import type { Schema } from '@sjsf/form';
import { createValidator } from '@sjsf/ajv8-validator';

import { initForm, makeFormDataParser, validateForm } from '@sjsf/sveltekit/server';

import type { Actions } from './$types';

const validator = createValidator();

const parseFormData = makeFormDataParser({
  validator
});

const schema: Schema = {
  title: 'Registration form',
  type: 'object',
  required: ['firstName', 'lastName'],
  properties: {
    firstName: {
      type: 'string',
      title: 'First name'
    },
    lastName: {
      type: 'string',
      title: 'Last name'
    }
  }
};

export const load = async () => {
  const form = initForm({ schema, validator, sendSchema: true });
  return { form };
};

export const actions = {
  default: async ({ request }) => {
    return {
      form: validateForm({
        schema,
        validator,
        data: await parseFormData({
          schema,
          request,
        }),
        sendData: true
      })
    };
  }
} satisfies Actions;

x0k added 30 commits November 7, 2024 02:29
Copy link

changeset-bot bot commented Nov 11, 2024

🦋 Changeset detected

Latest commit: ed43046

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 14 packages
Name Type
@sjsf/form Minor
playground Minor
@sjsf/ajv8-validator Minor
docs Minor
@sjsf/daisyui-theme Minor
@sjsf/flowbite-icons Minor
@sjsf/flowbite-theme Minor
@sjsf/lucide-icons Minor
@sjsf/radix-icons Minor
@sjsf/shadcn-theme Minor
@sjsf/skeleton-theme Minor
@sjsf/sveltekit Minor
testing Patch
@sjsf/zod-integration Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@x0k x0k marked this pull request as ready for review November 15, 2024 13:27
@x0k x0k merged commit 517f69d into main Nov 15, 2024
@github-actions github-actions bot mentioned this pull request Nov 15, 2024
@x0k x0k deleted the schema-analysis branch November 27, 2024 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant