2.0.4-alpha • Published 5 months ago

m-open-form v2.0.4-alpha

Weekly downloads
-
License
MIT
Repository
-
Last release
5 months 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.4-alpha

5 months ago

2.0.3-alpha

5 months ago

2.0.2-alpha

1 year ago

2.0.1-alpha

1 year ago

2.0.0-alpha

1 year ago

1.1.23

2 years ago

1.1.22

2 years ago

1.1.24

2 years ago

1.1.19

2 years ago

1.1.18

2 years ago

1.1.17

2 years ago

1.1.21

2 years ago

1.1.20

2 years ago

1.1.12

3 years ago

1.1.11

3 years ago

1.1.16

2 years ago

1.1.15

3 years ago

1.1.14

3 years ago

1.1.13

3 years ago

1.1.10

3 years ago

1.1.9

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.1.2

3 years ago

1.0.19

3 years ago

1.0.18

3 years ago

1.0.16

3 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.20

3 years ago

1.0.26

3 years ago

1.0.25

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago