Skip to content

Implement a hook-based, all-in-one Form component #1160

@radekmie

Description

@radekmie

During our recent meeting regarding v4, I presented an idea of a general Form component, replacing all of the existing *Form components. The main differences would be:

  1. It'd no longer be a class component but a hook-based functional one.
  2. It'd include all of the functionalities in one (with one exception) to make the API surface even smaller and easier to work with.

What could it look like? Well, we'd basically merge all of the current props of all form components and make it into one. All of the instance methods could be replaced with internal callbacks and exposed with useImperativeHandle for programmatic access.

Migration:

  • BaseForm is trivial - it just works.
  • ValidatedForm too, as there's nothing special about it.
  • QuickForm and QuickValidatedForm have to know which components to render, and instead of instance methods, we'd have to use props for that. It'll be hidden for the theme users, as the themes would expose their own Form components with provided fields.
  • AutoForm is the only special one. Not everyone uses it, and we'd like to make the automatic state management opt-in. The API would look like this:
    function AutoForm(props) {
      const [model, onChange] = useAutoForm(props.model);
      return <Form {...props} model={model} onChange={onChange} />;
    }
    
    function useAutoForm(initialModel = {}) {
      const [model, setModel] = useState(initialModel);
      const onChange = useCallback(function onChange(key, value) {
        setModel(model => setWith(clone(state.model), key, value, clone));
      }, [setModel]);
      return [model, onChange];
    }
    Of course, both AutoForm and useAutoForm would be provided in the uniforms package as well. (We're unsure about the naming, though.) The onChangeModel is no longer needed, as you could simply useEffect on the model returned from useAutoForm.

While I'll work on a prototype, I'd like to hear everyones' feedback. Our goal would be to release it with 4.0 or soon after and make it the only form component available in v5.0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area: CoreAffects the uniforms packageType: FeatureNew features and feature requests

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    On Hold

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions