Skip to main content

API

withFinalForm(component)#

Creates a final form wrapper for a component. Automatically unsubscribes and removes form when component unmounts. Configuration callbacks are all called bound to the riot component, so the lexical this will be the same as onMounted. The following configuration options are available:

ParamTypeDescription
component.formElementfunctionRequired function that returns the form element to bind to
component.onSubmitfunctionFinal Form submit function. Prevents defeault behavior. If undefined, default DOM behavior will occur.
component.initialValuesobjectFinal Form initialValues
component.validatefunctionForm validate function
component.onFormChangeonFormChangeFinal Form listener that passes form state
component.onFormMutatedonFormMutatedMutationObserver that listens for changes in the HTML form
component.formSubscriptionsobjectFinal Form subscriptions
component.formConfigobjectFinal Form configs
component.onFieldChangeonFieldChangeCallback ran when a field changes
component.fieldSubscriptionsobjectFinal Form field subscriptions
component.fieldConfigsobjectFinal Form field configs
component.manuallyInitializeFinalFormbooleanIn case you want to manually initialize final form after some async event. Read more about this flag.

onFormChange : function#

Form change callback

ParamTypeDescription
formStateobjectfinal form state

onFormMutated : function#

Mutation observer callback

ParamTypeDescription
formMutationOptionsobjectOptions to perform changes to final form state
<script>
    withFinalForm({
        // ...
        onFormMutated(formMutationOptions) {
            const {                // Mutation observer callback                mutationsList,                observer,
                // Map of registrations (Map<HTMLElement, deregisterFunction()>)                registrations,
                // Final form API                form,
                // registerField(field: HTMLFormInputElement)                registerField            } = formMutationOptions;

            for (const mutations of mutationsList) {
                const {                    addedNodes,                    removedNodes                } = mutation;
                for (const el of [...addedNodes]) {
                    if (/^(?:input|select|textarea)$/i.test(el.nodeName)) {
                        registerField(el);                    }                }
                for (const el of [...removedNodes]) {
                    if (registrations.has(el)) {
                        const unregister = registrations.get(el);                        unregister();                    }                }            }        }
        // ...
    });</script>

onFieldChange : function#

Field change callback

ParamTypeDescription
fieldHTMLElementform field
fieldStateobjectfinal form field state