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 🙏

© 2024 – Pkg Stats / Ryan Hefner

wb-forms

v1.0.0-alpha.1

Published

react library to build forms and being happy at the same time

Downloads

6

Readme

wbox-forms

react library to build forms and being happy at the same time

Content :

  • Introduction
  • Quick Start
  • Form Options
  • Field Options
  • Form Factory
  • Defaults
  • Action Button

Introduction

this library help you build ready to go forms, what means we handle for you:

  • Rendering
  • State Management
  • Validation
  • Submitting

in addition to that its very easy to customize and extend.

Quick Start

import {Form} from "wbox-forms"
/*
 * for PasswordField,TextField,SubmitButton 
 * we assume you used one of our extention libraries like wbox-forms-tailwind or wbox-forms-material-ui
 * or you've built them your self
 */

const submitOptions = {
    url : 'http://example.com/login',
    onSuccess : response => console.log(response) 
};
<Form options={{submit : submitOptions}}>
    <TextField name={'username'} validationRules={'^\\.{3:10}$'}/>
    <PasswordField name={'password'} validationRules={'^[0-9]+$'}/>
    <SubmitButton>Login</SubmitButton>
</Form>

from just this code you will have fully functional form.

Form Options

{
    serviceFactoryCallback?: (dispatch: DispatchFunction, state: RootState, props: FormProps) => ServiceFactory;
    getDispatch?: (dispatch: DispatchFunction) => void;
    getState?: (state: RootState) => void;
    reducers?: RootReducer<Action<unknown,unknown>>[];
    serviceOptions?: {
        [serviceName: string]: unknown;
    }
}
  • serviceFactoryCallback callback function used to create your custom service factory, the callback will receive dispatch , rootState and props the passed props to the form

  • reducers array of reducers that will be combined with the root reducer to handle your custom state management

  • serviceOptions object that you used to inject options of each service, for example the options for submit service, the key is of each service option is the service name

  • getDispatch callback function that used to get dispatch function

  • getState callback function that used to get rootState object

one note to mention that when you use getDispatch or getState callback you need to useCallback hook to create the callback if you used function component, or a reference to instance method if you used class component, the main idea is that you shouldn't create new callback on each render.

Field Options

{
    name: string;
    valueSelector?: ValueSelector;
    initialValue?: string;
    validationRules?: unknown;
    skipValidation?: boolean;
    validateOnChange?: boolean;
    initialValid?: boolean;
    hidden?: boolean;
    readonly?: boolean;
    onValueChange?: (newValue: FieldValue) => void;
    clearValue?: FieldValue;
}
  • name name is th only required prop that you need to path to the field, this should be unique in the form.

  • valueSelector callback that used to extract value from the input change event.

  • initialValue the initial value of the field.

  • validationRules the rules that will be used by the validation service to validate field value.

  • skipValidation flag to indicate either to validate this field or not.

  • validateOnChange flag to indicate either to validate this field on value change.

  • initialValid the initial valid state for the field.

  • hidden flag to indicate either to render the input or not.

  • readonly flag to indicate either to prevent changes on field or not.

  • onValueChange callback that will be called on value changes.

  • clearValue the value that will be used to set field value when clear action triggered.

Form Factory

we provide DefaultFormFactory class that can be used to render form from configuration object

look at this example

const types = {
    'text' : TextField,
    'password' : PasswordField
}

const formFactory = new DefaultFormFactory(types);

return formFactory.create({
    formConfig: {
        serviceOptions: {submit: submitOptions}
    },
    fieldConfig: {
        username : {
            fieldConfig: {name: 'username', initialValue: 'admin'},
            type: 'text'
        },
        password : {
            fieldConfig: {name: 'password'},
            type: 'password'
        }
    }
});

DefaultFormFactory accepts field type map in its constructor, it's an object that what component to render.

in the extension libraries you should not worry about this, because each ui extension library will provide its own factory.

Defaults

if you want to override the default values you can wrap your Form component with DefaultsProvider

const defaultValues = {...};
<DefaultsProvider value={defaultValues}>
    <Form>
        ...
    </Form>
<DefaultsProvider/>

the values that you can override are

{
    clearValue: FieldValue;
    fieldValue: string;
    valueSelector: ValueSelector;
}

the current default values are

import {textValueSelector} from "wbox-forms";

{
    fieldValue: '',
    clearValue: '',
    valueSelector: textValueSelector
};

Action Button

you can use Button component to render button that could be used to do dispatch actions.

the component take render a callback function that will receive serviceFactory,dispatch,rootState as arguments, and return react element.

example:

<Button render={(fs,dispatch,state) => <button onClick={() => ...}>DO SOMETHING</button>}/>