2.0.4 • Published 1 year ago

form-part v2.0.4

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

FORM PART

Form part is a tiny library written in pure Javascript, to transform your big forms into smaller, more managable forms, cut in parts.

it aims to be very simple to use. here is an example :

Example

Lets say we have this form :

<label for="firstname" class="form-label">Firstname</label>
<input class="form-control" type="text" required="true" name="firstname"><br>
<label for="lastname" class="form-label">Lastname</label>
<input class="form-control" type="text" required="true" name="lastname"><br>
<label for="date_of_birth" class="form-label">Date of birth</label>
<input class="form-control" type="date" required="true" name="date_of_birth"><br>
<label for="line1" class="form-label">Line 1</label>
<input class="form-control" type="text" required="true" name="line1"><br>
<label for="city" class="form-label">City</label>
<input class="form-control" type="text" required="true" name="city"><br>
<label for="country" class="form-label">Country</label>
<input class="form-control" type="text" required="true" name="country"><br>

nothing bad here, but a bit clunky !

say we now want to split it in two blocks, personnal infos and address. With form part, you would just need to englobe the form like this to do the trick :

<div id="form">
    <div class="form-part">
        <h1>Personnal infos</h1>
        <hr>
        <label for="firstname" class="form-label">Firstname</label>
        <input class="form-control" type="text" required="true" name="firstname"><br>
        <label for="lastname" class="form-label">Lastname</label>
        <input class="form-control" type="text" required="true" name="lastname"><br>
        <label for="date_of_birth" class="form-label">Date of birth</label>
        <input class="form-control" type="date" required="true" name="date_of_birth"><br>
    </div>
    <div class="form-part">
        <h1>Address</h1>
        <hr>
        <label for="line1" class="form-label">Line 1</label>
        <input class="form-control" type="text" required="true" name="line1"><br>
        <label for="city" class="form-label">City</label>
        <input class="form-control" type="text" required="true" name="city"><br>
        <label for="country" class="form-label">Country</label>
        <input class="form-control" type="text" required="true" name="country"><br>
    </div>
</div>

<script type="module">
    import AnimatedForm from "./form-part.js";

    const form = new AnimatedForm({
        holder: document.getElementById("form")
    });
</script>

(wrap your form in a div (eg: the holder))

your form is now split in two pages !

CONFIGURATION

The AnimatedForm Javascript class takes only one argument : the config object. Available options are listed below :

NameTypeDefaultRequiredDescription
holderHtmlElementtrueHtmlElement
controlsbooltrueactivate controls
navbooltrueactivate navigation
previousPageText:string"Previous"
nextPageText:string"Next"
submitButtonbooltrue
submitButtonTextstring"Submit"
partMarginnumber25px between parts
defaultFieldAlertTextnumberfalse
emptyFieldsAlertTextstring"Please fill in required inputs to go to the next page"
validatorbooltruedisable constraints validation
debugboolfalseSome visual debug output
blockEmptyInputsbooltruePrevent page progress if required inputs are empty

PAGE TITLE (nav)

to set a page title (instead of using numbers in navs), set the data-part-name attribute to the title you want :

<div class="form-part" data-part-name="Address"> ... </div>

'Address' will now show as the title in the navigation !

Required fields

Required fields will automatically prevent you from going to the next page, and show you an alert on top of the page. you can change this message using the emptyFieldsAlertText option

if you want field-specific alerts, you can provide a data-alert attribute with the message inside like so :

<input class="form-control" type="text" required="true" name="country" data-alert="Please indicate your country">

You can also provide a form-wide input alert by setting the defaultFieldAlertText :

const form = new AnimatedForm({
    holder: document.getElementById("form"),
    defaultFieldAlertText: "Please fill in this field",
});

CSS Variables

--form-part-theme
2.0.3

1 year ago

2.0.2

1 year ago

2.0.4

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago