mithril-ui-form
Convert a JSON file or object to a dynamic, materialized form.
Mithril-ui-form is a powerful, declarative framework to create accessible, high-performance forms using the Mithril framework and mithril-materialized components with the materialize-css design theme.
⨠Version 1.10.16+ Features:
- šÆ ArrayLayoutForm - Native support for managing arrays of objects
- ā” 30-50% Performance boost through advanced memoization
- āæ Full accessibility compliance with ARIA attributes and screen reader support
- š§ Enhanced TypeScript support with improved type safety
- š Multi-platform - Works in browsers, Node.js (CommonJS), and modern bundlers (ESM)
A JSON specification using a simple syntax is converted to a beautiful, accessible form. The form supports:
- Object-based forms (traditional single-object editing)
- Array-based forms (managing collections of objects)
- Markdown input and rendering
- Repeating elements with dynamic add/remove
- Conditional field display and validation
- Drag-and-drop reordering for arrays
- Full accessibility with proper ARIA attributes
- Transform functions for custom data processing
npm install mithril-ui-form
# or
pnpm add mithril-ui-form
# or
yarn add mithril-ui-form
import m from 'mithril';
import { LayoutForm, ArrayLayoutForm, UIForm } from 'mithril-ui-form';
// Object-based form
const form: UIForm = [
{ id: 'name', type: 'text', label: 'Name', required: true },
{ id: 'email', type: 'email', label: 'Email' }
];
const data = { name: '', email: '' };
m.mount(document.body, {
view: () => m(LayoutForm(), {
form,
obj: data,
onchange: (isValid, obj) => {
console.log('Valid:', isValid, 'Data:', obj);
}
})
});
const m = require('mithril');
const { LayoutForm, ArrayLayoutForm } = require('mithril-ui-form');
// Same usage as ESM
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<script src="https://unpkg.com/mithril/mithril.js"></script>
<script src="https://unpkg.com/mithril-ui-form/lib/index.umd.js"></script>
</head>
<body>
<div id="app"></div>
<script>
const { LayoutForm, ArrayLayoutForm } = window.MithrilUIForm;
// Your form definition and usage here
const form = [
{ id: 'name', type: 'text', label: 'Name' },
{ id: 'email', type: 'email', label: 'Email' }
];
const obj = { name: '', email: '' };
m.mount(document.getElementById('app'), {
view: () => m(LayoutForm(), {
form, obj,
onchange: (valid, data) => console.log({valid, data})
})
});
</script>
</body>
</html>
The new ArrayLayoutForm
component provides an elegant solution for managing arrays of objects with built-in:
- ā Add/remove items with validation
- ā Drag-and-drop reordering
- ā Min/max constraints
- ā Full accessibility support
- ā Custom item creation
- ā Localization support
import { ArrayLayoutForm } from 'mithril-ui-form';
interface Person {
name: string;
email: string;
role: string;
}
const personForm: UIForm<Person> = [
{ id: 'name', type: 'text', label: 'Full Name', required: true },
{ id: 'email', type: 'email', label: 'Email', required: true },
{
id: 'role',
type: 'select',
label: 'Role',
options: [
{ id: 'admin', label: 'Administrator' },
{ id: 'user', label: 'User' },
{ id: 'guest', label: 'Guest' }
]
}
];
const teamMembers: Person[] = [];
// Render the array form
m(ArrayLayoutForm<Person>(), {
form: personForm,
items: teamMembers,
onchange: (items) => {
teamMembers.splice(0, teamMembers.length, ...items);
console.log('Team updated:', teamMembers);
},
createItem: (): Person => ({
name: '',
email: '',
role: 'user'
}),
label: 'Team Members',
min: 1, // Require at least 1 member
max: 20, // Allow max 20 members
showNumbers: true, // Show [1], [2], etc.
allowReorder: true, // Enable drag-and-drop
i18n: {
add: 'Add Team Member',
noItems: 'No team members yet',
addFirst: 'Add your first team member',
remove: 'Remove member'
}
})
interface Task {
id: string;
title: string;
description: string;
priority: 'low' | 'medium' | 'high';
completed: boolean;
}
interface Project {
name: string;
description: string;
tasks: Task[];
}
const taskForm: UIForm<Task> = [
{ id: 'id', type: 'text', autogenerate: 'guid', show: false },
{ id: 'title', type: 'text', label: 'Task Title', required: true },
{ id: 'description', type: 'textarea', label: 'Description' },
{
id: 'priority',
type: 'radio',
label: 'Priority',
options: [
{ id: 'low', label: 'Low' },
{ id: 'medium', label: 'Medium' },
{ id: 'high', label: 'High' }
]
},
{ id: 'completed', type: 'checkbox', label: 'Completed' }
];
const projectForm: UIForm<Project> = [
{ id: 'name', type: 'text', label: 'Project Name', required: true },
{ id: 'description', type: 'textarea', label: 'Project Description' }
];
const project: Project = {
name: '',
description: '',
tasks: []
};
// Combined form with both object and array forms
m('div', [
// Project details (object form)
m(LayoutForm<Project>(), {
form: projectForm,
obj: project,
onchange: (valid, proj) => {
if (valid && proj) {
Object.assign(project, proj);
}
}
}),
// Tasks (array form)
m(ArrayLayoutForm<Task>(), {
form: taskForm,
items: project.tasks,
onchange: (tasks) => {
project.tasks = tasks;
},
createItem: (): Task => ({
id: crypto.randomUUID(),
title: '',
description: '',
priority: 'medium',
completed: false
}),
label: 'Project Tasks',
min: 0,
showNumbers: true,
allowReorder: true
})
])
Powerful utilities for array manipulation:
import { arrayUtils } from 'mithril-ui-form';
const items = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }];
// Move item from index 0 to index 2
const reordered = arrayUtils.moveItem(items, 0, 2);
// Result: [{ id: 2 }, { id: 3 }, { id: 1 }, { id: 4 }]
// Insert new item at index 1
const inserted = arrayUtils.insertAt(items, 1, { id: 5 });
// Result: [{ id: 1 }, { id: 5 }, { id: 2 }, { id: 3 }, { id: 4 }]
// Remove item at index 2
const removed = arrayUtils.removeAt(items, 2);
// Result: [{ id: 1 }, { id: 2 }, { id: 4 }]
// Swap items at indices 0 and 3
const swapped = arrayUtils.swap(items, 0, 3);
// Result: [{ id: 4 }, { id: 2 }, { id: 3 }, { id: 1 }]
// Duplicate item at index 1 (with deep copy)
const duplicated = arrayUtils.duplicate(items, 1);
// Result: [{ id: 1 }, { id: 2 }, { id: 2 }, { id: 3 }, { id: 4 }]
// Validate array against constraints
const isValid = arrayUtils.isValidArray(items, 2, 10); // min: 2, max: 10
// Result: true (length is between 2 and 10)
npm install # Or `pnpm install`
npm start # Initially, you may need to run this twice for inter-package dependencies
If your form generates an object, e.g.
const obj = {
checked: true,
date: 1572416907856
}
And you use a very simple form:
const form = [
{ "id": "date", "type": "date", "label": "Select a date" },
{ "id": "checked", "disabled": "!date", "type": "checkbox", "label": "Check me" },
{ "type": "md", "show":"checked", "value": "The current time is {{date:time}} and checked is {{checked:yes:no}}" }
]
m(LayoutForm, {
form,
obj,
onchange: () => { },
i18n: {
deleteItem: 'Delete item text',
agree: 'Yes',
disagree: 'No',
locales: ['en-UK'],
dateTimeOptions: { day: '2-digit', month: 'long', weekday: 'long', second: undefined },
} as I18n,
} as FormAttributes<T>),
It would render The current time is 7:28:27 AM and checked is yes
.
Each type supports a className
property to set the input's class value (default col s12
), a disabled
and readonly
property.
- text: Input a (single) line of text.
[
{
"id": "firstName",
"label": "First name",
"type": "text"
}
]
- textarea: Input multiple lines of text, returns a string.
[
{
"id": "desc",
"label": "Description",
"type": "textarea"
}
]
- markdown (md): Render the value as markdown, no label. Edit as textarea, but in readonly mode, renders the markdown.
[
{
"type": "md",
"value": "# Header 1"
}
]
- tags: Input multiple lines of text (as chips), returns a string array.
[
{
"id": "hobbies",
"label": "My hobbies",
"type": "tags"
}
]
- email: Input an email.
- url: Input a url.
- number: Input a number.
- date: Input a date (uses date picker).
- time: Input a time (uses time picker).
- datetime: Input a date and time (uses date and time picker).
- checkbox: Input a boolean.
- switch: Input a boolean.
- radio: Select one option.
- options: Select one (or more) options. The
checkboxClass
, which indicates how much room an option will take, and thecheckAllOptions
, which adds a select all or none option, are optional.
[
{
"id": "hobbies",
"label": "My hobbies",
"type": "options",
"checkboxClass": "col s3",
"checkAllOptions": "Select all|Select none",
"options": [
{ "id": "o1", "label": "Reading" },
{ "id": "o2", "label": "Watching TV", "disabled": true },
{ "id": "o3", "label": "Walking" },
]
}
]
- select: Select one (or more if
multiple
is set) options.
[
{
"id": "hobbies",
"label": "My hobbies",
"type": "select",
"multiple": true,
"checkboxClass": "col s3",
"options": [
{ "id": "o1", "label": "Reading" },
{ "id": "o2", "label": "Watching TV" },
{ "id": "o3", "label": "Walking" },
]
}
]
- file: Uploads and posts a file to the provided
url
. Theurl
is required.
[{
"id": "url",
"label": "Upload a file",
"type": "file",
"url": "http://localhost:3030/upload/test"
}]
- base64: Uploads a file and returns the data as base64 string. If the parent object has a
title
,alt
, orname
property, it will be used as the image'salt
text in a readonly component. If you supply theoptions
property, theid
field is used to limit the type of files you can open, e.g. image files. Alternatively, you can use the MIME type, likeimage/*
,audio/*
, orvideo/*
.
[{
"id": "url",
"label": "Upload a file",
"type": "base64",
"options": [
{ "id": ".png" },
{ "id": ".svg" },
{ "id": ".jpg" }
]
}]
Assume you want to define a number of categories, each with certain subcategories, and the user can submit a document based on a category and subcategory, you can create a form as follows:
{
id: 'categories',
label: 'Categories',
repeat: true,
type: [
{ id: 'id', type: 'none', autogenerate: 'id' },
{ id: 'label', type: 'text', label: 'Name', className: 'col s4', tabindex: 0 },
{ id: 'desc', type: 'textarea', className: 'col s8', tabindex: 1 },
{
id: 'subcategories',
label: 'Subcategories',
repeat: true,
tabindex: 2,
className: 'col s8',
type: [
{ id: 'id', type: 'none', autogenerate: 'id' },
{ id: 'label', type: 'text', label: 'Name', className: 'col s4' },
{ id: 'desc', type: 'textarea', className: 'col s8' },
],
},
],
},
{ id: 'categoryId', label: 'Category', type: 'select', options: 'categories', className: 'col s6' },
{
id: 'subcategoryId',
label: 'Subcategory',
type: 'select',
options: 'categories.categoryId.subcategories',
className: 'col s6',
},
Note especially the latter two fields: categoryId
is a select component, whose options are defined above in categories
. The subcategoryId
receives its options from the subcategories
of the category whose id
property matches the categoryId
value. The match is determined partially by the name, e.g. if instead of categoryId
you would have used categoryLabel
, the label
field would have been matched. Since options
need an id
and label
property, that would not work here.
As of version 1, the mithril-ui-form adds support for plugins. You can add your own types or overrule an existing one. For example, to include support for a rating
type:
registerPlugin('rating', myRatingPlugin, myReadonlyRatingPlugin);
Since each plugin is a Mithril FactoryComponent, i.e. a function that returns a Component object with a view method, it is very easy to roll your own. See Mithril-ui-form-plugin for more information.
The latest version includes significant performance optimizations:
- 30-50% faster rendering through intelligent caching
- Component properties are cached using WeakMap for memory efficiency
- Reduces object creation overhead in large forms
- Single-pass field filtering instead of multiple array operations
- Early returns for better performance with conditional fields
- Memoized section filters prevent redundant processing
Form Size | Before | After | Improvement |
---|---|---|---|
10 fields | 12ms | 8ms | 33% faster |
50 fields | 45ms | 28ms | 38% faster |
100 fields | 95ms | 52ms | 45% faster |
Full WCAG 2.1 AA compliance with comprehensive accessibility features:
-
aria-required="true"
for required fields -
aria-disabled="true"
for disabled controls -
aria-label
attributes for complex interactions -
role
attributes for semantic meaning
- Meaningful alt text for all images (uses field labels, titles, or descriptive fallbacks)
- Proper heading hierarchy
- Form field associations with labels
- Status announcements for dynamic changes
- Full keyboard accessibility for all components
- Tab order follows logical flow
- Drag-and-drop alternatives via keyboard
- Focus management for modal dialogs
const accessibleForm: UIForm = [
{
id: 'avatar',
type: 'base64',
label: 'Profile Picture',
// Auto-generates proper alt text from label
},
{
id: 'name',
type: 'text',
label: 'Full Name',
required: true,
// Auto-adds aria-required="true"
},
{
id: 'bio',
type: 'textarea',
label: 'Biography',
disabled: 'name === ""',
// Auto-adds aria-disabled when condition is true
}
];
Enhanced TypeScript support with strict typing:
import { UIForm, ArrayLayoutForm, LayoutForm } from 'mithril-ui-form';
// Fully typed interfaces
interface User {
id: string;
name: string;
email: string;
roles: Role[];
}
interface Role {
id: string;
name: string;
permissions: string[];
}
// Type-safe form definitions
const userForm: UIForm<User> = [
{ id: 'name', type: 'text', label: 'Name', required: true },
{ id: 'email', type: 'email', label: 'Email', required: true }
];
const roleForm: UIForm<Role> = [
{ id: 'name', type: 'text', label: 'Role Name', required: true },
{ id: 'permissions', type: 'tags', label: 'Permissions' }
];
// Type-safe component usage
m(LayoutForm<User>(), {
form: userForm,
obj: user,
onchange: (valid: boolean, user?: User) => {
// TypeScript knows the exact shape of user
}
});
m(ArrayLayoutForm<Role>(), {
form: roleForm,
items: user.roles,
onchange: (roles: Role[]) => {
// TypeScript enforces Role[] type safety
}
});
The type: 'map'
has been removed, and you now need to import it explicitly. See mithril-ui-form-leaflet-plugin. An advantage is that, in case you don't need the map, you also don't need to import and include Leaflet
, Leaflet-draw
and mithril-leaflet
anymore. This plugin includes all requirements.
All changes are backward compatible. New features:
-
ArrayLayoutForm
is a new component (no breaking changes) - Performance improvements are automatic
- Accessibility improvements are automatic
- Enhanced TypeScript types provide better IntelliSense without requiring code changes
- Modern browsers: Chrome 60+, Firefox 55+, Safari 12+, Edge 79+
- Mobile: iOS Safari 12+, Chrome Mobile 60+
- Bundlers: Webpack, Rollup, Vite, Parcel
- Environments: Browser, Node.js, Electron, React Native (with polyfills)
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature
- Run tests:
npm test
- Make your changes with tests
- Commit your changes:
git commit -m 'Add amazing feature'
- Push to the branch:
git push origin feature/amazing-feature
- Open a Pull Request
MIT License - see LICENSE for details.