npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@coherent.js/forms

v1.0.0-beta.5

Published

SSR + Hydration form system for Coherent.js applications

Downloads

16

Readme

@coherent.js/forms

Comprehensive forms handling and validation utilities for Coherent.js applications.

Installation

npm install @coherent.js/forms
# or
pnpm add @coherent.js/forms
# or
yarn add @coherent.js/forms

Overview

The @coherent.js/forms package provides powerful form handling capabilities including:

  • Form state management
  • Validation with built-in validators
  • Error handling and display
  • Form serialization and submission
  • Integration with Coherent.js components

Quick Start

import { createForm } from '@coherent.js/forms';
import { validators } from '@coherent.js/state';

const contactForm = createForm({
  fields: {
    email: {
      value: '',
      validators: [validators.email('Please enter a valid email')]
    },
    message: {
      value: '',
      validators: [validators.minLength(10, 'Message must be at least 10 characters')]
    }
  }
});

function ContactForm() {
  const handleSubmit = async (event) => {
    event.preventDefault();
    
    if (contactForm.validate()) {
      // Form is valid, submit data
      await submitFormData(contactForm.values);
      contactForm.reset();
    }
  };

  return {
    form: {
      onsubmit: handleSubmit,
      children: [
        {
          input: {
            type: 'email',
            value: contactForm.fields.email.value,
            oninput: (e) => contactForm.setField('email', e.target.value),
            className: contactForm.fields.email.error ? 'error' : ''
          }
        },
        {
          span: {
            text: contactForm.fields.email.error || '',
            className: 'error-message'
          }
        },
        {
          textarea: {
            value: contactForm.fields.message.value,
            oninput: (e) => contactForm.setField('message', e.target.value),
            className: contactForm.fields.message.error ? 'error' : ''
          }
        },
        {
          span: {
            text: contactForm.fields.message.error || '',
            className: 'error-message'
          }
        },
        {
          button: {
            type: 'submit',
            text: 'Send Message',
            disabled: contactForm.isSubmitting
          }
        }
      ]
    }
  };
}

Features

Form State Management

Automatically manage form state including values, errors, and submission status:

const form = createForm({
  fields: {
    username: { value: '' },
    password: { value: '' }
  }
});

// Access form values
console.log(form.values); // { username: '', password: '' }

// Update field values
form.setField('username', 'john_doe');

// Check form validity
console.log(form.isValid); // true/false

// Check submission status
console.log(form.isSubmitting); // true/false

Validation

Built-in validators with custom validation support:

import { validators } from '@coherent.js/state';

const form = createForm({
  fields: {
    email: {
      value: '',
      validators: [
        validators.required('Email is required'),
        validators.email('Please enter a valid email')
      ]
    },
    age: {
      value: '',
      validators: [
        validators.required('Age is required'),
        validators.min(18, 'Must be at least 18 years old')
      ]
    }
  }
});

// Custom validator
const customValidator = (value) => {
  if (value && value.length < 5) {
    return 'Value must be at least 5 characters';
  }
  return null; // null means valid
};

const formWithCustomValidation = createForm({
  fields: {
    customField: {
      value: '',
      validators: [customValidator]
    }
  }
});

Async Validation

Support for asynchronous validation (e.g., checking if username is available):

const asyncValidator = async (value) => {
  if (!value) return null;
  
  const response = await fetch(`/api/check-username/${value}`);
  const exists = await response.json();
  
  return exists ? 'Username is already taken' : null;
};

const signupForm = createForm({
  fields: {
    username: {
      value: '',
      validators: [asyncValidator]
    }
  }
});

API Reference

createForm(options)

Create a new form instance.

Parameters:

  • options.fields - Object defining form fields and their initial state
  • options.onSubmit - Optional function to handle form submission

Returns: Form instance with methods and properties

Form Instance Properties

  • values - Current form values
  • fields - Field state objects with value, error, touched, etc.
  • isValid - Boolean indicating if form is valid
  • isSubmitting - Boolean indicating if form is being submitted
  • errors - Object containing field errors

Form Instance Methods

  • setField(name, value) - Update a field's value
  • validate() - Validate all fields, returns boolean
  • reset() - Reset form to initial state
  • submit() - Trigger form submission

Integration with @coherent.js/state

The forms package integrates seamlessly with the reactive state system:

import { createForm } from '@coherent.js/forms';
import { observable } from '@coherent.js/state';

// Create reactive form
const form = createForm({
  fields: {
    search: { value: '' }
  }
});

// Create observable for search results
const searchResults = observable([]);

// Update search results when form changes
form.fields.search.watch((newValue) => {
  if (newValue.length > 2) {
    performSearch(newValue).then(results => {
      searchResults.value = results;
    });
  }
});

Examples

Login Form

import { createForm } from '@coherent.js/forms';
import { validators } from '@coherent.js/state';

const loginForm = createForm({
  fields: {
    email: {
      value: '',
      validators: [
        validators.required('Email is required'),
        validators.email('Please enter a valid email')
      ]
    },
    password: {
      value: '',
      validators: [
        validators.required('Password is required'),
        validators.minLength(8, 'Password must be at least 8 characters')
      ]
    }
  },
  async onSubmit(values) {
    try {
      const response = await fetch('/api/login', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(values)
      });
      
      if (response.ok) {
        // Handle successful login
        window.location.href = '/dashboard';
      } else {
        // Handle login error
        throw new Error('Invalid credentials');
      }
    } catch (error) {
      loginForm.setError('login', error.message);
    }
  }
});

Related Packages

License

MIT