Skip to content

extendFormBinding

extendFormBinding<Bindings>(bindings): Bindings

Defined in: binding.ts:250

Add a bind method to every form for each binding class

A method binds its class to the form it is called on with Form.bind. It takes the field name (or the list of field names, or nothing for a binding to the form) and the config, and an omitted config becomes an empty object.

Type the methods by extending Form with FormBindingMethods:

Bindings extends Record<string, new (...args: any) => unknown>

Bindings

Binding classes by method name. Each name is bind followed by a capital letter.

Bindings

bindings as is, for typing the methods with FormBindingMethods

export const myBindings = extendFormBinding({
bindDropdown: DropdownBinding,
});
declare module "@mobx-sentinel/form" {
interface Form<T> extends FormBindingMethods<T, typeof myBindings> {}
}
<Dropdown {...form.bindDropdown("country", { getter: () => model.country, setter: (v) => (model.country = v) })} />
  • Forms created before the call get the methods too
  • Adding a method under a name that is already taken replaces it on every form
  • The methods are shared by every form, like the methods of a class, so a method detached from its form fails

TypeError if a name does not start with bind and a capital letter, or a binding is not a class. No method is added then.