@wordpuppi/anyform-wasm-js
v0.5.1
Published
WASM bindings for anyform - form state, validation, and navigation
Downloads
276
Maintainers
Readme
@wordpuppi/anyform-wasm-js
Optional WebAssembly engine for anyform — faster validation for complex forms.
Note: This package is optional.
@wordpuppi/anyform-reactworks out of the box with a pure JS engine. Only install this if you need WASM performance for large/complex forms.
Installation
npm install @wordpuppi/anyform-wasm-jsUsage with React
import { useAnyForm } from '@wordpuppi/anyform-react';
// Enable WASM engine
const form = useAnyForm('contact', { engine: 'wasm' });See @wordpuppi/anyform-react README for bundler configuration (Vite, Next.js, Webpack).
Direct Usage (Vanilla JS)
import init, { FormClient } from '@wordpuppi/anyform-wasm-js';
async function main() {
// Initialize WASM
await init();
// Create client
const client = new FormClient('http://localhost:3000');
// Fetch form
const form = await client.fetch_form('contact');
// Set values
form.set_value('email', '[email protected]');
form.set_value('name', 'John Doe');
// Validate and submit
if (form.is_valid()) {
const result = await form.submit();
console.log('Submitted:', result);
}
}Multi-Step Forms
const form = await client.fetch_form('wizard');
// Get visible steps (respects conditions)
const steps = form.visible_steps();
// Navigate
form.go_to_step(steps[0].id);
form.set_value('country', 'US');
if (form.validate_step(form.current_step().id).length === 0) {
form.next_step(); // Skips hidden steps automatically
}
// Progress indicator
const [current, total] = form.progress();
console.log(`Step ${current} of ${total}`);Hydration Mode
For server-rendered forms, use automatic hydration:
<script type="module">
import init, { hydrate_all } from '@wordpuppi/anyform-wasm-js';
await init();
hydrate_all(); // Hydrates all forms with data-af-form attribute
</script>API
FormClient
new FormClient(base_url)- Create client instancefetch_form(slug)- Fetch form schema and create FormStatesubmit_form(slug, data)- Submit form data directly
FormState
Value Management:
set_value(field, value)- Set field valueget_value(field)- Get field valueget_values()- Get all values
Validation:
validate_field(field)- Validate single fieldvalidate_step(step_id)- Validate all fields in stepvalidate_all()- Validate entire formis_valid()- Check if form is validget_errors(field)- Get errors for field
Visibility:
visible_steps()- Get visible stepsvisible_fields(step_id)- Get visible fieldsis_step_visible(step_id)- Check step visibilityis_field_visible(field_name)- Check field visibility
Navigation:
current_step()- Get current stepnext_step()- Go to next visible stepprev_step()- Go to previous visible stepgo_to_step(step_id)- Go to specific stepcan_go_next()/can_go_prev()- Check navigationprogress()- Get [current, total] step numbers
Hydration
hydrate_all()- Hydrate all forms on pagehydrate(slug)- Hydrate specific form
License
MIT
