0.0.7 • Published 11 months ago

ninjaformkit v0.0.7

Weekly downloads
-
License
-
Repository
github
Last release
11 months ago

Ninja Form Kit

Elevate your form development with Ninja Form Kit, a powerful TypeScript library that seamlessly integrates with your theme. This robust toolkit unifies UI generation and form validation into a single configuration object, streamlining your workflow. Leverage type-safe components to create polished, responsive forms with minimal effort. From basic inputs to complex layouts, Ninja Form Kit empowers developers to build professional, user-friendly forms while maintaining code consistency across projects. Simplify your form creation process and enhance productivity with our comprehensive, TypeScript-first solution.

Table of Contents

Installation

To install Ninja Form Kit, use npm or yarn:

npm install ninjaformkit
# or
yarn add ninjaformkit

Quick Start

To configure your form, define a formConfig object in your +page.server.ts file. This object allows you to specify form properties such as method, action, fields, and validation rules. For optimal type safety, ensure your formConfig adheres to the FormConfig interface. Important: Define formConfig outside the load function to guarantee accessibility within actions.

Define the config in loader

import { type FormConfig } from 'ninjaformkit'

const formConfig = {
	method: 'post',
	fields: {
		email: {
			type: 'text',
			variant: 'email',
			placeholder: 'Enter your email...',
			label: 'Email',
			validate: {
				required: true,
				isEmail: true
			}
		},
		message: {
			type: 'textarea',
			placeholder: 'Enter your message...',
			label: 'Message',
			validate: {
				required: true,
				minLength: 10,
				maxLength: 500
			}
		},
		submit: {
			type: 'submit',
			label: 'Submit'
		}
	}
} satisfies FormConfig;

export const load: PageServerLoad = async () => {
	return { formConfig };
};

Use the Form in your template/component

<script lang="ts">
    import { Form } from 'ninjaformkit'
    export let data;
</script>

<Form config={data.formConfig} />

Validate the form with actions

import { validate } from 'ninjaformkit'

export const actions = {
	default: async ({ request }) => {
		const { errors, hasErrors, values } = await validate(request, formConfig);

		if (hasErrors) {
                        // Errors will appears under each input that validation fails
			return fail(400, { errors });
		}

	// Process the type safe form values 
        await sendEmail({
            email: values.email, // string
            message: values.message // string
        });
        
        return redirect(301, '/');
 
	}
} satisfies Actions;

Form Config Options

The formConfig object allows you to configure various aspects of your form. Below is a table explaining each option:

OptionTypeDescription
actionstringThe URL to which the form data will be submitted. Optional.
encType'application/x-www-form-urlencoded' \| 'multipart/form-data' \| 'text/plain'The encoding type for the form. Optional.
method'get' \| 'post'The HTTP method to use when submitting the form. Required.
fieldsRecord<string, FormField>An object where each key is a field name and the value is a FormField object. Required.
runLoaderbooleanDenotes weather to run sveltekits invalidateAll hook. Optional

FormField Options

Each field in the fields object is a FormField with the following options:

OptionTypeDescription
type'text' \| 'textarea' \| 'submit' \| 'checkbox' \| 'radio' \| 'toggle' \| 'select' \| 'file'The type of the form field. Required.
labelstringThe label for the form field. Required.
defaultValuestring \| number \| nullThe default value for the form field. Optional.
defaultCheckedbooleanThe default checked state for checkbox, radio, or toggle fields. Optional.
width1 \| 2 \| 3 \| 4 \| 5 \| 6 \| 7 \| 8 \| 9 \| 10 \| 11 \| 12The width of the form field, used for fieldset grid layout. Optional. Defaults to 12.
variant'password' \| 'email' \| 'text' \| 'color' \| 'number'The variant of the form field, specifying a more specific type. Works with the "text" input type. Defaults to "text". Optional.
placeholderstringThe placeholder text for the form field. Optional.
multiplebooleanWhether the field allows multiple values (e.g., for file and select inputs). Optional.
breakbooleanWeather to break into a new row in grid. Optional.
iconstringAn icon to display with the form field. This relies on @iconify library (e.g. material-symbols:verified-user) Optional.
options{ value: string \| number; label: string; }[]An array of options for select and radio fields. Each option has a value and a label. Optional. Note Select and Radio inputs will throw an error if these values are not set.
disabledbooleanWhether the form field is disabled. Optional.
beforestringContent to display before the form field. Optional.
afterstringContent to display after the form field. Optional.
validateobjectValidation rules for the form field. Optional.

Validation

Ninja Form Kit currently has 19 validate options.

Validation Options

The validate object allows you to specify validation rules for the form field. Below is a table explaining each option:

OptionTypeDescription
requiredbooleanWhether the field is required.
minLengthnumberThe minimum length of the field value.
maxLengthnumberThe maximum length of the field value.
minnumberThe minimum value for numeric fields.
maxnumberThe maximum value for numeric fields.
isEmailbooleanWhether the field value should be a valid email address.
isUrlbooleanWhether the field value should be a valid URL.
isNumberbooleanWhether the field value should be a valid number.
isFloatbooleanWhether the field value should be a valid float.
isAlphabooleanWhether the field value should contain only alphabetic characters.
isAlphanumericbooleanWhether the field value should contain only alphanumeric characters.
isDatebooleanWhether the field value should be a valid date.
isTimebooleanWhether the field value should be a valid time.
isDateTimebooleanWhether the field value should be a valid date-time.
mimeTypesArray<string>An array of acceptable MIME types for file inputs.
maxSizenumberThe maximum file size for file inputs, in bytes.
matchesstringA regex pattern that the field value should match.
badPasswordCheckbooleanWhether to check the field value against a list of common bad passwords. Utilises pwned passwords API.

Theming

Ninja Form Kit offers a wide array of customizable CSS variables. Simply import the CSS file into your +layout.svelte and tailor the variables to perfectly match your theme.

:root {
    --nfk-font: ui-sans-serif, system-ui, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
    --nfk-default: #005f8b; // Button, radio and toggle colours
    --nfk-default-hover: #0a5171; // Button, radio and toggle colours hover
    --nfk-text: #000000; // text colour for labels
    --nkf-input-bg: #f3f4f6; // Back ground colour for form inputs
    --nkf-input-bg-file-hover: #f3f4f6; // Hover state for file input
    --nfk-button-text: #ffffff; // Text colout for buttons and radio
    --nkf-placeholder: #a0aec0; // Placeholder text colour
    --nfk-border: #e2e8f0; // Input border
    --nkf-error: #e53e3e; // Error color for border and error text
    --nkf-required: #e84949; // Required asterix on inouts with validation rules
    --nfk-outline: #0073aa; // Outline for inout focused
    --nfk-gap-x: 14px; // Gap between inputs row
    --nfk-gap-y: 14px; // Gap between inputs colun
}

@media (prefers-color-scheme: dark) {
    :root {
        --nfk-default: #005f8b;
        --nfk-text: #FFFFFF;
        --nkf-input-bg: #35383c;
        --nkf-input-bg-file-hover: #595c61;
        --nfk-button-text: #ffffff;
        --nkf-placeholder: #dfe3e7;
        --nfk-border: #e2e8f0;
        --nkf-error: #e53e3e;
        --nkf-required: #e84949;
        --nfk-outline: #0073aa;
        --nkf-btn-text: #ffffff;
    }
}

More examples

Register Form

const formConfig = {
	method: 'post',
	fields: {
		username: {
			type: 'text',
			label: 'Username',
			placeholder: 'Enter your username...',
			width: 5,
			validate: {
				required: true,
				minLength: 3,
				maxLength: 20
			}
		},
		email: {
			type: 'text',
			variant: 'email',
			label: 'Email',
			placeholder: 'Enter your email...',
			width: 7,
			validate: {
				required: true,
				isEmail: true
			}
		},
		referer: {
			type: 'select',
			label: 'Referer',
			placeholder: 'Select an option...',
			width: 4,
			options: [
				{ label: 'Google', value: 'google' },
				{ label: 'Friend', value: 'friend' },
				{ label: 'Other', value: 'other' }
			],
			break: true,
			validate: {
				required: true
			}
		},
		accountType: {
			type: 'radio',
			label: 'Account Type',
			width: 8,
			options: [
				{ label: 'Personal', value: 'personal' },
				{ label: 'Business', value: 'business' }
			],
			validate: {
				required: true
			}
		},
		password: {
			type: 'text',
			variant: 'password',
			label: 'Password',
			placeholder: 'Enter your password...',
			width: 6,
			validate: {
				required: true,
				minLength: 8,
				badPasswordCheck: true
			}
		},
		confirmPassword: {
			type: 'text',
			variant: 'password',
			label: 'Confirm Password',
			placeholder: 'Confirm your password...',
			width: 6,
			validate: {
				required: true,
				matches: 'password'
			}
		},
		newsletter: {
			type: 'checkbox',
			label: 'Subscribe to newsletter',
			defaultChecked: true
		},
		submit: {
			type: 'submit',
			label: 'Submit'
		}
	}
} satisfies FormConfig;

Login Form

const formConfig = {
	method: 'post',
	fields: {
		email: {
			type: 'text',
			variant: 'email',
			placeholder: 'Enter your email...',
			label: 'Email',
			validate: {
				required: true,
				isEmail: true
			},
                        after: `<a href="/reset-password" class="text-sm mt-3 text-right">Forgot Password</a>`
		},
		password: {
			type: 'text',
			variant: 'password',
			placeholder: 'Enter your password...',
			label: 'Password',
			validate: {
				required: true
			}
		},
		rememberMe: {
			type: 'checkbox',
			label: 'Remember me'
		},
		submit: {
			type: 'submit',
			label: 'Submit'
		}
	}
} satisfies FormConfig;

License

This project is licensed under the MIT License. See the LICENSE file for details.

0.0.7

11 months ago

0.0.6

11 months ago

0.0.5

11 months ago

0.0.4

11 months ago

0.0.3

11 months ago

0.0.2

11 months ago

0.0.1

11 months ago