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:
- It'd no longer be a class component but a hook-based functional one.
- 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.
During our recent meeting regarding v4, I presented an idea of a general
Formcomponent, replacing all of the existing*Formcomponents. The main differences would be: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
useImperativeHandlefor programmatic access.Migration:
BaseFormis trivial - it just works.ValidatedFormtoo, as there's nothing special about it.QuickFormandQuickValidatedFormhave 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 ownFormcomponents with provided fields.AutoFormis 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:AutoFormanduseAutoFormwould be provided in theuniformspackage as well. (We're unsure about the naming, though.) TheonChangeModelis no longer needed, as you could simplyuseEffecton themodelreturned fromuseAutoForm.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.