2.0.2-alpha • Published 10 days ago

m-open-form v2.0.2-alpha

Weekly downloads
-
License
MIT
Repository
-
Last release
10 days ago

open-form

An observable form lib

This lib depends on open-observable

Recommendations

We recommend other libs from the 'open' universe.

Installation

with npm

  npm install open-form

Or with yarn

  yarn add open-form

Setup

In the index.js file, wrap your components with GlobalObservable to recognize the observables

root.render(
    <React.StrictMode>
        <GlobalObservable>
            <App />
        </GlobalObservable>
    </React.StrictMode>
);

Usage/Examples

Form

import { Form, useForm } from 'm-open-form';
import { TextBlock } from '../components/form/TextBlock';
import { Person } from './types.d';

const Contact = () => {
    const handleSubmit = (person: Person) => {
        console.log('person', person); //{name: 'john', email: 'john@mail.com', __FORM_SUBMIT_CHECK: true}
    };

    const [model, form] = useForm(handleSubmit);

    return (
        <div>
            <h1>Contact</h1>

            <Form control={form}>
                <TextBlock type='text' name={model('name')} label='Name' />
                <TextBlock type='email' name={model('email')} label='E-mail' />

                <button type='submit'>Send</button>
            </Form>
        </div>
    );
};

export default Contact;

Field / TextBlock

import { useField } from 'm-open-form';
import { useSubscriber } from 'open-observable';

type Props = {
    label: string;
    name: string;
} & JSX.IntrinsicElements['input'];

export const TextBlock = ({ label, name, ...rest }: Props) => {
    const field$ = useField<string>(name);

    const fieldValue = useSubscriber(field$);

    return (
        <label>
            {label}:
            <input onChange={(x) => field$.next(x.target.value)} value={fieldValue} name={name} {...rest}></input>
        </label>
    );
}

Hooks

  • useForm
  • useField
  • useFormControl
  • useFormResolver
  • useFormSuccessResolver
  • useFormLock
  • useIsFormLocked

API Reference

Hooks

NameDescription
useFormInitialize the form. Returns two objects. model, form Model is a finder by name of prop. Form is a FormControl instance, that yout should pass to component
useFormControlGet the current FormControl instance
useFieldGet the field instance. Use to control and manipulate your inputs
useFormResolverAdd a callback to intercept before submit. Pass a Promise to indicate whether to continue submit or stop it
useFormSuccessResolverAfter success submit. Receive a callback with input and output form submit action
useFormLockLock or unlock the form. When locked, the submit does not occur.
useIsFormLockedCheck if form is locked

useForm(handleSubmit, configurator)

HandleSubmit accepts a function callback or promisse.

Objects

  • FormControl Controls the form. It's possible invoke submit, lock, add resolvers, check if there are changes, etc
  • Field Controls the form field. It is possible to change or get the value, once it is an observable.
2.0.2-alpha

10 days ago

2.0.1-alpha

15 days ago

2.0.0-alpha

15 days ago

1.1.23

1 year ago

1.1.22

1 year ago

1.1.24

1 year ago

1.1.19

1 year ago

1.1.18

1 year ago

1.1.17

1 year ago

1.1.21

1 year ago

1.1.20

1 year ago

1.1.12

1 year ago

1.1.11

2 years ago

1.1.16

1 year ago

1.1.15

1 year ago

1.1.14

1 year ago

1.1.13

1 year ago

1.1.10

2 years ago

1.1.9

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.1.2

2 years ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.16

2 years ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.26

2 years ago

1.0.25

2 years ago

1.0.24

2 years ago

1.0.23

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago