2.0.12 • Published 4 years ago

@gferrand/react-forms v2.0.12

Weekly downloads
143
License
ISC
Repository
-
Last release
4 years ago

React-forms

React-forms is here to simplify your forms.

Installing

First, install the library :

npm i @gferrand/react-forms

Be sure to receive at least the version 2.X.X.

... and that's it ! You can now import everything you need inside your project.

Creating your form

First, you will import Form inside your code.

import { Form } from '@gferrand/react-forms';

Form, and all our inputs are based on the ES6 classes, to use them you will have to put the keyword new in front of them.

Instanciate your form inside your constructor, or before the return of your stateless component, like so :

this.form = new Form();

Form take an object as arguments, with few mandatories things :

  • The name of your form;

  • The inputs your want to put inside;

  • A callback to invoke on submitting;

A very basic form would look like this :

this.form = new Form({
    name:  'form',  // Must be a string
    inputs: [ // Must be an array
        new TextInput("name",  "Name")
    ],
    submitCbk // Respect the name convention otherwise it won't work
});

We will come back later on the Input constructor.

Once you've wrote your callback, you can render your form in two different ways :

  • First, you can use our Beautify class, it's gonna make a simple grid system based on Bootstrap. To do so :

DefaultTemplate.form(this.form);

This method is gonna loop through all the inputs you've declared.

  • Second, you will manually render them one by one. This method offer a finer control on the inputs and the way to render them. To do so, you first have to get your input from the Form :

this.form.getInput('name');

You now have access to all the inner methods of the inputs. To keep it simple for now, let's just get the field, the label and the component to render in case of errors.

this.form.getInput('name').getLabel();

this.form.getInput('name').getField();

this.form.getInput('name').getError();

As they are all different React components, you can place them as your desires inside your HTML, inside Bootsrap cards for example.

Obviously, as we have never tell to the Form how to validate our inputs, the error component is never gonna render.

Create your inputs

The previous example lead us to the next step, configuring our inputs. Every inputs take two mandatories parameters : his name, and his label name just like we did before.

But you can pass an extra parameter that we call the validators.

new  TextInput("name",  "Name", [Required]);

The validators goal is too ensure your data is the one you're expecting every time your gonna submit.

The 'Required' validator is also imported from the library. This one will ensure that the field must be fulfilled by at least one character to pass the submission process.

Note that even if you give one validator, this one must be in an array.

Every time you'll submit your form, we'll loop through all the validators for every inputs, and check if they pass the test we wrote for it.

Submit your form

To help you submitting your form, we expose a method inside the Form you just created, submit().

It's your job to create the button, or whatever you want to trigger the submit.

this.form.submit();

Extra parameters for the form constructor

this.form = new Form({
    name:  'form',  // Must be a string
    inputs: [ // Must be an array
        new TextInput("name",  "Name")
    ],
    submitCbk // Respect the name convention otherwise it won't work
});

Previously we left our form like this, but it does accept a few more things to make your developpement even easier.

data

data : {}

Passing an object called data will auto complete your inputs with the data contained into the object. For example :

data : {
    name : "Jacques"
}

Will add the word "Jacques" as a value for the "name" input. You can pass as much data as you want, just pay attention to respect your inputs names for the data keys.

changeCbk

changeCbk : yourFunction

The form accept another function, an onChange function. Wrote it, and do whatever you want inside, that function will be called on every changes for every inputs for the form.

blurCbk

blurCbk : yourFunction

It does accept aswell an onBlur function wich will proc everytime the user will lost the focus on an input.

parent

parent : this

Only use it when you plan on using nested forms, it allow react-forms to notify your component that a new form has been added and need to be rendered.

formatting

formatting : {
    field: yourCustomClass,
    fieldError: yourCustomClass,
    label: yourCustomClass,
    labelError: yourCustomClass,
    error : yourCustomClass
}

In case you need to overwrite some, or even all, the preformatted styles inside your form you can use the formatting object. We'll see right after how to create a custom class, but for now let's focus on how to use the object. You NEED to respect the keys spelling, otherwise it won't work. React-forms is made so it does have fallback values for all the keys specified above, if you only want to overwrite one, pass this one, no need to pass them all everytime.

Customize your form

React-form styling is based on classes that we expose to you :

  • DefaultFieldFormatting
  • DefaultErrorFieldFormatting
  • DefaultLabelFormatting
  • DefaultErrorLabelFormatting
  • DefaultErrorFormatting

All of them contains numerous methods to target specific type of inputs, they all follow that template :

static oneOfTheFollowingNames = () => "someClassNames";
  • defaultClassNames

  • text

  • password

  • number

  • select

  • radio

  • date
  • combobox

  • file

  • multiselect

  • range

  • time

  • timesel

  • numeric

  • datetime

  • email

  • tel

  • checkbox

Keep in mind that, by default, most of the methods are based on the defaultClassNames result.

To create your own class, create a new file, and create a new class inside who extend the default class you want to overwrite.

Inside of the class, you can choose to overwrite whatever method you want just be recopying it and changing the return value :

static text = () => "myTextInputClasses";

Then import your custom class inside the component containing your form, and pass it to the formatting object.

Form inner methods

All the following methods can be applied after

this.form;

mongo

mongo();

This method is going to log you in the console what to put inside your persisters for the back-end, you can copy-paste it blindly.

joi

joi();

Same as the method above, it does log you the joi you'll need for your back-end in the console.

validate

validate();

It will check all your inputs and check if they pass the validators you've setted up nor the default ones.

clearInputs

clearInputs();

Will clear all the inputs.

getRawData

getRawData();

Extract all the data from the form at the given time of execution.

getInput

getInput($input_name);

Return the input class, giving access to input properties. (Warn : It's not a React Component, it's a js class).

disable/enable

disable();
enable();

They enable or disable all the inputs inside the form, if they are disabled, no changed are allowed.

Subform

addSubForm

addSubForm($subform);

Allow you to add a nested form inside your form (a sub form basically), work recursively so you can add as much as you want, and as deep as you want.

Take care, if you plan on using nested forms, you MUST pass an extra property inside the object constructing your form :

parent : this

removeSubForm

removeSubForm($subform_id);

Remove that subform, need the subform specific id to be able to find it.

getSubForm

getSubForm($subform_id);

Return the subform. (Warn : Just like getInput(), it does return a js class, not a component) You can apply every form methods to it.

Inputs inner methods.

setFieldProperty

setFieldProperty($name, $value, [$cbk]);

Call this method when you want to manually add/update props passed to the input.

  • E.g :
this.form.getInput('email').setFieldProperty("placeholder",  "Enter your email");

This is gonna update this specific input to add the placeholder inside. Keep in mind that the code above will be translated as :

placeholder="Enter your email"

so don't forget to update your code accordingly if you need to wrap inside an object, for the style property for example.

The callback will be called once the property is set.

setLabelProperty / setErrorProperty

setLabelProperty($name, $value, [$cbk]);
setErrorProperty($name, $value, [$cbk]);

Both do the same as setFieldProperty, but applied respectively to the label, and error components.

setFieldValue

setFieldValue($value, [$cbk]);

Allow you to manually set a value inside the field.

The callback will be called once the update is done.

setLabelValue / setErrorValue

Same as above.

getFieldValue

getFieldValue();

Return the current value of that specific field, at the given time of execution.

show / hide

show(); 
hide();

Apply to the field, label, and error. Will hide or show them, it's the same as applying :

hidden=true

enable / disable

enable(); 
disable();

Will disable or enable the field, it's working the same as manually setting the property of the field to disabled true or false.

getLabel / getField / getError

getLabel(); 
getField(); 
getError();

As mentionned at the beginning, the methods will return you the React component to put inside your code.

invalidate

invalidate($errorMessage);

Using this allow you to manually invalidate your input. You'll see your error message where you put the component, aswell as the error style appearing around your field and your label.

validate

validate();

Will go through all the validators you have set, and check if the input is valid or not. If not, it'll call invalidate(). It's useful when you don't want to wait for the submit to check for potential errors.

2.0.12

4 years ago

2.0.11

4 years ago

2.0.10

4 years ago

2.0.9

4 years ago

2.0.8

4 years ago

2.0.7

4 years ago

2.0.6

4 years ago

1.0.127

4 years ago

2.0.5

4 years ago

1.0.126

4 years ago

2.0.4

4 years ago

1.0.125

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

1.0.123

4 years ago

1.0.122

4 years ago

1.0.124

4 years ago

1.0.121

4 years ago

1.0.120

4 years ago

1.0.119

4 years ago

1.0.118

4 years ago

1.0.117

4 years ago

1.0.116

4 years ago

1.0.114

4 years ago

1.0.113

4 years ago

1.0.112

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.0.110

4 years ago

1.0.111

4 years ago

1.0.109

4 years ago

1.0.108

4 years ago

1.0.107

5 years ago

1.0.106

5 years ago

1.0.105

5 years ago

1.0.104

5 years ago

1.0.103

5 years ago

1.0.102

5 years ago

1.0.101

5 years ago

1.0.100

5 years ago

1.0.99

5 years ago

1.0.98

5 years ago

1.0.97

5 years ago

1.0.96

5 years ago

1.0.95

5 years ago

1.0.94

5 years ago

1.0.93

5 years ago

1.0.92

5 years ago

1.0.91

5 years ago

1.0.90

5 years ago

1.0.89

5 years ago

1.0.88

5 years ago

1.0.87

5 years ago

1.0.85

5 years ago

1.0.84

5 years ago

1.0.83

5 years ago

1.0.82

5 years ago

1.0.81

5 years ago

1.0.80

5 years ago

1.0.79

5 years ago

1.0.78

5 years ago

1.0.77

5 years ago

1.0.76

5 years ago

1.0.75

5 years ago

1.0.74

5 years ago

1.0.73

5 years ago

1.0.72

5 years ago

1.0.71

5 years ago

1.0.69

5 years ago

1.0.68

5 years ago

1.0.67

5 years ago

1.0.66

5 years ago

1.0.65

5 years ago

1.0.63

5 years ago

1.0.61

5 years ago

1.0.60

5 years ago

1.0.58

5 years ago

1.0.57

5 years ago

1.0.56

5 years ago

1.0.55

5 years ago

1.0.54

5 years ago

1.0.52

5 years ago

1.0.51

5 years ago

1.0.50

5 years ago

1.0.49

5 years ago

1.0.48

5 years ago

1.0.47

5 years ago

1.0.46

5 years ago

1.0.45

5 years ago

1.0.44

5 years ago

1.0.43

5 years ago

1.0.41

5 years ago

1.0.39

5 years ago

1.0.38

5 years ago

1.0.36

5 years ago

1.0.35

5 years ago

1.0.34

5 years ago

1.0.33

5 years ago

1.0.31

5 years ago

1.0.30

5 years ago

1.0.27

5 years ago

1.0.26

5 years ago

1.0.25

5 years ago

1.0.24

5 years ago

1.0.23

5 years ago

1.0.22

5 years ago

1.0.21

5 years ago

1.0.20

5 years ago

1.0.19

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.0.0

5 years ago