• Stars
    star
    145
  • Rank 254,144 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 2 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Multi-step wizard helpers for Alpine.js

Alpine Wizard

Tests npm version

This package provides an Alpine directive (x-wizard) and a magic helper ($wizard) that allow you to quickly build multi-step wizards using Alpine.js (only 1.1 kB gzipped).

x-wizard-github

Installation

via cdn

<script defer src="https://unpkg.com/@glhd/alpine-wizard@1/dist/wizard.cdn.min.js"></script>
<script defer src="https://unpkg.com/alpinejs@3/dist/cdn.min.js"></script>

via npm

npm install @glhd/alpine-wizard

via yarn

yarn add @glhd/alpine-wizard

Usage

<!-- This is the data our wizard will work with. It's just plain Alpine. -->
<form x-data="{ name: '', age: 0, coppa: false }">
	
	<!-- Add steps with x-wizard:step -->
	<div x-wizard:step>
		Welcome! This step has no requirements, so we can continue
		immediately. This is useful for things like introductions.
	</div>
	
	<!-- Steps can have logic about when they're complete. In this
	     case, you must have entered a value for "name" to continue. -->
	<div x-wizard:step="name.trim() !== ''">
		Your name: <input x-model="name" name="name" />
	</div>
	
	<!-- Here's another step that has logic about when it's complete -->
	<div x-wizard:step="age > 0">
		Your age: <input x-model="age" name="age" />
	</div>
	
	<!-- Steps can also have logic about when they apply. In this
	     case, this step is only shown if the person is under 13. -->
	<div x-wizard:if="age < 13" x-wizard:step="coppa === true">
		<label>
			<input type="checkbox" x-model="coppa" />
			I have my parent or guardian's consent
		</label>
	</div>
	
	<!-- We also expose a $wizard magic that gives lots of helper
	     functionality. Here we have the logic for a continue button -->
	<button
		@click="$wizard.forward()"
		:disabled="$wizard.cannotGoForward()"
		x-show="$wizard.isNotLast()"
	>
		Continue
	</button>
	
	<!-- And here's a "submit" button when they get to the end. -->
	<button
		:disabled="$wizard.isNotComplete()"
		x-show="$wizard.isLast()"
	>
		Submit
	</button>

</form>

Try it out

Validation Rules

The wizard also supports Laravel-style validation rules via the validatorjs package. If you're using the CDN version of the script, be sure to add the Validator package to the page before the wizard plugin (if you're installing via npm or yarn this functionality is available by default):

<script defer src="https://unpkg.com/validatorjs@3/src/validator.js"></script>

This plugin allows you to add a .rules modifier to steps:

<div x-wizard:step.rules="{ age: 'required|numeric|min:1|max:120' }">
    Your age: <input x-model="age" name="age" />
</div>

See the validatorjs documentation for a list of all supported rules and how to configure them.

API

Alpine Directives

x-wizard:step

Use x-wizard:step to define each wizard step. This directive optionally accepts a javascript expression which must return a truthy value before the step is considered complete.

x-wizard:if

Use x-wizard:if to make a step conditional. Any steps that have x-wizard:if will only show if the expression passed to the directive returns a truthy value.

x-wizard:title

Use x-wizard:title to set the step title. This is useful if you want to present the title of the current step elsewhere in your UI. By default, this is just a plain string, but you can generate the title dynamically by using the .dynamic modifier.

Alpine Magics

$wizard

The $wizard magic provides access to the state of your current wizard. It provides a number of useful helper functions:

  • current() — get the current wizard step
  • next() — get the next wizard step (or null if at end)
  • previous() — get the previous wizard step (or null if at beginning)
  • progress() — get current progress, a JS object:
    • current — the current step number (as of 1.2.0)
    • total — the total number of applicable steps
    • complete — the number of completed steps
    • incomplete — the number of steps still to complete
    • percentage — the percent complete, as a string (i.e. "33%")
    • percentage_int — the percent complete, as an integer (i.e. 33) (as of 1.2.0)
    • percentage_float — the percent complete, as an float (i.e. 0.33) (as of 1.2.0)
  • isFirst() — check if we're currently on the first step
  • isNotFirst() — check if we're NOT currently on the first step
  • isLast() — check if we're on the last step
  • isNotLast() — check if we're NOT on the last step
  • isComplete() — check if we're on the last step and all steps are complete
  • isNotComplete()/isIncomplete() — check if we're not on the last step or all steps aren't complete
  • canGoForward() — check if we can move to the next step
  • cannotGoForward() — check if we CANNOT move to the next step
  • forward() — move to the next step if possible
  • canGoBack() — check if we can go to the previous step
  • cannotGoBack() — check if we CANNOT go to the previous step
  • back() — move to the previous step if possible

Each step is a plain javascript object with a few properties:

  • el — the DOM element associated with the step
  • title — the title of the step
  • is_applicable — whether this step applies given the current data
  • is_complete — whether this step is complete

More Repositories

1

aire

Modern form builder for Laravel
PHP
535
star
2

laravel-dumper

Upgrade dd() with Laravel-specific improvements
PHP
387
star
3

gretel

Laravel breadcrumbs right out of a fairy tale
PHP
221
star
4

conveyor-belt

All the underlying mechanics necessary to write artisan commands that process lots of data efficiently
PHP
133
star
5

tailwindcss-plugins

Plugins & helpers for tailwindcss
JavaScript
85
star
6

bits

PHP
73
star
7

dawn

Experimental next-gen browser testing for Laravel
PHP
72
star
8

laravel-addressing

Laravel package providing addressing functionality
PHP
62
star
9

linen

Lightweight spreadsheets for Laravel
PHP
50
star
10

special

PHP
17
star
11

hooks

PHP
14
star
12

tinker-gui

Laravel Tinker GUI
TypeScript
12
star
13

linearavel

PHP
9
star
14

laravel-timezone-mapper

Map latitude/longitude to a timezone without making external API calls
PHP
8
star
15

wp-bootstrap-widgets

Bootstrap Components as WordPress Widgets
PHP
6
star
16

aire-bootstrap

Bootstrap theme for Aire
PHP
6
star
17

laravel-dusk-browserstack

Easily run Laravel Dusk tests on BrowserStack
PHP
6
star
18

ansipants

PHP
6
star
19

laralint

Framework for linting Laravel projects
PHP
6
star
20

laravel-package-template

Template for Laravel packages
PHP
4
star
21

laravel-prismoquent

Prismoquent lets you access Prismic.io using the same methods you're used to in Eloquent.
PHP
2
star
22

medusa

Headless content management
JavaScript
2
star
23

ansible-interactive

Interactive CLI for ansible plays
JavaScript
2
star
24

fs-of-holding

A filesystem that has every file in it
PHP
1
star
25

aire-livewire

1
star
26

omniponent

JavaScript
1
star
27

suspend

PHP
1
star
28

aire-docs

Blade
1
star
29

vbulletin-bbcode-parser

Parser package for vBulletin bbCode
PHP
1
star
30

verbs-github-webhooks

A collection of Verbs events to use with GitHub webhooks
PHP
1
star
31

accounts-payable

Help collect tax documents and pay independent contractors
PHP
1
star